1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_algo.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{algorithm}
61 #include <cstdlib> // for rand
62 #include <bits/algorithmfwd.h>
63 #include <bits/stl_heap.h>
64 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
66 #ifdef __GXX_EXPERIMENTAL_CXX0X__
67 #include <random> // for std::uniform_int_distribution
68 #include <functional> // for std::bind
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 namespace std _GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 /// Swaps the median value of *__a, *__b and *__c to *__a
78 template<typename _Iterator>
80 __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c)
82 // concept requirements
83 __glibcxx_function_requires(_LessThanComparableConcept<
84 typename iterator_traits<_Iterator>::value_type>)
89 std::iter_swap(__a, __b);
91 std::iter_swap(__a, __c);
96 std::iter_swap(__a, __c);
98 std::iter_swap(__a, __b);
101 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
102 template<typename _Iterator, typename _Compare>
104 __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
107 // concept requirements
108 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
109 typename iterator_traits<_Iterator>::value_type,
110 typename iterator_traits<_Iterator>::value_type>)
112 if (__comp(*__a, *__b))
114 if (__comp(*__b, *__c))
115 std::iter_swap(__a, __b);
116 else if (__comp(*__a, *__c))
117 std::iter_swap(__a, __c);
119 else if (__comp(*__a, *__c))
121 else if (__comp(*__b, *__c))
122 std::iter_swap(__a, __c);
124 std::iter_swap(__a, __b);
129 /// This is an overload used by find() for the Input Iterator case.
130 template<typename _InputIterator, typename _Tp>
131 inline _InputIterator
132 __find(_InputIterator __first, _InputIterator __last,
133 const _Tp& __val, input_iterator_tag)
135 while (__first != __last && !(*__first == __val))
140 /// This is an overload used by find_if() for the Input Iterator case.
141 template<typename _InputIterator, typename _Predicate>
142 inline _InputIterator
143 __find_if(_InputIterator __first, _InputIterator __last,
144 _Predicate __pred, input_iterator_tag)
146 while (__first != __last && !bool(__pred(*__first)))
151 /// This is an overload used by find() for the RAI case.
152 template<typename _RandomAccessIterator, typename _Tp>
153 _RandomAccessIterator
154 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
155 const _Tp& __val, random_access_iterator_tag)
157 typename iterator_traits<_RandomAccessIterator>::difference_type
158 __trip_count = (__last - __first) >> 2;
160 for (; __trip_count > 0; --__trip_count)
162 if (*__first == __val)
166 if (*__first == __val)
170 if (*__first == __val)
174 if (*__first == __val)
179 switch (__last - __first)
182 if (*__first == __val)
186 if (*__first == __val)
190 if (*__first == __val)
199 /// This is an overload used by find_if() for the RAI case.
200 template<typename _RandomAccessIterator, typename _Predicate>
201 _RandomAccessIterator
202 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
203 _Predicate __pred, random_access_iterator_tag)
205 typename iterator_traits<_RandomAccessIterator>::difference_type
206 __trip_count = (__last - __first) >> 2;
208 for (; __trip_count > 0; --__trip_count)
210 if (__pred(*__first))
214 if (__pred(*__first))
218 if (__pred(*__first))
222 if (__pred(*__first))
227 switch (__last - __first)
230 if (__pred(*__first))
234 if (__pred(*__first))
238 if (__pred(*__first))
247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
248 /// This is an overload used by find_if_not() for the Input Iterator case.
249 template<typename _InputIterator, typename _Predicate>
250 inline _InputIterator
251 __find_if_not(_InputIterator __first, _InputIterator __last,
252 _Predicate __pred, input_iterator_tag)
254 while (__first != __last && bool(__pred(*__first)))
259 /// This is an overload used by find_if_not() for the RAI case.
260 template<typename _RandomAccessIterator, typename _Predicate>
261 _RandomAccessIterator
262 __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
263 _Predicate __pred, random_access_iterator_tag)
265 typename iterator_traits<_RandomAccessIterator>::difference_type
266 __trip_count = (__last - __first) >> 2;
268 for (; __trip_count > 0; --__trip_count)
270 if (!bool(__pred(*__first)))
274 if (!bool(__pred(*__first)))
278 if (!bool(__pred(*__first)))
282 if (!bool(__pred(*__first)))
287 switch (__last - __first)
290 if (!bool(__pred(*__first)))
294 if (!bool(__pred(*__first)))
298 if (!bool(__pred(*__first)))
310 // set_symmetric_difference
322 * This is an uglified
323 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
324 * overloaded for forward iterators.
326 template<typename _ForwardIterator, typename _Integer, typename _Tp>
328 __search_n(_ForwardIterator __first, _ForwardIterator __last,
329 _Integer __count, const _Tp& __val,
330 std::forward_iterator_tag)
332 __first = _GLIBCXX_STD_A::find(__first, __last, __val);
333 while (__first != __last)
335 typename iterator_traits<_ForwardIterator>::difference_type
337 _ForwardIterator __i = __first;
339 while (__i != __last && __n != 1 && *__i == __val)
348 __first = _GLIBCXX_STD_A::find(++__i, __last, __val);
354 * This is an uglified
355 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
356 * overloaded for random access iterators.
358 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
360 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
361 _Integer __count, const _Tp& __val,
362 std::random_access_iterator_tag)
365 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
368 _DistanceType __tailSize = __last - __first;
369 const _DistanceType __pattSize = __count;
371 if (__tailSize < __pattSize)
374 const _DistanceType __skipOffset = __pattSize - 1;
375 _RandomAccessIter __lookAhead = __first + __skipOffset;
376 __tailSize -= __pattSize;
378 while (1) // the main loop...
380 // __lookAhead here is always pointing to the last element of next
382 while (!(*__lookAhead == __val)) // the skip loop...
384 if (__tailSize < __pattSize)
385 return __last; // Failure
386 __lookAhead += __pattSize;
387 __tailSize -= __pattSize;
389 _DistanceType __remainder = __skipOffset;
390 for (_RandomAccessIter __backTrack = __lookAhead - 1;
391 *__backTrack == __val; --__backTrack)
393 if (--__remainder == 0)
394 return (__lookAhead - __skipOffset); // Success
396 if (__remainder > __tailSize)
397 return __last; // Failure
398 __lookAhead += __remainder;
399 __tailSize -= __remainder;
406 * This is an uglified
407 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
409 * overloaded for forward iterators.
411 template<typename _ForwardIterator, typename _Integer, typename _Tp,
412 typename _BinaryPredicate>
414 __search_n(_ForwardIterator __first, _ForwardIterator __last,
415 _Integer __count, const _Tp& __val,
416 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
418 while (__first != __last && !bool(__binary_pred(*__first, __val)))
421 while (__first != __last)
423 typename iterator_traits<_ForwardIterator>::difference_type
425 _ForwardIterator __i = __first;
427 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
437 while (__first != __last
438 && !bool(__binary_pred(*__first, __val)))
445 * This is an uglified
446 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
448 * overloaded for random access iterators.
450 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
451 typename _BinaryPredicate>
453 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
454 _Integer __count, const _Tp& __val,
455 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
458 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
461 _DistanceType __tailSize = __last - __first;
462 const _DistanceType __pattSize = __count;
464 if (__tailSize < __pattSize)
467 const _DistanceType __skipOffset = __pattSize - 1;
468 _RandomAccessIter __lookAhead = __first + __skipOffset;
469 __tailSize -= __pattSize;
471 while (1) // the main loop...
473 // __lookAhead here is always pointing to the last element of next
475 while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
477 if (__tailSize < __pattSize)
478 return __last; // Failure
479 __lookAhead += __pattSize;
480 __tailSize -= __pattSize;
482 _DistanceType __remainder = __skipOffset;
483 for (_RandomAccessIter __backTrack = __lookAhead - 1;
484 __binary_pred(*__backTrack, __val); --__backTrack)
486 if (--__remainder == 0)
487 return (__lookAhead - __skipOffset); // Success
489 if (__remainder > __tailSize)
490 return __last; // Failure
491 __lookAhead += __remainder;
492 __tailSize -= __remainder;
496 // find_end for forward iterators.
497 template<typename _ForwardIterator1, typename _ForwardIterator2>
499 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
500 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
501 forward_iterator_tag, forward_iterator_tag)
503 if (__first2 == __last2)
507 _ForwardIterator1 __result = __last1;
510 _ForwardIterator1 __new_result
511 = _GLIBCXX_STD_A::search(__first1, __last1, __first2, __last2);
512 if (__new_result == __last1)
516 __result = __new_result;
517 __first1 = __new_result;
524 template<typename _ForwardIterator1, typename _ForwardIterator2,
525 typename _BinaryPredicate>
527 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
528 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
529 forward_iterator_tag, forward_iterator_tag,
530 _BinaryPredicate __comp)
532 if (__first2 == __last2)
536 _ForwardIterator1 __result = __last1;
539 _ForwardIterator1 __new_result
540 = _GLIBCXX_STD_A::search(__first1, __last1, __first2,
542 if (__new_result == __last1)
546 __result = __new_result;
547 __first1 = __new_result;
554 // find_end for bidirectional iterators (much faster).
555 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
556 _BidirectionalIterator1
557 __find_end(_BidirectionalIterator1 __first1,
558 _BidirectionalIterator1 __last1,
559 _BidirectionalIterator2 __first2,
560 _BidirectionalIterator2 __last2,
561 bidirectional_iterator_tag, bidirectional_iterator_tag)
563 // concept requirements
564 __glibcxx_function_requires(_BidirectionalIteratorConcept<
565 _BidirectionalIterator1>)
566 __glibcxx_function_requires(_BidirectionalIteratorConcept<
567 _BidirectionalIterator2>)
569 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
570 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
572 _RevIterator1 __rlast1(__first1);
573 _RevIterator2 __rlast2(__first2);
574 _RevIterator1 __rresult = _GLIBCXX_STD_A::search(_RevIterator1(__last1),
576 _RevIterator2(__last2),
579 if (__rresult == __rlast1)
583 _BidirectionalIterator1 __result = __rresult.base();
584 std::advance(__result, -std::distance(__first2, __last2));
589 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
590 typename _BinaryPredicate>
591 _BidirectionalIterator1
592 __find_end(_BidirectionalIterator1 __first1,
593 _BidirectionalIterator1 __last1,
594 _BidirectionalIterator2 __first2,
595 _BidirectionalIterator2 __last2,
596 bidirectional_iterator_tag, bidirectional_iterator_tag,
597 _BinaryPredicate __comp)
599 // concept requirements
600 __glibcxx_function_requires(_BidirectionalIteratorConcept<
601 _BidirectionalIterator1>)
602 __glibcxx_function_requires(_BidirectionalIteratorConcept<
603 _BidirectionalIterator2>)
605 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
606 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
608 _RevIterator1 __rlast1(__first1);
609 _RevIterator2 __rlast2(__first2);
610 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
611 _RevIterator2(__last2), __rlast2,
614 if (__rresult == __rlast1)
618 _BidirectionalIterator1 __result = __rresult.base();
619 std::advance(__result, -std::distance(__first2, __last2));
625 * @brief Find last matching subsequence in a sequence.
626 * @ingroup non_mutating_algorithms
627 * @param first1 Start of range to search.
628 * @param last1 End of range to search.
629 * @param first2 Start of sequence to match.
630 * @param last2 End of sequence to match.
631 * @return The last iterator @c i in the range
632 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
633 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
634 * such iterator exists.
636 * Searches the range @p [first1,last1) for a sub-sequence that compares
637 * equal value-by-value with the sequence given by @p [first2,last2) and
638 * returns an iterator to the first element of the sub-sequence, or
639 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
640 * last such subsequence contained in [first,last1).
642 * Because the sub-sequence must lie completely within the range
643 * @p [first1,last1) it must start at a position less than
644 * @p last1-(last2-first2) where @p last2-first2 is the length of the
646 * This means that the returned iterator @c i will be in the range
647 * @p [first1,last1-(last2-first2))
649 template<typename _ForwardIterator1, typename _ForwardIterator2>
650 inline _ForwardIterator1
651 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
652 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
654 // concept requirements
655 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
656 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
657 __glibcxx_function_requires(_EqualOpConcept<
658 typename iterator_traits<_ForwardIterator1>::value_type,
659 typename iterator_traits<_ForwardIterator2>::value_type>)
660 __glibcxx_requires_valid_range(__first1, __last1);
661 __glibcxx_requires_valid_range(__first2, __last2);
663 return std::__find_end(__first1, __last1, __first2, __last2,
664 std::__iterator_category(__first1),
665 std::__iterator_category(__first2));
669 * @brief Find last matching subsequence in a sequence using a predicate.
670 * @ingroup non_mutating_algorithms
671 * @param first1 Start of range to search.
672 * @param last1 End of range to search.
673 * @param first2 Start of sequence to match.
674 * @param last2 End of sequence to match.
675 * @param comp The predicate to use.
676 * @return The last iterator @c i in the range
677 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
678 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
679 * @p last1 if no such iterator exists.
681 * Searches the range @p [first1,last1) for a sub-sequence that compares
682 * equal value-by-value with the sequence given by @p [first2,last2) using
683 * comp as a predicate and returns an iterator to the first element of the
684 * sub-sequence, or @p last1 if the sub-sequence is not found. The
685 * sub-sequence will be the last such subsequence contained in
688 * Because the sub-sequence must lie completely within the range
689 * @p [first1,last1) it must start at a position less than
690 * @p last1-(last2-first2) where @p last2-first2 is the length of the
692 * This means that the returned iterator @c i will be in the range
693 * @p [first1,last1-(last2-first2))
695 template<typename _ForwardIterator1, typename _ForwardIterator2,
696 typename _BinaryPredicate>
697 inline _ForwardIterator1
698 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
699 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
700 _BinaryPredicate __comp)
702 // concept requirements
703 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
704 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
705 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
706 typename iterator_traits<_ForwardIterator1>::value_type,
707 typename iterator_traits<_ForwardIterator2>::value_type>)
708 __glibcxx_requires_valid_range(__first1, __last1);
709 __glibcxx_requires_valid_range(__first2, __last2);
711 return std::__find_end(__first1, __last1, __first2, __last2,
712 std::__iterator_category(__first1),
713 std::__iterator_category(__first2),
717 #ifdef __GXX_EXPERIMENTAL_CXX0X__
719 * @brief Checks that a predicate is true for all the elements
721 * @ingroup non_mutating_algorithms
722 * @param first An input iterator.
723 * @param last An input iterator.
724 * @param pred A predicate.
725 * @return True if the check is true, false otherwise.
727 * Returns true if @p pred is true for each element in the range
728 * @p [first,last), and false otherwise.
730 template<typename _InputIterator, typename _Predicate>
732 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
733 { return __last == std::find_if_not(__first, __last, __pred); }
736 * @brief Checks that a predicate is false for all the elements
738 * @ingroup non_mutating_algorithms
739 * @param first An input iterator.
740 * @param last An input iterator.
741 * @param pred A predicate.
742 * @return True if the check is true, false otherwise.
744 * Returns true if @p pred is false for each element in the range
745 * @p [first,last), and false otherwise.
747 template<typename _InputIterator, typename _Predicate>
749 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
750 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
753 * @brief Checks that a predicate is false for at least an element
755 * @ingroup non_mutating_algorithms
756 * @param first An input iterator.
757 * @param last An input iterator.
758 * @param pred A predicate.
759 * @return True if the check is true, false otherwise.
761 * Returns true if an element exists in the range @p [first,last) such that
762 * @p pred is true, and false otherwise.
764 template<typename _InputIterator, typename _Predicate>
766 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
767 { return !std::none_of(__first, __last, __pred); }
770 * @brief Find the first element in a sequence for which a
771 * predicate is false.
772 * @ingroup non_mutating_algorithms
773 * @param first An input iterator.
774 * @param last An input iterator.
775 * @param pred A predicate.
776 * @return The first iterator @c i in the range @p [first,last)
777 * such that @p pred(*i) is false, or @p last if no such iterator exists.
779 template<typename _InputIterator, typename _Predicate>
780 inline _InputIterator
781 find_if_not(_InputIterator __first, _InputIterator __last,
784 // concept requirements
785 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
786 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
787 typename iterator_traits<_InputIterator>::value_type>)
788 __glibcxx_requires_valid_range(__first, __last);
789 return std::__find_if_not(__first, __last, __pred,
790 std::__iterator_category(__first));
794 * @brief Checks whether the sequence is partitioned.
795 * @ingroup mutating_algorithms
796 * @param first An input iterator.
797 * @param last An input iterator.
798 * @param pred A predicate.
799 * @return True if the range @p [first,last) is partioned by @p pred,
800 * i.e. if all elements that satisfy @p pred appear before those that
803 template<typename _InputIterator, typename _Predicate>
805 is_partitioned(_InputIterator __first, _InputIterator __last,
808 __first = std::find_if_not(__first, __last, __pred);
809 return std::none_of(__first, __last, __pred);
813 * @brief Find the partition point of a partitioned range.
814 * @ingroup mutating_algorithms
815 * @param first An iterator.
816 * @param last Another iterator.
817 * @param pred A predicate.
818 * @return An iterator @p mid such that @p all_of(first, mid, pred)
819 * and @p none_of(mid, last, pred) are both true.
821 template<typename _ForwardIterator, typename _Predicate>
823 partition_point(_ForwardIterator __first, _ForwardIterator __last,
826 // concept requirements
827 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
828 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
829 typename iterator_traits<_ForwardIterator>::value_type>)
831 // A specific debug-mode test will be necessary...
832 __glibcxx_requires_valid_range(__first, __last);
834 typedef typename iterator_traits<_ForwardIterator>::difference_type
837 _DistanceType __len = std::distance(__first, __last);
838 _DistanceType __half;
839 _ForwardIterator __middle;
845 std::advance(__middle, __half);
846 if (__pred(*__middle))
850 __len = __len - __half - 1;
861 * @brief Copy a sequence, removing elements of a given value.
862 * @ingroup mutating_algorithms
863 * @param first An input iterator.
864 * @param last An input iterator.
865 * @param result An output iterator.
866 * @param value The value to be removed.
867 * @return An iterator designating the end of the resulting sequence.
869 * Copies each element in the range @p [first,last) not equal to @p value
870 * to the range beginning at @p result.
871 * remove_copy() is stable, so the relative order of elements that are
872 * copied is unchanged.
874 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
876 remove_copy(_InputIterator __first, _InputIterator __last,
877 _OutputIterator __result, const _Tp& __value)
879 // concept requirements
880 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
881 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
882 typename iterator_traits<_InputIterator>::value_type>)
883 __glibcxx_function_requires(_EqualOpConcept<
884 typename iterator_traits<_InputIterator>::value_type, _Tp>)
885 __glibcxx_requires_valid_range(__first, __last);
887 for (; __first != __last; ++__first)
888 if (!(*__first == __value))
890 *__result = *__first;
897 * @brief Copy a sequence, removing elements for which a predicate is true.
898 * @ingroup mutating_algorithms
899 * @param first An input iterator.
900 * @param last An input iterator.
901 * @param result An output iterator.
902 * @param pred A predicate.
903 * @return An iterator designating the end of the resulting sequence.
905 * Copies each element in the range @p [first,last) for which
906 * @p pred returns false to the range beginning at @p result.
908 * remove_copy_if() is stable, so the relative order of elements that are
909 * copied is unchanged.
911 template<typename _InputIterator, typename _OutputIterator,
914 remove_copy_if(_InputIterator __first, _InputIterator __last,
915 _OutputIterator __result, _Predicate __pred)
917 // concept requirements
918 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
919 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
920 typename iterator_traits<_InputIterator>::value_type>)
921 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
922 typename iterator_traits<_InputIterator>::value_type>)
923 __glibcxx_requires_valid_range(__first, __last);
925 for (; __first != __last; ++__first)
926 if (!bool(__pred(*__first)))
928 *__result = *__first;
934 #ifdef __GXX_EXPERIMENTAL_CXX0X__
936 * @brief Copy the elements of a sequence for which a predicate is true.
937 * @ingroup mutating_algorithms
938 * @param first An input iterator.
939 * @param last An input iterator.
940 * @param result An output iterator.
941 * @param pred A predicate.
942 * @return An iterator designating the end of the resulting sequence.
944 * Copies each element in the range @p [first,last) for which
945 * @p pred returns true to the range beginning at @p result.
947 * copy_if() is stable, so the relative order of elements that are
948 * copied is unchanged.
950 template<typename _InputIterator, typename _OutputIterator,
953 copy_if(_InputIterator __first, _InputIterator __last,
954 _OutputIterator __result, _Predicate __pred)
956 // concept requirements
957 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
958 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
959 typename iterator_traits<_InputIterator>::value_type>)
960 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
961 typename iterator_traits<_InputIterator>::value_type>)
962 __glibcxx_requires_valid_range(__first, __last);
964 for (; __first != __last; ++__first)
965 if (__pred(*__first))
967 *__result = *__first;
974 template<typename _InputIterator, typename _Size, typename _OutputIterator>
976 __copy_n(_InputIterator __first, _Size __n,
977 _OutputIterator __result, input_iterator_tag)
979 for (; __n > 0; --__n)
981 *__result = *__first;
988 template<typename _RandomAccessIterator, typename _Size,
989 typename _OutputIterator>
990 inline _OutputIterator
991 __copy_n(_RandomAccessIterator __first, _Size __n,
992 _OutputIterator __result, random_access_iterator_tag)
993 { return std::copy(__first, __first + __n, __result); }
996 * @brief Copies the range [first,first+n) into [result,result+n).
997 * @ingroup mutating_algorithms
998 * @param first An input iterator.
999 * @param n The number of elements to copy.
1000 * @param result An output iterator.
1003 * This inline function will boil down to a call to @c memmove whenever
1004 * possible. Failing that, if random access iterators are passed, then the
1005 * loop count will be known (and therefore a candidate for compiler
1006 * optimizations such as unrolling).
1008 template<typename _InputIterator, typename _Size, typename _OutputIterator>
1009 inline _OutputIterator
1010 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1012 // concept requirements
1013 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1014 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1015 typename iterator_traits<_InputIterator>::value_type>)
1017 return std::__copy_n(__first, __n, __result,
1018 std::__iterator_category(__first));
1022 * @brief Copy the elements of a sequence to separate output sequences
1023 * depending on the truth value of a predicate.
1024 * @ingroup mutating_algorithms
1025 * @param first An input iterator.
1026 * @param last An input iterator.
1027 * @param out_true An output iterator.
1028 * @param out_false An output iterator.
1029 * @param pred A predicate.
1030 * @return A pair designating the ends of the resulting sequences.
1032 * Copies each element in the range @p [first,last) for which
1033 * @p pred returns true to the range beginning at @p out_true
1034 * and each element for which @p pred returns false to @p out_false.
1036 template<typename _InputIterator, typename _OutputIterator1,
1037 typename _OutputIterator2, typename _Predicate>
1038 pair<_OutputIterator1, _OutputIterator2>
1039 partition_copy(_InputIterator __first, _InputIterator __last,
1040 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
1043 // concept requirements
1044 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1045 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
1046 typename iterator_traits<_InputIterator>::value_type>)
1047 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
1048 typename iterator_traits<_InputIterator>::value_type>)
1049 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1050 typename iterator_traits<_InputIterator>::value_type>)
1051 __glibcxx_requires_valid_range(__first, __last);
1053 for (; __first != __last; ++__first)
1054 if (__pred(*__first))
1056 *__out_true = *__first;
1061 *__out_false = *__first;
1065 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
1070 * @brief Remove elements from a sequence.
1071 * @ingroup mutating_algorithms
1072 * @param first An input iterator.
1073 * @param last An input iterator.
1074 * @param value The value to be removed.
1075 * @return An iterator designating the end of the resulting sequence.
1077 * All elements equal to @p value are removed from the range
1080 * remove() is stable, so the relative order of elements that are
1081 * not removed is unchanged.
1083 * Elements between the end of the resulting sequence and @p last
1084 * are still present, but their value is unspecified.
1086 template<typename _ForwardIterator, typename _Tp>
1088 remove(_ForwardIterator __first, _ForwardIterator __last,
1091 // concept requirements
1092 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1094 __glibcxx_function_requires(_EqualOpConcept<
1095 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1096 __glibcxx_requires_valid_range(__first, __last);
1098 __first = _GLIBCXX_STD_A::find(__first, __last, __value);
1099 if(__first == __last)
1101 _ForwardIterator __result = __first;
1103 for(; __first != __last; ++__first)
1104 if(!(*__first == __value))
1106 *__result = _GLIBCXX_MOVE(*__first);
1113 * @brief Remove elements from a sequence using a predicate.
1114 * @ingroup mutating_algorithms
1115 * @param first A forward iterator.
1116 * @param last A forward iterator.
1117 * @param pred A predicate.
1118 * @return An iterator designating the end of the resulting sequence.
1120 * All elements for which @p pred returns true are removed from the range
1123 * remove_if() is stable, so the relative order of elements that are
1124 * not removed is unchanged.
1126 * Elements between the end of the resulting sequence and @p last
1127 * are still present, but their value is unspecified.
1129 template<typename _ForwardIterator, typename _Predicate>
1131 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1134 // concept requirements
1135 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1137 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1138 typename iterator_traits<_ForwardIterator>::value_type>)
1139 __glibcxx_requires_valid_range(__first, __last);
1141 __first = _GLIBCXX_STD_A::find_if(__first, __last, __pred);
1142 if(__first == __last)
1144 _ForwardIterator __result = __first;
1146 for(; __first != __last; ++__first)
1147 if(!bool(__pred(*__first)))
1149 *__result = _GLIBCXX_MOVE(*__first);
1156 * @brief Remove consecutive duplicate values from a sequence.
1157 * @ingroup mutating_algorithms
1158 * @param first A forward iterator.
1159 * @param last A forward iterator.
1160 * @return An iterator designating the end of the resulting sequence.
1162 * Removes all but the first element from each group of consecutive
1163 * values that compare equal.
1164 * unique() is stable, so the relative order of elements that are
1165 * not removed is unchanged.
1166 * Elements between the end of the resulting sequence and @p last
1167 * are still present, but their value is unspecified.
1169 template<typename _ForwardIterator>
1171 unique(_ForwardIterator __first, _ForwardIterator __last)
1173 // concept requirements
1174 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1176 __glibcxx_function_requires(_EqualityComparableConcept<
1177 typename iterator_traits<_ForwardIterator>::value_type>)
1178 __glibcxx_requires_valid_range(__first, __last);
1180 // Skip the beginning, if already unique.
1181 __first = _GLIBCXX_STD_A::adjacent_find(__first, __last);
1182 if (__first == __last)
1185 // Do the real copy work.
1186 _ForwardIterator __dest = __first;
1188 while (++__first != __last)
1189 if (!(*__dest == *__first))
1190 *++__dest = _GLIBCXX_MOVE(*__first);
1195 * @brief Remove consecutive values from a sequence using a predicate.
1196 * @ingroup mutating_algorithms
1197 * @param first A forward iterator.
1198 * @param last A forward iterator.
1199 * @param binary_pred A binary predicate.
1200 * @return An iterator designating the end of the resulting sequence.
1202 * Removes all but the first element from each group of consecutive
1203 * values for which @p binary_pred returns true.
1204 * unique() is stable, so the relative order of elements that are
1205 * not removed is unchanged.
1206 * Elements between the end of the resulting sequence and @p last
1207 * are still present, but their value is unspecified.
1209 template<typename _ForwardIterator, typename _BinaryPredicate>
1211 unique(_ForwardIterator __first, _ForwardIterator __last,
1212 _BinaryPredicate __binary_pred)
1214 // concept requirements
1215 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1217 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1218 typename iterator_traits<_ForwardIterator>::value_type,
1219 typename iterator_traits<_ForwardIterator>::value_type>)
1220 __glibcxx_requires_valid_range(__first, __last);
1222 // Skip the beginning, if already unique.
1223 __first = _GLIBCXX_STD_A::adjacent_find(__first, __last, __binary_pred);
1224 if (__first == __last)
1227 // Do the real copy work.
1228 _ForwardIterator __dest = __first;
1230 while (++__first != __last)
1231 if (!bool(__binary_pred(*__dest, *__first)))
1232 *++__dest = _GLIBCXX_MOVE(*__first);
1237 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1239 * overloaded for forward iterators and output iterator as result.
1241 template<typename _ForwardIterator, typename _OutputIterator>
1243 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1244 _OutputIterator __result,
1245 forward_iterator_tag, output_iterator_tag)
1247 // concept requirements -- taken care of in dispatching function
1248 _ForwardIterator __next = __first;
1249 *__result = *__first;
1250 while (++__next != __last)
1251 if (!(*__first == *__next))
1254 *++__result = *__first;
1260 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1262 * overloaded for input iterators and output iterator as result.
1264 template<typename _InputIterator, typename _OutputIterator>
1266 __unique_copy(_InputIterator __first, _InputIterator __last,
1267 _OutputIterator __result,
1268 input_iterator_tag, output_iterator_tag)
1270 // concept requirements -- taken care of in dispatching function
1271 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1272 *__result = __value;
1273 while (++__first != __last)
1274 if (!(__value == *__first))
1277 *++__result = __value;
1283 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1285 * overloaded for input iterators and forward iterator as result.
1287 template<typename _InputIterator, typename _ForwardIterator>
1289 __unique_copy(_InputIterator __first, _InputIterator __last,
1290 _ForwardIterator __result,
1291 input_iterator_tag, forward_iterator_tag)
1293 // concept requirements -- taken care of in dispatching function
1294 *__result = *__first;
1295 while (++__first != __last)
1296 if (!(*__result == *__first))
1297 *++__result = *__first;
1302 * This is an uglified
1303 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1305 * overloaded for forward iterators and output iterator as result.
1307 template<typename _ForwardIterator, typename _OutputIterator,
1308 typename _BinaryPredicate>
1310 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1311 _OutputIterator __result, _BinaryPredicate __binary_pred,
1312 forward_iterator_tag, output_iterator_tag)
1314 // concept requirements -- iterators already checked
1315 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1316 typename iterator_traits<_ForwardIterator>::value_type,
1317 typename iterator_traits<_ForwardIterator>::value_type>)
1319 _ForwardIterator __next = __first;
1320 *__result = *__first;
1321 while (++__next != __last)
1322 if (!bool(__binary_pred(*__first, *__next)))
1325 *++__result = *__first;
1331 * This is an uglified
1332 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1334 * overloaded for input iterators and output iterator as result.
1336 template<typename _InputIterator, typename _OutputIterator,
1337 typename _BinaryPredicate>
1339 __unique_copy(_InputIterator __first, _InputIterator __last,
1340 _OutputIterator __result, _BinaryPredicate __binary_pred,
1341 input_iterator_tag, output_iterator_tag)
1343 // concept requirements -- iterators already checked
1344 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1345 typename iterator_traits<_InputIterator>::value_type,
1346 typename iterator_traits<_InputIterator>::value_type>)
1348 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1349 *__result = __value;
1350 while (++__first != __last)
1351 if (!bool(__binary_pred(__value, *__first)))
1354 *++__result = __value;
1360 * This is an uglified
1361 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1363 * overloaded for input iterators and forward iterator as result.
1365 template<typename _InputIterator, typename _ForwardIterator,
1366 typename _BinaryPredicate>
1368 __unique_copy(_InputIterator __first, _InputIterator __last,
1369 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1370 input_iterator_tag, forward_iterator_tag)
1372 // concept requirements -- iterators already checked
1373 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1374 typename iterator_traits<_ForwardIterator>::value_type,
1375 typename iterator_traits<_InputIterator>::value_type>)
1377 *__result = *__first;
1378 while (++__first != __last)
1379 if (!bool(__binary_pred(*__result, *__first)))
1380 *++__result = *__first;
1385 * This is an uglified reverse(_BidirectionalIterator,
1386 * _BidirectionalIterator)
1387 * overloaded for bidirectional iterators.
1389 template<typename _BidirectionalIterator>
1391 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1392 bidirectional_iterator_tag)
1395 if (__first == __last || __first == --__last)
1399 std::iter_swap(__first, __last);
1405 * This is an uglified reverse(_BidirectionalIterator,
1406 * _BidirectionalIterator)
1407 * overloaded for random access iterators.
1409 template<typename _RandomAccessIterator>
1411 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1412 random_access_iterator_tag)
1414 if (__first == __last)
1417 while (__first < __last)
1419 std::iter_swap(__first, __last);
1426 * @brief Reverse a sequence.
1427 * @ingroup mutating_algorithms
1428 * @param first A bidirectional iterator.
1429 * @param last A bidirectional iterator.
1430 * @return reverse() returns no value.
1432 * Reverses the order of the elements in the range @p [first,last),
1433 * so that the first element becomes the last etc.
1434 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1435 * swaps @p *(first+i) and @p *(last-(i+1))
1437 template<typename _BidirectionalIterator>
1439 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1441 // concept requirements
1442 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1443 _BidirectionalIterator>)
1444 __glibcxx_requires_valid_range(__first, __last);
1445 std::__reverse(__first, __last, std::__iterator_category(__first));
1449 * @brief Copy a sequence, reversing its elements.
1450 * @ingroup mutating_algorithms
1451 * @param first A bidirectional iterator.
1452 * @param last A bidirectional iterator.
1453 * @param result An output iterator.
1454 * @return An iterator designating the end of the resulting sequence.
1456 * Copies the elements in the range @p [first,last) to the range
1457 * @p [result,result+(last-first)) such that the order of the
1458 * elements is reversed.
1459 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1460 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1461 * The ranges @p [first,last) and @p [result,result+(last-first))
1464 template<typename _BidirectionalIterator, typename _OutputIterator>
1466 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1467 _OutputIterator __result)
1469 // concept requirements
1470 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1471 _BidirectionalIterator>)
1472 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1473 typename iterator_traits<_BidirectionalIterator>::value_type>)
1474 __glibcxx_requires_valid_range(__first, __last);
1476 while (__first != __last)
1479 *__result = *__last;
1486 * This is a helper function for the rotate algorithm specialized on RAIs.
1487 * It returns the greatest common divisor of two integer values.
1489 template<typename _EuclideanRingElement>
1490 _EuclideanRingElement
1491 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1495 _EuclideanRingElement __t = __m % __n;
1502 /// This is a helper function for the rotate algorithm.
1503 template<typename _ForwardIterator>
1505 __rotate(_ForwardIterator __first,
1506 _ForwardIterator __middle,
1507 _ForwardIterator __last,
1508 forward_iterator_tag)
1510 if (__first == __middle || __last == __middle)
1513 _ForwardIterator __first2 = __middle;
1516 std::iter_swap(__first, __first2);
1519 if (__first == __middle)
1520 __middle = __first2;
1522 while (__first2 != __last);
1524 __first2 = __middle;
1526 while (__first2 != __last)
1528 std::iter_swap(__first, __first2);
1531 if (__first == __middle)
1532 __middle = __first2;
1533 else if (__first2 == __last)
1534 __first2 = __middle;
1538 /// This is a helper function for the rotate algorithm.
1539 template<typename _BidirectionalIterator>
1541 __rotate(_BidirectionalIterator __first,
1542 _BidirectionalIterator __middle,
1543 _BidirectionalIterator __last,
1544 bidirectional_iterator_tag)
1546 // concept requirements
1547 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1548 _BidirectionalIterator>)
1550 if (__first == __middle || __last == __middle)
1553 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1554 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1556 while (__first != __middle && __middle != __last)
1558 std::iter_swap(__first, --__last);
1562 if (__first == __middle)
1563 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1565 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1568 /// This is a helper function for the rotate algorithm.
1569 template<typename _RandomAccessIterator>
1571 __rotate(_RandomAccessIterator __first,
1572 _RandomAccessIterator __middle,
1573 _RandomAccessIterator __last,
1574 random_access_iterator_tag)
1576 // concept requirements
1577 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1578 _RandomAccessIterator>)
1580 if (__first == __middle || __last == __middle)
1583 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1585 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1588 _Distance __n = __last - __first;
1589 _Distance __k = __middle - __first;
1591 if (__k == __n - __k)
1593 std::swap_ranges(__first, __middle, __middle);
1597 _RandomAccessIterator __p = __first;
1601 if (__k < __n - __k)
1603 if (__is_pod(_ValueType) && __k == 1)
1605 _ValueType __t = _GLIBCXX_MOVE(*__p);
1606 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
1607 *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
1610 _RandomAccessIterator __q = __p + __k;
1611 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1613 std::iter_swap(__p, __q);
1620 std::swap(__n, __k);
1626 if (__is_pod(_ValueType) && __k == 1)
1628 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
1629 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
1630 *__p = _GLIBCXX_MOVE(__t);
1633 _RandomAccessIterator __q = __p + __n;
1635 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1639 std::iter_swap(__p, __q);
1644 std::swap(__n, __k);
1650 * @brief Rotate the elements of a sequence.
1651 * @ingroup mutating_algorithms
1652 * @param first A forward iterator.
1653 * @param middle A forward iterator.
1654 * @param last A forward iterator.
1657 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1658 * positions so that the element at @p middle is moved to @p first, the
1659 * element at @p middle+1 is moved to @first+1 and so on for each element
1660 * in the range @p [first,last).
1662 * This effectively swaps the ranges @p [first,middle) and
1665 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1666 * each @p n in the range @p [0,last-first).
1668 template<typename _ForwardIterator>
1670 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1671 _ForwardIterator __last)
1673 // concept requirements
1674 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1676 __glibcxx_requires_valid_range(__first, __middle);
1677 __glibcxx_requires_valid_range(__middle, __last);
1679 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1681 std::__rotate(__first, __middle, __last, _IterType());
1685 * @brief Copy a sequence, rotating its elements.
1686 * @ingroup mutating_algorithms
1687 * @param first A forward iterator.
1688 * @param middle A forward iterator.
1689 * @param last A forward iterator.
1690 * @param result An output iterator.
1691 * @return An iterator designating the end of the resulting sequence.
1693 * Copies the elements of the range @p [first,last) to the range
1694 * beginning at @result, rotating the copied elements by @p (middle-first)
1695 * positions so that the element at @p middle is moved to @p result, the
1696 * element at @p middle+1 is moved to @result+1 and so on for each element
1697 * in the range @p [first,last).
1699 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1700 * each @p n in the range @p [0,last-first).
1702 template<typename _ForwardIterator, typename _OutputIterator>
1704 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1705 _ForwardIterator __last, _OutputIterator __result)
1707 // concept requirements
1708 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1709 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1710 typename iterator_traits<_ForwardIterator>::value_type>)
1711 __glibcxx_requires_valid_range(__first, __middle);
1712 __glibcxx_requires_valid_range(__middle, __last);
1714 return std::copy(__first, __middle,
1715 std::copy(__middle, __last, __result));
1718 /// This is a helper function...
1719 template<typename _ForwardIterator, typename _Predicate>
1721 __partition(_ForwardIterator __first, _ForwardIterator __last,
1722 _Predicate __pred, forward_iterator_tag)
1724 if (__first == __last)
1727 while (__pred(*__first))
1728 if (++__first == __last)
1731 _ForwardIterator __next = __first;
1733 while (++__next != __last)
1734 if (__pred(*__next))
1736 std::iter_swap(__first, __next);
1743 /// This is a helper function...
1744 template<typename _BidirectionalIterator, typename _Predicate>
1745 _BidirectionalIterator
1746 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1747 _Predicate __pred, bidirectional_iterator_tag)
1752 if (__first == __last)
1754 else if (__pred(*__first))
1760 if (__first == __last)
1762 else if (!bool(__pred(*__last)))
1766 std::iter_swap(__first, __last);
1773 /// This is a helper function...
1774 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1776 __inplace_stable_partition(_ForwardIterator __first,
1777 _ForwardIterator __last,
1778 _Predicate __pred, _Distance __len)
1781 return __pred(*__first) ? __last : __first;
1782 _ForwardIterator __middle = __first;
1783 std::advance(__middle, __len / 2);
1784 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1788 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1792 std::rotate(__begin, __middle, __end);
1793 std::advance(__begin, std::distance(__middle, __end));
1797 /// This is a helper function...
1798 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1801 __stable_partition_adaptive(_ForwardIterator __first,
1802 _ForwardIterator __last,
1803 _Predicate __pred, _Distance __len,
1805 _Distance __buffer_size)
1807 if (__len <= __buffer_size)
1809 _ForwardIterator __result1 = __first;
1810 _Pointer __result2 = __buffer;
1811 for (; __first != __last; ++__first)
1812 if (__pred(*__first))
1814 if (__result1 != __first)
1815 *__result1 = _GLIBCXX_MOVE(*__first);
1820 *__result2 = _GLIBCXX_MOVE(*__first);
1823 _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1828 _ForwardIterator __middle = __first;
1829 std::advance(__middle, __len / 2);
1830 _ForwardIterator __begin =
1831 std::__stable_partition_adaptive(__first, __middle, __pred,
1832 __len / 2, __buffer,
1834 _ForwardIterator __end =
1835 std::__stable_partition_adaptive(__middle, __last, __pred,
1837 __buffer, __buffer_size);
1838 std::rotate(__begin, __middle, __end);
1839 std::advance(__begin, std::distance(__middle, __end));
1845 * @brief Move elements for which a predicate is true to the beginning
1846 * of a sequence, preserving relative ordering.
1847 * @ingroup mutating_algorithms
1848 * @param first A forward iterator.
1849 * @param last A forward iterator.
1850 * @param pred A predicate functor.
1851 * @return An iterator @p middle such that @p pred(i) is true for each
1852 * iterator @p i in the range @p [first,middle) and false for each @p i
1853 * in the range @p [middle,last).
1855 * Performs the same function as @p partition() with the additional
1856 * guarantee that the relative ordering of elements in each group is
1857 * preserved, so any two elements @p x and @p y in the range
1858 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1859 * relative ordering after calling @p stable_partition().
1861 template<typename _ForwardIterator, typename _Predicate>
1863 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1866 // concept requirements
1867 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1869 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1870 typename iterator_traits<_ForwardIterator>::value_type>)
1871 __glibcxx_requires_valid_range(__first, __last);
1873 if (__first == __last)
1877 typedef typename iterator_traits<_ForwardIterator>::value_type
1879 typedef typename iterator_traits<_ForwardIterator>::difference_type
1882 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1884 if (__buf.size() > 0)
1886 std::__stable_partition_adaptive(__first, __last, __pred,
1887 _DistanceType(__buf.requested_size()),
1889 _DistanceType(__buf.size()));
1892 std::__inplace_stable_partition(__first, __last, __pred,
1893 _DistanceType(__buf.requested_size()));
1897 /// This is a helper function for the sort routines.
1898 template<typename _RandomAccessIterator>
1900 __heap_select(_RandomAccessIterator __first,
1901 _RandomAccessIterator __middle,
1902 _RandomAccessIterator __last)
1904 std::make_heap(__first, __middle);
1905 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1906 if (*__i < *__first)
1907 std::__pop_heap(__first, __middle, __i);
1910 /// This is a helper function for the sort routines.
1911 template<typename _RandomAccessIterator, typename _Compare>
1913 __heap_select(_RandomAccessIterator __first,
1914 _RandomAccessIterator __middle,
1915 _RandomAccessIterator __last, _Compare __comp)
1917 std::make_heap(__first, __middle, __comp);
1918 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1919 if (__comp(*__i, *__first))
1920 std::__pop_heap(__first, __middle, __i, __comp);
1926 * @brief Copy the smallest elements of a sequence.
1927 * @ingroup sorting_algorithms
1928 * @param first An iterator.
1929 * @param last Another iterator.
1930 * @param result_first A random-access iterator.
1931 * @param result_last Another random-access iterator.
1932 * @return An iterator indicating the end of the resulting sequence.
1934 * Copies and sorts the smallest N values from the range @p [first,last)
1935 * to the range beginning at @p result_first, where the number of
1936 * elements to be copied, @p N, is the smaller of @p (last-first) and
1937 * @p (result_last-result_first).
1938 * After the sort if @p i and @j are iterators in the range
1939 * @p [result_first,result_first+N) such that @i precedes @j then
1940 * @p *j<*i is false.
1941 * The value returned is @p result_first+N.
1943 template<typename _InputIterator, typename _RandomAccessIterator>
1944 _RandomAccessIterator
1945 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1946 _RandomAccessIterator __result_first,
1947 _RandomAccessIterator __result_last)
1949 typedef typename iterator_traits<_InputIterator>::value_type
1951 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1953 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1956 // concept requirements
1957 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1958 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1960 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1962 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1963 __glibcxx_requires_valid_range(__first, __last);
1964 __glibcxx_requires_valid_range(__result_first, __result_last);
1966 if (__result_first == __result_last)
1967 return __result_last;
1968 _RandomAccessIterator __result_real_last = __result_first;
1969 while(__first != __last && __result_real_last != __result_last)
1971 *__result_real_last = *__first;
1972 ++__result_real_last;
1975 std::make_heap(__result_first, __result_real_last);
1976 while (__first != __last)
1978 if (*__first < *__result_first)
1979 std::__adjust_heap(__result_first, _DistanceType(0),
1980 _DistanceType(__result_real_last
1982 _InputValueType(*__first));
1985 std::sort_heap(__result_first, __result_real_last);
1986 return __result_real_last;
1990 * @brief Copy the smallest elements of a sequence using a predicate for
1992 * @ingroup sorting_algorithms
1993 * @param first An input iterator.
1994 * @param last Another input iterator.
1995 * @param result_first A random-access iterator.
1996 * @param result_last Another random-access iterator.
1997 * @param comp A comparison functor.
1998 * @return An iterator indicating the end of the resulting sequence.
2000 * Copies and sorts the smallest N values from the range @p [first,last)
2001 * to the range beginning at @p result_first, where the number of
2002 * elements to be copied, @p N, is the smaller of @p (last-first) and
2003 * @p (result_last-result_first).
2004 * After the sort if @p i and @j are iterators in the range
2005 * @p [result_first,result_first+N) such that @i precedes @j then
2006 * @p comp(*j,*i) is false.
2007 * The value returned is @p result_first+N.
2009 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2010 _RandomAccessIterator
2011 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2012 _RandomAccessIterator __result_first,
2013 _RandomAccessIterator __result_last,
2016 typedef typename iterator_traits<_InputIterator>::value_type
2018 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2020 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2023 // concept requirements
2024 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2025 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2026 _RandomAccessIterator>)
2027 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2029 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2030 _InputValueType, _OutputValueType>)
2031 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2032 _OutputValueType, _OutputValueType>)
2033 __glibcxx_requires_valid_range(__first, __last);
2034 __glibcxx_requires_valid_range(__result_first, __result_last);
2036 if (__result_first == __result_last)
2037 return __result_last;
2038 _RandomAccessIterator __result_real_last = __result_first;
2039 while(__first != __last && __result_real_last != __result_last)
2041 *__result_real_last = *__first;
2042 ++__result_real_last;
2045 std::make_heap(__result_first, __result_real_last, __comp);
2046 while (__first != __last)
2048 if (__comp(*__first, *__result_first))
2049 std::__adjust_heap(__result_first, _DistanceType(0),
2050 _DistanceType(__result_real_last
2052 _InputValueType(*__first),
2056 std::sort_heap(__result_first, __result_real_last, __comp);
2057 return __result_real_last;
2060 /// This is a helper function for the sort routine.
2061 template<typename _RandomAccessIterator>
2063 __unguarded_linear_insert(_RandomAccessIterator __last)
2065 typename iterator_traits<_RandomAccessIterator>::value_type
2066 __val = _GLIBCXX_MOVE(*__last);
2067 _RandomAccessIterator __next = __last;
2069 while (__val < *__next)
2071 *__last = _GLIBCXX_MOVE(*__next);
2075 *__last = _GLIBCXX_MOVE(__val);
2078 /// This is a helper function for the sort routine.
2079 template<typename _RandomAccessIterator, typename _Compare>
2081 __unguarded_linear_insert(_RandomAccessIterator __last,
2084 typename iterator_traits<_RandomAccessIterator>::value_type
2085 __val = _GLIBCXX_MOVE(*__last);
2086 _RandomAccessIterator __next = __last;
2088 while (__comp(__val, *__next))
2090 *__last = _GLIBCXX_MOVE(*__next);
2094 *__last = _GLIBCXX_MOVE(__val);
2097 /// This is a helper function for the sort routine.
2098 template<typename _RandomAccessIterator>
2100 __insertion_sort(_RandomAccessIterator __first,
2101 _RandomAccessIterator __last)
2103 if (__first == __last)
2106 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2108 if (*__i < *__first)
2110 typename iterator_traits<_RandomAccessIterator>::value_type
2111 __val = _GLIBCXX_MOVE(*__i);
2112 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2113 *__first = _GLIBCXX_MOVE(__val);
2116 std::__unguarded_linear_insert(__i);
2120 /// This is a helper function for the sort routine.
2121 template<typename _RandomAccessIterator, typename _Compare>
2123 __insertion_sort(_RandomAccessIterator __first,
2124 _RandomAccessIterator __last, _Compare __comp)
2126 if (__first == __last) return;
2128 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2130 if (__comp(*__i, *__first))
2132 typename iterator_traits<_RandomAccessIterator>::value_type
2133 __val = _GLIBCXX_MOVE(*__i);
2134 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2135 *__first = _GLIBCXX_MOVE(__val);
2138 std::__unguarded_linear_insert(__i, __comp);
2142 /// This is a helper function for the sort routine.
2143 template<typename _RandomAccessIterator>
2145 __unguarded_insertion_sort(_RandomAccessIterator __first,
2146 _RandomAccessIterator __last)
2148 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2151 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2152 std::__unguarded_linear_insert(__i);
2155 /// This is a helper function for the sort routine.
2156 template<typename _RandomAccessIterator, typename _Compare>
2158 __unguarded_insertion_sort(_RandomAccessIterator __first,
2159 _RandomAccessIterator __last, _Compare __comp)
2161 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2164 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2165 std::__unguarded_linear_insert(__i, __comp);
2170 * This controls some aspect of the sort routines.
2172 enum { _S_threshold = 16 };
2174 /// This is a helper function for the sort routine.
2175 template<typename _RandomAccessIterator>
2177 __final_insertion_sort(_RandomAccessIterator __first,
2178 _RandomAccessIterator __last)
2180 if (__last - __first > int(_S_threshold))
2182 std::__insertion_sort(__first, __first + int(_S_threshold));
2183 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2186 std::__insertion_sort(__first, __last);
2189 /// This is a helper function for the sort routine.
2190 template<typename _RandomAccessIterator, typename _Compare>
2192 __final_insertion_sort(_RandomAccessIterator __first,
2193 _RandomAccessIterator __last, _Compare __comp)
2195 if (__last - __first > int(_S_threshold))
2197 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2198 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2202 std::__insertion_sort(__first, __last, __comp);
2205 /// This is a helper function...
2206 template<typename _RandomAccessIterator, typename _Tp>
2207 _RandomAccessIterator
2208 __unguarded_partition(_RandomAccessIterator __first,
2209 _RandomAccessIterator __last, const _Tp& __pivot)
2213 while (*__first < __pivot)
2216 while (__pivot < *__last)
2218 if (!(__first < __last))
2220 std::iter_swap(__first, __last);
2225 /// This is a helper function...
2226 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2227 _RandomAccessIterator
2228 __unguarded_partition(_RandomAccessIterator __first,
2229 _RandomAccessIterator __last,
2230 const _Tp& __pivot, _Compare __comp)
2234 while (__comp(*__first, __pivot))
2237 while (__comp(__pivot, *__last))
2239 if (!(__first < __last))
2241 std::iter_swap(__first, __last);
2246 /// This is a helper function...
2247 template<typename _RandomAccessIterator>
2248 inline _RandomAccessIterator
2249 __unguarded_partition_pivot(_RandomAccessIterator __first,
2250 _RandomAccessIterator __last)
2252 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2253 std::__move_median_first(__first, __mid, (__last - 1));
2254 return std::__unguarded_partition(__first + 1, __last, *__first);
2258 /// This is a helper function...
2259 template<typename _RandomAccessIterator, typename _Compare>
2260 inline _RandomAccessIterator
2261 __unguarded_partition_pivot(_RandomAccessIterator __first,
2262 _RandomAccessIterator __last, _Compare __comp)
2264 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2265 std::__move_median_first(__first, __mid, (__last - 1), __comp);
2266 return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
2269 /// This is a helper function for the sort routine.
2270 template<typename _RandomAccessIterator, typename _Size>
2272 __introsort_loop(_RandomAccessIterator __first,
2273 _RandomAccessIterator __last,
2274 _Size __depth_limit)
2276 while (__last - __first > int(_S_threshold))
2278 if (__depth_limit == 0)
2280 _GLIBCXX_STD_A::partial_sort(__first, __last, __last);
2284 _RandomAccessIterator __cut =
2285 std::__unguarded_partition_pivot(__first, __last);
2286 std::__introsort_loop(__cut, __last, __depth_limit);
2291 /// This is a helper function for the sort routine.
2292 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2294 __introsort_loop(_RandomAccessIterator __first,
2295 _RandomAccessIterator __last,
2296 _Size __depth_limit, _Compare __comp)
2298 while (__last - __first > int(_S_threshold))
2300 if (__depth_limit == 0)
2302 _GLIBCXX_STD_A::partial_sort(__first, __last, __last, __comp);
2306 _RandomAccessIterator __cut =
2307 std::__unguarded_partition_pivot(__first, __last, __comp);
2308 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2315 template<typename _RandomAccessIterator, typename _Size>
2317 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2318 _RandomAccessIterator __last, _Size __depth_limit)
2320 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2323 while (__last - __first > 3)
2325 if (__depth_limit == 0)
2327 std::__heap_select(__first, __nth + 1, __last);
2329 // Place the nth largest element in its final position.
2330 std::iter_swap(__first, __nth);
2334 _RandomAccessIterator __cut =
2335 std::__unguarded_partition_pivot(__first, __last);
2341 std::__insertion_sort(__first, __last);
2344 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2346 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2347 _RandomAccessIterator __last, _Size __depth_limit,
2350 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2353 while (__last - __first > 3)
2355 if (__depth_limit == 0)
2357 std::__heap_select(__first, __nth + 1, __last, __comp);
2358 // Place the nth largest element in its final position.
2359 std::iter_swap(__first, __nth);
2363 _RandomAccessIterator __cut =
2364 std::__unguarded_partition_pivot(__first, __last, __comp);
2370 std::__insertion_sort(__first, __last, __comp);
2375 // lower_bound moved to stl_algobase.h
2378 * @brief Finds the first position in which @a val could be inserted
2379 * without changing the ordering.
2380 * @ingroup binary_search_algorithms
2381 * @param first An iterator.
2382 * @param last Another iterator.
2383 * @param val The search term.
2384 * @param comp A functor to use for comparisons.
2385 * @return An iterator pointing to the first element <em>not less
2386 * than</em> @a val, or end() if every element is less
2388 * @ingroup binary_search_algorithms
2390 * The comparison function should have the same effects on ordering as
2391 * the function used for the initial sort.
2393 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2395 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2396 const _Tp& __val, _Compare __comp)
2398 typedef typename iterator_traits<_ForwardIterator>::value_type
2400 typedef typename iterator_traits<_ForwardIterator>::difference_type
2403 // concept requirements
2404 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2405 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2407 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2410 _DistanceType __len = std::distance(__first, __last);
2414 _DistanceType __half = __len >> 1;
2415 _ForwardIterator __middle = __first;
2416 std::advance(__middle, __half);
2417 if (__comp(*__middle, __val))
2421 __len = __len - __half - 1;
2430 * @brief Finds the last position in which @a val could be inserted
2431 * without changing the ordering.
2432 * @ingroup binary_search_algorithms
2433 * @param first An iterator.
2434 * @param last Another iterator.
2435 * @param val The search term.
2436 * @return An iterator pointing to the first element greater than @a val,
2437 * or end() if no elements are greater than @a val.
2438 * @ingroup binary_search_algorithms
2440 template<typename _ForwardIterator, typename _Tp>
2442 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2445 typedef typename iterator_traits<_ForwardIterator>::value_type
2447 typedef typename iterator_traits<_ForwardIterator>::difference_type
2450 // concept requirements
2451 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2452 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2453 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2455 _DistanceType __len = std::distance(__first, __last);
2459 _DistanceType __half = __len >> 1;
2460 _ForwardIterator __middle = __first;
2461 std::advance(__middle, __half);
2462 if (__val < *__middle)
2468 __len = __len - __half - 1;
2475 * @brief Finds the last position in which @a val could be inserted
2476 * without changing the ordering.
2477 * @ingroup binary_search_algorithms
2478 * @param first An iterator.
2479 * @param last Another iterator.
2480 * @param val The search term.
2481 * @param comp A functor to use for comparisons.
2482 * @return An iterator pointing to the first element greater than @a val,
2483 * or end() if no elements are greater than @a val.
2484 * @ingroup binary_search_algorithms
2486 * The comparison function should have the same effects on ordering as
2487 * the function used for the initial sort.
2489 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2491 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2492 const _Tp& __val, _Compare __comp)
2494 typedef typename iterator_traits<_ForwardIterator>::value_type
2496 typedef typename iterator_traits<_ForwardIterator>::difference_type
2499 // concept requirements
2500 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2501 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2503 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2506 _DistanceType __len = std::distance(__first, __last);
2510 _DistanceType __half = __len >> 1;
2511 _ForwardIterator __middle = __first;
2512 std::advance(__middle, __half);
2513 if (__comp(__val, *__middle))
2519 __len = __len - __half - 1;
2526 * @brief Finds the largest subrange in which @a val could be inserted
2527 * at any place in it without changing the ordering.
2528 * @ingroup binary_search_algorithms
2529 * @param first An iterator.
2530 * @param last Another iterator.
2531 * @param val The search term.
2532 * @return An pair of iterators defining the subrange.
2533 * @ingroup binary_search_algorithms
2535 * This is equivalent to
2537 * std::make_pair(lower_bound(first, last, val),
2538 * upper_bound(first, last, val))
2540 * but does not actually call those functions.
2542 template<typename _ForwardIterator, typename _Tp>
2543 pair<_ForwardIterator, _ForwardIterator>
2544 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2547 typedef typename iterator_traits<_ForwardIterator>::value_type
2549 typedef typename iterator_traits<_ForwardIterator>::difference_type
2552 // concept requirements
2553 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2554 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2555 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2556 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2557 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2559 _DistanceType __len = std::distance(__first, __last);
2563 _DistanceType __half = __len >> 1;
2564 _ForwardIterator __middle = __first;
2565 std::advance(__middle, __half);
2566 if (*__middle < __val)
2570 __len = __len - __half - 1;
2572 else if (__val < *__middle)
2576 _ForwardIterator __left = std::lower_bound(__first, __middle,
2578 std::advance(__first, __len);
2579 _ForwardIterator __right = std::upper_bound(++__middle, __first,
2581 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2584 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2588 * @brief Finds the largest subrange in which @a val could be inserted
2589 * at any place in it without changing the ordering.
2590 * @param first An iterator.
2591 * @param last Another iterator.
2592 * @param val The search term.
2593 * @param comp A functor to use for comparisons.
2594 * @return An pair of iterators defining the subrange.
2595 * @ingroup binary_search_algorithms
2597 * This is equivalent to
2599 * std::make_pair(lower_bound(first, last, val, comp),
2600 * upper_bound(first, last, val, comp))
2602 * but does not actually call those functions.
2604 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2605 pair<_ForwardIterator, _ForwardIterator>
2606 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2607 const _Tp& __val, _Compare __comp)
2609 typedef typename iterator_traits<_ForwardIterator>::value_type
2611 typedef typename iterator_traits<_ForwardIterator>::difference_type
2614 // concept requirements
2615 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2616 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2618 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2620 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2622 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2625 _DistanceType __len = std::distance(__first, __last);
2629 _DistanceType __half = __len >> 1;
2630 _ForwardIterator __middle = __first;
2631 std::advance(__middle, __half);
2632 if (__comp(*__middle, __val))
2636 __len = __len - __half - 1;
2638 else if (__comp(__val, *__middle))
2642 _ForwardIterator __left = std::lower_bound(__first, __middle,
2644 std::advance(__first, __len);
2645 _ForwardIterator __right = std::upper_bound(++__middle, __first,
2647 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2650 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2654 * @brief Determines whether an element exists in a range.
2655 * @ingroup binary_search_algorithms
2656 * @param first An iterator.
2657 * @param last Another iterator.
2658 * @param val The search term.
2659 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2661 * Note that this does not actually return an iterator to @a val. For
2662 * that, use std::find or a container's specialized find member functions.
2664 template<typename _ForwardIterator, typename _Tp>
2666 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2669 typedef typename iterator_traits<_ForwardIterator>::value_type
2672 // concept requirements
2673 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2674 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2675 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2676 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2678 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2679 return __i != __last && !(__val < *__i);
2683 * @brief Determines whether an element exists in a range.
2684 * @ingroup binary_search_algorithms
2685 * @param first An iterator.
2686 * @param last Another iterator.
2687 * @param val The search term.
2688 * @param comp A functor to use for comparisons.
2689 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2691 * Note that this does not actually return an iterator to @a val. For
2692 * that, use std::find or a container's specialized find member functions.
2694 * The comparison function should have the same effects on ordering as
2695 * the function used for the initial sort.
2697 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2699 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2700 const _Tp& __val, _Compare __comp)
2702 typedef typename iterator_traits<_ForwardIterator>::value_type
2705 // concept requirements
2706 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2707 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2709 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2711 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2714 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2715 return __i != __last && !bool(__comp(__val, *__i));
2720 /// This is a helper function for the __merge_adaptive routines.
2721 template<typename _InputIterator1, typename _InputIterator2,
2722 typename _OutputIterator>
2724 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2725 _InputIterator2 __first2, _InputIterator2 __last2,
2726 _OutputIterator __result)
2728 while (__first1 != __last1 && __first2 != __last2)
2730 if (*__first2 < *__first1)
2732 *__result = _GLIBCXX_MOVE(*__first2);
2737 *__result = _GLIBCXX_MOVE(*__first1);
2742 if (__first1 != __last1)
2743 _GLIBCXX_MOVE3(__first1, __last1, __result);
2746 /// This is a helper function for the __merge_adaptive routines.
2747 template<typename _InputIterator1, typename _InputIterator2,
2748 typename _OutputIterator, typename _Compare>
2750 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2751 _InputIterator2 __first2, _InputIterator2 __last2,
2752 _OutputIterator __result, _Compare __comp)
2754 while (__first1 != __last1 && __first2 != __last2)
2756 if (__comp(*__first2, *__first1))
2758 *__result = _GLIBCXX_MOVE(*__first2);
2763 *__result = _GLIBCXX_MOVE(*__first1);
2768 if (__first1 != __last1)
2769 _GLIBCXX_MOVE3(__first1, __last1, __result);
2772 /// This is a helper function for the __merge_adaptive routines.
2773 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2774 typename _BidirectionalIterator3>
2776 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2777 _BidirectionalIterator1 __last1,
2778 _BidirectionalIterator2 __first2,
2779 _BidirectionalIterator2 __last2,
2780 _BidirectionalIterator3 __result)
2782 if (__first1 == __last1)
2784 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
2787 else if (__first2 == __last2)
2794 if (*__last2 < *__last1)
2796 *--__result = _GLIBCXX_MOVE(*__last1);
2797 if (__first1 == __last1)
2799 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
2806 *--__result = _GLIBCXX_MOVE(*__last2);
2807 if (__first2 == __last2)
2814 /// This is a helper function for the __merge_adaptive routines.
2815 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2816 typename _BidirectionalIterator3, typename _Compare>
2818 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2819 _BidirectionalIterator1 __last1,
2820 _BidirectionalIterator2 __first2,
2821 _BidirectionalIterator2 __last2,
2822 _BidirectionalIterator3 __result,
2825 if (__first1 == __last1)
2827 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
2830 else if (__first2 == __last2)
2837 if (__comp(*__last2, *__last1))
2839 *--__result = _GLIBCXX_MOVE(*__last1);
2840 if (__first1 == __last1)
2842 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
2849 *--__result = _GLIBCXX_MOVE(*__last2);
2850 if (__first2 == __last2)
2857 /// This is a helper function for the merge routines.
2858 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2860 _BidirectionalIterator1
2861 __rotate_adaptive(_BidirectionalIterator1 __first,
2862 _BidirectionalIterator1 __middle,
2863 _BidirectionalIterator1 __last,
2864 _Distance __len1, _Distance __len2,
2865 _BidirectionalIterator2 __buffer,
2866 _Distance __buffer_size)
2868 _BidirectionalIterator2 __buffer_end;
2869 if (__len1 > __len2 && __len2 <= __buffer_size)
2873 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2874 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2875 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2880 else if (__len1 <= __buffer_size)
2884 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2885 _GLIBCXX_MOVE3(__middle, __last, __first);
2886 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2893 std::rotate(__first, __middle, __last);
2894 std::advance(__first, std::distance(__middle, __last));
2899 /// This is a helper function for the merge routines.
2900 template<typename _BidirectionalIterator, typename _Distance,
2903 __merge_adaptive(_BidirectionalIterator __first,
2904 _BidirectionalIterator __middle,
2905 _BidirectionalIterator __last,
2906 _Distance __len1, _Distance __len2,
2907 _Pointer __buffer, _Distance __buffer_size)
2909 if (__len1 <= __len2 && __len1 <= __buffer_size)
2911 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2912 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2915 else if (__len2 <= __buffer_size)
2917 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2918 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2919 __buffer_end, __last);
2923 _BidirectionalIterator __first_cut = __first;
2924 _BidirectionalIterator __second_cut = __middle;
2925 _Distance __len11 = 0;
2926 _Distance __len22 = 0;
2927 if (__len1 > __len2)
2929 __len11 = __len1 / 2;
2930 std::advance(__first_cut, __len11);
2931 __second_cut = std::lower_bound(__middle, __last,
2933 __len22 = std::distance(__middle, __second_cut);
2937 __len22 = __len2 / 2;
2938 std::advance(__second_cut, __len22);
2939 __first_cut = std::upper_bound(__first, __middle,
2941 __len11 = std::distance(__first, __first_cut);
2943 _BidirectionalIterator __new_middle =
2944 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2945 __len1 - __len11, __len22, __buffer,
2947 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2948 __len22, __buffer, __buffer_size);
2949 std::__merge_adaptive(__new_middle, __second_cut, __last,
2951 __len2 - __len22, __buffer, __buffer_size);
2955 /// This is a helper function for the merge routines.
2956 template<typename _BidirectionalIterator, typename _Distance,
2957 typename _Pointer, typename _Compare>
2959 __merge_adaptive(_BidirectionalIterator __first,
2960 _BidirectionalIterator __middle,
2961 _BidirectionalIterator __last,
2962 _Distance __len1, _Distance __len2,
2963 _Pointer __buffer, _Distance __buffer_size,
2966 if (__len1 <= __len2 && __len1 <= __buffer_size)
2968 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2969 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2972 else if (__len2 <= __buffer_size)
2974 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2975 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2976 __buffer_end, __last, __comp);
2980 _BidirectionalIterator __first_cut = __first;
2981 _BidirectionalIterator __second_cut = __middle;
2982 _Distance __len11 = 0;
2983 _Distance __len22 = 0;
2984 if (__len1 > __len2)
2986 __len11 = __len1 / 2;
2987 std::advance(__first_cut, __len11);
2988 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2990 __len22 = std::distance(__middle, __second_cut);
2994 __len22 = __len2 / 2;
2995 std::advance(__second_cut, __len22);
2996 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2998 __len11 = std::distance(__first, __first_cut);
3000 _BidirectionalIterator __new_middle =
3001 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3002 __len1 - __len11, __len22, __buffer,
3004 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3005 __len22, __buffer, __buffer_size, __comp);
3006 std::__merge_adaptive(__new_middle, __second_cut, __last,
3008 __len2 - __len22, __buffer,
3009 __buffer_size, __comp);
3013 /// This is a helper function for the merge routines.
3014 template<typename _BidirectionalIterator, typename _Distance>
3016 __merge_without_buffer(_BidirectionalIterator __first,
3017 _BidirectionalIterator __middle,
3018 _BidirectionalIterator __last,
3019 _Distance __len1, _Distance __len2)
3021 if (__len1 == 0 || __len2 == 0)
3023 if (__len1 + __len2 == 2)
3025 if (*__middle < *__first)
3026 std::iter_swap(__first, __middle);
3029 _BidirectionalIterator __first_cut = __first;
3030 _BidirectionalIterator __second_cut = __middle;
3031 _Distance __len11 = 0;
3032 _Distance __len22 = 0;
3033 if (__len1 > __len2)
3035 __len11 = __len1 / 2;
3036 std::advance(__first_cut, __len11);
3037 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3038 __len22 = std::distance(__middle, __second_cut);
3042 __len22 = __len2 / 2;
3043 std::advance(__second_cut, __len22);
3044 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3045 __len11 = std::distance(__first, __first_cut);
3047 std::rotate(__first_cut, __middle, __second_cut);
3048 _BidirectionalIterator __new_middle = __first_cut;
3049 std::advance(__new_middle, std::distance(__middle, __second_cut));
3050 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3052 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3053 __len1 - __len11, __len2 - __len22);
3056 /// This is a helper function for the merge routines.
3057 template<typename _BidirectionalIterator, typename _Distance,
3060 __merge_without_buffer(_BidirectionalIterator __first,
3061 _BidirectionalIterator __middle,
3062 _BidirectionalIterator __last,
3063 _Distance __len1, _Distance __len2,
3066 if (__len1 == 0 || __len2 == 0)
3068 if (__len1 + __len2 == 2)
3070 if (__comp(*__middle, *__first))
3071 std::iter_swap(__first, __middle);
3074 _BidirectionalIterator __first_cut = __first;
3075 _BidirectionalIterator __second_cut = __middle;
3076 _Distance __len11 = 0;
3077 _Distance __len22 = 0;
3078 if (__len1 > __len2)
3080 __len11 = __len1 / 2;
3081 std::advance(__first_cut, __len11);
3082 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3084 __len22 = std::distance(__middle, __second_cut);
3088 __len22 = __len2 / 2;
3089 std::advance(__second_cut, __len22);
3090 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3092 __len11 = std::distance(__first, __first_cut);
3094 std::rotate(__first_cut, __middle, __second_cut);
3095 _BidirectionalIterator __new_middle = __first_cut;
3096 std::advance(__new_middle, std::distance(__middle, __second_cut));
3097 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3098 __len11, __len22, __comp);
3099 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3100 __len1 - __len11, __len2 - __len22, __comp);
3104 * @brief Merges two sorted ranges in place.
3105 * @ingroup sorting_algorithms
3106 * @param first An iterator.
3107 * @param middle Another iterator.
3108 * @param last Another iterator.
3111 * Merges two sorted and consecutive ranges, [first,middle) and
3112 * [middle,last), and puts the result in [first,last). The output will
3113 * be sorted. The sort is @e stable, that is, for equivalent
3114 * elements in the two ranges, elements from the first range will always
3115 * come before elements from the second.
3117 * If enough additional memory is available, this takes (last-first)-1
3118 * comparisons. Otherwise an NlogN algorithm is used, where N is
3119 * distance(first,last).
3121 template<typename _BidirectionalIterator>
3123 inplace_merge(_BidirectionalIterator __first,
3124 _BidirectionalIterator __middle,
3125 _BidirectionalIterator __last)
3127 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3129 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3132 // concept requirements
3133 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3134 _BidirectionalIterator>)
3135 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3136 __glibcxx_requires_sorted(__first, __middle);
3137 __glibcxx_requires_sorted(__middle, __last);
3139 if (__first == __middle || __middle == __last)
3142 _DistanceType __len1 = std::distance(__first, __middle);
3143 _DistanceType __len2 = std::distance(__middle, __last);
3145 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3147 if (__buf.begin() == 0)
3148 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3150 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3151 __buf.begin(), _DistanceType(__buf.size()));
3155 * @brief Merges two sorted ranges in place.
3156 * @ingroup sorting_algorithms
3157 * @param first An iterator.
3158 * @param middle Another iterator.
3159 * @param last Another iterator.
3160 * @param comp A functor to use for comparisons.
3163 * Merges two sorted and consecutive ranges, [first,middle) and
3164 * [middle,last), and puts the result in [first,last). The output will
3165 * be sorted. The sort is @e stable, that is, for equivalent
3166 * elements in the two ranges, elements from the first range will always
3167 * come before elements from the second.
3169 * If enough additional memory is available, this takes (last-first)-1
3170 * comparisons. Otherwise an NlogN algorithm is used, where N is
3171 * distance(first,last).
3173 * The comparison function should have the same effects on ordering as
3174 * the function used for the initial sort.
3176 template<typename _BidirectionalIterator, typename _Compare>
3178 inplace_merge(_BidirectionalIterator __first,
3179 _BidirectionalIterator __middle,
3180 _BidirectionalIterator __last,
3183 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3185 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3188 // concept requirements
3189 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3190 _BidirectionalIterator>)
3191 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3192 _ValueType, _ValueType>)
3193 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3194 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3196 if (__first == __middle || __middle == __last)
3199 const _DistanceType __len1 = std::distance(__first, __middle);
3200 const _DistanceType __len2 = std::distance(__middle, __last);
3202 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3204 if (__buf.begin() == 0)
3205 std::__merge_without_buffer(__first, __middle, __last, __len1,
3208 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3209 __buf.begin(), _DistanceType(__buf.size()),
3214 /// This is a helper function for the __merge_sort_loop routines.
3215 template<typename _InputIterator1, typename _InputIterator2,
3216 typename _OutputIterator>
3218 __move_merge(_InputIterator1 __first1, _InputIterator1 __last1,
3219 _InputIterator2 __first2, _InputIterator2 __last2,
3220 _OutputIterator __result)
3222 while (__first1 != __last1 && __first2 != __last2)
3224 if (*__first2 < *__first1)
3226 *__result = _GLIBCXX_MOVE(*__first2);
3231 *__result = _GLIBCXX_MOVE(*__first1);
3236 return _GLIBCXX_MOVE3(__first2, __last2,
3237 _GLIBCXX_MOVE3(__first1, __last1,
3241 /// This is a helper function for the __merge_sort_loop routines.
3242 template<typename _InputIterator1, typename _InputIterator2,
3243 typename _OutputIterator, typename _Compare>
3245 __move_merge(_InputIterator1 __first1, _InputIterator1 __last1,
3246 _InputIterator2 __first2, _InputIterator2 __last2,
3247 _OutputIterator __result, _Compare __comp)
3249 while (__first1 != __last1 && __first2 != __last2)
3251 if (__comp(*__first2, *__first1))
3253 *__result = _GLIBCXX_MOVE(*__first2);
3258 *__result = _GLIBCXX_MOVE(*__first1);
3263 return _GLIBCXX_MOVE3(__first2, __last2,
3264 _GLIBCXX_MOVE3(__first1, __last1,
3268 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3271 __merge_sort_loop(_RandomAccessIterator1 __first,
3272 _RandomAccessIterator1 __last,
3273 _RandomAccessIterator2 __result,
3274 _Distance __step_size)
3276 const _Distance __two_step = 2 * __step_size;
3278 while (__last - __first >= __two_step)
3280 __result = std::__move_merge(__first, __first + __step_size,
3281 __first + __step_size,
3282 __first + __two_step, __result);
3283 __first += __two_step;
3286 __step_size = std::min(_Distance(__last - __first), __step_size);
3287 std::__move_merge(__first, __first + __step_size,
3288 __first + __step_size, __last, __result);
3291 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3292 typename _Distance, typename _Compare>
3294 __merge_sort_loop(_RandomAccessIterator1 __first,
3295 _RandomAccessIterator1 __last,
3296 _RandomAccessIterator2 __result, _Distance __step_size,
3299 const _Distance __two_step = 2 * __step_size;
3301 while (__last - __first >= __two_step)
3303 __result = std::__move_merge(__first, __first + __step_size,
3304 __first + __step_size,
3305 __first + __two_step,
3307 __first += __two_step;
3309 __step_size = std::min(_Distance(__last - __first), __step_size);
3311 std::__move_merge(__first,__first + __step_size,
3312 __first + __step_size, __last, __result, __comp);
3315 template<typename _RandomAccessIterator, typename _Distance>
3317 __chunk_insertion_sort(_RandomAccessIterator __first,
3318 _RandomAccessIterator __last,
3319 _Distance __chunk_size)
3321 while (__last - __first >= __chunk_size)
3323 std::__insertion_sort(__first, __first + __chunk_size);
3324 __first += __chunk_size;
3326 std::__insertion_sort(__first, __last);
3329 template<typename _RandomAccessIterator, typename _Distance,
3332 __chunk_insertion_sort(_RandomAccessIterator __first,
3333 _RandomAccessIterator __last,
3334 _Distance __chunk_size, _Compare __comp)
3336 while (__last - __first >= __chunk_size)
3338 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3339 __first += __chunk_size;
3341 std::__insertion_sort(__first, __last, __comp);
3344 enum { _S_chunk_size = 7 };
3346 template<typename _RandomAccessIterator, typename _Pointer>
3348 __merge_sort_with_buffer(_RandomAccessIterator __first,
3349 _RandomAccessIterator __last,
3352 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3355 const _Distance __len = __last - __first;
3356 const _Pointer __buffer_last = __buffer + __len;
3358 _Distance __step_size = _S_chunk_size;
3359 std::__chunk_insertion_sort(__first, __last, __step_size);
3361 while (__step_size < __len)
3363 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3365 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3370 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3372 __merge_sort_with_buffer(_RandomAccessIterator __first,
3373 _RandomAccessIterator __last,
3374 _Pointer __buffer, _Compare __comp)
3376 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3379 const _Distance __len = __last - __first;
3380 const _Pointer __buffer_last = __buffer + __len;
3382 _Distance __step_size = _S_chunk_size;
3383 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3385 while (__step_size < __len)
3387 std::__merge_sort_loop(__first, __last, __buffer,
3388 __step_size, __comp);
3390 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3391 __step_size, __comp);
3396 template<typename _RandomAccessIterator, typename _Pointer,
3399 __stable_sort_adaptive(_RandomAccessIterator __first,
3400 _RandomAccessIterator __last,
3401 _Pointer __buffer, _Distance __buffer_size)
3403 const _Distance __len = (__last - __first + 1) / 2;
3404 const _RandomAccessIterator __middle = __first + __len;
3405 if (__len > __buffer_size)
3407 std::__stable_sort_adaptive(__first, __middle,
3408 __buffer, __buffer_size);
3409 std::__stable_sort_adaptive(__middle, __last,
3410 __buffer, __buffer_size);
3414 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3415 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3417 std::__merge_adaptive(__first, __middle, __last,
3418 _Distance(__middle - __first),
3419 _Distance(__last - __middle),
3420 __buffer, __buffer_size);
3423 template<typename _RandomAccessIterator, typename _Pointer,
3424 typename _Distance, typename _Compare>
3426 __stable_sort_adaptive(_RandomAccessIterator __first,
3427 _RandomAccessIterator __last,
3428 _Pointer __buffer, _Distance __buffer_size,
3431 const _Distance __len = (__last - __first + 1) / 2;
3432 const _RandomAccessIterator __middle = __first + __len;
3433 if (__len > __buffer_size)
3435 std::__stable_sort_adaptive(__first, __middle, __buffer,
3436 __buffer_size, __comp);
3437 std::__stable_sort_adaptive(__middle, __last, __buffer,
3438 __buffer_size, __comp);
3442 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3443 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3445 std::__merge_adaptive(__first, __middle, __last,
3446 _Distance(__middle - __first),
3447 _Distance(__last - __middle),
3448 __buffer, __buffer_size,
3452 /// This is a helper function for the stable sorting routines.
3453 template<typename _RandomAccessIterator>
3455 __inplace_stable_sort(_RandomAccessIterator __first,
3456 _RandomAccessIterator __last)
3458 if (__last - __first < 15)
3460 std::__insertion_sort(__first, __last);
3463 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3464 std::__inplace_stable_sort(__first, __middle);
3465 std::__inplace_stable_sort(__middle, __last);
3466 std::__merge_without_buffer(__first, __middle, __last,
3471 /// This is a helper function for the stable sorting routines.
3472 template<typename _RandomAccessIterator, typename _Compare>
3474 __inplace_stable_sort(_RandomAccessIterator __first,
3475 _RandomAccessIterator __last, _Compare __comp)
3477 if (__last - __first < 15)
3479 std::__insertion_sort(__first, __last, __comp);
3482 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3483 std::__inplace_stable_sort(__first, __middle, __comp);
3484 std::__inplace_stable_sort(__middle, __last, __comp);
3485 std::__merge_without_buffer(__first, __middle, __last,
3493 // Set algorithms: includes, set_union, set_intersection, set_difference,
3494 // set_symmetric_difference. All of these algorithms have the precondition
3495 // that their input ranges are sorted and the postcondition that their output
3496 // ranges are sorted.
3499 * @brief Determines whether all elements of a sequence exists in a range.
3500 * @param first1 Start of search range.
3501 * @param last1 End of search range.
3502 * @param first2 Start of sequence
3503 * @param last2 End of sequence.
3504 * @return True if each element in [first2,last2) is contained in order
3505 * within [first1,last1). False otherwise.
3506 * @ingroup set_algorithms
3508 * This operation expects both [first1,last1) and [first2,last2) to be
3509 * sorted. Searches for the presence of each element in [first2,last2)
3510 * within [first1,last1). The iterators over each range only move forward,
3511 * so this is a linear algorithm. If an element in [first2,last2) is not
3512 * found before the search iterator reaches @a last2, false is returned.
3514 template<typename _InputIterator1, typename _InputIterator2>
3516 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3517 _InputIterator2 __first2, _InputIterator2 __last2)
3519 typedef typename iterator_traits<_InputIterator1>::value_type
3521 typedef typename iterator_traits<_InputIterator2>::value_type
3524 // concept requirements
3525 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3526 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3527 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3528 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3529 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3530 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3532 while (__first1 != __last1 && __first2 != __last2)
3533 if (*__first2 < *__first1)
3535 else if(*__first1 < *__first2)
3538 ++__first1, ++__first2;
3540 return __first2 == __last2;
3544 * @brief Determines whether all elements of a sequence exists in a range
3546 * @ingroup set_algorithms
3547 * @param first1 Start of search range.
3548 * @param last1 End of search range.
3549 * @param first2 Start of sequence
3550 * @param last2 End of sequence.
3551 * @param comp Comparison function to use.
3552 * @return True if each element in [first2,last2) is contained in order
3553 * within [first1,last1) according to comp. False otherwise.
3554 * @ingroup set_algorithms
3556 * This operation expects both [first1,last1) and [first2,last2) to be
3557 * sorted. Searches for the presence of each element in [first2,last2)
3558 * within [first1,last1), using comp to decide. The iterators over each
3559 * range only move forward, so this is a linear algorithm. If an element
3560 * in [first2,last2) is not found before the search iterator reaches @a
3561 * last2, false is returned.
3563 template<typename _InputIterator1, typename _InputIterator2,
3566 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3567 _InputIterator2 __first2, _InputIterator2 __last2,
3570 typedef typename iterator_traits<_InputIterator1>::value_type
3572 typedef typename iterator_traits<_InputIterator2>::value_type
3575 // concept requirements
3576 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3577 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3578 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3579 _ValueType1, _ValueType2>)
3580 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3581 _ValueType2, _ValueType1>)
3582 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3583 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3585 while (__first1 != __last1 && __first2 != __last2)
3586 if (__comp(*__first2, *__first1))
3588 else if(__comp(*__first1, *__first2))
3591 ++__first1, ++__first2;
3593 return __first2 == __last2;
3602 // set_symmetric_difference
3607 * @brief Permute range into the next @a dictionary ordering.
3608 * @ingroup sorting_algorithms
3609 * @param first Start of range.
3610 * @param last End of range.
3611 * @return False if wrapped to first permutation, true otherwise.
3613 * Treats all permutations of the range as a set of @a dictionary sorted
3614 * sequences. Permutes the current sequence into the next one of this set.
3615 * Returns true if there are more sequences to generate. If the sequence
3616 * is the largest of the set, the smallest is generated and false returned.
3618 template<typename _BidirectionalIterator>
3620 next_permutation(_BidirectionalIterator __first,
3621 _BidirectionalIterator __last)
3623 // concept requirements
3624 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3625 _BidirectionalIterator>)
3626 __glibcxx_function_requires(_LessThanComparableConcept<
3627 typename iterator_traits<_BidirectionalIterator>::value_type>)
3628 __glibcxx_requires_valid_range(__first, __last);
3630 if (__first == __last)
3632 _BidirectionalIterator __i = __first;
3641 _BidirectionalIterator __ii = __i;
3645 _BidirectionalIterator __j = __last;
3646 while (!(*__i < *--__j))
3648 std::iter_swap(__i, __j);
3649 std::reverse(__ii, __last);
3654 std::reverse(__first, __last);
3661 * @brief Permute range into the next @a dictionary ordering using
3662 * comparison functor.
3663 * @ingroup sorting_algorithms
3664 * @param first Start of range.
3665 * @param last End of range.
3666 * @param comp A comparison functor.
3667 * @return False if wrapped to first permutation, true otherwise.
3669 * Treats all permutations of the range [first,last) as a set of
3670 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3671 * sequence into the next one of this set. Returns true if there are more
3672 * sequences to generate. If the sequence is the largest of the set, the
3673 * smallest is generated and false returned.
3675 template<typename _BidirectionalIterator, typename _Compare>
3677 next_permutation(_BidirectionalIterator __first,
3678 _BidirectionalIterator __last, _Compare __comp)
3680 // concept requirements
3681 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3682 _BidirectionalIterator>)
3683 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3684 typename iterator_traits<_BidirectionalIterator>::value_type,
3685 typename iterator_traits<_BidirectionalIterator>::value_type>)
3686 __glibcxx_requires_valid_range(__first, __last);
3688 if (__first == __last)
3690 _BidirectionalIterator __i = __first;
3699 _BidirectionalIterator __ii = __i;
3701 if (__comp(*__i, *__ii))
3703 _BidirectionalIterator __j = __last;
3704 while (!bool(__comp(*__i, *--__j)))
3706 std::iter_swap(__i, __j);
3707 std::reverse(__ii, __last);
3712 std::reverse(__first, __last);
3719 * @brief Permute range into the previous @a dictionary ordering.
3720 * @ingroup sorting_algorithms
3721 * @param first Start of range.
3722 * @param last End of range.
3723 * @return False if wrapped to last permutation, true otherwise.
3725 * Treats all permutations of the range as a set of @a dictionary sorted
3726 * sequences. Permutes the current sequence into the previous one of this
3727 * set. Returns true if there are more sequences to generate. If the
3728 * sequence is the smallest of the set, the largest is generated and false
3731 template<typename _BidirectionalIterator>
3733 prev_permutation(_BidirectionalIterator __first,
3734 _BidirectionalIterator __last)
3736 // concept requirements
3737 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3738 _BidirectionalIterator>)
3739 __glibcxx_function_requires(_LessThanComparableConcept<
3740 typename iterator_traits<_BidirectionalIterator>::value_type>)
3741 __glibcxx_requires_valid_range(__first, __last);
3743 if (__first == __last)
3745 _BidirectionalIterator __i = __first;
3754 _BidirectionalIterator __ii = __i;
3758 _BidirectionalIterator __j = __last;
3759 while (!(*--__j < *__i))
3761 std::iter_swap(__i, __j);
3762 std::reverse(__ii, __last);
3767 std::reverse(__first, __last);
3774 * @brief Permute range into the previous @a dictionary ordering using
3775 * comparison functor.
3776 * @ingroup sorting_algorithms
3777 * @param first Start of range.
3778 * @param last End of range.
3779 * @param comp A comparison functor.
3780 * @return False if wrapped to last permutation, true otherwise.
3782 * Treats all permutations of the range [first,last) as a set of
3783 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3784 * sequence into the previous one of this set. Returns true if there are
3785 * more sequences to generate. If the sequence is the smallest of the set,
3786 * the largest is generated and false returned.
3788 template<typename _BidirectionalIterator, typename _Compare>
3790 prev_permutation(_BidirectionalIterator __first,
3791 _BidirectionalIterator __last, _Compare __comp)
3793 // concept requirements
3794 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3795 _BidirectionalIterator>)
3796 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3797 typename iterator_traits<_BidirectionalIterator>::value_type,
3798 typename iterator_traits<_BidirectionalIterator>::value_type>)
3799 __glibcxx_requires_valid_range(__first, __last);
3801 if (__first == __last)
3803 _BidirectionalIterator __i = __first;
3812 _BidirectionalIterator __ii = __i;
3814 if (__comp(*__ii, *__i))
3816 _BidirectionalIterator __j = __last;
3817 while (!bool(__comp(*--__j, *__i)))
3819 std::iter_swap(__i, __j);
3820 std::reverse(__ii, __last);
3825 std::reverse(__first, __last);
3835 * @brief Copy a sequence, replacing each element of one value with another
3837 * @param first An input iterator.
3838 * @param last An input iterator.
3839 * @param result An output iterator.
3840 * @param old_value The value to be replaced.
3841 * @param new_value The replacement value.
3842 * @return The end of the output sequence, @p result+(last-first).
3844 * Copies each element in the input range @p [first,last) to the
3845 * output range @p [result,result+(last-first)) replacing elements
3846 * equal to @p old_value with @p new_value.
3848 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3850 replace_copy(_InputIterator __first, _InputIterator __last,
3851 _OutputIterator __result,
3852 const _Tp& __old_value, const _Tp& __new_value)
3854 // concept requirements
3855 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3856 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3857 typename iterator_traits<_InputIterator>::value_type>)
3858 __glibcxx_function_requires(_EqualOpConcept<
3859 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3860 __glibcxx_requires_valid_range(__first, __last);
3862 for (; __first != __last; ++__first, ++__result)
3863 if (*__first == __old_value)
3864 *__result = __new_value;
3866 *__result = *__first;
3871 * @brief Copy a sequence, replacing each value for which a predicate
3872 * returns true with another value.
3873 * @ingroup mutating_algorithms
3874 * @param first An input iterator.
3875 * @param last An input iterator.
3876 * @param result An output iterator.
3877 * @param pred A predicate.
3878 * @param new_value The replacement value.
3879 * @return The end of the output sequence, @p result+(last-first).
3881 * Copies each element in the range @p [first,last) to the range
3882 * @p [result,result+(last-first)) replacing elements for which
3883 * @p pred returns true with @p new_value.
3885 template<typename _InputIterator, typename _OutputIterator,
3886 typename _Predicate, typename _Tp>
3888 replace_copy_if(_InputIterator __first, _InputIterator __last,
3889 _OutputIterator __result,
3890 _Predicate __pred, const _Tp& __new_value)
3892 // concept requirements
3893 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3894 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3895 typename iterator_traits<_InputIterator>::value_type>)
3896 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3897 typename iterator_traits<_InputIterator>::value_type>)
3898 __glibcxx_requires_valid_range(__first, __last);
3900 for (; __first != __last; ++__first, ++__result)
3901 if (__pred(*__first))
3902 *__result = __new_value;
3904 *__result = *__first;
3908 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3910 * @brief Determines whether the elements of a sequence are sorted.
3911 * @ingroup sorting_algorithms
3912 * @param first An iterator.
3913 * @param last Another iterator.
3914 * @return True if the elements are sorted, false otherwise.
3916 template<typename _ForwardIterator>
3918 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3919 { return std::is_sorted_until(__first, __last) == __last; }
3922 * @brief Determines whether the elements of a sequence are sorted
3923 * according to a comparison functor.
3924 * @ingroup sorting_algorithms
3925 * @param first An iterator.
3926 * @param last Another iterator.
3927 * @param comp A comparison functor.
3928 * @return True if the elements are sorted, false otherwise.
3930 template<typename _ForwardIterator, typename _Compare>
3932 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3934 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3937 * @brief Determines the end of a sorted sequence.
3938 * @ingroup sorting_algorithms
3939 * @param first An iterator.
3940 * @param last Another iterator.
3941 * @return An iterator pointing to the last iterator i in [first, last)
3942 * for which the range [first, i) is sorted.
3944 template<typename _ForwardIterator>
3946 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3948 // concept requirements
3949 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3950 __glibcxx_function_requires(_LessThanComparableConcept<
3951 typename iterator_traits<_ForwardIterator>::value_type>)
3952 __glibcxx_requires_valid_range(__first, __last);
3954 if (__first == __last)
3957 _ForwardIterator __next = __first;
3958 for (++__next; __next != __last; __first = __next, ++__next)
3959 if (*__next < *__first)
3965 * @brief Determines the end of a sorted sequence using comparison functor.
3966 * @ingroup sorting_algorithms
3967 * @param first An iterator.
3968 * @param last Another iterator.
3969 * @param comp A comparison functor.
3970 * @return An iterator pointing to the last iterator i in [first, last)
3971 * for which the range [first, i) is sorted.
3973 template<typename _ForwardIterator, typename _Compare>
3975 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3978 // concept requirements
3979 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3980 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3981 typename iterator_traits<_ForwardIterator>::value_type,
3982 typename iterator_traits<_ForwardIterator>::value_type>)
3983 __glibcxx_requires_valid_range(__first, __last);
3985 if (__first == __last)
3988 _ForwardIterator __next = __first;
3989 for (++__next; __next != __last; __first = __next, ++__next)
3990 if (__comp(*__next, *__first))
3996 * @brief Determines min and max at once as an ordered pair.
3997 * @ingroup sorting_algorithms
3998 * @param a A thing of arbitrary type.
3999 * @param b Another thing of arbitrary type.
4000 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4002 template<typename _Tp>
4003 inline pair<const _Tp&, const _Tp&>
4004 minmax(const _Tp& __a, const _Tp& __b)
4006 // concept requirements
4007 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
4009 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
4010 : pair<const _Tp&, const _Tp&>(__a, __b);
4014 * @brief Determines min and max at once as an ordered pair.
4015 * @ingroup sorting_algorithms
4016 * @param a A thing of arbitrary type.
4017 * @param b Another thing of arbitrary type.
4018 * @param comp A @link comparison_functor comparison functor@endlink.
4019 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
4021 template<typename _Tp, typename _Compare>
4022 inline pair<const _Tp&, const _Tp&>
4023 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
4025 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
4026 : pair<const _Tp&, const _Tp&>(__a, __b);
4030 * @brief Return a pair of iterators pointing to the minimum and maximum
4031 * elements in a range.
4032 * @ingroup sorting_algorithms
4033 * @param first Start of range.
4034 * @param last End of range.
4035 * @return make_pair(m, M), where m is the first iterator i in
4036 * [first, last) such that no other element in the range is
4037 * smaller, and where M is the last iterator i in [first, last)
4038 * such that no other element in the range is larger.
4040 template<typename _ForwardIterator>
4041 pair<_ForwardIterator, _ForwardIterator>
4042 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
4044 // concept requirements
4045 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4046 __glibcxx_function_requires(_LessThanComparableConcept<
4047 typename iterator_traits<_ForwardIterator>::value_type>)
4048 __glibcxx_requires_valid_range(__first, __last);
4050 _ForwardIterator __next = __first;
4051 if (__first == __last
4052 || ++__next == __last)
4053 return std::make_pair(__first, __first);
4055 _ForwardIterator __min, __max;
4056 if (*__next < *__first)
4070 while (__first != __last)
4073 if (++__next == __last)
4075 if (*__first < *__min)
4077 else if (!(*__first < *__max))
4082 if (*__next < *__first)
4084 if (*__next < *__min)
4086 if (!(*__first < *__max))
4091 if (*__first < *__min)
4093 if (!(*__next < *__max))
4101 return std::make_pair(__min, __max);
4105 * @brief Return a pair of iterators pointing to the minimum and maximum
4106 * elements in a range.
4107 * @ingroup sorting_algorithms
4108 * @param first Start of range.
4109 * @param last End of range.
4110 * @param comp Comparison functor.
4111 * @return make_pair(m, M), where m is the first iterator i in
4112 * [first, last) such that no other element in the range is
4113 * smaller, and where M is the last iterator i in [first, last)
4114 * such that no other element in the range is larger.
4116 template<typename _ForwardIterator, typename _Compare>
4117 pair<_ForwardIterator, _ForwardIterator>
4118 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
4121 // concept requirements
4122 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4123 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4124 typename iterator_traits<_ForwardIterator>::value_type,
4125 typename iterator_traits<_ForwardIterator>::value_type>)
4126 __glibcxx_requires_valid_range(__first, __last);
4128 _ForwardIterator __next = __first;
4129 if (__first == __last
4130 || ++__next == __last)
4131 return std::make_pair(__first, __first);
4133 _ForwardIterator __min, __max;
4134 if (__comp(*__next, *__first))
4148 while (__first != __last)
4151 if (++__next == __last)
4153 if (__comp(*__first, *__min))
4155 else if (!__comp(*__first, *__max))
4160 if (__comp(*__next, *__first))
4162 if (__comp(*__next, *__min))
4164 if (!__comp(*__first, *__max))
4169 if (__comp(*__first, *__min))
4171 if (!__comp(*__next, *__max))
4179 return std::make_pair(__min, __max);
4183 template<typename _Tp>
4185 min(initializer_list<_Tp> __l)
4186 { return *std::min_element(__l.begin(), __l.end()); }
4188 template<typename _Tp, typename _Compare>
4190 min(initializer_list<_Tp> __l, _Compare __comp)
4191 { return *std::min_element(__l.begin(), __l.end(), __comp); }
4193 template<typename _Tp>
4195 max(initializer_list<_Tp> __l)
4196 { return *std::max_element(__l.begin(), __l.end()); }
4198 template<typename _Tp, typename _Compare>
4200 max(initializer_list<_Tp> __l, _Compare __comp)
4201 { return *std::max_element(__l.begin(), __l.end(), __comp); }
4203 template<typename _Tp>
4204 inline pair<_Tp, _Tp>
4205 minmax(initializer_list<_Tp> __l)
4207 pair<const _Tp*, const _Tp*> __p =
4208 std::minmax_element(__l.begin(), __l.end());
4209 return std::make_pair(*__p.first, *__p.second);
4212 template<typename _Tp, typename _Compare>
4213 inline pair<_Tp, _Tp>
4214 minmax(initializer_list<_Tp> __l, _Compare __comp)
4216 pair<const _Tp*, const _Tp*> __p =
4217 std::minmax_element(__l.begin(), __l.end(), __comp);
4218 return std::make_pair(*__p.first, *__p.second);
4222 * @brief Checks whether a permutaion of the second sequence is equal
4223 * to the first sequence.
4224 * @ingroup non_mutating_algorithms
4225 * @param first1 Start of first range.
4226 * @param last1 End of first range.
4227 * @param first2 Start of second range.
4228 * @return true if there exists a permutation of the elements in the range
4229 * [first2, first2 + (last1 - first1)), beginning with
4230 * ForwardIterator2 begin, such that equal(first1, last1, begin)
4231 * returns true; otherwise, returns false.
4233 template<typename _ForwardIterator1, typename _ForwardIterator2>
4235 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4236 _ForwardIterator2 __first2)
4238 // Efficiently compare identical prefixes: O(N) if sequences
4239 // have the same elements in the same order.
4240 for (; __first1 != __last1; ++__first1, ++__first2)
4241 if (!(*__first1 == *__first2))
4244 if (__first1 == __last1)
4247 // Establish __last2 assuming equal ranges by iterating over the
4248 // rest of the list.
4249 _ForwardIterator2 __last2 = __first2;
4250 std::advance(__last2, std::distance(__first1, __last1));
4251 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
4253 if (__scan != _GLIBCXX_STD_A::find(__first1, __scan, *__scan))
4254 continue; // We've seen this one before.
4256 auto __matches = std::count(__first2, __last2, *__scan);
4258 || std::count(__scan, __last1, *__scan) != __matches)
4265 * @brief Checks whether a permutation of the second sequence is equal
4266 * to the first sequence.
4267 * @ingroup non_mutating_algorithms
4268 * @param first1 Start of first range.
4269 * @param last1 End of first range.
4270 * @param first2 Start of second range.
4271 * @param pred A binary predicate.
4272 * @return true if there exists a permutation of the elements in the range
4273 * [first2, first2 + (last1 - first1)), beginning with
4274 * ForwardIterator2 begin, such that equal(first1, last1, begin,
4275 * pred) returns true; otherwise, returns false.
4277 template<typename _ForwardIterator1, typename _ForwardIterator2,
4278 typename _BinaryPredicate>
4280 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4281 _ForwardIterator2 __first2, _BinaryPredicate __pred)
4283 // Efficiently compare identical prefixes: O(N) if sequences
4284 // have the same elements in the same order.
4285 for (; __first1 != __last1; ++__first1, ++__first2)
4286 if (!bool(__pred(*__first1, *__first2)))
4289 if (__first1 == __last1)
4292 // Establish __last2 assuming equal ranges by iterating over the
4293 // rest of the list.
4294 _ForwardIterator2 __last2 = __first2;
4295 std::advance(__last2, std::distance(__first1, __last1));
4296 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
4298 using std::placeholders::_1;
4300 if (__scan != _GLIBCXX_STD_A::find_if(__first1, __scan,
4301 std::bind(__pred, _1, *__scan)))
4302 continue; // We've seen this one before.
4304 auto __matches = std::count_if(__first2, __last2,
4305 std::bind(__pred, _1, *__scan));
4307 || std::count_if(__scan, __last1,
4308 std::bind(__pred, _1, *__scan)) != __matches)
4314 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4316 * @brief Shuffle the elements of a sequence using a uniform random
4318 * @ingroup mutating_algorithms
4319 * @param first A forward iterator.
4320 * @param last A forward iterator.
4321 * @param g A UniformRandomNumberGenerator (26.5.1.3).
4324 * Reorders the elements in the range @p [first,last) using @p g to
4325 * provide random numbers.
4327 template<typename _RandomAccessIterator,
4328 typename _UniformRandomNumberGenerator>
4330 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4331 _UniformRandomNumberGenerator&& __g)
4333 // concept requirements
4334 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4335 _RandomAccessIterator>)
4336 __glibcxx_requires_valid_range(__first, __last);
4338 if (__first == __last)
4341 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4344 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
4345 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
4346 typedef typename __distr_type::param_type __p_type;
4349 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4350 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
4354 #endif // __GXX_EXPERIMENTAL_CXX0X__
4356 _GLIBCXX_END_NAMESPACE_VERSION
4358 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4361 * @brief Apply a function to every element of a sequence.
4362 * @ingroup non_mutating_algorithms
4363 * @param first An input iterator.
4364 * @param last An input iterator.
4365 * @param f A unary function object.
4366 * @return @p f (std::move(@p f) in C++0x).
4368 * Applies the function object @p f to each element in the range
4369 * @p [first,last). @p f must not modify the order of the sequence.
4370 * If @p f has a return value it is ignored.
4372 template<typename _InputIterator, typename _Function>
4374 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4376 // concept requirements
4377 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4378 __glibcxx_requires_valid_range(__first, __last);
4379 for (; __first != __last; ++__first)
4381 return _GLIBCXX_MOVE(__f);
4385 * @brief Find the first occurrence of a value in a sequence.
4386 * @ingroup non_mutating_algorithms
4387 * @param first An input iterator.
4388 * @param last An input iterator.
4389 * @param val The value to find.
4390 * @return The first iterator @c i in the range @p [first,last)
4391 * such that @c *i == @p val, or @p last if no such iterator exists.
4393 template<typename _InputIterator, typename _Tp>
4394 inline _InputIterator
4395 find(_InputIterator __first, _InputIterator __last,
4398 // concept requirements
4399 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4400 __glibcxx_function_requires(_EqualOpConcept<
4401 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4402 __glibcxx_requires_valid_range(__first, __last);
4403 return std::__find(__first, __last, __val,
4404 std::__iterator_category(__first));
4408 * @brief Find the first element in a sequence for which a
4409 * predicate is true.
4410 * @ingroup non_mutating_algorithms
4411 * @param first An input iterator.
4412 * @param last An input iterator.
4413 * @param pred A predicate.
4414 * @return The first iterator @c i in the range @p [first,last)
4415 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4417 template<typename _InputIterator, typename _Predicate>
4418 inline _InputIterator
4419 find_if(_InputIterator __first, _InputIterator __last,
4422 // concept requirements
4423 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4424 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4425 typename iterator_traits<_InputIterator>::value_type>)
4426 __glibcxx_requires_valid_range(__first, __last);
4427 return std::__find_if(__first, __last, __pred,
4428 std::__iterator_category(__first));
4432 * @brief Find element from a set in a sequence.
4433 * @ingroup non_mutating_algorithms
4434 * @param first1 Start of range to search.
4435 * @param last1 End of range to search.
4436 * @param first2 Start of match candidates.
4437 * @param last2 End of match candidates.
4438 * @return The first iterator @c i in the range
4439 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4440 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4442 * Searches the range @p [first1,last1) for an element that is equal to
4443 * some element in the range [first2,last2). If found, returns an iterator
4444 * in the range [first1,last1), otherwise returns @p last1.
4446 template<typename _InputIterator, typename _ForwardIterator>
4448 find_first_of(_InputIterator __first1, _InputIterator __last1,
4449 _ForwardIterator __first2, _ForwardIterator __last2)
4451 // concept requirements
4452 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4453 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4454 __glibcxx_function_requires(_EqualOpConcept<
4455 typename iterator_traits<_InputIterator>::value_type,
4456 typename iterator_traits<_ForwardIterator>::value_type>)
4457 __glibcxx_requires_valid_range(__first1, __last1);
4458 __glibcxx_requires_valid_range(__first2, __last2);
4460 for (; __first1 != __last1; ++__first1)
4461 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4462 if (*__first1 == *__iter)
4468 * @brief Find element from a set in a sequence using a predicate.
4469 * @ingroup non_mutating_algorithms
4470 * @param first1 Start of range to search.
4471 * @param last1 End of range to search.
4472 * @param first2 Start of match candidates.
4473 * @param last2 End of match candidates.
4474 * @param comp Predicate to use.
4475 * @return The first iterator @c i in the range
4476 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4477 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4480 * Searches the range @p [first1,last1) for an element that is
4481 * equal to some element in the range [first2,last2). If found,
4482 * returns an iterator in the range [first1,last1), otherwise
4485 template<typename _InputIterator, typename _ForwardIterator,
4486 typename _BinaryPredicate>
4488 find_first_of(_InputIterator __first1, _InputIterator __last1,
4489 _ForwardIterator __first2, _ForwardIterator __last2,
4490 _BinaryPredicate __comp)
4492 // concept requirements
4493 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4494 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4495 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4496 typename iterator_traits<_InputIterator>::value_type,
4497 typename iterator_traits<_ForwardIterator>::value_type>)
4498 __glibcxx_requires_valid_range(__first1, __last1);
4499 __glibcxx_requires_valid_range(__first2, __last2);
4501 for (; __first1 != __last1; ++__first1)
4502 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4503 if (__comp(*__first1, *__iter))
4509 * @brief Find two adjacent values in a sequence that are equal.
4510 * @ingroup non_mutating_algorithms
4511 * @param first A forward iterator.
4512 * @param last A forward iterator.
4513 * @return The first iterator @c i such that @c i and @c i+1 are both
4514 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4515 * or @p last if no such iterator exists.
4517 template<typename _ForwardIterator>
4519 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4521 // concept requirements
4522 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4523 __glibcxx_function_requires(_EqualityComparableConcept<
4524 typename iterator_traits<_ForwardIterator>::value_type>)
4525 __glibcxx_requires_valid_range(__first, __last);
4526 if (__first == __last)
4528 _ForwardIterator __next = __first;
4529 while(++__next != __last)
4531 if (*__first == *__next)
4539 * @brief Find two adjacent values in a sequence using a predicate.
4540 * @ingroup non_mutating_algorithms
4541 * @param first A forward iterator.
4542 * @param last A forward iterator.
4543 * @param binary_pred A binary predicate.
4544 * @return The first iterator @c i such that @c i and @c i+1 are both
4545 * valid iterators in @p [first,last) and such that
4546 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4549 template<typename _ForwardIterator, typename _BinaryPredicate>
4551 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4552 _BinaryPredicate __binary_pred)
4554 // concept requirements
4555 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4556 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4557 typename iterator_traits<_ForwardIterator>::value_type,
4558 typename iterator_traits<_ForwardIterator>::value_type>)
4559 __glibcxx_requires_valid_range(__first, __last);
4560 if (__first == __last)
4562 _ForwardIterator __next = __first;
4563 while(++__next != __last)
4565 if (__binary_pred(*__first, *__next))
4573 * @brief Count the number of copies of a value in a sequence.
4574 * @ingroup non_mutating_algorithms
4575 * @param first An input iterator.
4576 * @param last An input iterator.
4577 * @param value The value to be counted.
4578 * @return The number of iterators @c i in the range @p [first,last)
4579 * for which @c *i == @p value
4581 template<typename _InputIterator, typename _Tp>
4582 typename iterator_traits<_InputIterator>::difference_type
4583 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4585 // concept requirements
4586 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4587 __glibcxx_function_requires(_EqualOpConcept<
4588 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4589 __glibcxx_requires_valid_range(__first, __last);
4590 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4591 for (; __first != __last; ++__first)
4592 if (*__first == __value)
4598 * @brief Count the elements of a sequence for which a predicate is true.
4599 * @ingroup non_mutating_algorithms
4600 * @param first An input iterator.
4601 * @param last An input iterator.
4602 * @param pred A predicate.
4603 * @return The number of iterators @c i in the range @p [first,last)
4604 * for which @p pred(*i) is true.
4606 template<typename _InputIterator, typename _Predicate>
4607 typename iterator_traits<_InputIterator>::difference_type
4608 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4610 // concept requirements
4611 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4612 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4613 typename iterator_traits<_InputIterator>::value_type>)
4614 __glibcxx_requires_valid_range(__first, __last);
4615 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4616 for (; __first != __last; ++__first)
4617 if (__pred(*__first))
4623 * @brief Search a sequence for a matching sub-sequence.
4624 * @ingroup non_mutating_algorithms
4625 * @param first1 A forward iterator.
4626 * @param last1 A forward iterator.
4627 * @param first2 A forward iterator.
4628 * @param last2 A forward iterator.
4629 * @return The first iterator @c i in the range
4630 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4631 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4632 * such iterator exists.
4634 * Searches the range @p [first1,last1) for a sub-sequence that compares
4635 * equal value-by-value with the sequence given by @p [first2,last2) and
4636 * returns an iterator to the first element of the sub-sequence, or
4637 * @p last1 if the sub-sequence is not found.
4639 * Because the sub-sequence must lie completely within the range
4640 * @p [first1,last1) it must start at a position less than
4641 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4643 * This means that the returned iterator @c i will be in the range
4644 * @p [first1,last1-(last2-first2))
4646 template<typename _ForwardIterator1, typename _ForwardIterator2>
4648 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4649 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4651 // concept requirements
4652 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4653 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4654 __glibcxx_function_requires(_EqualOpConcept<
4655 typename iterator_traits<_ForwardIterator1>::value_type,
4656 typename iterator_traits<_ForwardIterator2>::value_type>)
4657 __glibcxx_requires_valid_range(__first1, __last1);
4658 __glibcxx_requires_valid_range(__first2, __last2);
4660 // Test for empty ranges
4661 if (__first1 == __last1 || __first2 == __last2)
4664 // Test for a pattern of length 1.
4665 _ForwardIterator2 __p1(__first2);
4666 if (++__p1 == __last2)
4667 return _GLIBCXX_STD_A::find(__first1, __last1, *__first2);
4670 _ForwardIterator2 __p;
4671 _ForwardIterator1 __current = __first1;
4675 __first1 = _GLIBCXX_STD_A::find(__first1, __last1, *__first2);
4676 if (__first1 == __last1)
4680 __current = __first1;
4681 if (++__current == __last1)
4684 while (*__current == *__p)
4686 if (++__p == __last2)
4688 if (++__current == __last1)
4697 * @brief Search a sequence for a matching sub-sequence using a predicate.
4698 * @ingroup non_mutating_algorithms
4699 * @param first1 A forward iterator.
4700 * @param last1 A forward iterator.
4701 * @param first2 A forward iterator.
4702 * @param last2 A forward iterator.
4703 * @param predicate A binary predicate.
4704 * @return The first iterator @c i in the range
4705 * @p [first1,last1-(last2-first2)) such that
4706 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4707 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4709 * Searches the range @p [first1,last1) for a sub-sequence that compares
4710 * equal value-by-value with the sequence given by @p [first2,last2),
4711 * using @p predicate to determine equality, and returns an iterator
4712 * to the first element of the sub-sequence, or @p last1 if no such
4715 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4717 template<typename _ForwardIterator1, typename _ForwardIterator2,
4718 typename _BinaryPredicate>
4720 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4721 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4722 _BinaryPredicate __predicate)
4724 // concept requirements
4725 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4726 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4727 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4728 typename iterator_traits<_ForwardIterator1>::value_type,
4729 typename iterator_traits<_ForwardIterator2>::value_type>)
4730 __glibcxx_requires_valid_range(__first1, __last1);
4731 __glibcxx_requires_valid_range(__first2, __last2);
4733 // Test for empty ranges
4734 if (__first1 == __last1 || __first2 == __last2)
4737 // Test for a pattern of length 1.
4738 _ForwardIterator2 __p1(__first2);
4739 if (++__p1 == __last2)
4741 while (__first1 != __last1
4742 && !bool(__predicate(*__first1, *__first2)))
4748 _ForwardIterator2 __p;
4749 _ForwardIterator1 __current = __first1;
4753 while (__first1 != __last1
4754 && !bool(__predicate(*__first1, *__first2)))
4756 if (__first1 == __last1)
4760 __current = __first1;
4761 if (++__current == __last1)
4764 while (__predicate(*__current, *__p))
4766 if (++__p == __last2)
4768 if (++__current == __last1)
4778 * @brief Search a sequence for a number of consecutive values.
4779 * @ingroup non_mutating_algorithms
4780 * @param first A forward iterator.
4781 * @param last A forward iterator.
4782 * @param count The number of consecutive values.
4783 * @param val The value to find.
4784 * @return The first iterator @c i in the range @p [first,last-count)
4785 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4786 * or @p last if no such iterator exists.
4788 * Searches the range @p [first,last) for @p count consecutive elements
4791 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4793 search_n(_ForwardIterator __first, _ForwardIterator __last,
4794 _Integer __count, const _Tp& __val)
4796 // concept requirements
4797 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4798 __glibcxx_function_requires(_EqualOpConcept<
4799 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4800 __glibcxx_requires_valid_range(__first, __last);
4805 return _GLIBCXX_STD_A::find(__first, __last, __val);
4806 return std::__search_n(__first, __last, __count, __val,
4807 std::__iterator_category(__first));
4812 * @brief Search a sequence for a number of consecutive values using a
4814 * @ingroup non_mutating_algorithms
4815 * @param first A forward iterator.
4816 * @param last A forward iterator.
4817 * @param count The number of consecutive values.
4818 * @param val The value to find.
4819 * @param binary_pred A binary predicate.
4820 * @return The first iterator @c i in the range @p [first,last-count)
4821 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4822 * range @p [0,count), or @p last if no such iterator exists.
4824 * Searches the range @p [first,last) for @p count consecutive elements
4825 * for which the predicate returns true.
4827 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4828 typename _BinaryPredicate>
4830 search_n(_ForwardIterator __first, _ForwardIterator __last,
4831 _Integer __count, const _Tp& __val,
4832 _BinaryPredicate __binary_pred)
4834 // concept requirements
4835 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4836 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4837 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4838 __glibcxx_requires_valid_range(__first, __last);
4844 while (__first != __last && !bool(__binary_pred(*__first, __val)))
4848 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4849 std::__iterator_category(__first));
4854 * @brief Perform an operation on a sequence.
4855 * @ingroup mutating_algorithms
4856 * @param first An input iterator.
4857 * @param last An input iterator.
4858 * @param result An output iterator.
4859 * @param unary_op A unary operator.
4860 * @return An output iterator equal to @p result+(last-first).
4862 * Applies the operator to each element in the input range and assigns
4863 * the results to successive elements of the output sequence.
4864 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4865 * range @p [0,last-first).
4867 * @p unary_op must not alter its argument.
4869 template<typename _InputIterator, typename _OutputIterator,
4870 typename _UnaryOperation>
4872 transform(_InputIterator __first, _InputIterator __last,
4873 _OutputIterator __result, _UnaryOperation __unary_op)
4875 // concept requirements
4876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4877 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4878 // "the type returned by a _UnaryOperation"
4879 __typeof__(__unary_op(*__first))>)
4880 __glibcxx_requires_valid_range(__first, __last);
4882 for (; __first != __last; ++__first, ++__result)
4883 *__result = __unary_op(*__first);
4888 * @brief Perform an operation on corresponding elements of two sequences.
4889 * @ingroup mutating_algorithms
4890 * @param first1 An input iterator.
4891 * @param last1 An input iterator.
4892 * @param first2 An input iterator.
4893 * @param result An output iterator.
4894 * @param binary_op A binary operator.
4895 * @return An output iterator equal to @p result+(last-first).
4897 * Applies the operator to the corresponding elements in the two
4898 * input ranges and assigns the results to successive elements of the
4900 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4901 * @c N in the range @p [0,last1-first1).
4903 * @p binary_op must not alter either of its arguments.
4905 template<typename _InputIterator1, typename _InputIterator2,
4906 typename _OutputIterator, typename _BinaryOperation>
4908 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4909 _InputIterator2 __first2, _OutputIterator __result,
4910 _BinaryOperation __binary_op)
4912 // concept requirements
4913 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4914 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4915 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4916 // "the type returned by a _BinaryOperation"
4917 __typeof__(__binary_op(*__first1,*__first2))>)
4918 __glibcxx_requires_valid_range(__first1, __last1);
4920 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4921 *__result = __binary_op(*__first1, *__first2);
4926 * @brief Replace each occurrence of one value in a sequence with another
4928 * @ingroup mutating_algorithms
4929 * @param first A forward iterator.
4930 * @param last A forward iterator.
4931 * @param old_value The value to be replaced.
4932 * @param new_value The replacement value.
4933 * @return replace() returns no value.
4935 * For each iterator @c i in the range @p [first,last) if @c *i ==
4936 * @p old_value then the assignment @c *i = @p new_value is performed.
4938 template<typename _ForwardIterator, typename _Tp>
4940 replace(_ForwardIterator __first, _ForwardIterator __last,
4941 const _Tp& __old_value, const _Tp& __new_value)
4943 // concept requirements
4944 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4946 __glibcxx_function_requires(_EqualOpConcept<
4947 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4948 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4949 typename iterator_traits<_ForwardIterator>::value_type>)
4950 __glibcxx_requires_valid_range(__first, __last);
4952 for (; __first != __last; ++__first)
4953 if (*__first == __old_value)
4954 *__first = __new_value;
4958 * @brief Replace each value in a sequence for which a predicate returns
4959 * true with another value.
4960 * @ingroup mutating_algorithms
4961 * @param first A forward iterator.
4962 * @param last A forward iterator.
4963 * @param pred A predicate.
4964 * @param new_value The replacement value.
4965 * @return replace_if() returns no value.
4967 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4968 * is true then the assignment @c *i = @p new_value is performed.
4970 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4972 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4973 _Predicate __pred, const _Tp& __new_value)
4975 // concept requirements
4976 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4978 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4979 typename iterator_traits<_ForwardIterator>::value_type>)
4980 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4981 typename iterator_traits<_ForwardIterator>::value_type>)
4982 __glibcxx_requires_valid_range(__first, __last);
4984 for (; __first != __last; ++__first)
4985 if (__pred(*__first))
4986 *__first = __new_value;
4990 * @brief Assign the result of a function object to each value in a
4992 * @ingroup mutating_algorithms
4993 * @param first A forward iterator.
4994 * @param last A forward iterator.
4995 * @param gen A function object taking no arguments and returning
4996 * std::iterator_traits<_ForwardIterator>::value_type
4997 * @return generate() returns no value.
4999 * Performs the assignment @c *i = @p gen() for each @c i in the range
5002 template<typename _ForwardIterator, typename _Generator>
5004 generate(_ForwardIterator __first, _ForwardIterator __last,
5007 // concept requirements
5008 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5009 __glibcxx_function_requires(_GeneratorConcept<_Generator,
5010 typename iterator_traits<_ForwardIterator>::value_type>)
5011 __glibcxx_requires_valid_range(__first, __last);
5013 for (; __first != __last; ++__first)
5018 * @brief Assign the result of a function object to each value in a
5020 * @ingroup mutating_algorithms
5021 * @param first A forward iterator.
5022 * @param n The length of the sequence.
5023 * @param gen A function object taking no arguments and returning
5024 * std::iterator_traits<_ForwardIterator>::value_type
5025 * @return The end of the sequence, @p first+n
5027 * Performs the assignment @c *i = @p gen() for each @c i in the range
5028 * @p [first,first+n).
5030 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5031 * DR 865. More algorithms that throw away information
5033 template<typename _OutputIterator, typename _Size, typename _Generator>
5035 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
5037 // concept requirements
5038 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5039 // "the type returned by a _Generator"
5040 __typeof__(__gen())>)
5042 for (__decltype(__n + 0) __niter = __n;
5043 __niter > 0; --__niter, ++__first)
5050 * @brief Copy a sequence, removing consecutive duplicate values.
5051 * @ingroup mutating_algorithms
5052 * @param first An input iterator.
5053 * @param last An input iterator.
5054 * @param result An output iterator.
5055 * @return An iterator designating the end of the resulting sequence.
5057 * Copies each element in the range @p [first,last) to the range
5058 * beginning at @p result, except that only the first element is copied
5059 * from groups of consecutive elements that compare equal.
5060 * unique_copy() is stable, so the relative order of elements that are
5061 * copied is unchanged.
5063 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5064 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5066 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5067 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5070 template<typename _InputIterator, typename _OutputIterator>
5071 inline _OutputIterator
5072 unique_copy(_InputIterator __first, _InputIterator __last,
5073 _OutputIterator __result)
5075 // concept requirements
5076 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5077 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5078 typename iterator_traits<_InputIterator>::value_type>)
5079 __glibcxx_function_requires(_EqualityComparableConcept<
5080 typename iterator_traits<_InputIterator>::value_type>)
5081 __glibcxx_requires_valid_range(__first, __last);
5083 if (__first == __last)
5085 return std::__unique_copy(__first, __last, __result,
5086 std::__iterator_category(__first),
5087 std::__iterator_category(__result));
5091 * @brief Copy a sequence, removing consecutive values using a predicate.
5092 * @ingroup mutating_algorithms
5093 * @param first An input iterator.
5094 * @param last An input iterator.
5095 * @param result An output iterator.
5096 * @param binary_pred A binary predicate.
5097 * @return An iterator designating the end of the resulting sequence.
5099 * Copies each element in the range @p [first,last) to the range
5100 * beginning at @p result, except that only the first element is copied
5101 * from groups of consecutive elements for which @p binary_pred returns
5103 * unique_copy() is stable, so the relative order of elements that are
5104 * copied is unchanged.
5106 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5107 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5109 template<typename _InputIterator, typename _OutputIterator,
5110 typename _BinaryPredicate>
5111 inline _OutputIterator
5112 unique_copy(_InputIterator __first, _InputIterator __last,
5113 _OutputIterator __result,
5114 _BinaryPredicate __binary_pred)
5116 // concept requirements -- predicates checked later
5117 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5118 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5119 typename iterator_traits<_InputIterator>::value_type>)
5120 __glibcxx_requires_valid_range(__first, __last);
5122 if (__first == __last)
5124 return std::__unique_copy(__first, __last, __result, __binary_pred,
5125 std::__iterator_category(__first),
5126 std::__iterator_category(__result));
5131 * @brief Randomly shuffle the elements of a sequence.
5132 * @ingroup mutating_algorithms
5133 * @param first A forward iterator.
5134 * @param last A forward iterator.
5137 * Reorder the elements in the range @p [first,last) using a random
5138 * distribution, so that every possible ordering of the sequence is
5141 template<typename _RandomAccessIterator>
5143 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
5145 // concept requirements
5146 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5147 _RandomAccessIterator>)
5148 __glibcxx_requires_valid_range(__first, __last);
5150 if (__first != __last)
5151 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5152 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
5156 * @brief Shuffle the elements of a sequence using a random number
5158 * @ingroup mutating_algorithms
5159 * @param first A forward iterator.
5160 * @param last A forward iterator.
5161 * @param rand The RNG functor or function.
5164 * Reorders the elements in the range @p [first,last) using @p rand to
5165 * provide a random distribution. Calling @p rand(N) for a positive
5166 * integer @p N should return a randomly chosen integer from the
5169 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
5171 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
5172 #ifdef __GXX_EXPERIMENTAL_CXX0X__
5173 _RandomNumberGenerator&& __rand)
5175 _RandomNumberGenerator& __rand)
5178 // concept requirements
5179 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5180 _RandomAccessIterator>)
5181 __glibcxx_requires_valid_range(__first, __last);
5183 if (__first == __last)
5185 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5186 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
5191 * @brief Move elements for which a predicate is true to the beginning
5193 * @ingroup mutating_algorithms
5194 * @param first A forward iterator.
5195 * @param last A forward iterator.
5196 * @param pred A predicate functor.
5197 * @return An iterator @p middle such that @p pred(i) is true for each
5198 * iterator @p i in the range @p [first,middle) and false for each @p i
5199 * in the range @p [middle,last).
5201 * @p pred must not modify its operand. @p partition() does not preserve
5202 * the relative ordering of elements in each group, use
5203 * @p stable_partition() if this is needed.
5205 template<typename _ForwardIterator, typename _Predicate>
5206 inline _ForwardIterator
5207 partition(_ForwardIterator __first, _ForwardIterator __last,
5210 // concept requirements
5211 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
5213 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
5214 typename iterator_traits<_ForwardIterator>::value_type>)
5215 __glibcxx_requires_valid_range(__first, __last);
5217 return std::__partition(__first, __last, __pred,
5218 std::__iterator_category(__first));
5224 * @brief Sort the smallest elements of a sequence.
5225 * @ingroup sorting_algorithms
5226 * @param first An iterator.
5227 * @param middle Another iterator.
5228 * @param last Another iterator.
5231 * Sorts the smallest @p (middle-first) elements in the range
5232 * @p [first,last) and moves them to the range @p [first,middle). The
5233 * order of the remaining elements in the range @p [middle,last) is
5235 * After the sort if @p i and @j are iterators in the range
5236 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5237 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5239 template<typename _RandomAccessIterator>
5241 partial_sort(_RandomAccessIterator __first,
5242 _RandomAccessIterator __middle,
5243 _RandomAccessIterator __last)
5245 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5248 // concept requirements
5249 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5250 _RandomAccessIterator>)
5251 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5252 __glibcxx_requires_valid_range(__first, __middle);
5253 __glibcxx_requires_valid_range(__middle, __last);
5255 std::__heap_select(__first, __middle, __last);
5256 std::sort_heap(__first, __middle);
5260 * @brief Sort the smallest elements of a sequence using a predicate
5262 * @ingroup sorting_algorithms
5263 * @param first An iterator.
5264 * @param middle Another iterator.
5265 * @param last Another iterator.
5266 * @param comp A comparison functor.
5269 * Sorts the smallest @p (middle-first) elements in the range
5270 * @p [first,last) and moves them to the range @p [first,middle). The
5271 * order of the remaining elements in the range @p [middle,last) is
5273 * After the sort if @p i and @j are iterators in the range
5274 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5275 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5278 template<typename _RandomAccessIterator, typename _Compare>
5280 partial_sort(_RandomAccessIterator __first,
5281 _RandomAccessIterator __middle,
5282 _RandomAccessIterator __last,
5285 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5288 // concept requirements
5289 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5290 _RandomAccessIterator>)
5291 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5292 _ValueType, _ValueType>)
5293 __glibcxx_requires_valid_range(__first, __middle);
5294 __glibcxx_requires_valid_range(__middle, __last);
5296 std::__heap_select(__first, __middle, __last, __comp);
5297 std::sort_heap(__first, __middle, __comp);
5301 * @brief Sort a sequence just enough to find a particular position.
5302 * @ingroup sorting_algorithms
5303 * @param first An iterator.
5304 * @param nth Another iterator.
5305 * @param last Another iterator.
5308 * Rearranges the elements in the range @p [first,last) so that @p *nth
5309 * is the same element that would have been in that position had the
5310 * whole sequence been sorted.
5311 * whole sequence been sorted. The elements either side of @p *nth are
5312 * not completely sorted, but for any iterator @i in the range
5313 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5314 * holds that @p *j<*i is false.
5316 template<typename _RandomAccessIterator>
5318 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5319 _RandomAccessIterator __last)
5321 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5324 // concept requirements
5325 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5326 _RandomAccessIterator>)
5327 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5328 __glibcxx_requires_valid_range(__first, __nth);
5329 __glibcxx_requires_valid_range(__nth, __last);
5331 if (__first == __last || __nth == __last)
5334 std::__introselect(__first, __nth, __last,
5335 std::__lg(__last - __first) * 2);
5339 * @brief Sort a sequence just enough to find a particular position
5340 * using a predicate for comparison.
5341 * @ingroup sorting_algorithms
5342 * @param first An iterator.
5343 * @param nth Another iterator.
5344 * @param last Another iterator.
5345 * @param comp A comparison functor.
5348 * Rearranges the elements in the range @p [first,last) so that @p *nth
5349 * is the same element that would have been in that position had the
5350 * whole sequence been sorted. The elements either side of @p *nth are
5351 * not completely sorted, but for any iterator @i in the range
5352 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5353 * holds that @p comp(*j,*i) is false.
5355 template<typename _RandomAccessIterator, typename _Compare>
5357 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5358 _RandomAccessIterator __last, _Compare __comp)
5360 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5363 // concept requirements
5364 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5365 _RandomAccessIterator>)
5366 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5367 _ValueType, _ValueType>)
5368 __glibcxx_requires_valid_range(__first, __nth);
5369 __glibcxx_requires_valid_range(__nth, __last);
5371 if (__first == __last || __nth == __last)
5374 std::__introselect(__first, __nth, __last,
5375 std::__lg(__last - __first) * 2, __comp);
5380 * @brief Sort the elements of a sequence.
5381 * @ingroup sorting_algorithms
5382 * @param first An iterator.
5383 * @param last Another iterator.
5386 * Sorts the elements in the range @p [first,last) in ascending order,
5387 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5388 * @p [first,last-1).
5390 * The relative ordering of equivalent elements is not preserved, use
5391 * @p stable_sort() if this is needed.
5393 template<typename _RandomAccessIterator>
5395 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5397 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5400 // concept requirements
5401 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5402 _RandomAccessIterator>)
5403 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5404 __glibcxx_requires_valid_range(__first, __last);
5406 if (__first != __last)
5408 std::__introsort_loop(__first, __last,
5409 std::__lg(__last - __first) * 2);
5410 std::__final_insertion_sort(__first, __last);
5415 * @brief Sort the elements of a sequence using a predicate for comparison.
5416 * @ingroup sorting_algorithms
5417 * @param first An iterator.
5418 * @param last Another iterator.
5419 * @param comp A comparison functor.
5422 * Sorts the elements in the range @p [first,last) in ascending order,
5423 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5424 * range @p [first,last-1).
5426 * The relative ordering of equivalent elements is not preserved, use
5427 * @p stable_sort() if this is needed.
5429 template<typename _RandomAccessIterator, typename _Compare>
5431 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5434 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5437 // concept requirements
5438 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5439 _RandomAccessIterator>)
5440 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
5442 __glibcxx_requires_valid_range(__first, __last);
5444 if (__first != __last)
5446 std::__introsort_loop(__first, __last,
5447 std::__lg(__last - __first) * 2, __comp);
5448 std::__final_insertion_sort(__first, __last, __comp);
5453 * @brief Merges two sorted ranges.
5454 * @ingroup sorting_algorithms
5455 * @param first1 An iterator.
5456 * @param first2 Another iterator.
5457 * @param last1 Another iterator.
5458 * @param last2 Another iterator.
5459 * @param result An iterator pointing to the end of the merged range.
5460 * @return An iterator pointing to the first element <em>not less
5463 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5464 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5465 * must be sorted, and the output range must not overlap with either of
5466 * the input ranges. The sort is @e stable, that is, for equivalent
5467 * elements in the two ranges, elements from the first range will always
5468 * come before elements from the second.
5470 template<typename _InputIterator1, typename _InputIterator2,
5471 typename _OutputIterator>
5473 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5474 _InputIterator2 __first2, _InputIterator2 __last2,
5475 _OutputIterator __result)
5477 typedef typename iterator_traits<_InputIterator1>::value_type
5479 typedef typename iterator_traits<_InputIterator2>::value_type
5482 // concept requirements
5483 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5484 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5485 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5487 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5489 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5490 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5491 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5493 while (__first1 != __last1 && __first2 != __last2)
5495 if (*__first2 < *__first1)
5497 *__result = *__first2;
5502 *__result = *__first1;
5507 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5512 * @brief Merges two sorted ranges.
5513 * @ingroup sorting_algorithms
5514 * @param first1 An iterator.
5515 * @param first2 Another iterator.
5516 * @param last1 Another iterator.
5517 * @param last2 Another iterator.
5518 * @param result An iterator pointing to the end of the merged range.
5519 * @param comp A functor to use for comparisons.
5520 * @return An iterator pointing to the first element "not less
5523 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5524 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5525 * must be sorted, and the output range must not overlap with either of
5526 * the input ranges. The sort is @e stable, that is, for equivalent
5527 * elements in the two ranges, elements from the first range will always
5528 * come before elements from the second.
5530 * The comparison function should have the same effects on ordering as
5531 * the function used for the initial sort.
5533 template<typename _InputIterator1, typename _InputIterator2,
5534 typename _OutputIterator, typename _Compare>
5536 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5537 _InputIterator2 __first2, _InputIterator2 __last2,
5538 _OutputIterator __result, _Compare __comp)
5540 typedef typename iterator_traits<_InputIterator1>::value_type
5542 typedef typename iterator_traits<_InputIterator2>::value_type
5545 // concept requirements
5546 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5547 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5548 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5550 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5552 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5553 _ValueType2, _ValueType1>)
5554 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5555 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5557 while (__first1 != __last1 && __first2 != __last2)
5559 if (__comp(*__first2, *__first1))
5561 *__result = *__first2;
5566 *__result = *__first1;
5571 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5577 * @brief Sort the elements of a sequence, preserving the relative order
5578 * of equivalent elements.
5579 * @ingroup sorting_algorithms
5580 * @param first An iterator.
5581 * @param last Another iterator.
5584 * Sorts the elements in the range @p [first,last) in ascending order,
5585 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5586 * @p [first,last-1).
5588 * The relative ordering of equivalent elements is preserved, so any two
5589 * elements @p x and @p y in the range @p [first,last) such that
5590 * @p x<y is false and @p y<x is false will have the same relative
5591 * ordering after calling @p stable_sort().
5593 template<typename _RandomAccessIterator>
5595 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5597 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5599 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5602 // concept requirements
5603 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5604 _RandomAccessIterator>)
5605 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5606 __glibcxx_requires_valid_range(__first, __last);
5608 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5610 if (__buf.begin() == 0)
5611 std::__inplace_stable_sort(__first, __last);
5613 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5614 _DistanceType(__buf.size()));
5618 * @brief Sort the elements of a sequence using a predicate for comparison,
5619 * preserving the relative order of equivalent elements.
5620 * @ingroup sorting_algorithms
5621 * @param first An iterator.
5622 * @param last Another iterator.
5623 * @param comp A comparison functor.
5626 * Sorts the elements in the range @p [first,last) in ascending order,
5627 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5628 * range @p [first,last-1).
5630 * The relative ordering of equivalent elements is preserved, so any two
5631 * elements @p x and @p y in the range @p [first,last) such that
5632 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5633 * relative ordering after calling @p stable_sort().
5635 template<typename _RandomAccessIterator, typename _Compare>
5637 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5640 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5642 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5645 // concept requirements
5646 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5647 _RandomAccessIterator>)
5648 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5651 __glibcxx_requires_valid_range(__first, __last);
5653 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5655 if (__buf.begin() == 0)
5656 std::__inplace_stable_sort(__first, __last, __comp);
5658 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5659 _DistanceType(__buf.size()), __comp);
5664 * @brief Return the union of two sorted ranges.
5665 * @ingroup set_algorithms
5666 * @param first1 Start of first range.
5667 * @param last1 End of first range.
5668 * @param first2 Start of second range.
5669 * @param last2 End of second range.
5670 * @return End of the output range.
5671 * @ingroup set_algorithms
5673 * This operation iterates over both ranges, copying elements present in
5674 * each range in order to the output range. Iterators increment for each
5675 * range. When the current element of one range is less than the other,
5676 * that element is copied and the iterator advanced. If an element is
5677 * contained in both ranges, the element from the first range is copied and
5678 * both ranges advance. The output range may not overlap either input
5681 template<typename _InputIterator1, typename _InputIterator2,
5682 typename _OutputIterator>
5684 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5685 _InputIterator2 __first2, _InputIterator2 __last2,
5686 _OutputIterator __result)
5688 typedef typename iterator_traits<_InputIterator1>::value_type
5690 typedef typename iterator_traits<_InputIterator2>::value_type
5693 // concept requirements
5694 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5695 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5696 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5698 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5700 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5701 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5702 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5703 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5705 while (__first1 != __last1 && __first2 != __last2)
5707 if (*__first1 < *__first2)
5709 *__result = *__first1;
5712 else if (*__first2 < *__first1)
5714 *__result = *__first2;
5719 *__result = *__first1;
5725 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5730 * @brief Return the union of two sorted ranges using a comparison functor.
5731 * @ingroup set_algorithms
5732 * @param first1 Start of first range.
5733 * @param last1 End of first range.
5734 * @param first2 Start of second range.
5735 * @param last2 End of second range.
5736 * @param comp The comparison functor.
5737 * @return End of the output range.
5738 * @ingroup set_algorithms
5740 * This operation iterates over both ranges, copying elements present in
5741 * each range in order to the output range. Iterators increment for each
5742 * range. When the current element of one range is less than the other
5743 * according to @a comp, that element is copied and the iterator advanced.
5744 * If an equivalent element according to @a comp is contained in both
5745 * ranges, the element from the first range is copied and both ranges
5746 * advance. The output range may not overlap either input range.
5748 template<typename _InputIterator1, typename _InputIterator2,
5749 typename _OutputIterator, typename _Compare>
5751 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5752 _InputIterator2 __first2, _InputIterator2 __last2,
5753 _OutputIterator __result, _Compare __comp)
5755 typedef typename iterator_traits<_InputIterator1>::value_type
5757 typedef typename iterator_traits<_InputIterator2>::value_type
5760 // concept requirements
5761 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5762 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5763 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5765 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5767 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5768 _ValueType1, _ValueType2>)
5769 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5770 _ValueType2, _ValueType1>)
5771 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5772 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5774 while (__first1 != __last1 && __first2 != __last2)
5776 if (__comp(*__first1, *__first2))
5778 *__result = *__first1;
5781 else if (__comp(*__first2, *__first1))
5783 *__result = *__first2;
5788 *__result = *__first1;
5794 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5799 * @brief Return the intersection of two sorted ranges.
5800 * @ingroup set_algorithms
5801 * @param first1 Start of first range.
5802 * @param last1 End of first range.
5803 * @param first2 Start of second range.
5804 * @param last2 End of second range.
5805 * @return End of the output range.
5806 * @ingroup set_algorithms
5808 * This operation iterates over both ranges, copying elements present in
5809 * both ranges in order to the output range. Iterators increment for each
5810 * range. When the current element of one range is less than the other,
5811 * that iterator advances. If an element is contained in both ranges, the
5812 * element from the first range is copied and both ranges advance. The
5813 * output range may not overlap either input range.
5815 template<typename _InputIterator1, typename _InputIterator2,
5816 typename _OutputIterator>
5818 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5819 _InputIterator2 __first2, _InputIterator2 __last2,
5820 _OutputIterator __result)
5822 typedef typename iterator_traits<_InputIterator1>::value_type
5824 typedef typename iterator_traits<_InputIterator2>::value_type
5827 // concept requirements
5828 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5829 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5830 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5832 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5833 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5834 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5835 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5837 while (__first1 != __last1 && __first2 != __last2)
5838 if (*__first1 < *__first2)
5840 else if (*__first2 < *__first1)
5844 *__result = *__first1;
5853 * @brief Return the intersection of two sorted ranges using comparison
5855 * @ingroup set_algorithms
5856 * @param first1 Start of first range.
5857 * @param last1 End of first range.
5858 * @param first2 Start of second range.
5859 * @param last2 End of second range.
5860 * @param comp The comparison functor.
5861 * @return End of the output range.
5862 * @ingroup set_algorithms
5864 * This operation iterates over both ranges, copying elements present in
5865 * both ranges in order to the output range. Iterators increment for each
5866 * range. When the current element of one range is less than the other
5867 * according to @a comp, that iterator advances. If an element is
5868 * contained in both ranges according to @a comp, the element from the
5869 * first range is copied and both ranges advance. The output range may not
5870 * overlap either input range.
5872 template<typename _InputIterator1, typename _InputIterator2,
5873 typename _OutputIterator, typename _Compare>
5875 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5876 _InputIterator2 __first2, _InputIterator2 __last2,
5877 _OutputIterator __result, _Compare __comp)
5879 typedef typename iterator_traits<_InputIterator1>::value_type
5881 typedef typename iterator_traits<_InputIterator2>::value_type
5884 // concept requirements
5885 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5886 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5887 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5889 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5890 _ValueType1, _ValueType2>)
5891 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5892 _ValueType2, _ValueType1>)
5893 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5894 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5896 while (__first1 != __last1 && __first2 != __last2)
5897 if (__comp(*__first1, *__first2))
5899 else if (__comp(*__first2, *__first1))
5903 *__result = *__first1;
5912 * @brief Return the difference of two sorted ranges.
5913 * @ingroup set_algorithms
5914 * @param first1 Start of first range.
5915 * @param last1 End of first range.
5916 * @param first2 Start of second range.
5917 * @param last2 End of second range.
5918 * @return End of the output range.
5919 * @ingroup set_algorithms
5921 * This operation iterates over both ranges, copying elements present in
5922 * the first range but not the second in order to the output range.
5923 * Iterators increment for each range. When the current element of the
5924 * first range is less than the second, that element is copied and the
5925 * iterator advances. If the current element of the second range is less,
5926 * the iterator advances, but no element is copied. If an element is
5927 * contained in both ranges, no elements are copied and both ranges
5928 * advance. The output range may not overlap either input range.
5930 template<typename _InputIterator1, typename _InputIterator2,
5931 typename _OutputIterator>
5933 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5934 _InputIterator2 __first2, _InputIterator2 __last2,
5935 _OutputIterator __result)
5937 typedef typename iterator_traits<_InputIterator1>::value_type
5939 typedef typename iterator_traits<_InputIterator2>::value_type
5942 // concept requirements
5943 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5944 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5945 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5947 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5948 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5949 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5950 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5952 while (__first1 != __last1 && __first2 != __last2)
5953 if (*__first1 < *__first2)
5955 *__result = *__first1;
5959 else if (*__first2 < *__first1)
5966 return std::copy(__first1, __last1, __result);
5970 * @brief Return the difference of two sorted ranges using comparison
5972 * @ingroup set_algorithms
5973 * @param first1 Start of first range.
5974 * @param last1 End of first range.
5975 * @param first2 Start of second range.
5976 * @param last2 End of second range.
5977 * @param comp The comparison functor.
5978 * @return End of the output range.
5979 * @ingroup set_algorithms
5981 * This operation iterates over both ranges, copying elements present in
5982 * the first range but not the second in order to the output range.
5983 * Iterators increment for each range. When the current element of the
5984 * first range is less than the second according to @a comp, that element
5985 * is copied and the iterator advances. If the current element of the
5986 * second range is less, no element is copied and the iterator advances.
5987 * If an element is contained in both ranges according to @a comp, no
5988 * elements are copied and both ranges advance. The output range may not
5989 * overlap either input range.
5991 template<typename _InputIterator1, typename _InputIterator2,
5992 typename _OutputIterator, typename _Compare>
5994 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5995 _InputIterator2 __first2, _InputIterator2 __last2,
5996 _OutputIterator __result, _Compare __comp)
5998 typedef typename iterator_traits<_InputIterator1>::value_type
6000 typedef typename iterator_traits<_InputIterator2>::value_type
6003 // concept requirements
6004 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
6005 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
6006 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
6008 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6009 _ValueType1, _ValueType2>)
6010 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6011 _ValueType2, _ValueType1>)
6012 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
6013 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
6015 while (__first1 != __last1 && __first2 != __last2)
6016 if (__comp(*__first1, *__first2))
6018 *__result = *__first1;
6022 else if (__comp(*__first2, *__first1))
6029 return std::copy(__first1, __last1, __result);
6033 * @brief Return the symmetric difference of two sorted ranges.
6034 * @ingroup set_algorithms
6035 * @param first1 Start of first range.
6036 * @param last1 End of first range.
6037 * @param first2 Start of second range.
6038 * @param last2 End of second range.
6039 * @return End of the output range.
6040 * @ingroup set_algorithms
6042 * This operation iterates over both ranges, copying elements present in
6043 * one range but not the other in order to the output range. Iterators
6044 * increment for each range. When the current element of one range is less
6045 * than the other, that element is copied and the iterator advances. If an
6046 * element is contained in both ranges, no elements are copied and both
6047 * ranges advance. The output range may not overlap either input range.
6049 template<typename _InputIterator1, typename _InputIterator2,
6050 typename _OutputIterator>
6052 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
6053 _InputIterator2 __first2, _InputIterator2 __last2,
6054 _OutputIterator __result)
6056 typedef typename iterator_traits<_InputIterator1>::value_type
6058 typedef typename iterator_traits<_InputIterator2>::value_type
6061 // concept requirements
6062 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
6063 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
6064 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
6066 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
6068 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
6069 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
6070 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
6071 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
6073 while (__first1 != __last1 && __first2 != __last2)
6074 if (*__first1 < *__first2)
6076 *__result = *__first1;
6080 else if (*__first2 < *__first1)
6082 *__result = *__first2;
6091 return std::copy(__first2, __last2, std::copy(__first1,
6092 __last1, __result));
6096 * @brief Return the symmetric difference of two sorted ranges using
6097 * comparison functor.
6098 * @ingroup set_algorithms
6099 * @param first1 Start of first range.
6100 * @param last1 End of first range.
6101 * @param first2 Start of second range.
6102 * @param last2 End of second range.
6103 * @param comp The comparison functor.
6104 * @return End of the output range.
6105 * @ingroup set_algorithms
6107 * This operation iterates over both ranges, copying elements present in
6108 * one range but not the other in order to the output range. Iterators
6109 * increment for each range. When the current element of one range is less
6110 * than the other according to @a comp, that element is copied and the
6111 * iterator advances. If an element is contained in both ranges according
6112 * to @a comp, no elements are copied and both ranges advance. The output
6113 * range may not overlap either input range.
6115 template<typename _InputIterator1, typename _InputIterator2,
6116 typename _OutputIterator, typename _Compare>
6118 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
6119 _InputIterator2 __first2, _InputIterator2 __last2,
6120 _OutputIterator __result,
6123 typedef typename iterator_traits<_InputIterator1>::value_type
6125 typedef typename iterator_traits<_InputIterator2>::value_type
6128 // concept requirements
6129 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
6130 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
6131 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
6133 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
6135 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6136 _ValueType1, _ValueType2>)
6137 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6138 _ValueType2, _ValueType1>)
6139 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
6140 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
6142 while (__first1 != __last1 && __first2 != __last2)
6143 if (__comp(*__first1, *__first2))
6145 *__result = *__first1;
6149 else if (__comp(*__first2, *__first1))
6151 *__result = *__first2;
6160 return std::copy(__first2, __last2,
6161 std::copy(__first1, __last1, __result));
6166 * @brief Return the minimum element in a range.
6167 * @ingroup sorting_algorithms
6168 * @param first Start of range.
6169 * @param last End of range.
6170 * @return Iterator referencing the first instance of the smallest value.
6172 template<typename _ForwardIterator>
6174 min_element(_ForwardIterator __first, _ForwardIterator __last)
6176 // concept requirements
6177 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6178 __glibcxx_function_requires(_LessThanComparableConcept<
6179 typename iterator_traits<_ForwardIterator>::value_type>)
6180 __glibcxx_requires_valid_range(__first, __last);
6182 if (__first == __last)
6184 _ForwardIterator __result = __first;
6185 while (++__first != __last)
6186 if (*__first < *__result)
6192 * @brief Return the minimum element in a range using comparison functor.
6193 * @ingroup sorting_algorithms
6194 * @param first Start of range.
6195 * @param last End of range.
6196 * @param comp Comparison functor.
6197 * @return Iterator referencing the first instance of the smallest value
6198 * according to comp.
6200 template<typename _ForwardIterator, typename _Compare>
6202 min_element(_ForwardIterator __first, _ForwardIterator __last,
6205 // concept requirements
6206 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6207 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6208 typename iterator_traits<_ForwardIterator>::value_type,
6209 typename iterator_traits<_ForwardIterator>::value_type>)
6210 __glibcxx_requires_valid_range(__first, __last);
6212 if (__first == __last)
6214 _ForwardIterator __result = __first;
6215 while (++__first != __last)
6216 if (__comp(*__first, *__result))
6222 * @brief Return the maximum element in a range.
6223 * @ingroup sorting_algorithms
6224 * @param first Start of range.
6225 * @param last End of range.
6226 * @return Iterator referencing the first instance of the largest value.
6228 template<typename _ForwardIterator>
6230 max_element(_ForwardIterator __first, _ForwardIterator __last)
6232 // concept requirements
6233 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6234 __glibcxx_function_requires(_LessThanComparableConcept<
6235 typename iterator_traits<_ForwardIterator>::value_type>)
6236 __glibcxx_requires_valid_range(__first, __last);
6238 if (__first == __last)
6240 _ForwardIterator __result = __first;
6241 while (++__first != __last)
6242 if (*__result < *__first)
6248 * @brief Return the maximum element in a range using comparison functor.
6249 * @ingroup sorting_algorithms
6250 * @param first Start of range.
6251 * @param last End of range.
6252 * @param comp Comparison functor.
6253 * @return Iterator referencing the first instance of the largest value
6254 * according to comp.
6256 template<typename _ForwardIterator, typename _Compare>
6258 max_element(_ForwardIterator __first, _ForwardIterator __last,
6261 // concept requirements
6262 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6263 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6264 typename iterator_traits<_ForwardIterator>::value_type,
6265 typename iterator_traits<_ForwardIterator>::value_type>)
6266 __glibcxx_requires_valid_range(__first, __last);
6268 if (__first == __last) return __first;
6269 _ForwardIterator __result = __first;
6270 while (++__first != __last)
6271 if (__comp(*__result, *__first))
6276 _GLIBCXX_END_NAMESPACE_ALGO
6279 #endif /* _STL_ALGO_H */