1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
60 #include <cstdlib> // for rand
61 #include <bits/algorithmfwd.h>
62 #include <bits/stl_heap.h>
63 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
65 // See concept_check.h for the __glibcxx_*_requires macros.
67 _GLIBCXX_BEGIN_NAMESPACE(std)
69 /// Swaps the median value of *__a, *__b and *__c to *__a
70 template<typename _Iterator>
72 __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c)
74 // concept requirements
75 __glibcxx_function_requires(_LessThanComparableConcept<
76 typename iterator_traits<_Iterator>::value_type>)
81 std::iter_swap(__a, __b);
83 std::iter_swap(__a, __c);
88 std::iter_swap(__a, __c);
90 std::iter_swap(__a, __b);
93 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
94 template<typename _Iterator, typename _Compare>
96 __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
99 // concept requirements
100 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
101 typename iterator_traits<_Iterator>::value_type,
102 typename iterator_traits<_Iterator>::value_type>)
104 if (__comp(*__a, *__b))
106 if (__comp(*__b, *__c))
107 std::iter_swap(__a, __b);
108 else if (__comp(*__a, *__c))
109 std::iter_swap(__a, __c);
111 else if (__comp(*__a, *__c))
113 else if (__comp(*__b, *__c))
114 std::iter_swap(__a, __c);
116 std::iter_swap(__a, __b);
121 /// This is an overload used by find() for the Input Iterator case.
122 template<typename _InputIterator, typename _Tp>
123 inline _InputIterator
124 __find(_InputIterator __first, _InputIterator __last,
125 const _Tp& __val, input_iterator_tag)
127 while (__first != __last && !(*__first == __val))
132 /// This is an overload used by find_if() for the Input Iterator case.
133 template<typename _InputIterator, typename _Predicate>
134 inline _InputIterator
135 __find_if(_InputIterator __first, _InputIterator __last,
136 _Predicate __pred, input_iterator_tag)
138 while (__first != __last && !bool(__pred(*__first)))
143 /// This is an overload used by find() for the RAI case.
144 template<typename _RandomAccessIterator, typename _Tp>
145 _RandomAccessIterator
146 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
147 const _Tp& __val, random_access_iterator_tag)
149 typename iterator_traits<_RandomAccessIterator>::difference_type
150 __trip_count = (__last - __first) >> 2;
152 for (; __trip_count > 0; --__trip_count)
154 if (*__first == __val)
158 if (*__first == __val)
162 if (*__first == __val)
166 if (*__first == __val)
171 switch (__last - __first)
174 if (*__first == __val)
178 if (*__first == __val)
182 if (*__first == __val)
191 /// This is an overload used by find_if() for the RAI case.
192 template<typename _RandomAccessIterator, typename _Predicate>
193 _RandomAccessIterator
194 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
195 _Predicate __pred, random_access_iterator_tag)
197 typename iterator_traits<_RandomAccessIterator>::difference_type
198 __trip_count = (__last - __first) >> 2;
200 for (; __trip_count > 0; --__trip_count)
202 if (__pred(*__first))
206 if (__pred(*__first))
210 if (__pred(*__first))
214 if (__pred(*__first))
219 switch (__last - __first)
222 if (__pred(*__first))
226 if (__pred(*__first))
230 if (__pred(*__first))
239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
240 /// This is an overload used by find_if_not() for the Input Iterator case.
241 template<typename _InputIterator, typename _Predicate>
242 inline _InputIterator
243 __find_if_not(_InputIterator __first, _InputIterator __last,
244 _Predicate __pred, input_iterator_tag)
246 while (__first != __last && bool(__pred(*__first)))
251 /// This is an overload used by find_if_not() for the RAI case.
252 template<typename _RandomAccessIterator, typename _Predicate>
253 _RandomAccessIterator
254 __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
255 _Predicate __pred, random_access_iterator_tag)
257 typename iterator_traits<_RandomAccessIterator>::difference_type
258 __trip_count = (__last - __first) >> 2;
260 for (; __trip_count > 0; --__trip_count)
262 if (!bool(__pred(*__first)))
266 if (!bool(__pred(*__first)))
270 if (!bool(__pred(*__first)))
274 if (!bool(__pred(*__first)))
279 switch (__last - __first)
282 if (!bool(__pred(*__first)))
286 if (!bool(__pred(*__first)))
290 if (!bool(__pred(*__first)))
302 // set_symmetric_difference
314 * This is an uglified
315 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
316 * overloaded for forward iterators.
318 template<typename _ForwardIterator, typename _Integer, typename _Tp>
320 __search_n(_ForwardIterator __first, _ForwardIterator __last,
321 _Integer __count, const _Tp& __val,
322 std::forward_iterator_tag)
324 __first = _GLIBCXX_STD_P::find(__first, __last, __val);
325 while (__first != __last)
327 typename iterator_traits<_ForwardIterator>::difference_type
329 _ForwardIterator __i = __first;
331 while (__i != __last && __n != 1 && *__i == __val)
340 __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
346 * This is an uglified
347 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
348 * overloaded for random access iterators.
350 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
352 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
353 _Integer __count, const _Tp& __val,
354 std::random_access_iterator_tag)
357 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
360 _DistanceType __tailSize = __last - __first;
361 const _DistanceType __pattSize = __count;
363 if (__tailSize < __pattSize)
366 const _DistanceType __skipOffset = __pattSize - 1;
367 _RandomAccessIter __lookAhead = __first + __skipOffset;
368 __tailSize -= __pattSize;
370 while (1) // the main loop...
372 // __lookAhead here is always pointing to the last element of next
374 while (!(*__lookAhead == __val)) // the skip loop...
376 if (__tailSize < __pattSize)
377 return __last; // Failure
378 __lookAhead += __pattSize;
379 __tailSize -= __pattSize;
381 _DistanceType __remainder = __skipOffset;
382 for (_RandomAccessIter __backTrack = __lookAhead - 1;
383 *__backTrack == __val; --__backTrack)
385 if (--__remainder == 0)
386 return (__lookAhead - __skipOffset); // Success
388 if (__remainder > __tailSize)
389 return __last; // Failure
390 __lookAhead += __remainder;
391 __tailSize -= __remainder;
398 * This is an uglified
399 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
401 * overloaded for forward iterators.
403 template<typename _ForwardIterator, typename _Integer, typename _Tp,
404 typename _BinaryPredicate>
406 __search_n(_ForwardIterator __first, _ForwardIterator __last,
407 _Integer __count, const _Tp& __val,
408 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
410 while (__first != __last && !bool(__binary_pred(*__first, __val)))
413 while (__first != __last)
415 typename iterator_traits<_ForwardIterator>::difference_type
417 _ForwardIterator __i = __first;
419 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
429 while (__first != __last
430 && !bool(__binary_pred(*__first, __val)))
437 * This is an uglified
438 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
440 * overloaded for random access iterators.
442 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
443 typename _BinaryPredicate>
445 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
446 _Integer __count, const _Tp& __val,
447 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
450 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
453 _DistanceType __tailSize = __last - __first;
454 const _DistanceType __pattSize = __count;
456 if (__tailSize < __pattSize)
459 const _DistanceType __skipOffset = __pattSize - 1;
460 _RandomAccessIter __lookAhead = __first + __skipOffset;
461 __tailSize -= __pattSize;
463 while (1) // the main loop...
465 // __lookAhead here is always pointing to the last element of next
467 while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
469 if (__tailSize < __pattSize)
470 return __last; // Failure
471 __lookAhead += __pattSize;
472 __tailSize -= __pattSize;
474 _DistanceType __remainder = __skipOffset;
475 for (_RandomAccessIter __backTrack = __lookAhead - 1;
476 __binary_pred(*__backTrack, __val); --__backTrack)
478 if (--__remainder == 0)
479 return (__lookAhead - __skipOffset); // Success
481 if (__remainder > __tailSize)
482 return __last; // Failure
483 __lookAhead += __remainder;
484 __tailSize -= __remainder;
488 // find_end for forward iterators.
489 template<typename _ForwardIterator1, typename _ForwardIterator2>
491 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
492 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
493 forward_iterator_tag, forward_iterator_tag)
495 if (__first2 == __last2)
499 _ForwardIterator1 __result = __last1;
502 _ForwardIterator1 __new_result
503 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
504 if (__new_result == __last1)
508 __result = __new_result;
509 __first1 = __new_result;
516 template<typename _ForwardIterator1, typename _ForwardIterator2,
517 typename _BinaryPredicate>
519 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
520 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
521 forward_iterator_tag, forward_iterator_tag,
522 _BinaryPredicate __comp)
524 if (__first2 == __last2)
528 _ForwardIterator1 __result = __last1;
531 _ForwardIterator1 __new_result
532 = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
534 if (__new_result == __last1)
538 __result = __new_result;
539 __first1 = __new_result;
546 // find_end for bidirectional iterators (much faster).
547 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
548 _BidirectionalIterator1
549 __find_end(_BidirectionalIterator1 __first1,
550 _BidirectionalIterator1 __last1,
551 _BidirectionalIterator2 __first2,
552 _BidirectionalIterator2 __last2,
553 bidirectional_iterator_tag, bidirectional_iterator_tag)
555 // concept requirements
556 __glibcxx_function_requires(_BidirectionalIteratorConcept<
557 _BidirectionalIterator1>)
558 __glibcxx_function_requires(_BidirectionalIteratorConcept<
559 _BidirectionalIterator2>)
561 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
562 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
564 _RevIterator1 __rlast1(__first1);
565 _RevIterator2 __rlast2(__first2);
566 _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
568 _RevIterator2(__last2),
571 if (__rresult == __rlast1)
575 _BidirectionalIterator1 __result = __rresult.base();
576 std::advance(__result, -std::distance(__first2, __last2));
581 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
582 typename _BinaryPredicate>
583 _BidirectionalIterator1
584 __find_end(_BidirectionalIterator1 __first1,
585 _BidirectionalIterator1 __last1,
586 _BidirectionalIterator2 __first2,
587 _BidirectionalIterator2 __last2,
588 bidirectional_iterator_tag, bidirectional_iterator_tag,
589 _BinaryPredicate __comp)
591 // concept requirements
592 __glibcxx_function_requires(_BidirectionalIteratorConcept<
593 _BidirectionalIterator1>)
594 __glibcxx_function_requires(_BidirectionalIteratorConcept<
595 _BidirectionalIterator2>)
597 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
598 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
600 _RevIterator1 __rlast1(__first1);
601 _RevIterator2 __rlast2(__first2);
602 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
603 _RevIterator2(__last2), __rlast2,
606 if (__rresult == __rlast1)
610 _BidirectionalIterator1 __result = __rresult.base();
611 std::advance(__result, -std::distance(__first2, __last2));
617 * @brief Find last matching subsequence in a sequence.
618 * @ingroup non_mutating_algorithms
619 * @param first1 Start of range to search.
620 * @param last1 End of range to search.
621 * @param first2 Start of sequence to match.
622 * @param last2 End of sequence to match.
623 * @return The last iterator @c i in the range
624 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
625 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
626 * such iterator exists.
628 * Searches the range @p [first1,last1) for a sub-sequence that compares
629 * equal value-by-value with the sequence given by @p [first2,last2) and
630 * returns an iterator to the first element of the sub-sequence, or
631 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
632 * last such subsequence contained in [first,last1).
634 * Because the sub-sequence must lie completely within the range
635 * @p [first1,last1) it must start at a position less than
636 * @p last1-(last2-first2) where @p last2-first2 is the length of the
638 * This means that the returned iterator @c i will be in the range
639 * @p [first1,last1-(last2-first2))
641 template<typename _ForwardIterator1, typename _ForwardIterator2>
642 inline _ForwardIterator1
643 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
644 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
646 // concept requirements
647 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
648 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
649 __glibcxx_function_requires(_EqualOpConcept<
650 typename iterator_traits<_ForwardIterator1>::value_type,
651 typename iterator_traits<_ForwardIterator2>::value_type>)
652 __glibcxx_requires_valid_range(__first1, __last1);
653 __glibcxx_requires_valid_range(__first2, __last2);
655 return std::__find_end(__first1, __last1, __first2, __last2,
656 std::__iterator_category(__first1),
657 std::__iterator_category(__first2));
661 * @brief Find last matching subsequence in a sequence using a predicate.
662 * @ingroup non_mutating_algorithms
663 * @param first1 Start of range to search.
664 * @param last1 End of range to search.
665 * @param first2 Start of sequence to match.
666 * @param last2 End of sequence to match.
667 * @param comp The predicate to use.
668 * @return The last iterator @c i in the range
669 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
670 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
671 * @p last1 if no such iterator exists.
673 * Searches the range @p [first1,last1) for a sub-sequence that compares
674 * equal value-by-value with the sequence given by @p [first2,last2) using
675 * comp as a predicate and returns an iterator to the first element of the
676 * sub-sequence, or @p last1 if the sub-sequence is not found. The
677 * sub-sequence will be the last such subsequence contained in
680 * Because the sub-sequence must lie completely within the range
681 * @p [first1,last1) it must start at a position less than
682 * @p last1-(last2-first2) where @p last2-first2 is the length of the
684 * This means that the returned iterator @c i will be in the range
685 * @p [first1,last1-(last2-first2))
687 template<typename _ForwardIterator1, typename _ForwardIterator2,
688 typename _BinaryPredicate>
689 inline _ForwardIterator1
690 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
691 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
692 _BinaryPredicate __comp)
694 // concept requirements
695 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
696 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
697 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
698 typename iterator_traits<_ForwardIterator1>::value_type,
699 typename iterator_traits<_ForwardIterator2>::value_type>)
700 __glibcxx_requires_valid_range(__first1, __last1);
701 __glibcxx_requires_valid_range(__first2, __last2);
703 return std::__find_end(__first1, __last1, __first2, __last2,
704 std::__iterator_category(__first1),
705 std::__iterator_category(__first2),
709 #ifdef __GXX_EXPERIMENTAL_CXX0X__
711 * @brief Checks that a predicate is true for all the elements
713 * @ingroup non_mutating_algorithms
714 * @param first An input iterator.
715 * @param last An input iterator.
716 * @param pred A predicate.
717 * @return True if the check is true, false otherwise.
719 * Returns true if @p pred is true for each element in the range
720 * @p [first,last), and false otherwise.
722 template<typename _InputIterator, typename _Predicate>
724 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
725 { return __last == std::find_if_not(__first, __last, __pred); }
728 * @brief Checks that a predicate is false for all the elements
730 * @ingroup non_mutating_algorithms
731 * @param first An input iterator.
732 * @param last An input iterator.
733 * @param pred A predicate.
734 * @return True if the check is true, false otherwise.
736 * Returns true if @p pred is false for each element in the range
737 * @p [first,last), and false otherwise.
739 template<typename _InputIterator, typename _Predicate>
741 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
742 { return __last == _GLIBCXX_STD_P::find_if(__first, __last, __pred); }
745 * @brief Checks that a predicate is false for at least an element
747 * @ingroup non_mutating_algorithms
748 * @param first An input iterator.
749 * @param last An input iterator.
750 * @param pred A predicate.
751 * @return True if the check is true, false otherwise.
753 * Returns true if an element exists in the range @p [first,last) such that
754 * @p pred is true, and false otherwise.
756 template<typename _InputIterator, typename _Predicate>
758 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
759 { return !std::none_of(__first, __last, __pred); }
762 * @brief Find the first element in a sequence for which a
763 * predicate is false.
764 * @ingroup non_mutating_algorithms
765 * @param first An input iterator.
766 * @param last An input iterator.
767 * @param pred A predicate.
768 * @return The first iterator @c i in the range @p [first,last)
769 * such that @p pred(*i) is false, or @p last if no such iterator exists.
771 template<typename _InputIterator, typename _Predicate>
772 inline _InputIterator
773 find_if_not(_InputIterator __first, _InputIterator __last,
776 // concept requirements
777 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
778 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
779 typename iterator_traits<_InputIterator>::value_type>)
780 __glibcxx_requires_valid_range(__first, __last);
781 return std::__find_if_not(__first, __last, __pred,
782 std::__iterator_category(__first));
786 * @brief Checks whether the sequence is partitioned.
787 * @ingroup mutating_algorithms
788 * @param first An input iterator.
789 * @param last An input iterator.
790 * @param pred A predicate.
791 * @return True if the range @p [first,last) is partioned by @p pred,
792 * i.e. if all elements that satisfy @p pred appear before those that
795 template<typename _InputIterator, typename _Predicate>
797 is_partitioned(_InputIterator __first, _InputIterator __last,
800 __first = std::find_if_not(__first, __last, __pred);
801 return std::none_of(__first, __last, __pred);
805 * @brief Find the partition point of a partitioned range.
806 * @ingroup mutating_algorithms
807 * @param first An iterator.
808 * @param last Another iterator.
809 * @param pred A predicate.
810 * @return An iterator @p mid such that @p all_of(first, mid, pred)
811 * and @p none_of(mid, last, pred) are both true.
813 template<typename _ForwardIterator, typename _Predicate>
815 partition_point(_ForwardIterator __first, _ForwardIterator __last,
818 // concept requirements
819 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
820 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
821 typename iterator_traits<_ForwardIterator>::value_type>)
823 // A specific debug-mode test will be necessary...
824 __glibcxx_requires_valid_range(__first, __last);
826 typedef typename iterator_traits<_ForwardIterator>::difference_type
829 _DistanceType __len = std::distance(__first, __last);
830 _DistanceType __half;
831 _ForwardIterator __middle;
837 std::advance(__middle, __half);
838 if (__pred(*__middle))
842 __len = __len - __half - 1;
853 * @brief Copy a sequence, removing elements of a given value.
854 * @ingroup mutating_algorithms
855 * @param first An input iterator.
856 * @param last An input iterator.
857 * @param result An output iterator.
858 * @param value The value to be removed.
859 * @return An iterator designating the end of the resulting sequence.
861 * Copies each element in the range @p [first,last) not equal to @p value
862 * to the range beginning at @p result.
863 * remove_copy() is stable, so the relative order of elements that are
864 * copied is unchanged.
866 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
868 remove_copy(_InputIterator __first, _InputIterator __last,
869 _OutputIterator __result, const _Tp& __value)
871 // concept requirements
872 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
873 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
874 typename iterator_traits<_InputIterator>::value_type>)
875 __glibcxx_function_requires(_EqualOpConcept<
876 typename iterator_traits<_InputIterator>::value_type, _Tp>)
877 __glibcxx_requires_valid_range(__first, __last);
879 for (; __first != __last; ++__first)
880 if (!(*__first == __value))
882 *__result = *__first;
889 * @brief Copy a sequence, removing elements for which a predicate is true.
890 * @ingroup mutating_algorithms
891 * @param first An input iterator.
892 * @param last An input iterator.
893 * @param result An output iterator.
894 * @param pred A predicate.
895 * @return An iterator designating the end of the resulting sequence.
897 * Copies each element in the range @p [first,last) for which
898 * @p pred returns false to the range beginning at @p result.
900 * remove_copy_if() is stable, so the relative order of elements that are
901 * copied is unchanged.
903 template<typename _InputIterator, typename _OutputIterator,
906 remove_copy_if(_InputIterator __first, _InputIterator __last,
907 _OutputIterator __result, _Predicate __pred)
909 // concept requirements
910 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
911 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
912 typename iterator_traits<_InputIterator>::value_type>)
913 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
914 typename iterator_traits<_InputIterator>::value_type>)
915 __glibcxx_requires_valid_range(__first, __last);
917 for (; __first != __last; ++__first)
918 if (!bool(__pred(*__first)))
920 *__result = *__first;
926 #ifdef __GXX_EXPERIMENTAL_CXX0X__
928 * @brief Copy the elements of a sequence for which a predicate is true.
929 * @ingroup mutating_algorithms
930 * @param first An input iterator.
931 * @param last An input iterator.
932 * @param result An output iterator.
933 * @param pred A predicate.
934 * @return An iterator designating the end of the resulting sequence.
936 * Copies each element in the range @p [first,last) for which
937 * @p pred returns true to the range beginning at @p result.
939 * copy_if() is stable, so the relative order of elements that are
940 * copied is unchanged.
942 template<typename _InputIterator, typename _OutputIterator,
945 copy_if(_InputIterator __first, _InputIterator __last,
946 _OutputIterator __result, _Predicate __pred)
948 // concept requirements
949 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
950 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
951 typename iterator_traits<_InputIterator>::value_type>)
952 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
953 typename iterator_traits<_InputIterator>::value_type>)
954 __glibcxx_requires_valid_range(__first, __last);
956 for (; __first != __last; ++__first)
957 if (__pred(*__first))
959 *__result = *__first;
966 template<typename _InputIterator, typename _Size, typename _OutputIterator>
968 __copy_n(_InputIterator __first, _Size __n,
969 _OutputIterator __result, input_iterator_tag)
971 for (; __n > 0; --__n)
973 *__result = *__first;
980 template<typename _RandomAccessIterator, typename _Size,
981 typename _OutputIterator>
982 inline _OutputIterator
983 __copy_n(_RandomAccessIterator __first, _Size __n,
984 _OutputIterator __result, random_access_iterator_tag)
985 { return std::copy(__first, __first + __n, __result); }
988 * @brief Copies the range [first,first+n) into [result,result+n).
989 * @ingroup mutating_algorithms
990 * @param first An input iterator.
991 * @param n The number of elements to copy.
992 * @param result An output iterator.
995 * This inline function will boil down to a call to @c memmove whenever
996 * possible. Failing that, if random access iterators are passed, then the
997 * loop count will be known (and therefore a candidate for compiler
998 * optimizations such as unrolling).
1000 template<typename _InputIterator, typename _Size, typename _OutputIterator>
1001 inline _OutputIterator
1002 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1004 // concept requirements
1005 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1006 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1007 typename iterator_traits<_InputIterator>::value_type>)
1009 return std::__copy_n(__first, __n, __result,
1010 std::__iterator_category(__first));
1014 * @brief Copy the elements of a sequence to separate output sequences
1015 * depending on the truth value of a predicate.
1016 * @ingroup mutating_algorithms
1017 * @param first An input iterator.
1018 * @param last An input iterator.
1019 * @param out_true An output iterator.
1020 * @param out_false An output iterator.
1021 * @param pred A predicate.
1022 * @return A pair designating the ends of the resulting sequences.
1024 * Copies each element in the range @p [first,last) for which
1025 * @p pred returns true to the range beginning at @p out_true
1026 * and each element for which @p pred returns false to @p out_false.
1028 template<typename _InputIterator, typename _OutputIterator1,
1029 typename _OutputIterator2, typename _Predicate>
1030 pair<_OutputIterator1, _OutputIterator2>
1031 partition_copy(_InputIterator __first, _InputIterator __last,
1032 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
1035 // concept requirements
1036 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1037 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
1038 typename iterator_traits<_InputIterator>::value_type>)
1039 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
1040 typename iterator_traits<_InputIterator>::value_type>)
1041 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1042 typename iterator_traits<_InputIterator>::value_type>)
1043 __glibcxx_requires_valid_range(__first, __last);
1045 for (; __first != __last; ++__first)
1046 if (__pred(*__first))
1048 *__out_true = *__first;
1053 *__out_false = *__first;
1057 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
1062 * @brief Remove elements from a sequence.
1063 * @ingroup mutating_algorithms
1064 * @param first An input iterator.
1065 * @param last An input iterator.
1066 * @param value The value to be removed.
1067 * @return An iterator designating the end of the resulting sequence.
1069 * All elements equal to @p value are removed from the range
1072 * remove() is stable, so the relative order of elements that are
1073 * not removed is unchanged.
1075 * Elements between the end of the resulting sequence and @p last
1076 * are still present, but their value is unspecified.
1078 template<typename _ForwardIterator, typename _Tp>
1080 remove(_ForwardIterator __first, _ForwardIterator __last,
1083 // concept requirements
1084 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1086 __glibcxx_function_requires(_EqualOpConcept<
1087 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1088 __glibcxx_requires_valid_range(__first, __last);
1090 __first = _GLIBCXX_STD_P::find(__first, __last, __value);
1091 if(__first == __last)
1093 _ForwardIterator __result = __first;
1095 for(; __first != __last; ++__first)
1096 if(!(*__first == __value))
1098 *__result = _GLIBCXX_MOVE(*__first);
1105 * @brief Remove elements from a sequence using a predicate.
1106 * @ingroup mutating_algorithms
1107 * @param first A forward iterator.
1108 * @param last A forward iterator.
1109 * @param pred A predicate.
1110 * @return An iterator designating the end of the resulting sequence.
1112 * All elements for which @p pred returns true are removed from the range
1115 * remove_if() is stable, so the relative order of elements that are
1116 * not removed is unchanged.
1118 * Elements between the end of the resulting sequence and @p last
1119 * are still present, but their value is unspecified.
1121 template<typename _ForwardIterator, typename _Predicate>
1123 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1126 // concept requirements
1127 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1129 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1130 typename iterator_traits<_ForwardIterator>::value_type>)
1131 __glibcxx_requires_valid_range(__first, __last);
1133 __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
1134 if(__first == __last)
1136 _ForwardIterator __result = __first;
1138 for(; __first != __last; ++__first)
1139 if(!bool(__pred(*__first)))
1141 *__result = _GLIBCXX_MOVE(*__first);
1148 * @brief Remove consecutive duplicate values from a sequence.
1149 * @ingroup mutating_algorithms
1150 * @param first A forward iterator.
1151 * @param last A forward iterator.
1152 * @return An iterator designating the end of the resulting sequence.
1154 * Removes all but the first element from each group of consecutive
1155 * values that compare equal.
1156 * unique() is stable, so the relative order of elements that are
1157 * not removed is unchanged.
1158 * Elements between the end of the resulting sequence and @p last
1159 * are still present, but their value is unspecified.
1161 template<typename _ForwardIterator>
1163 unique(_ForwardIterator __first, _ForwardIterator __last)
1165 // concept requirements
1166 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1168 __glibcxx_function_requires(_EqualityComparableConcept<
1169 typename iterator_traits<_ForwardIterator>::value_type>)
1170 __glibcxx_requires_valid_range(__first, __last);
1172 // Skip the beginning, if already unique.
1173 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
1174 if (__first == __last)
1177 // Do the real copy work.
1178 _ForwardIterator __dest = __first;
1180 while (++__first != __last)
1181 if (!(*__dest == *__first))
1182 *++__dest = _GLIBCXX_MOVE(*__first);
1187 * @brief Remove consecutive values from a sequence using a predicate.
1188 * @ingroup mutating_algorithms
1189 * @param first A forward iterator.
1190 * @param last A forward iterator.
1191 * @param binary_pred A binary predicate.
1192 * @return An iterator designating the end of the resulting sequence.
1194 * Removes all but the first element from each group of consecutive
1195 * values for which @p binary_pred returns true.
1196 * unique() is stable, so the relative order of elements that are
1197 * not removed is unchanged.
1198 * Elements between the end of the resulting sequence and @p last
1199 * are still present, but their value is unspecified.
1201 template<typename _ForwardIterator, typename _BinaryPredicate>
1203 unique(_ForwardIterator __first, _ForwardIterator __last,
1204 _BinaryPredicate __binary_pred)
1206 // concept requirements
1207 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1209 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1210 typename iterator_traits<_ForwardIterator>::value_type,
1211 typename iterator_traits<_ForwardIterator>::value_type>)
1212 __glibcxx_requires_valid_range(__first, __last);
1214 // Skip the beginning, if already unique.
1215 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
1216 if (__first == __last)
1219 // Do the real copy work.
1220 _ForwardIterator __dest = __first;
1222 while (++__first != __last)
1223 if (!bool(__binary_pred(*__dest, *__first)))
1224 *++__dest = _GLIBCXX_MOVE(*__first);
1229 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1231 * overloaded for forward iterators and output iterator as result.
1233 template<typename _ForwardIterator, typename _OutputIterator>
1235 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1236 _OutputIterator __result,
1237 forward_iterator_tag, output_iterator_tag)
1239 // concept requirements -- taken care of in dispatching function
1240 _ForwardIterator __next = __first;
1241 *__result = *__first;
1242 while (++__next != __last)
1243 if (!(*__first == *__next))
1246 *++__result = *__first;
1252 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1254 * overloaded for input iterators and output iterator as result.
1256 template<typename _InputIterator, typename _OutputIterator>
1258 __unique_copy(_InputIterator __first, _InputIterator __last,
1259 _OutputIterator __result,
1260 input_iterator_tag, output_iterator_tag)
1262 // concept requirements -- taken care of in dispatching function
1263 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1264 *__result = __value;
1265 while (++__first != __last)
1266 if (!(__value == *__first))
1269 *++__result = __value;
1275 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1277 * overloaded for input iterators and forward iterator as result.
1279 template<typename _InputIterator, typename _ForwardIterator>
1281 __unique_copy(_InputIterator __first, _InputIterator __last,
1282 _ForwardIterator __result,
1283 input_iterator_tag, forward_iterator_tag)
1285 // concept requirements -- taken care of in dispatching function
1286 *__result = *__first;
1287 while (++__first != __last)
1288 if (!(*__result == *__first))
1289 *++__result = *__first;
1294 * This is an uglified
1295 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1297 * overloaded for forward iterators and output iterator as result.
1299 template<typename _ForwardIterator, typename _OutputIterator,
1300 typename _BinaryPredicate>
1302 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1303 _OutputIterator __result, _BinaryPredicate __binary_pred,
1304 forward_iterator_tag, output_iterator_tag)
1306 // concept requirements -- iterators already checked
1307 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1308 typename iterator_traits<_ForwardIterator>::value_type,
1309 typename iterator_traits<_ForwardIterator>::value_type>)
1311 _ForwardIterator __next = __first;
1312 *__result = *__first;
1313 while (++__next != __last)
1314 if (!bool(__binary_pred(*__first, *__next)))
1317 *++__result = *__first;
1323 * This is an uglified
1324 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1326 * overloaded for input iterators and output iterator as result.
1328 template<typename _InputIterator, typename _OutputIterator,
1329 typename _BinaryPredicate>
1331 __unique_copy(_InputIterator __first, _InputIterator __last,
1332 _OutputIterator __result, _BinaryPredicate __binary_pred,
1333 input_iterator_tag, output_iterator_tag)
1335 // concept requirements -- iterators already checked
1336 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1337 typename iterator_traits<_InputIterator>::value_type,
1338 typename iterator_traits<_InputIterator>::value_type>)
1340 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1341 *__result = __value;
1342 while (++__first != __last)
1343 if (!bool(__binary_pred(__value, *__first)))
1346 *++__result = __value;
1352 * This is an uglified
1353 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1355 * overloaded for input iterators and forward iterator as result.
1357 template<typename _InputIterator, typename _ForwardIterator,
1358 typename _BinaryPredicate>
1360 __unique_copy(_InputIterator __first, _InputIterator __last,
1361 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1362 input_iterator_tag, forward_iterator_tag)
1364 // concept requirements -- iterators already checked
1365 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1366 typename iterator_traits<_ForwardIterator>::value_type,
1367 typename iterator_traits<_InputIterator>::value_type>)
1369 *__result = *__first;
1370 while (++__first != __last)
1371 if (!bool(__binary_pred(*__result, *__first)))
1372 *++__result = *__first;
1377 * This is an uglified reverse(_BidirectionalIterator,
1378 * _BidirectionalIterator)
1379 * overloaded for bidirectional iterators.
1381 template<typename _BidirectionalIterator>
1383 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1384 bidirectional_iterator_tag)
1387 if (__first == __last || __first == --__last)
1391 std::iter_swap(__first, __last);
1397 * This is an uglified reverse(_BidirectionalIterator,
1398 * _BidirectionalIterator)
1399 * overloaded for random access iterators.
1401 template<typename _RandomAccessIterator>
1403 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1404 random_access_iterator_tag)
1406 if (__first == __last)
1409 while (__first < __last)
1411 std::iter_swap(__first, __last);
1418 * @brief Reverse a sequence.
1419 * @ingroup mutating_algorithms
1420 * @param first A bidirectional iterator.
1421 * @param last A bidirectional iterator.
1422 * @return reverse() returns no value.
1424 * Reverses the order of the elements in the range @p [first,last),
1425 * so that the first element becomes the last etc.
1426 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1427 * swaps @p *(first+i) and @p *(last-(i+1))
1429 template<typename _BidirectionalIterator>
1431 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1433 // concept requirements
1434 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1435 _BidirectionalIterator>)
1436 __glibcxx_requires_valid_range(__first, __last);
1437 std::__reverse(__first, __last, std::__iterator_category(__first));
1441 * @brief Copy a sequence, reversing its elements.
1442 * @ingroup mutating_algorithms
1443 * @param first A bidirectional iterator.
1444 * @param last A bidirectional iterator.
1445 * @param result An output iterator.
1446 * @return An iterator designating the end of the resulting sequence.
1448 * Copies the elements in the range @p [first,last) to the range
1449 * @p [result,result+(last-first)) such that the order of the
1450 * elements is reversed.
1451 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1452 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1453 * The ranges @p [first,last) and @p [result,result+(last-first))
1456 template<typename _BidirectionalIterator, typename _OutputIterator>
1458 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1459 _OutputIterator __result)
1461 // concept requirements
1462 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1463 _BidirectionalIterator>)
1464 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1465 typename iterator_traits<_BidirectionalIterator>::value_type>)
1466 __glibcxx_requires_valid_range(__first, __last);
1468 while (__first != __last)
1471 *__result = *__last;
1478 * This is a helper function for the rotate algorithm specialized on RAIs.
1479 * It returns the greatest common divisor of two integer values.
1481 template<typename _EuclideanRingElement>
1482 _EuclideanRingElement
1483 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1487 _EuclideanRingElement __t = __m % __n;
1494 /// This is a helper function for the rotate algorithm.
1495 template<typename _ForwardIterator>
1497 __rotate(_ForwardIterator __first,
1498 _ForwardIterator __middle,
1499 _ForwardIterator __last,
1500 forward_iterator_tag)
1502 if (__first == __middle || __last == __middle)
1505 _ForwardIterator __first2 = __middle;
1508 std::iter_swap(__first, __first2);
1511 if (__first == __middle)
1512 __middle = __first2;
1514 while (__first2 != __last);
1516 __first2 = __middle;
1518 while (__first2 != __last)
1520 std::iter_swap(__first, __first2);
1523 if (__first == __middle)
1524 __middle = __first2;
1525 else if (__first2 == __last)
1526 __first2 = __middle;
1530 /// This is a helper function for the rotate algorithm.
1531 template<typename _BidirectionalIterator>
1533 __rotate(_BidirectionalIterator __first,
1534 _BidirectionalIterator __middle,
1535 _BidirectionalIterator __last,
1536 bidirectional_iterator_tag)
1538 // concept requirements
1539 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1540 _BidirectionalIterator>)
1542 if (__first == __middle || __last == __middle)
1545 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1546 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1548 while (__first != __middle && __middle != __last)
1550 std::iter_swap(__first, --__last);
1554 if (__first == __middle)
1555 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1557 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1560 /// This is a helper function for the rotate algorithm.
1561 template<typename _RandomAccessIterator>
1563 __rotate(_RandomAccessIterator __first,
1564 _RandomAccessIterator __middle,
1565 _RandomAccessIterator __last,
1566 random_access_iterator_tag)
1568 // concept requirements
1569 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1570 _RandomAccessIterator>)
1572 if (__first == __middle || __last == __middle)
1575 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1577 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1580 _Distance __n = __last - __first;
1581 _Distance __k = __middle - __first;
1583 if (__k == __n - __k)
1585 std::swap_ranges(__first, __middle, __middle);
1589 _RandomAccessIterator __p = __first;
1593 if (__k < __n - __k)
1595 if (__is_pod(_ValueType) && __k == 1)
1597 _ValueType __t = _GLIBCXX_MOVE(*__p);
1598 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
1599 *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
1602 _RandomAccessIterator __q = __p + __k;
1603 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1605 std::iter_swap(__p, __q);
1612 std::swap(__n, __k);
1618 if (__is_pod(_ValueType) && __k == 1)
1620 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
1621 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
1622 *__p = _GLIBCXX_MOVE(__t);
1625 _RandomAccessIterator __q = __p + __n;
1627 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1631 std::iter_swap(__p, __q);
1636 std::swap(__n, __k);
1642 * @brief Rotate the elements of a sequence.
1643 * @ingroup mutating_algorithms
1644 * @param first A forward iterator.
1645 * @param middle A forward iterator.
1646 * @param last A forward iterator.
1649 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1650 * positions so that the element at @p middle is moved to @p first, the
1651 * element at @p middle+1 is moved to @first+1 and so on for each element
1652 * in the range @p [first,last).
1654 * This effectively swaps the ranges @p [first,middle) and
1657 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1658 * each @p n in the range @p [0,last-first).
1660 template<typename _ForwardIterator>
1662 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1663 _ForwardIterator __last)
1665 // concept requirements
1666 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1668 __glibcxx_requires_valid_range(__first, __middle);
1669 __glibcxx_requires_valid_range(__middle, __last);
1671 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1673 std::__rotate(__first, __middle, __last, _IterType());
1677 * @brief Copy a sequence, rotating its elements.
1678 * @ingroup mutating_algorithms
1679 * @param first A forward iterator.
1680 * @param middle A forward iterator.
1681 * @param last A forward iterator.
1682 * @param result An output iterator.
1683 * @return An iterator designating the end of the resulting sequence.
1685 * Copies the elements of the range @p [first,last) to the range
1686 * beginning at @result, rotating the copied elements by @p (middle-first)
1687 * positions so that the element at @p middle is moved to @p result, the
1688 * element at @p middle+1 is moved to @result+1 and so on for each element
1689 * in the range @p [first,last).
1691 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1692 * each @p n in the range @p [0,last-first).
1694 template<typename _ForwardIterator, typename _OutputIterator>
1696 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1697 _ForwardIterator __last, _OutputIterator __result)
1699 // concept requirements
1700 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1701 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1702 typename iterator_traits<_ForwardIterator>::value_type>)
1703 __glibcxx_requires_valid_range(__first, __middle);
1704 __glibcxx_requires_valid_range(__middle, __last);
1706 return std::copy(__first, __middle,
1707 std::copy(__middle, __last, __result));
1710 /// This is a helper function...
1711 template<typename _ForwardIterator, typename _Predicate>
1713 __partition(_ForwardIterator __first, _ForwardIterator __last,
1714 _Predicate __pred, forward_iterator_tag)
1716 if (__first == __last)
1719 while (__pred(*__first))
1720 if (++__first == __last)
1723 _ForwardIterator __next = __first;
1725 while (++__next != __last)
1726 if (__pred(*__next))
1728 std::iter_swap(__first, __next);
1735 /// This is a helper function...
1736 template<typename _BidirectionalIterator, typename _Predicate>
1737 _BidirectionalIterator
1738 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1739 _Predicate __pred, bidirectional_iterator_tag)
1744 if (__first == __last)
1746 else if (__pred(*__first))
1752 if (__first == __last)
1754 else if (!bool(__pred(*__last)))
1758 std::iter_swap(__first, __last);
1765 /// This is a helper function...
1766 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1768 __inplace_stable_partition(_ForwardIterator __first,
1769 _ForwardIterator __last,
1770 _Predicate __pred, _Distance __len)
1773 return __pred(*__first) ? __last : __first;
1774 _ForwardIterator __middle = __first;
1775 std::advance(__middle, __len / 2);
1776 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1780 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1784 std::rotate(__begin, __middle, __end);
1785 std::advance(__begin, std::distance(__middle, __end));
1789 /// This is a helper function...
1790 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1793 __stable_partition_adaptive(_ForwardIterator __first,
1794 _ForwardIterator __last,
1795 _Predicate __pred, _Distance __len,
1797 _Distance __buffer_size)
1799 if (__len <= __buffer_size)
1801 _ForwardIterator __result1 = __first;
1802 _Pointer __result2 = __buffer;
1803 for (; __first != __last; ++__first)
1804 if (__pred(*__first))
1806 *__result1 = _GLIBCXX_MOVE(*__first);
1811 *__result2 = _GLIBCXX_MOVE(*__first);
1814 _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1819 _ForwardIterator __middle = __first;
1820 std::advance(__middle, __len / 2);
1821 _ForwardIterator __begin =
1822 std::__stable_partition_adaptive(__first, __middle, __pred,
1823 __len / 2, __buffer,
1825 _ForwardIterator __end =
1826 std::__stable_partition_adaptive(__middle, __last, __pred,
1828 __buffer, __buffer_size);
1829 std::rotate(__begin, __middle, __end);
1830 std::advance(__begin, std::distance(__middle, __end));
1836 * @brief Move elements for which a predicate is true to the beginning
1837 * of a sequence, preserving relative ordering.
1838 * @ingroup mutating_algorithms
1839 * @param first A forward iterator.
1840 * @param last A forward iterator.
1841 * @param pred A predicate functor.
1842 * @return An iterator @p middle such that @p pred(i) is true for each
1843 * iterator @p i in the range @p [first,middle) and false for each @p i
1844 * in the range @p [middle,last).
1846 * Performs the same function as @p partition() with the additional
1847 * guarantee that the relative ordering of elements in each group is
1848 * preserved, so any two elements @p x and @p y in the range
1849 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1850 * relative ordering after calling @p stable_partition().
1852 template<typename _ForwardIterator, typename _Predicate>
1854 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1857 // concept requirements
1858 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1860 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1861 typename iterator_traits<_ForwardIterator>::value_type>)
1862 __glibcxx_requires_valid_range(__first, __last);
1864 if (__first == __last)
1868 typedef typename iterator_traits<_ForwardIterator>::value_type
1870 typedef typename iterator_traits<_ForwardIterator>::difference_type
1873 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1875 if (__buf.size() > 0)
1877 std::__stable_partition_adaptive(__first, __last, __pred,
1878 _DistanceType(__buf.requested_size()),
1880 _DistanceType(__buf.size()));
1883 std::__inplace_stable_partition(__first, __last, __pred,
1884 _DistanceType(__buf.requested_size()));
1888 /// This is a helper function for the sort routines.
1889 template<typename _RandomAccessIterator>
1891 __heap_select(_RandomAccessIterator __first,
1892 _RandomAccessIterator __middle,
1893 _RandomAccessIterator __last)
1895 std::make_heap(__first, __middle);
1896 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1897 if (*__i < *__first)
1898 std::__pop_heap(__first, __middle, __i);
1901 /// This is a helper function for the sort routines.
1902 template<typename _RandomAccessIterator, typename _Compare>
1904 __heap_select(_RandomAccessIterator __first,
1905 _RandomAccessIterator __middle,
1906 _RandomAccessIterator __last, _Compare __comp)
1908 std::make_heap(__first, __middle, __comp);
1909 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1910 if (__comp(*__i, *__first))
1911 std::__pop_heap(__first, __middle, __i, __comp);
1917 * @brief Copy the smallest elements of a sequence.
1918 * @ingroup sorting_algorithms
1919 * @param first An iterator.
1920 * @param last Another iterator.
1921 * @param result_first A random-access iterator.
1922 * @param result_last Another random-access iterator.
1923 * @return An iterator indicating the end of the resulting sequence.
1925 * Copies and sorts the smallest N values from the range @p [first,last)
1926 * to the range beginning at @p result_first, where the number of
1927 * elements to be copied, @p N, is the smaller of @p (last-first) and
1928 * @p (result_last-result_first).
1929 * After the sort if @p i and @j are iterators in the range
1930 * @p [result_first,result_first+N) such that @i precedes @j then
1931 * @p *j<*i is false.
1932 * The value returned is @p result_first+N.
1934 template<typename _InputIterator, typename _RandomAccessIterator>
1935 _RandomAccessIterator
1936 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1937 _RandomAccessIterator __result_first,
1938 _RandomAccessIterator __result_last)
1940 typedef typename iterator_traits<_InputIterator>::value_type
1942 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1944 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1947 // concept requirements
1948 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1949 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1951 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1953 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1954 __glibcxx_requires_valid_range(__first, __last);
1955 __glibcxx_requires_valid_range(__result_first, __result_last);
1957 if (__result_first == __result_last)
1958 return __result_last;
1959 _RandomAccessIterator __result_real_last = __result_first;
1960 while(__first != __last && __result_real_last != __result_last)
1962 *__result_real_last = *__first;
1963 ++__result_real_last;
1966 std::make_heap(__result_first, __result_real_last);
1967 while (__first != __last)
1969 if (*__first < *__result_first)
1970 std::__adjust_heap(__result_first, _DistanceType(0),
1971 _DistanceType(__result_real_last
1973 _InputValueType(*__first));
1976 std::sort_heap(__result_first, __result_real_last);
1977 return __result_real_last;
1981 * @brief Copy the smallest elements of a sequence using a predicate for
1983 * @ingroup sorting_algorithms
1984 * @param first An input iterator.
1985 * @param last Another input iterator.
1986 * @param result_first A random-access iterator.
1987 * @param result_last Another random-access iterator.
1988 * @param comp A comparison functor.
1989 * @return An iterator indicating the end of the resulting sequence.
1991 * Copies and sorts the smallest N values from the range @p [first,last)
1992 * to the range beginning at @p result_first, where the number of
1993 * elements to be copied, @p N, is the smaller of @p (last-first) and
1994 * @p (result_last-result_first).
1995 * After the sort if @p i and @j are iterators in the range
1996 * @p [result_first,result_first+N) such that @i precedes @j then
1997 * @p comp(*j,*i) is false.
1998 * The value returned is @p result_first+N.
2000 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2001 _RandomAccessIterator
2002 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2003 _RandomAccessIterator __result_first,
2004 _RandomAccessIterator __result_last,
2007 typedef typename iterator_traits<_InputIterator>::value_type
2009 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2011 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2014 // concept requirements
2015 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2016 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2017 _RandomAccessIterator>)
2018 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2020 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2021 _InputValueType, _OutputValueType>)
2022 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2023 _OutputValueType, _OutputValueType>)
2024 __glibcxx_requires_valid_range(__first, __last);
2025 __glibcxx_requires_valid_range(__result_first, __result_last);
2027 if (__result_first == __result_last)
2028 return __result_last;
2029 _RandomAccessIterator __result_real_last = __result_first;
2030 while(__first != __last && __result_real_last != __result_last)
2032 *__result_real_last = *__first;
2033 ++__result_real_last;
2036 std::make_heap(__result_first, __result_real_last, __comp);
2037 while (__first != __last)
2039 if (__comp(*__first, *__result_first))
2040 std::__adjust_heap(__result_first, _DistanceType(0),
2041 _DistanceType(__result_real_last
2043 _InputValueType(*__first),
2047 std::sort_heap(__result_first, __result_real_last, __comp);
2048 return __result_real_last;
2051 /// This is a helper function for the sort routine.
2052 template<typename _RandomAccessIterator>
2054 __unguarded_linear_insert(_RandomAccessIterator __last)
2056 typename iterator_traits<_RandomAccessIterator>::value_type
2057 __val = _GLIBCXX_MOVE(*__last);
2058 _RandomAccessIterator __next = __last;
2060 while (__val < *__next)
2062 *__last = _GLIBCXX_MOVE(*__next);
2066 *__last = _GLIBCXX_MOVE(__val);
2069 /// This is a helper function for the sort routine.
2070 template<typename _RandomAccessIterator, typename _Compare>
2072 __unguarded_linear_insert(_RandomAccessIterator __last,
2075 typename iterator_traits<_RandomAccessIterator>::value_type
2076 __val = _GLIBCXX_MOVE(*__last);
2077 _RandomAccessIterator __next = __last;
2079 while (__comp(__val, *__next))
2081 *__last = _GLIBCXX_MOVE(*__next);
2085 *__last = _GLIBCXX_MOVE(__val);
2088 /// This is a helper function for the sort routine.
2089 template<typename _RandomAccessIterator>
2091 __insertion_sort(_RandomAccessIterator __first,
2092 _RandomAccessIterator __last)
2094 if (__first == __last)
2097 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2099 if (*__i < *__first)
2101 typename iterator_traits<_RandomAccessIterator>::value_type
2102 __val = _GLIBCXX_MOVE(*__i);
2103 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2104 *__first = _GLIBCXX_MOVE(__val);
2107 std::__unguarded_linear_insert(__i);
2111 /// This is a helper function for the sort routine.
2112 template<typename _RandomAccessIterator, typename _Compare>
2114 __insertion_sort(_RandomAccessIterator __first,
2115 _RandomAccessIterator __last, _Compare __comp)
2117 if (__first == __last) return;
2119 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2121 if (__comp(*__i, *__first))
2123 typename iterator_traits<_RandomAccessIterator>::value_type
2124 __val = _GLIBCXX_MOVE(*__i);
2125 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2126 *__first = _GLIBCXX_MOVE(__val);
2129 std::__unguarded_linear_insert(__i, __comp);
2133 /// This is a helper function for the sort routine.
2134 template<typename _RandomAccessIterator>
2136 __unguarded_insertion_sort(_RandomAccessIterator __first,
2137 _RandomAccessIterator __last)
2139 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2142 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2143 std::__unguarded_linear_insert(__i);
2146 /// This is a helper function for the sort routine.
2147 template<typename _RandomAccessIterator, typename _Compare>
2149 __unguarded_insertion_sort(_RandomAccessIterator __first,
2150 _RandomAccessIterator __last, _Compare __comp)
2152 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2155 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2156 std::__unguarded_linear_insert(__i, __comp);
2161 * This controls some aspect of the sort routines.
2163 enum { _S_threshold = 16 };
2165 /// This is a helper function for the sort routine.
2166 template<typename _RandomAccessIterator>
2168 __final_insertion_sort(_RandomAccessIterator __first,
2169 _RandomAccessIterator __last)
2171 if (__last - __first > int(_S_threshold))
2173 std::__insertion_sort(__first, __first + int(_S_threshold));
2174 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2177 std::__insertion_sort(__first, __last);
2180 /// This is a helper function for the sort routine.
2181 template<typename _RandomAccessIterator, typename _Compare>
2183 __final_insertion_sort(_RandomAccessIterator __first,
2184 _RandomAccessIterator __last, _Compare __comp)
2186 if (__last - __first > int(_S_threshold))
2188 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2189 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2193 std::__insertion_sort(__first, __last, __comp);
2196 /// This is a helper function...
2197 template<typename _RandomAccessIterator, typename _Tp>
2198 _RandomAccessIterator
2199 __unguarded_partition(_RandomAccessIterator __first,
2200 _RandomAccessIterator __last, const _Tp& __pivot)
2204 while (*__first < __pivot)
2207 while (__pivot < *__last)
2209 if (!(__first < __last))
2211 std::iter_swap(__first, __last);
2216 /// This is a helper function...
2217 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2218 _RandomAccessIterator
2219 __unguarded_partition(_RandomAccessIterator __first,
2220 _RandomAccessIterator __last,
2221 const _Tp& __pivot, _Compare __comp)
2225 while (__comp(*__first, __pivot))
2228 while (__comp(__pivot, *__last))
2230 if (!(__first < __last))
2232 std::iter_swap(__first, __last);
2237 /// This is a helper function...
2238 template<typename _RandomAccessIterator>
2239 inline _RandomAccessIterator
2240 __unguarded_partition_pivot(_RandomAccessIterator __first,
2241 _RandomAccessIterator __last)
2243 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2244 std::__move_median_first(__first, __mid, (__last - 1));
2245 return std::__unguarded_partition(__first + 1, __last, *__first);
2249 /// This is a helper function...
2250 template<typename _RandomAccessIterator, typename _Compare>
2251 inline _RandomAccessIterator
2252 __unguarded_partition_pivot(_RandomAccessIterator __first,
2253 _RandomAccessIterator __last, _Compare __comp)
2255 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2256 std::__move_median_first(__first, __mid, (__last - 1), __comp);
2257 return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
2260 /// This is a helper function for the sort routine.
2261 template<typename _RandomAccessIterator, typename _Size>
2263 __introsort_loop(_RandomAccessIterator __first,
2264 _RandomAccessIterator __last,
2265 _Size __depth_limit)
2267 while (__last - __first > int(_S_threshold))
2269 if (__depth_limit == 0)
2271 _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
2275 _RandomAccessIterator __cut =
2276 std::__unguarded_partition_pivot(__first, __last);
2277 std::__introsort_loop(__cut, __last, __depth_limit);
2282 /// This is a helper function for the sort routine.
2283 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2285 __introsort_loop(_RandomAccessIterator __first,
2286 _RandomAccessIterator __last,
2287 _Size __depth_limit, _Compare __comp)
2289 while (__last - __first > int(_S_threshold))
2291 if (__depth_limit == 0)
2293 _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
2297 _RandomAccessIterator __cut =
2298 std::__unguarded_partition_pivot(__first, __last, __comp);
2299 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2304 /// This is a helper function for the sort routines. Precondition: __n > 0.
2305 template<typename _Size>
2310 for (__k = 0; __n != 0; __n >>= 1)
2317 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
2321 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
2325 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
2329 template<typename _RandomAccessIterator, typename _Size>
2331 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2332 _RandomAccessIterator __last, _Size __depth_limit)
2334 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2337 while (__last - __first > 3)
2339 if (__depth_limit == 0)
2341 std::__heap_select(__first, __nth + 1, __last);
2343 // Place the nth largest element in its final position.
2344 std::iter_swap(__first, __nth);
2348 _RandomAccessIterator __cut =
2349 std::__unguarded_partition_pivot(__first, __last);
2355 std::__insertion_sort(__first, __last);
2358 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2360 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2361 _RandomAccessIterator __last, _Size __depth_limit,
2364 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2367 while (__last - __first > 3)
2369 if (__depth_limit == 0)
2371 std::__heap_select(__first, __nth + 1, __last, __comp);
2372 // Place the nth largest element in its final position.
2373 std::iter_swap(__first, __nth);
2377 _RandomAccessIterator __cut =
2378 std::__unguarded_partition_pivot(__first, __last, __comp);
2384 std::__insertion_sort(__first, __last, __comp);
2390 * @brief Finds the first position in which @a val could be inserted
2391 * without changing the ordering.
2392 * @param first An iterator.
2393 * @param last Another iterator.
2394 * @param val The search term.
2395 * @return An iterator pointing to the first element <em>not less
2396 * than</em> @a val, or end() if every element is less than
2398 * @ingroup binary_search_algorithms
2400 template<typename _ForwardIterator, typename _Tp>
2402 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2405 typedef typename iterator_traits<_ForwardIterator>::value_type
2407 typedef typename iterator_traits<_ForwardIterator>::difference_type
2410 // concept requirements
2411 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2412 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2413 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2415 _DistanceType __len = std::distance(__first, __last);
2416 _DistanceType __half;
2417 _ForwardIterator __middle;
2421 __half = __len >> 1;
2423 std::advance(__middle, __half);
2424 if (*__middle < __val)
2428 __len = __len - __half - 1;
2437 * @brief Finds the first position in which @a val could be inserted
2438 * without changing the ordering.
2439 * @ingroup binary_search_algorithms
2440 * @param first An iterator.
2441 * @param last Another iterator.
2442 * @param val The search term.
2443 * @param comp A functor to use for comparisons.
2444 * @return An iterator pointing to the first element <em>not less
2445 * than</em> @a val, or end() if every element is less
2447 * @ingroup binary_search_algorithms
2449 * The comparison function should have the same effects on ordering as
2450 * the function used for the initial sort.
2452 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2454 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2455 const _Tp& __val, _Compare __comp)
2457 typedef typename iterator_traits<_ForwardIterator>::value_type
2459 typedef typename iterator_traits<_ForwardIterator>::difference_type
2462 // concept requirements
2463 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2464 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2466 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2469 _DistanceType __len = std::distance(__first, __last);
2470 _DistanceType __half;
2471 _ForwardIterator __middle;
2475 __half = __len >> 1;
2477 std::advance(__middle, __half);
2478 if (__comp(*__middle, __val))
2482 __len = __len - __half - 1;
2491 * @brief Finds the last position in which @a val could be inserted
2492 * without changing the ordering.
2493 * @ingroup binary_search_algorithms
2494 * @param first An iterator.
2495 * @param last Another iterator.
2496 * @param val The search term.
2497 * @return An iterator pointing to the first element greater than @a val,
2498 * or end() if no elements are greater than @a val.
2499 * @ingroup binary_search_algorithms
2501 template<typename _ForwardIterator, typename _Tp>
2503 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2506 typedef typename iterator_traits<_ForwardIterator>::value_type
2508 typedef typename iterator_traits<_ForwardIterator>::difference_type
2511 // concept requirements
2512 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2513 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2514 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2516 _DistanceType __len = std::distance(__first, __last);
2517 _DistanceType __half;
2518 _ForwardIterator __middle;
2522 __half = __len >> 1;
2524 std::advance(__middle, __half);
2525 if (__val < *__middle)
2531 __len = __len - __half - 1;
2538 * @brief Finds the last position in which @a val could be inserted
2539 * without changing the ordering.
2540 * @ingroup binary_search_algorithms
2541 * @param first An iterator.
2542 * @param last Another iterator.
2543 * @param val The search term.
2544 * @param comp A functor to use for comparisons.
2545 * @return An iterator pointing to the first element greater than @a val,
2546 * or end() if no elements are greater than @a val.
2547 * @ingroup binary_search_algorithms
2549 * The comparison function should have the same effects on ordering as
2550 * the function used for the initial sort.
2552 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2554 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2555 const _Tp& __val, _Compare __comp)
2557 typedef typename iterator_traits<_ForwardIterator>::value_type
2559 typedef typename iterator_traits<_ForwardIterator>::difference_type
2562 // concept requirements
2563 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2564 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2566 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2569 _DistanceType __len = std::distance(__first, __last);
2570 _DistanceType __half;
2571 _ForwardIterator __middle;
2575 __half = __len >> 1;
2577 std::advance(__middle, __half);
2578 if (__comp(__val, *__middle))
2584 __len = __len - __half - 1;
2591 * @brief Finds the largest subrange in which @a val could be inserted
2592 * at any place in it without changing the ordering.
2593 * @ingroup binary_search_algorithms
2594 * @param first An iterator.
2595 * @param last Another iterator.
2596 * @param val The search term.
2597 * @return An pair of iterators defining the subrange.
2598 * @ingroup binary_search_algorithms
2600 * This is equivalent to
2602 * std::make_pair(lower_bound(first, last, val),
2603 * upper_bound(first, last, val))
2605 * but does not actually call those functions.
2607 template<typename _ForwardIterator, typename _Tp>
2608 pair<_ForwardIterator, _ForwardIterator>
2609 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2612 typedef typename iterator_traits<_ForwardIterator>::value_type
2614 typedef typename iterator_traits<_ForwardIterator>::difference_type
2617 // concept requirements
2618 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2619 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2620 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2621 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2622 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2624 _DistanceType __len = std::distance(__first, __last);
2625 _DistanceType __half;
2626 _ForwardIterator __middle, __left, __right;
2630 __half = __len >> 1;
2632 std::advance(__middle, __half);
2633 if (*__middle < __val)
2637 __len = __len - __half - 1;
2639 else if (__val < *__middle)
2643 __left = std::lower_bound(__first, __middle, __val);
2644 std::advance(__first, __len);
2645 __right = std::upper_bound(++__middle, __first, __val);
2646 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2649 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2653 * @brief Finds the largest subrange in which @a val could be inserted
2654 * at any place in it without changing the ordering.
2655 * @param first An iterator.
2656 * @param last Another iterator.
2657 * @param val The search term.
2658 * @param comp A functor to use for comparisons.
2659 * @return An pair of iterators defining the subrange.
2660 * @ingroup binary_search_algorithms
2662 * This is equivalent to
2664 * std::make_pair(lower_bound(first, last, val, comp),
2665 * upper_bound(first, last, val, comp))
2667 * but does not actually call those functions.
2669 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2670 pair<_ForwardIterator, _ForwardIterator>
2671 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2675 typedef typename iterator_traits<_ForwardIterator>::value_type
2677 typedef typename iterator_traits<_ForwardIterator>::difference_type
2680 // concept requirements
2681 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2682 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2684 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2686 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2688 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2691 _DistanceType __len = std::distance(__first, __last);
2692 _DistanceType __half;
2693 _ForwardIterator __middle, __left, __right;
2697 __half = __len >> 1;
2699 std::advance(__middle, __half);
2700 if (__comp(*__middle, __val))
2704 __len = __len - __half - 1;
2706 else if (__comp(__val, *__middle))
2710 __left = std::lower_bound(__first, __middle, __val, __comp);
2711 std::advance(__first, __len);
2712 __right = std::upper_bound(++__middle, __first, __val, __comp);
2713 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2716 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2720 * @brief Determines whether an element exists in a range.
2721 * @ingroup binary_search_algorithms
2722 * @param first An iterator.
2723 * @param last Another iterator.
2724 * @param val The search term.
2725 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2727 * Note that this does not actually return an iterator to @a val. For
2728 * that, use std::find or a container's specialized find member functions.
2730 template<typename _ForwardIterator, typename _Tp>
2732 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2735 typedef typename iterator_traits<_ForwardIterator>::value_type
2738 // concept requirements
2739 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2740 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2741 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2742 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2744 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2745 return __i != __last && !(__val < *__i);
2749 * @brief Determines whether an element exists in a range.
2750 * @ingroup binary_search_algorithms
2751 * @param first An iterator.
2752 * @param last Another iterator.
2753 * @param val The search term.
2754 * @param comp A functor to use for comparisons.
2755 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2757 * Note that this does not actually return an iterator to @a val. For
2758 * that, use std::find or a container's specialized find member functions.
2760 * The comparison function should have the same effects on ordering as
2761 * the function used for the initial sort.
2763 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2765 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2766 const _Tp& __val, _Compare __comp)
2768 typedef typename iterator_traits<_ForwardIterator>::value_type
2771 // concept requirements
2772 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2773 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2775 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2777 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2780 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2781 return __i != __last && !bool(__comp(__val, *__i));
2786 /// This is a helper function for the merge routines.
2787 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2788 typename _BidirectionalIterator3>
2789 _BidirectionalIterator3
2790 __merge_backward(_BidirectionalIterator1 __first1,
2791 _BidirectionalIterator1 __last1,
2792 _BidirectionalIterator2 __first2,
2793 _BidirectionalIterator2 __last2,
2794 _BidirectionalIterator3 __result)
2796 if (__first1 == __last1)
2797 return std::copy_backward(__first2, __last2, __result);
2798 if (__first2 == __last2)
2799 return std::copy_backward(__first1, __last1, __result);
2804 if (*__last2 < *__last1)
2806 *--__result = *__last1;
2807 if (__first1 == __last1)
2808 return std::copy_backward(__first2, ++__last2, __result);
2813 *--__result = *__last2;
2814 if (__first2 == __last2)
2815 return std::copy_backward(__first1, ++__last1, __result);
2821 /// This is a helper function for the merge routines.
2822 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2823 typename _BidirectionalIterator3, typename _Compare>
2824 _BidirectionalIterator3
2825 __merge_backward(_BidirectionalIterator1 __first1,
2826 _BidirectionalIterator1 __last1,
2827 _BidirectionalIterator2 __first2,
2828 _BidirectionalIterator2 __last2,
2829 _BidirectionalIterator3 __result,
2832 if (__first1 == __last1)
2833 return std::copy_backward(__first2, __last2, __result);
2834 if (__first2 == __last2)
2835 return std::copy_backward(__first1, __last1, __result);
2840 if (__comp(*__last2, *__last1))
2842 *--__result = *__last1;
2843 if (__first1 == __last1)
2844 return std::copy_backward(__first2, ++__last2, __result);
2849 *--__result = *__last2;
2850 if (__first2 == __last2)
2851 return std::copy_backward(__first1, ++__last1, __result);
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)
2871 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2872 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2873 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2875 else if (__len1 <= __buffer_size)
2877 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2878 _GLIBCXX_MOVE3(__middle, __last, __first);
2879 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2883 std::rotate(__first, __middle, __last);
2884 std::advance(__first, std::distance(__middle, __last));
2889 /// This is a helper function for the merge routines.
2890 template<typename _BidirectionalIterator, typename _Distance,
2893 __merge_adaptive(_BidirectionalIterator __first,
2894 _BidirectionalIterator __middle,
2895 _BidirectionalIterator __last,
2896 _Distance __len1, _Distance __len2,
2897 _Pointer __buffer, _Distance __buffer_size)
2899 if (__len1 <= __len2 && __len1 <= __buffer_size)
2901 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2902 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2903 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2904 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2905 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2908 else if (__len2 <= __buffer_size)
2910 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2911 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2912 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2913 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2914 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2919 _BidirectionalIterator __first_cut = __first;
2920 _BidirectionalIterator __second_cut = __middle;
2921 _Distance __len11 = 0;
2922 _Distance __len22 = 0;
2923 if (__len1 > __len2)
2925 __len11 = __len1 / 2;
2926 std::advance(__first_cut, __len11);
2927 __second_cut = std::lower_bound(__middle, __last,
2929 __len22 = std::distance(__middle, __second_cut);
2933 __len22 = __len2 / 2;
2934 std::advance(__second_cut, __len22);
2935 __first_cut = std::upper_bound(__first, __middle,
2937 __len11 = std::distance(__first, __first_cut);
2939 _BidirectionalIterator __new_middle =
2940 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2941 __len1 - __len11, __len22, __buffer,
2943 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2944 __len22, __buffer, __buffer_size);
2945 std::__merge_adaptive(__new_middle, __second_cut, __last,
2947 __len2 - __len22, __buffer, __buffer_size);
2951 /// This is a helper function for the merge routines.
2952 template<typename _BidirectionalIterator, typename _Distance,
2953 typename _Pointer, typename _Compare>
2955 __merge_adaptive(_BidirectionalIterator __first,
2956 _BidirectionalIterator __middle,
2957 _BidirectionalIterator __last,
2958 _Distance __len1, _Distance __len2,
2959 _Pointer __buffer, _Distance __buffer_size,
2962 if (__len1 <= __len2 && __len1 <= __buffer_size)
2964 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2965 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2966 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2967 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2968 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2971 else if (__len2 <= __buffer_size)
2973 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2974 std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2975 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2976 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2977 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2982 _BidirectionalIterator __first_cut = __first;
2983 _BidirectionalIterator __second_cut = __middle;
2984 _Distance __len11 = 0;
2985 _Distance __len22 = 0;
2986 if (__len1 > __len2)
2988 __len11 = __len1 / 2;
2989 std::advance(__first_cut, __len11);
2990 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2992 __len22 = std::distance(__middle, __second_cut);
2996 __len22 = __len2 / 2;
2997 std::advance(__second_cut, __len22);
2998 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3000 __len11 = std::distance(__first, __first_cut);
3002 _BidirectionalIterator __new_middle =
3003 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3004 __len1 - __len11, __len22, __buffer,
3006 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3007 __len22, __buffer, __buffer_size, __comp);
3008 std::__merge_adaptive(__new_middle, __second_cut, __last,
3010 __len2 - __len22, __buffer,
3011 __buffer_size, __comp);
3015 /// This is a helper function for the merge routines.
3016 template<typename _BidirectionalIterator, typename _Distance>
3018 __merge_without_buffer(_BidirectionalIterator __first,
3019 _BidirectionalIterator __middle,
3020 _BidirectionalIterator __last,
3021 _Distance __len1, _Distance __len2)
3023 if (__len1 == 0 || __len2 == 0)
3025 if (__len1 + __len2 == 2)
3027 if (*__middle < *__first)
3028 std::iter_swap(__first, __middle);
3031 _BidirectionalIterator __first_cut = __first;
3032 _BidirectionalIterator __second_cut = __middle;
3033 _Distance __len11 = 0;
3034 _Distance __len22 = 0;
3035 if (__len1 > __len2)
3037 __len11 = __len1 / 2;
3038 std::advance(__first_cut, __len11);
3039 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3040 __len22 = std::distance(__middle, __second_cut);
3044 __len22 = __len2 / 2;
3045 std::advance(__second_cut, __len22);
3046 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3047 __len11 = std::distance(__first, __first_cut);
3049 std::rotate(__first_cut, __middle, __second_cut);
3050 _BidirectionalIterator __new_middle = __first_cut;
3051 std::advance(__new_middle, std::distance(__middle, __second_cut));
3052 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3054 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3055 __len1 - __len11, __len2 - __len22);
3058 /// This is a helper function for the merge routines.
3059 template<typename _BidirectionalIterator, typename _Distance,
3062 __merge_without_buffer(_BidirectionalIterator __first,
3063 _BidirectionalIterator __middle,
3064 _BidirectionalIterator __last,
3065 _Distance __len1, _Distance __len2,
3068 if (__len1 == 0 || __len2 == 0)
3070 if (__len1 + __len2 == 2)
3072 if (__comp(*__middle, *__first))
3073 std::iter_swap(__first, __middle);
3076 _BidirectionalIterator __first_cut = __first;
3077 _BidirectionalIterator __second_cut = __middle;
3078 _Distance __len11 = 0;
3079 _Distance __len22 = 0;
3080 if (__len1 > __len2)
3082 __len11 = __len1 / 2;
3083 std::advance(__first_cut, __len11);
3084 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3086 __len22 = std::distance(__middle, __second_cut);
3090 __len22 = __len2 / 2;
3091 std::advance(__second_cut, __len22);
3092 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3094 __len11 = std::distance(__first, __first_cut);
3096 std::rotate(__first_cut, __middle, __second_cut);
3097 _BidirectionalIterator __new_middle = __first_cut;
3098 std::advance(__new_middle, std::distance(__middle, __second_cut));
3099 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3100 __len11, __len22, __comp);
3101 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3102 __len1 - __len11, __len2 - __len22, __comp);
3106 * @brief Merges two sorted ranges in place.
3107 * @ingroup sorting_algorithms
3108 * @param first An iterator.
3109 * @param middle Another iterator.
3110 * @param last Another iterator.
3113 * Merges two sorted and consecutive ranges, [first,middle) and
3114 * [middle,last), and puts the result in [first,last). The output will
3115 * be sorted. The sort is @e stable, that is, for equivalent
3116 * elements in the two ranges, elements from the first range will always
3117 * come before elements from the second.
3119 * If enough additional memory is available, this takes (last-first)-1
3120 * comparisons. Otherwise an NlogN algorithm is used, where N is
3121 * distance(first,last).
3123 template<typename _BidirectionalIterator>
3125 inplace_merge(_BidirectionalIterator __first,
3126 _BidirectionalIterator __middle,
3127 _BidirectionalIterator __last)
3129 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3131 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3134 // concept requirements
3135 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3136 _BidirectionalIterator>)
3137 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3138 __glibcxx_requires_sorted(__first, __middle);
3139 __glibcxx_requires_sorted(__middle, __last);
3141 if (__first == __middle || __middle == __last)
3144 _DistanceType __len1 = std::distance(__first, __middle);
3145 _DistanceType __len2 = std::distance(__middle, __last);
3147 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3149 if (__buf.begin() == 0)
3150 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3152 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3153 __buf.begin(), _DistanceType(__buf.size()));
3157 * @brief Merges two sorted ranges in place.
3158 * @ingroup sorting_algorithms
3159 * @param first An iterator.
3160 * @param middle Another iterator.
3161 * @param last Another iterator.
3162 * @param comp A functor to use for comparisons.
3165 * Merges two sorted and consecutive ranges, [first,middle) and
3166 * [middle,last), and puts the result in [first,last). The output will
3167 * be sorted. The sort is @e stable, that is, for equivalent
3168 * elements in the two ranges, elements from the first range will always
3169 * come before elements from the second.
3171 * If enough additional memory is available, this takes (last-first)-1
3172 * comparisons. Otherwise an NlogN algorithm is used, where N is
3173 * distance(first,last).
3175 * The comparison function should have the same effects on ordering as
3176 * the function used for the initial sort.
3178 template<typename _BidirectionalIterator, typename _Compare>
3180 inplace_merge(_BidirectionalIterator __first,
3181 _BidirectionalIterator __middle,
3182 _BidirectionalIterator __last,
3185 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3187 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3190 // concept requirements
3191 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3192 _BidirectionalIterator>)
3193 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3194 _ValueType, _ValueType>)
3195 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3196 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3198 if (__first == __middle || __middle == __last)
3201 const _DistanceType __len1 = std::distance(__first, __middle);
3202 const _DistanceType __len2 = std::distance(__middle, __last);
3204 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3206 if (__buf.begin() == 0)
3207 std::__merge_without_buffer(__first, __middle, __last, __len1,
3210 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3211 __buf.begin(), _DistanceType(__buf.size()),
3215 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3218 __merge_sort_loop(_RandomAccessIterator1 __first,
3219 _RandomAccessIterator1 __last,
3220 _RandomAccessIterator2 __result,
3221 _Distance __step_size)
3223 const _Distance __two_step = 2 * __step_size;
3225 while (__last - __first >= __two_step)
3227 __result = _GLIBCXX_STD_P::merge(
3228 _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3229 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3230 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3231 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3233 __first += __two_step;
3236 __step_size = std::min(_Distance(__last - __first), __step_size);
3237 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3238 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3240 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3242 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3246 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3247 typename _Distance, typename _Compare>
3249 __merge_sort_loop(_RandomAccessIterator1 __first,
3250 _RandomAccessIterator1 __last,
3251 _RandomAccessIterator2 __result, _Distance __step_size,
3254 const _Distance __two_step = 2 * __step_size;
3256 while (__last - __first >= __two_step)
3258 __result = _GLIBCXX_STD_P::merge(
3259 _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3260 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3261 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3262 _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3264 __first += __two_step;
3266 __step_size = std::min(_Distance(__last - __first), __step_size);
3268 _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3269 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3271 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3273 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3277 template<typename _RandomAccessIterator, typename _Distance>
3279 __chunk_insertion_sort(_RandomAccessIterator __first,
3280 _RandomAccessIterator __last,
3281 _Distance __chunk_size)
3283 while (__last - __first >= __chunk_size)
3285 std::__insertion_sort(__first, __first + __chunk_size);
3286 __first += __chunk_size;
3288 std::__insertion_sort(__first, __last);
3291 template<typename _RandomAccessIterator, typename _Distance,
3294 __chunk_insertion_sort(_RandomAccessIterator __first,
3295 _RandomAccessIterator __last,
3296 _Distance __chunk_size, _Compare __comp)
3298 while (__last - __first >= __chunk_size)
3300 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3301 __first += __chunk_size;
3303 std::__insertion_sort(__first, __last, __comp);
3306 enum { _S_chunk_size = 7 };
3308 template<typename _RandomAccessIterator, typename _Pointer>
3310 __merge_sort_with_buffer(_RandomAccessIterator __first,
3311 _RandomAccessIterator __last,
3314 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3317 const _Distance __len = __last - __first;
3318 const _Pointer __buffer_last = __buffer + __len;
3320 _Distance __step_size = _S_chunk_size;
3321 std::__chunk_insertion_sort(__first, __last, __step_size);
3323 while (__step_size < __len)
3325 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3327 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3332 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3334 __merge_sort_with_buffer(_RandomAccessIterator __first,
3335 _RandomAccessIterator __last,
3336 _Pointer __buffer, _Compare __comp)
3338 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3341 const _Distance __len = __last - __first;
3342 const _Pointer __buffer_last = __buffer + __len;
3344 _Distance __step_size = _S_chunk_size;
3345 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3347 while (__step_size < __len)
3349 std::__merge_sort_loop(__first, __last, __buffer,
3350 __step_size, __comp);
3352 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3353 __step_size, __comp);
3358 template<typename _RandomAccessIterator, typename _Pointer,
3361 __stable_sort_adaptive(_RandomAccessIterator __first,
3362 _RandomAccessIterator __last,
3363 _Pointer __buffer, _Distance __buffer_size)
3365 const _Distance __len = (__last - __first + 1) / 2;
3366 const _RandomAccessIterator __middle = __first + __len;
3367 if (__len > __buffer_size)
3369 std::__stable_sort_adaptive(__first, __middle,
3370 __buffer, __buffer_size);
3371 std::__stable_sort_adaptive(__middle, __last,
3372 __buffer, __buffer_size);
3376 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3377 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3379 std::__merge_adaptive(__first, __middle, __last,
3380 _Distance(__middle - __first),
3381 _Distance(__last - __middle),
3382 __buffer, __buffer_size);
3385 template<typename _RandomAccessIterator, typename _Pointer,
3386 typename _Distance, typename _Compare>
3388 __stable_sort_adaptive(_RandomAccessIterator __first,
3389 _RandomAccessIterator __last,
3390 _Pointer __buffer, _Distance __buffer_size,
3393 const _Distance __len = (__last - __first + 1) / 2;
3394 const _RandomAccessIterator __middle = __first + __len;
3395 if (__len > __buffer_size)
3397 std::__stable_sort_adaptive(__first, __middle, __buffer,
3398 __buffer_size, __comp);
3399 std::__stable_sort_adaptive(__middle, __last, __buffer,
3400 __buffer_size, __comp);
3404 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3405 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3407 std::__merge_adaptive(__first, __middle, __last,
3408 _Distance(__middle - __first),
3409 _Distance(__last - __middle),
3410 __buffer, __buffer_size,
3414 /// This is a helper function for the stable sorting routines.
3415 template<typename _RandomAccessIterator>
3417 __inplace_stable_sort(_RandomAccessIterator __first,
3418 _RandomAccessIterator __last)
3420 if (__last - __first < 15)
3422 std::__insertion_sort(__first, __last);
3425 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3426 std::__inplace_stable_sort(__first, __middle);
3427 std::__inplace_stable_sort(__middle, __last);
3428 std::__merge_without_buffer(__first, __middle, __last,
3433 /// This is a helper function for the stable sorting routines.
3434 template<typename _RandomAccessIterator, typename _Compare>
3436 __inplace_stable_sort(_RandomAccessIterator __first,
3437 _RandomAccessIterator __last, _Compare __comp)
3439 if (__last - __first < 15)
3441 std::__insertion_sort(__first, __last, __comp);
3444 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3445 std::__inplace_stable_sort(__first, __middle, __comp);
3446 std::__inplace_stable_sort(__middle, __last, __comp);
3447 std::__merge_without_buffer(__first, __middle, __last,
3455 // Set algorithms: includes, set_union, set_intersection, set_difference,
3456 // set_symmetric_difference. All of these algorithms have the precondition
3457 // that their input ranges are sorted and the postcondition that their output
3458 // ranges are sorted.
3461 * @brief Determines whether all elements of a sequence exists in a range.
3462 * @param first1 Start of search range.
3463 * @param last1 End of search range.
3464 * @param first2 Start of sequence
3465 * @param last2 End of sequence.
3466 * @return True if each element in [first2,last2) is contained in order
3467 * within [first1,last1). False otherwise.
3468 * @ingroup set_algorithms
3470 * This operation expects both [first1,last1) and [first2,last2) to be
3471 * sorted. Searches for the presence of each element in [first2,last2)
3472 * within [first1,last1). The iterators over each range only move forward,
3473 * so this is a linear algorithm. If an element in [first2,last2) is not
3474 * found before the search iterator reaches @a last2, false is returned.
3476 template<typename _InputIterator1, typename _InputIterator2>
3478 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3479 _InputIterator2 __first2, _InputIterator2 __last2)
3481 typedef typename iterator_traits<_InputIterator1>::value_type
3483 typedef typename iterator_traits<_InputIterator2>::value_type
3486 // concept requirements
3487 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3488 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3489 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3490 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3491 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3492 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3494 while (__first1 != __last1 && __first2 != __last2)
3495 if (*__first2 < *__first1)
3497 else if(*__first1 < *__first2)
3500 ++__first1, ++__first2;
3502 return __first2 == __last2;
3506 * @brief Determines whether all elements of a sequence exists in a range
3508 * @ingroup set_algorithms
3509 * @param first1 Start of search range.
3510 * @param last1 End of search range.
3511 * @param first2 Start of sequence
3512 * @param last2 End of sequence.
3513 * @param comp Comparison function to use.
3514 * @return True if each element in [first2,last2) is contained in order
3515 * within [first1,last1) according to comp. False otherwise.
3516 * @ingroup set_algorithms
3518 * This operation expects both [first1,last1) and [first2,last2) to be
3519 * sorted. Searches for the presence of each element in [first2,last2)
3520 * within [first1,last1), using comp to decide. The iterators over each
3521 * range only move forward, so this is a linear algorithm. If an element
3522 * in [first2,last2) is not found before the search iterator reaches @a
3523 * last2, false is returned.
3525 template<typename _InputIterator1, typename _InputIterator2,
3528 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3529 _InputIterator2 __first2, _InputIterator2 __last2,
3532 typedef typename iterator_traits<_InputIterator1>::value_type
3534 typedef typename iterator_traits<_InputIterator2>::value_type
3537 // concept requirements
3538 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3539 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3540 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3541 _ValueType1, _ValueType2>)
3542 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3543 _ValueType2, _ValueType1>)
3544 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3545 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3547 while (__first1 != __last1 && __first2 != __last2)
3548 if (__comp(*__first2, *__first1))
3550 else if(__comp(*__first1, *__first2))
3553 ++__first1, ++__first2;
3555 return __first2 == __last2;
3564 // set_symmetric_difference
3569 * @brief Permute range into the next @a dictionary ordering.
3570 * @ingroup sorting_algorithms
3571 * @param first Start of range.
3572 * @param last End of range.
3573 * @return False if wrapped to first permutation, true otherwise.
3575 * Treats all permutations of the range as a set of @a dictionary sorted
3576 * sequences. Permutes the current sequence into the next one of this set.
3577 * Returns true if there are more sequences to generate. If the sequence
3578 * is the largest of the set, the smallest is generated and false returned.
3580 template<typename _BidirectionalIterator>
3582 next_permutation(_BidirectionalIterator __first,
3583 _BidirectionalIterator __last)
3585 // concept requirements
3586 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3587 _BidirectionalIterator>)
3588 __glibcxx_function_requires(_LessThanComparableConcept<
3589 typename iterator_traits<_BidirectionalIterator>::value_type>)
3590 __glibcxx_requires_valid_range(__first, __last);
3592 if (__first == __last)
3594 _BidirectionalIterator __i = __first;
3603 _BidirectionalIterator __ii = __i;
3607 _BidirectionalIterator __j = __last;
3608 while (!(*__i < *--__j))
3610 std::iter_swap(__i, __j);
3611 std::reverse(__ii, __last);
3616 std::reverse(__first, __last);
3623 * @brief Permute range into the next @a dictionary ordering using
3624 * comparison functor.
3625 * @ingroup sorting_algorithms
3626 * @param first Start of range.
3627 * @param last End of range.
3628 * @param comp A comparison functor.
3629 * @return False if wrapped to first permutation, true otherwise.
3631 * Treats all permutations of the range [first,last) as a set of
3632 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3633 * sequence into the next one of this set. Returns true if there are more
3634 * sequences to generate. If the sequence is the largest of the set, the
3635 * smallest is generated and false returned.
3637 template<typename _BidirectionalIterator, typename _Compare>
3639 next_permutation(_BidirectionalIterator __first,
3640 _BidirectionalIterator __last, _Compare __comp)
3642 // concept requirements
3643 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3644 _BidirectionalIterator>)
3645 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3646 typename iterator_traits<_BidirectionalIterator>::value_type,
3647 typename iterator_traits<_BidirectionalIterator>::value_type>)
3648 __glibcxx_requires_valid_range(__first, __last);
3650 if (__first == __last)
3652 _BidirectionalIterator __i = __first;
3661 _BidirectionalIterator __ii = __i;
3663 if (__comp(*__i, *__ii))
3665 _BidirectionalIterator __j = __last;
3666 while (!bool(__comp(*__i, *--__j)))
3668 std::iter_swap(__i, __j);
3669 std::reverse(__ii, __last);
3674 std::reverse(__first, __last);
3681 * @brief Permute range into the previous @a dictionary ordering.
3682 * @ingroup sorting_algorithms
3683 * @param first Start of range.
3684 * @param last End of range.
3685 * @return False if wrapped to last permutation, true otherwise.
3687 * Treats all permutations of the range as a set of @a dictionary sorted
3688 * sequences. Permutes the current sequence into the previous one of this
3689 * set. Returns true if there are more sequences to generate. If the
3690 * sequence is the smallest of the set, the largest is generated and false
3693 template<typename _BidirectionalIterator>
3695 prev_permutation(_BidirectionalIterator __first,
3696 _BidirectionalIterator __last)
3698 // concept requirements
3699 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3700 _BidirectionalIterator>)
3701 __glibcxx_function_requires(_LessThanComparableConcept<
3702 typename iterator_traits<_BidirectionalIterator>::value_type>)
3703 __glibcxx_requires_valid_range(__first, __last);
3705 if (__first == __last)
3707 _BidirectionalIterator __i = __first;
3716 _BidirectionalIterator __ii = __i;
3720 _BidirectionalIterator __j = __last;
3721 while (!(*--__j < *__i))
3723 std::iter_swap(__i, __j);
3724 std::reverse(__ii, __last);
3729 std::reverse(__first, __last);
3736 * @brief Permute range into the previous @a dictionary ordering using
3737 * comparison functor.
3738 * @ingroup sorting_algorithms
3739 * @param first Start of range.
3740 * @param last End of range.
3741 * @param comp A comparison functor.
3742 * @return False if wrapped to last permutation, true otherwise.
3744 * Treats all permutations of the range [first,last) as a set of
3745 * @a dictionary sorted sequences ordered by @a comp. Permutes the current
3746 * sequence into the previous one of this set. Returns true if there are
3747 * more sequences to generate. If the sequence is the smallest of the set,
3748 * the largest is generated and false returned.
3750 template<typename _BidirectionalIterator, typename _Compare>
3752 prev_permutation(_BidirectionalIterator __first,
3753 _BidirectionalIterator __last, _Compare __comp)
3755 // concept requirements
3756 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3757 _BidirectionalIterator>)
3758 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3759 typename iterator_traits<_BidirectionalIterator>::value_type,
3760 typename iterator_traits<_BidirectionalIterator>::value_type>)
3761 __glibcxx_requires_valid_range(__first, __last);
3763 if (__first == __last)
3765 _BidirectionalIterator __i = __first;
3774 _BidirectionalIterator __ii = __i;
3776 if (__comp(*__ii, *__i))
3778 _BidirectionalIterator __j = __last;
3779 while (!bool(__comp(*--__j, *__i)))
3781 std::iter_swap(__i, __j);
3782 std::reverse(__ii, __last);
3787 std::reverse(__first, __last);
3797 * @brief Copy a sequence, replacing each element of one value with another
3799 * @param first An input iterator.
3800 * @param last An input iterator.
3801 * @param result An output iterator.
3802 * @param old_value The value to be replaced.
3803 * @param new_value The replacement value.
3804 * @return The end of the output sequence, @p result+(last-first).
3806 * Copies each element in the input range @p [first,last) to the
3807 * output range @p [result,result+(last-first)) replacing elements
3808 * equal to @p old_value with @p new_value.
3810 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3812 replace_copy(_InputIterator __first, _InputIterator __last,
3813 _OutputIterator __result,
3814 const _Tp& __old_value, const _Tp& __new_value)
3816 // concept requirements
3817 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3818 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3819 typename iterator_traits<_InputIterator>::value_type>)
3820 __glibcxx_function_requires(_EqualOpConcept<
3821 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3822 __glibcxx_requires_valid_range(__first, __last);
3824 for (; __first != __last; ++__first, ++__result)
3825 if (*__first == __old_value)
3826 *__result = __new_value;
3828 *__result = *__first;
3833 * @brief Copy a sequence, replacing each value for which a predicate
3834 * returns true with another value.
3835 * @ingroup mutating_algorithms
3836 * @param first An input iterator.
3837 * @param last An input iterator.
3838 * @param result An output iterator.
3839 * @param pred A predicate.
3840 * @param new_value The replacement value.
3841 * @return The end of the output sequence, @p result+(last-first).
3843 * Copies each element in the range @p [first,last) to the range
3844 * @p [result,result+(last-first)) replacing elements for which
3845 * @p pred returns true with @p new_value.
3847 template<typename _InputIterator, typename _OutputIterator,
3848 typename _Predicate, typename _Tp>
3850 replace_copy_if(_InputIterator __first, _InputIterator __last,
3851 _OutputIterator __result,
3852 _Predicate __pred, 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(_UnaryPredicateConcept<_Predicate,
3859 typename iterator_traits<_InputIterator>::value_type>)
3860 __glibcxx_requires_valid_range(__first, __last);
3862 for (; __first != __last; ++__first, ++__result)
3863 if (__pred(*__first))
3864 *__result = __new_value;
3866 *__result = *__first;
3870 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3872 * @brief Determines whether the elements of a sequence are sorted.
3873 * @ingroup sorting_algorithms
3874 * @param first An iterator.
3875 * @param last Another iterator.
3876 * @return True if the elements are sorted, false otherwise.
3878 template<typename _ForwardIterator>
3880 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3881 { return std::is_sorted_until(__first, __last) == __last; }
3884 * @brief Determines whether the elements of a sequence are sorted
3885 * according to a comparison functor.
3886 * @ingroup sorting_algorithms
3887 * @param first An iterator.
3888 * @param last Another iterator.
3889 * @param comp A comparison functor.
3890 * @return True if the elements are sorted, false otherwise.
3892 template<typename _ForwardIterator, typename _Compare>
3894 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3896 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3899 * @brief Determines the end of a sorted sequence.
3900 * @ingroup sorting_algorithms
3901 * @param first An iterator.
3902 * @param last Another iterator.
3903 * @return An iterator pointing to the last iterator i in [first, last)
3904 * for which the range [first, i) is sorted.
3906 template<typename _ForwardIterator>
3908 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3910 // concept requirements
3911 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3912 __glibcxx_function_requires(_LessThanComparableConcept<
3913 typename iterator_traits<_ForwardIterator>::value_type>)
3914 __glibcxx_requires_valid_range(__first, __last);
3916 if (__first == __last)
3919 _ForwardIterator __next = __first;
3920 for (++__next; __next != __last; __first = __next, ++__next)
3921 if (*__next < *__first)
3927 * @brief Determines the end of a sorted sequence using comparison functor.
3928 * @ingroup sorting_algorithms
3929 * @param first An iterator.
3930 * @param last Another iterator.
3931 * @param comp A comparison functor.
3932 * @return An iterator pointing to the last iterator i in [first, last)
3933 * for which the range [first, i) is sorted.
3935 template<typename _ForwardIterator, typename _Compare>
3937 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3940 // concept requirements
3941 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3942 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3943 typename iterator_traits<_ForwardIterator>::value_type,
3944 typename iterator_traits<_ForwardIterator>::value_type>)
3945 __glibcxx_requires_valid_range(__first, __last);
3947 if (__first == __last)
3950 _ForwardIterator __next = __first;
3951 for (++__next; __next != __last; __first = __next, ++__next)
3952 if (__comp(*__next, *__first))
3958 * @brief Determines min and max at once as an ordered pair.
3959 * @ingroup sorting_algorithms
3960 * @param a A thing of arbitrary type.
3961 * @param b Another thing of arbitrary type.
3962 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3964 template<typename _Tp>
3965 inline pair<const _Tp&, const _Tp&>
3966 minmax(const _Tp& __a, const _Tp& __b)
3968 // concept requirements
3969 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3971 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3972 : pair<const _Tp&, const _Tp&>(__a, __b);
3976 * @brief Determines min and max at once as an ordered pair.
3977 * @ingroup sorting_algorithms
3978 * @param a A thing of arbitrary type.
3979 * @param b Another thing of arbitrary type.
3980 * @param comp A @link comparison_functor comparison functor@endlink.
3981 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3983 template<typename _Tp, typename _Compare>
3984 inline pair<const _Tp&, const _Tp&>
3985 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3987 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3988 : pair<const _Tp&, const _Tp&>(__a, __b);
3992 * @brief Return a pair of iterators pointing to the minimum and maximum
3993 * elements in a range.
3994 * @ingroup sorting_algorithms
3995 * @param first Start of range.
3996 * @param last End of range.
3997 * @return make_pair(m, M), where m is the first iterator i in
3998 * [first, last) such that no other element in the range is
3999 * smaller, and where M is the last iterator i in [first, last)
4000 * such that no other element in the range is larger.
4002 template<typename _ForwardIterator>
4003 pair<_ForwardIterator, _ForwardIterator>
4004 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
4006 // concept requirements
4007 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4008 __glibcxx_function_requires(_LessThanComparableConcept<
4009 typename iterator_traits<_ForwardIterator>::value_type>)
4010 __glibcxx_requires_valid_range(__first, __last);
4012 _ForwardIterator __next = __first;
4013 if (__first == __last
4014 || ++__next == __last)
4015 return std::make_pair(__first, __first);
4017 _ForwardIterator __min, __max;
4018 if (*__next < *__first)
4032 while (__first != __last)
4035 if (++__next == __last)
4037 if (*__first < *__min)
4039 else if (!(*__first < *__max))
4044 if (*__next < *__first)
4046 if (*__next < *__min)
4048 if (!(*__first < *__max))
4053 if (*__first < *__min)
4055 if (!(*__next < *__max))
4063 return std::make_pair(__min, __max);
4067 * @brief Return a pair of iterators pointing to the minimum and maximum
4068 * elements in a range.
4069 * @ingroup sorting_algorithms
4070 * @param first Start of range.
4071 * @param last End of range.
4072 * @param comp Comparison functor.
4073 * @return make_pair(m, M), where m is the first iterator i in
4074 * [first, last) such that no other element in the range is
4075 * smaller, and where M is the last iterator i in [first, last)
4076 * such that no other element in the range is larger.
4078 template<typename _ForwardIterator, typename _Compare>
4079 pair<_ForwardIterator, _ForwardIterator>
4080 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
4083 // concept requirements
4084 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4085 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4086 typename iterator_traits<_ForwardIterator>::value_type,
4087 typename iterator_traits<_ForwardIterator>::value_type>)
4088 __glibcxx_requires_valid_range(__first, __last);
4090 _ForwardIterator __next = __first;
4091 if (__first == __last
4092 || ++__next == __last)
4093 return std::make_pair(__first, __first);
4095 _ForwardIterator __min, __max;
4096 if (__comp(*__next, *__first))
4110 while (__first != __last)
4113 if (++__next == __last)
4115 if (__comp(*__first, *__min))
4117 else if (!__comp(*__first, *__max))
4122 if (__comp(*__next, *__first))
4124 if (__comp(*__next, *__min))
4126 if (!__comp(*__first, *__max))
4131 if (__comp(*__first, *__min))
4133 if (!__comp(*__next, *__max))
4141 return std::make_pair(__min, __max);
4145 template<typename _Tp>
4147 min(initializer_list<_Tp> __l)
4148 { return *std::min_element(__l.begin(), __l.end()); }
4150 template<typename _Tp, typename _Compare>
4152 min(initializer_list<_Tp> __l, _Compare __comp)
4153 { return *std::min_element(__l.begin(), __l.end(), __comp); }
4155 template<typename _Tp>
4157 max(initializer_list<_Tp> __l)
4158 { return *std::max_element(__l.begin(), __l.end()); }
4160 template<typename _Tp, typename _Compare>
4162 max(initializer_list<_Tp> __l, _Compare __comp)
4163 { return *std::max_element(__l.begin(), __l.end(), __comp); }
4165 template<typename _Tp>
4166 inline pair<_Tp, _Tp>
4167 minmax(initializer_list<_Tp> __l)
4169 pair<const _Tp*, const _Tp*> __p =
4170 std::minmax_element(__l.begin(), __l.end());
4171 return std::make_pair(*__p.first, *__p.second);
4174 template<typename _Tp, typename _Compare>
4175 inline pair<_Tp, _Tp>
4176 minmax(initializer_list<_Tp> __l, _Compare __comp)
4178 pair<const _Tp*, const _Tp*> __p =
4179 std::minmax_element(__l.begin(), __l.end(), __comp);
4180 return std::make_pair(*__p.first, *__p.second);
4182 #endif // __GXX_EXPERIMENTAL_CXX0X__
4184 _GLIBCXX_END_NAMESPACE
4186 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
4189 * @brief Apply a function to every element of a sequence.
4190 * @ingroup non_mutating_algorithms
4191 * @param first An input iterator.
4192 * @param last An input iterator.
4193 * @param f A unary function object.
4194 * @return @p f (std::move(@p f) in C++0x).
4196 * Applies the function object @p f to each element in the range
4197 * @p [first,last). @p f must not modify the order of the sequence.
4198 * If @p f has a return value it is ignored.
4200 template<typename _InputIterator, typename _Function>
4202 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4204 // concept requirements
4205 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4206 __glibcxx_requires_valid_range(__first, __last);
4207 for (; __first != __last; ++__first)
4209 return _GLIBCXX_MOVE(__f);
4213 * @brief Find the first occurrence of a value in a sequence.
4214 * @ingroup non_mutating_algorithms
4215 * @param first An input iterator.
4216 * @param last An input iterator.
4217 * @param val The value to find.
4218 * @return The first iterator @c i in the range @p [first,last)
4219 * such that @c *i == @p val, or @p last if no such iterator exists.
4221 template<typename _InputIterator, typename _Tp>
4222 inline _InputIterator
4223 find(_InputIterator __first, _InputIterator __last,
4226 // concept requirements
4227 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4228 __glibcxx_function_requires(_EqualOpConcept<
4229 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4230 __glibcxx_requires_valid_range(__first, __last);
4231 return std::__find(__first, __last, __val,
4232 std::__iterator_category(__first));
4236 * @brief Find the first element in a sequence for which a
4237 * predicate is true.
4238 * @ingroup non_mutating_algorithms
4239 * @param first An input iterator.
4240 * @param last An input iterator.
4241 * @param pred A predicate.
4242 * @return The first iterator @c i in the range @p [first,last)
4243 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4245 template<typename _InputIterator, typename _Predicate>
4246 inline _InputIterator
4247 find_if(_InputIterator __first, _InputIterator __last,
4250 // concept requirements
4251 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4252 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4253 typename iterator_traits<_InputIterator>::value_type>)
4254 __glibcxx_requires_valid_range(__first, __last);
4255 return std::__find_if(__first, __last, __pred,
4256 std::__iterator_category(__first));
4260 * @brief Find element from a set in a sequence.
4261 * @ingroup non_mutating_algorithms
4262 * @param first1 Start of range to search.
4263 * @param last1 End of range to search.
4264 * @param first2 Start of match candidates.
4265 * @param last2 End of match candidates.
4266 * @return The first iterator @c i in the range
4267 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4268 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4270 * Searches the range @p [first1,last1) for an element that is equal to
4271 * some element in the range [first2,last2). If found, returns an iterator
4272 * in the range [first1,last1), otherwise returns @p last1.
4274 template<typename _InputIterator, typename _ForwardIterator>
4276 find_first_of(_InputIterator __first1, _InputIterator __last1,
4277 _ForwardIterator __first2, _ForwardIterator __last2)
4279 // concept requirements
4280 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4281 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4282 __glibcxx_function_requires(_EqualOpConcept<
4283 typename iterator_traits<_InputIterator>::value_type,
4284 typename iterator_traits<_ForwardIterator>::value_type>)
4285 __glibcxx_requires_valid_range(__first1, __last1);
4286 __glibcxx_requires_valid_range(__first2, __last2);
4288 for (; __first1 != __last1; ++__first1)
4289 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4290 if (*__first1 == *__iter)
4296 * @brief Find element from a set in a sequence using a predicate.
4297 * @ingroup non_mutating_algorithms
4298 * @param first1 Start of range to search.
4299 * @param last1 End of range to search.
4300 * @param first2 Start of match candidates.
4301 * @param last2 End of match candidates.
4302 * @param comp Predicate to use.
4303 * @return The first iterator @c i in the range
4304 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4305 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4308 * Searches the range @p [first1,last1) for an element that is
4309 * equal to some element in the range [first2,last2). If found,
4310 * returns an iterator in the range [first1,last1), otherwise
4313 template<typename _InputIterator, typename _ForwardIterator,
4314 typename _BinaryPredicate>
4316 find_first_of(_InputIterator __first1, _InputIterator __last1,
4317 _ForwardIterator __first2, _ForwardIterator __last2,
4318 _BinaryPredicate __comp)
4320 // concept requirements
4321 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4322 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4323 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4324 typename iterator_traits<_InputIterator>::value_type,
4325 typename iterator_traits<_ForwardIterator>::value_type>)
4326 __glibcxx_requires_valid_range(__first1, __last1);
4327 __glibcxx_requires_valid_range(__first2, __last2);
4329 for (; __first1 != __last1; ++__first1)
4330 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4331 if (__comp(*__first1, *__iter))
4337 * @brief Find two adjacent values in a sequence that are equal.
4338 * @ingroup non_mutating_algorithms
4339 * @param first A forward iterator.
4340 * @param last A forward iterator.
4341 * @return The first iterator @c i such that @c i and @c i+1 are both
4342 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4343 * or @p last if no such iterator exists.
4345 template<typename _ForwardIterator>
4347 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4349 // concept requirements
4350 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4351 __glibcxx_function_requires(_EqualityComparableConcept<
4352 typename iterator_traits<_ForwardIterator>::value_type>)
4353 __glibcxx_requires_valid_range(__first, __last);
4354 if (__first == __last)
4356 _ForwardIterator __next = __first;
4357 while(++__next != __last)
4359 if (*__first == *__next)
4367 * @brief Find two adjacent values in a sequence using a predicate.
4368 * @ingroup non_mutating_algorithms
4369 * @param first A forward iterator.
4370 * @param last A forward iterator.
4371 * @param binary_pred A binary predicate.
4372 * @return The first iterator @c i such that @c i and @c i+1 are both
4373 * valid iterators in @p [first,last) and such that
4374 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4377 template<typename _ForwardIterator, typename _BinaryPredicate>
4379 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4380 _BinaryPredicate __binary_pred)
4382 // concept requirements
4383 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4384 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4385 typename iterator_traits<_ForwardIterator>::value_type,
4386 typename iterator_traits<_ForwardIterator>::value_type>)
4387 __glibcxx_requires_valid_range(__first, __last);
4388 if (__first == __last)
4390 _ForwardIterator __next = __first;
4391 while(++__next != __last)
4393 if (__binary_pred(*__first, *__next))
4401 * @brief Count the number of copies of a value in a sequence.
4402 * @ingroup non_mutating_algorithms
4403 * @param first An input iterator.
4404 * @param last An input iterator.
4405 * @param value The value to be counted.
4406 * @return The number of iterators @c i in the range @p [first,last)
4407 * for which @c *i == @p value
4409 template<typename _InputIterator, typename _Tp>
4410 typename iterator_traits<_InputIterator>::difference_type
4411 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4413 // concept requirements
4414 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4415 __glibcxx_function_requires(_EqualOpConcept<
4416 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4417 __glibcxx_requires_valid_range(__first, __last);
4418 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4419 for (; __first != __last; ++__first)
4420 if (*__first == __value)
4426 * @brief Count the elements of a sequence for which a predicate is true.
4427 * @ingroup non_mutating_algorithms
4428 * @param first An input iterator.
4429 * @param last An input iterator.
4430 * @param pred A predicate.
4431 * @return The number of iterators @c i in the range @p [first,last)
4432 * for which @p pred(*i) is true.
4434 template<typename _InputIterator, typename _Predicate>
4435 typename iterator_traits<_InputIterator>::difference_type
4436 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4438 // concept requirements
4439 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4440 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4441 typename iterator_traits<_InputIterator>::value_type>)
4442 __glibcxx_requires_valid_range(__first, __last);
4443 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4444 for (; __first != __last; ++__first)
4445 if (__pred(*__first))
4451 * @brief Search a sequence for a matching sub-sequence.
4452 * @ingroup non_mutating_algorithms
4453 * @param first1 A forward iterator.
4454 * @param last1 A forward iterator.
4455 * @param first2 A forward iterator.
4456 * @param last2 A forward iterator.
4457 * @return The first iterator @c i in the range
4458 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4459 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4460 * such iterator exists.
4462 * Searches the range @p [first1,last1) for a sub-sequence that compares
4463 * equal value-by-value with the sequence given by @p [first2,last2) and
4464 * returns an iterator to the first element of the sub-sequence, or
4465 * @p last1 if the sub-sequence is not found.
4467 * Because the sub-sequence must lie completely within the range
4468 * @p [first1,last1) it must start at a position less than
4469 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4471 * This means that the returned iterator @c i will be in the range
4472 * @p [first1,last1-(last2-first2))
4474 template<typename _ForwardIterator1, typename _ForwardIterator2>
4476 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4477 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4479 // concept requirements
4480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4481 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4482 __glibcxx_function_requires(_EqualOpConcept<
4483 typename iterator_traits<_ForwardIterator1>::value_type,
4484 typename iterator_traits<_ForwardIterator2>::value_type>)
4485 __glibcxx_requires_valid_range(__first1, __last1);
4486 __glibcxx_requires_valid_range(__first2, __last2);
4488 // Test for empty ranges
4489 if (__first1 == __last1 || __first2 == __last2)
4492 // Test for a pattern of length 1.
4493 _ForwardIterator2 __p1(__first2);
4494 if (++__p1 == __last2)
4495 return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4498 _ForwardIterator2 __p;
4499 _ForwardIterator1 __current = __first1;
4503 __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4504 if (__first1 == __last1)
4508 __current = __first1;
4509 if (++__current == __last1)
4512 while (*__current == *__p)
4514 if (++__p == __last2)
4516 if (++__current == __last1)
4525 * @brief Search a sequence for a matching sub-sequence using a predicate.
4526 * @ingroup non_mutating_algorithms
4527 * @param first1 A forward iterator.
4528 * @param last1 A forward iterator.
4529 * @param first2 A forward iterator.
4530 * @param last2 A forward iterator.
4531 * @param predicate A binary predicate.
4532 * @return The first iterator @c i in the range
4533 * @p [first1,last1-(last2-first2)) such that
4534 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4535 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4537 * Searches the range @p [first1,last1) for a sub-sequence that compares
4538 * equal value-by-value with the sequence given by @p [first2,last2),
4539 * using @p predicate to determine equality, and returns an iterator
4540 * to the first element of the sub-sequence, or @p last1 if no such
4543 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4545 template<typename _ForwardIterator1, typename _ForwardIterator2,
4546 typename _BinaryPredicate>
4548 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4549 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4550 _BinaryPredicate __predicate)
4552 // concept requirements
4553 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4554 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4555 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4556 typename iterator_traits<_ForwardIterator1>::value_type,
4557 typename iterator_traits<_ForwardIterator2>::value_type>)
4558 __glibcxx_requires_valid_range(__first1, __last1);
4559 __glibcxx_requires_valid_range(__first2, __last2);
4561 // Test for empty ranges
4562 if (__first1 == __last1 || __first2 == __last2)
4565 // Test for a pattern of length 1.
4566 _ForwardIterator2 __p1(__first2);
4567 if (++__p1 == __last2)
4569 while (__first1 != __last1
4570 && !bool(__predicate(*__first1, *__first2)))
4576 _ForwardIterator2 __p;
4577 _ForwardIterator1 __current = __first1;
4581 while (__first1 != __last1
4582 && !bool(__predicate(*__first1, *__first2)))
4584 if (__first1 == __last1)
4588 __current = __first1;
4589 if (++__current == __last1)
4592 while (__predicate(*__current, *__p))
4594 if (++__p == __last2)
4596 if (++__current == __last1)
4606 * @brief Search a sequence for a number of consecutive values.
4607 * @ingroup non_mutating_algorithms
4608 * @param first A forward iterator.
4609 * @param last A forward iterator.
4610 * @param count The number of consecutive values.
4611 * @param val The value to find.
4612 * @return The first iterator @c i in the range @p [first,last-count)
4613 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4614 * or @p last if no such iterator exists.
4616 * Searches the range @p [first,last) for @p count consecutive elements
4619 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4621 search_n(_ForwardIterator __first, _ForwardIterator __last,
4622 _Integer __count, const _Tp& __val)
4624 // concept requirements
4625 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4626 __glibcxx_function_requires(_EqualOpConcept<
4627 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4628 __glibcxx_requires_valid_range(__first, __last);
4633 return _GLIBCXX_STD_P::find(__first, __last, __val);
4634 return std::__search_n(__first, __last, __count, __val,
4635 std::__iterator_category(__first));
4640 * @brief Search a sequence for a number of consecutive values using a
4642 * @ingroup non_mutating_algorithms
4643 * @param first A forward iterator.
4644 * @param last A forward iterator.
4645 * @param count The number of consecutive values.
4646 * @param val The value to find.
4647 * @param binary_pred A binary predicate.
4648 * @return The first iterator @c i in the range @p [first,last-count)
4649 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4650 * range @p [0,count), or @p last if no such iterator exists.
4652 * Searches the range @p [first,last) for @p count consecutive elements
4653 * for which the predicate returns true.
4655 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4656 typename _BinaryPredicate>
4658 search_n(_ForwardIterator __first, _ForwardIterator __last,
4659 _Integer __count, const _Tp& __val,
4660 _BinaryPredicate __binary_pred)
4662 // concept requirements
4663 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4664 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4665 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4666 __glibcxx_requires_valid_range(__first, __last);
4672 while (__first != __last && !bool(__binary_pred(*__first, __val)))
4676 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4677 std::__iterator_category(__first));
4682 * @brief Perform an operation on a sequence.
4683 * @ingroup mutating_algorithms
4684 * @param first An input iterator.
4685 * @param last An input iterator.
4686 * @param result An output iterator.
4687 * @param unary_op A unary operator.
4688 * @return An output iterator equal to @p result+(last-first).
4690 * Applies the operator to each element in the input range and assigns
4691 * the results to successive elements of the output sequence.
4692 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4693 * range @p [0,last-first).
4695 * @p unary_op must not alter its argument.
4697 template<typename _InputIterator, typename _OutputIterator,
4698 typename _UnaryOperation>
4700 transform(_InputIterator __first, _InputIterator __last,
4701 _OutputIterator __result, _UnaryOperation __unary_op)
4703 // concept requirements
4704 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4705 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4706 // "the type returned by a _UnaryOperation"
4707 __typeof__(__unary_op(*__first))>)
4708 __glibcxx_requires_valid_range(__first, __last);
4710 for (; __first != __last; ++__first, ++__result)
4711 *__result = __unary_op(*__first);
4716 * @brief Perform an operation on corresponding elements of two sequences.
4717 * @ingroup mutating_algorithms
4718 * @param first1 An input iterator.
4719 * @param last1 An input iterator.
4720 * @param first2 An input iterator.
4721 * @param result An output iterator.
4722 * @param binary_op A binary operator.
4723 * @return An output iterator equal to @p result+(last-first).
4725 * Applies the operator to the corresponding elements in the two
4726 * input ranges and assigns the results to successive elements of the
4728 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4729 * @c N in the range @p [0,last1-first1).
4731 * @p binary_op must not alter either of its arguments.
4733 template<typename _InputIterator1, typename _InputIterator2,
4734 typename _OutputIterator, typename _BinaryOperation>
4736 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4737 _InputIterator2 __first2, _OutputIterator __result,
4738 _BinaryOperation __binary_op)
4740 // concept requirements
4741 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4742 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4743 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4744 // "the type returned by a _BinaryOperation"
4745 __typeof__(__binary_op(*__first1,*__first2))>)
4746 __glibcxx_requires_valid_range(__first1, __last1);
4748 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4749 *__result = __binary_op(*__first1, *__first2);
4754 * @brief Replace each occurrence of one value in a sequence with another
4756 * @ingroup mutating_algorithms
4757 * @param first A forward iterator.
4758 * @param last A forward iterator.
4759 * @param old_value The value to be replaced.
4760 * @param new_value The replacement value.
4761 * @return replace() returns no value.
4763 * For each iterator @c i in the range @p [first,last) if @c *i ==
4764 * @p old_value then the assignment @c *i = @p new_value is performed.
4766 template<typename _ForwardIterator, typename _Tp>
4768 replace(_ForwardIterator __first, _ForwardIterator __last,
4769 const _Tp& __old_value, const _Tp& __new_value)
4771 // concept requirements
4772 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4774 __glibcxx_function_requires(_EqualOpConcept<
4775 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4776 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4777 typename iterator_traits<_ForwardIterator>::value_type>)
4778 __glibcxx_requires_valid_range(__first, __last);
4780 for (; __first != __last; ++__first)
4781 if (*__first == __old_value)
4782 *__first = __new_value;
4786 * @brief Replace each value in a sequence for which a predicate returns
4787 * true with another value.
4788 * @ingroup mutating_algorithms
4789 * @param first A forward iterator.
4790 * @param last A forward iterator.
4791 * @param pred A predicate.
4792 * @param new_value The replacement value.
4793 * @return replace_if() returns no value.
4795 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4796 * is true then the assignment @c *i = @p new_value is performed.
4798 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4800 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4801 _Predicate __pred, const _Tp& __new_value)
4803 // concept requirements
4804 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4806 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4807 typename iterator_traits<_ForwardIterator>::value_type>)
4808 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4809 typename iterator_traits<_ForwardIterator>::value_type>)
4810 __glibcxx_requires_valid_range(__first, __last);
4812 for (; __first != __last; ++__first)
4813 if (__pred(*__first))
4814 *__first = __new_value;
4818 * @brief Assign the result of a function object to each value in a
4820 * @ingroup mutating_algorithms
4821 * @param first A forward iterator.
4822 * @param last A forward iterator.
4823 * @param gen A function object taking no arguments and returning
4824 * std::iterator_traits<_ForwardIterator>::value_type
4825 * @return generate() returns no value.
4827 * Performs the assignment @c *i = @p gen() for each @c i in the range
4830 template<typename _ForwardIterator, typename _Generator>
4832 generate(_ForwardIterator __first, _ForwardIterator __last,
4835 // concept requirements
4836 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4837 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4838 typename iterator_traits<_ForwardIterator>::value_type>)
4839 __glibcxx_requires_valid_range(__first, __last);
4841 for (; __first != __last; ++__first)
4846 * @brief Assign the result of a function object to each value in a
4848 * @ingroup mutating_algorithms
4849 * @param first A forward iterator.
4850 * @param n The length of the sequence.
4851 * @param gen A function object taking no arguments and returning
4852 * std::iterator_traits<_ForwardIterator>::value_type
4853 * @return The end of the sequence, @p first+n
4855 * Performs the assignment @c *i = @p gen() for each @c i in the range
4856 * @p [first,first+n).
4858 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4859 * DR 865. More algorithms that throw away information
4861 template<typename _OutputIterator, typename _Size, typename _Generator>
4863 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4865 // concept requirements
4866 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4867 // "the type returned by a _Generator"
4868 __typeof__(__gen())>)
4870 for (; __n > 0; --__n, ++__first)
4877 * @brief Copy a sequence, removing consecutive duplicate values.
4878 * @ingroup mutating_algorithms
4879 * @param first An input iterator.
4880 * @param last An input iterator.
4881 * @param result An output iterator.
4882 * @return An iterator designating the end of the resulting sequence.
4884 * Copies each element in the range @p [first,last) to the range
4885 * beginning at @p result, except that only the first element is copied
4886 * from groups of consecutive elements that compare equal.
4887 * unique_copy() is stable, so the relative order of elements that are
4888 * copied is unchanged.
4890 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4891 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4893 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4894 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4897 template<typename _InputIterator, typename _OutputIterator>
4898 inline _OutputIterator
4899 unique_copy(_InputIterator __first, _InputIterator __last,
4900 _OutputIterator __result)
4902 // concept requirements
4903 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4904 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4905 typename iterator_traits<_InputIterator>::value_type>)
4906 __glibcxx_function_requires(_EqualityComparableConcept<
4907 typename iterator_traits<_InputIterator>::value_type>)
4908 __glibcxx_requires_valid_range(__first, __last);
4910 if (__first == __last)
4912 return std::__unique_copy(__first, __last, __result,
4913 std::__iterator_category(__first),
4914 std::__iterator_category(__result));
4918 * @brief Copy a sequence, removing consecutive values using a predicate.
4919 * @ingroup mutating_algorithms
4920 * @param first An input iterator.
4921 * @param last An input iterator.
4922 * @param result An output iterator.
4923 * @param binary_pred A binary predicate.
4924 * @return An iterator designating the end of the resulting sequence.
4926 * Copies each element in the range @p [first,last) to the range
4927 * beginning at @p result, except that only the first element is copied
4928 * from groups of consecutive elements for which @p binary_pred returns
4930 * unique_copy() is stable, so the relative order of elements that are
4931 * copied is unchanged.
4933 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4934 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4936 template<typename _InputIterator, typename _OutputIterator,
4937 typename _BinaryPredicate>
4938 inline _OutputIterator
4939 unique_copy(_InputIterator __first, _InputIterator __last,
4940 _OutputIterator __result,
4941 _BinaryPredicate __binary_pred)
4943 // concept requirements -- predicates checked later
4944 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4945 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4946 typename iterator_traits<_InputIterator>::value_type>)
4947 __glibcxx_requires_valid_range(__first, __last);
4949 if (__first == __last)
4951 return std::__unique_copy(__first, __last, __result, __binary_pred,
4952 std::__iterator_category(__first),
4953 std::__iterator_category(__result));
4958 * @brief Randomly shuffle the elements of a sequence.
4959 * @ingroup mutating_algorithms
4960 * @param first A forward iterator.
4961 * @param last A forward iterator.
4964 * Reorder the elements in the range @p [first,last) using a random
4965 * distribution, so that every possible ordering of the sequence is
4968 template<typename _RandomAccessIterator>
4970 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4972 // concept requirements
4973 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4974 _RandomAccessIterator>)
4975 __glibcxx_requires_valid_range(__first, __last);
4977 if (__first != __last)
4978 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4979 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4983 * @brief Shuffle the elements of a sequence using a random number
4985 * @ingroup mutating_algorithms
4986 * @param first A forward iterator.
4987 * @param last A forward iterator.
4988 * @param rand The RNG functor or function.
4991 * Reorders the elements in the range @p [first,last) using @p rand to
4992 * provide a random distribution. Calling @p rand(N) for a positive
4993 * integer @p N should return a randomly chosen integer from the
4996 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4998 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4999 _RandomNumberGenerator& __rand)
5001 // concept requirements
5002 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5003 _RandomAccessIterator>)
5004 __glibcxx_requires_valid_range(__first, __last);
5006 if (__first == __last)
5008 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5009 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
5014 * @brief Move elements for which a predicate is true to the beginning
5016 * @ingroup mutating_algorithms
5017 * @param first A forward iterator.
5018 * @param last A forward iterator.
5019 * @param pred A predicate functor.
5020 * @return An iterator @p middle such that @p pred(i) is true for each
5021 * iterator @p i in the range @p [first,middle) and false for each @p i
5022 * in the range @p [middle,last).
5024 * @p pred must not modify its operand. @p partition() does not preserve
5025 * the relative ordering of elements in each group, use
5026 * @p stable_partition() if this is needed.
5028 template<typename _ForwardIterator, typename _Predicate>
5029 inline _ForwardIterator
5030 partition(_ForwardIterator __first, _ForwardIterator __last,
5033 // concept requirements
5034 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
5036 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
5037 typename iterator_traits<_ForwardIterator>::value_type>)
5038 __glibcxx_requires_valid_range(__first, __last);
5040 return std::__partition(__first, __last, __pred,
5041 std::__iterator_category(__first));
5047 * @brief Sort the smallest elements of a sequence.
5048 * @ingroup sorting_algorithms
5049 * @param first An iterator.
5050 * @param middle Another iterator.
5051 * @param last Another iterator.
5054 * Sorts the smallest @p (middle-first) elements in the range
5055 * @p [first,last) and moves them to the range @p [first,middle). The
5056 * order of the remaining elements in the range @p [middle,last) is
5058 * After the sort if @p i and @j are iterators in the range
5059 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5060 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5062 template<typename _RandomAccessIterator>
5064 partial_sort(_RandomAccessIterator __first,
5065 _RandomAccessIterator __middle,
5066 _RandomAccessIterator __last)
5068 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5071 // concept requirements
5072 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5073 _RandomAccessIterator>)
5074 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5075 __glibcxx_requires_valid_range(__first, __middle);
5076 __glibcxx_requires_valid_range(__middle, __last);
5078 std::__heap_select(__first, __middle, __last);
5079 std::sort_heap(__first, __middle);
5083 * @brief Sort the smallest elements of a sequence using a predicate
5085 * @ingroup sorting_algorithms
5086 * @param first An iterator.
5087 * @param middle Another iterator.
5088 * @param last Another iterator.
5089 * @param comp A comparison functor.
5092 * Sorts the smallest @p (middle-first) elements in the range
5093 * @p [first,last) and moves them to the range @p [first,middle). The
5094 * order of the remaining elements in the range @p [middle,last) is
5096 * After the sort if @p i and @j are iterators in the range
5097 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5098 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5101 template<typename _RandomAccessIterator, typename _Compare>
5103 partial_sort(_RandomAccessIterator __first,
5104 _RandomAccessIterator __middle,
5105 _RandomAccessIterator __last,
5108 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5111 // concept requirements
5112 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5113 _RandomAccessIterator>)
5114 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5115 _ValueType, _ValueType>)
5116 __glibcxx_requires_valid_range(__first, __middle);
5117 __glibcxx_requires_valid_range(__middle, __last);
5119 std::__heap_select(__first, __middle, __last, __comp);
5120 std::sort_heap(__first, __middle, __comp);
5124 * @brief Sort a sequence just enough to find a particular position.
5125 * @ingroup sorting_algorithms
5126 * @param first An iterator.
5127 * @param nth Another iterator.
5128 * @param last Another iterator.
5131 * Rearranges the elements in the range @p [first,last) so that @p *nth
5132 * is the same element that would have been in that position had the
5133 * whole sequence been sorted.
5134 * whole sequence been sorted. The elements either side of @p *nth are
5135 * not completely sorted, but for any iterator @i in the range
5136 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5137 * holds that @p *j<*i is false.
5139 template<typename _RandomAccessIterator>
5141 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5142 _RandomAccessIterator __last)
5144 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5147 // concept requirements
5148 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5149 _RandomAccessIterator>)
5150 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5151 __glibcxx_requires_valid_range(__first, __nth);
5152 __glibcxx_requires_valid_range(__nth, __last);
5154 if (__first == __last || __nth == __last)
5157 std::__introselect(__first, __nth, __last,
5158 std::__lg(__last - __first) * 2);
5162 * @brief Sort a sequence just enough to find a particular position
5163 * using a predicate for comparison.
5164 * @ingroup sorting_algorithms
5165 * @param first An iterator.
5166 * @param nth Another iterator.
5167 * @param last Another iterator.
5168 * @param comp A comparison functor.
5171 * Rearranges the elements in the range @p [first,last) so that @p *nth
5172 * is the same element that would have been in that position had the
5173 * whole sequence been sorted. The elements either side of @p *nth are
5174 * not completely sorted, but for any iterator @i in the range
5175 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5176 * holds that @p comp(*j,*i) is false.
5178 template<typename _RandomAccessIterator, typename _Compare>
5180 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5181 _RandomAccessIterator __last, _Compare __comp)
5183 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5186 // concept requirements
5187 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5188 _RandomAccessIterator>)
5189 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5190 _ValueType, _ValueType>)
5191 __glibcxx_requires_valid_range(__first, __nth);
5192 __glibcxx_requires_valid_range(__nth, __last);
5194 if (__first == __last || __nth == __last)
5197 std::__introselect(__first, __nth, __last,
5198 std::__lg(__last - __first) * 2, __comp);
5203 * @brief Sort the elements of a sequence.
5204 * @ingroup sorting_algorithms
5205 * @param first An iterator.
5206 * @param last Another iterator.
5209 * Sorts the elements in the range @p [first,last) in ascending order,
5210 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5211 * @p [first,last-1).
5213 * The relative ordering of equivalent elements is not preserved, use
5214 * @p stable_sort() if this is needed.
5216 template<typename _RandomAccessIterator>
5218 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5220 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5223 // concept requirements
5224 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5225 _RandomAccessIterator>)
5226 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5227 __glibcxx_requires_valid_range(__first, __last);
5229 if (__first != __last)
5231 std::__introsort_loop(__first, __last,
5232 std::__lg(__last - __first) * 2);
5233 std::__final_insertion_sort(__first, __last);
5238 * @brief Sort the elements of a sequence using a predicate for comparison.
5239 * @ingroup sorting_algorithms
5240 * @param first An iterator.
5241 * @param last Another iterator.
5242 * @param comp A comparison functor.
5245 * Sorts the elements in the range @p [first,last) in ascending order,
5246 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5247 * range @p [first,last-1).
5249 * The relative ordering of equivalent elements is not preserved, use
5250 * @p stable_sort() if this is needed.
5252 template<typename _RandomAccessIterator, typename _Compare>
5254 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5257 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5260 // concept requirements
5261 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5262 _RandomAccessIterator>)
5263 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
5265 __glibcxx_requires_valid_range(__first, __last);
5267 if (__first != __last)
5269 std::__introsort_loop(__first, __last,
5270 std::__lg(__last - __first) * 2, __comp);
5271 std::__final_insertion_sort(__first, __last, __comp);
5276 * @brief Merges two sorted ranges.
5277 * @ingroup sorting_algorithms
5278 * @param first1 An iterator.
5279 * @param first2 Another iterator.
5280 * @param last1 Another iterator.
5281 * @param last2 Another iterator.
5282 * @param result An iterator pointing to the end of the merged range.
5283 * @return An iterator pointing to the first element <em>not less
5286 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5287 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5288 * must be sorted, and the output range must not overlap with either of
5289 * the input ranges. The sort is @e stable, that is, for equivalent
5290 * elements in the two ranges, elements from the first range will always
5291 * come before elements from the second.
5293 template<typename _InputIterator1, typename _InputIterator2,
5294 typename _OutputIterator>
5296 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5297 _InputIterator2 __first2, _InputIterator2 __last2,
5298 _OutputIterator __result)
5300 typedef typename iterator_traits<_InputIterator1>::value_type
5302 typedef typename iterator_traits<_InputIterator2>::value_type
5305 // concept requirements
5306 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5307 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5308 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5310 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5312 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5313 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5314 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5316 while (__first1 != __last1 && __first2 != __last2)
5318 if (*__first2 < *__first1)
5320 *__result = *__first2;
5325 *__result = *__first1;
5330 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5335 * @brief Merges two sorted ranges.
5336 * @ingroup sorting_algorithms
5337 * @param first1 An iterator.
5338 * @param first2 Another iterator.
5339 * @param last1 Another iterator.
5340 * @param last2 Another iterator.
5341 * @param result An iterator pointing to the end of the merged range.
5342 * @param comp A functor to use for comparisons.
5343 * @return An iterator pointing to the first element "not less
5346 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5347 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5348 * must be sorted, and the output range must not overlap with either of
5349 * the input ranges. The sort is @e stable, that is, for equivalent
5350 * elements in the two ranges, elements from the first range will always
5351 * come before elements from the second.
5353 * The comparison function should have the same effects on ordering as
5354 * the function used for the initial sort.
5356 template<typename _InputIterator1, typename _InputIterator2,
5357 typename _OutputIterator, typename _Compare>
5359 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5360 _InputIterator2 __first2, _InputIterator2 __last2,
5361 _OutputIterator __result, _Compare __comp)
5363 typedef typename iterator_traits<_InputIterator1>::value_type
5365 typedef typename iterator_traits<_InputIterator2>::value_type
5368 // concept requirements
5369 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5370 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5371 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5373 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5375 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5376 _ValueType2, _ValueType1>)
5377 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5378 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5380 while (__first1 != __last1 && __first2 != __last2)
5382 if (__comp(*__first2, *__first1))
5384 *__result = *__first2;
5389 *__result = *__first1;
5394 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5400 * @brief Sort the elements of a sequence, preserving the relative order
5401 * of equivalent elements.
5402 * @ingroup sorting_algorithms
5403 * @param first An iterator.
5404 * @param last Another iterator.
5407 * Sorts the elements in the range @p [first,last) in ascending order,
5408 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5409 * @p [first,last-1).
5411 * The relative ordering of equivalent elements is preserved, so any two
5412 * elements @p x and @p y in the range @p [first,last) such that
5413 * @p x<y is false and @p y<x is false will have the same relative
5414 * ordering after calling @p stable_sort().
5416 template<typename _RandomAccessIterator>
5418 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5420 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5422 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5425 // concept requirements
5426 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5427 _RandomAccessIterator>)
5428 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5429 __glibcxx_requires_valid_range(__first, __last);
5431 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5433 if (__buf.begin() == 0)
5434 std::__inplace_stable_sort(__first, __last);
5436 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5437 _DistanceType(__buf.size()));
5441 * @brief Sort the elements of a sequence using a predicate for comparison,
5442 * preserving the relative order of equivalent elements.
5443 * @ingroup sorting_algorithms
5444 * @param first An iterator.
5445 * @param last Another iterator.
5446 * @param comp A comparison functor.
5449 * Sorts the elements in the range @p [first,last) in ascending order,
5450 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5451 * range @p [first,last-1).
5453 * The relative ordering of equivalent elements is preserved, so any two
5454 * elements @p x and @p y in the range @p [first,last) such that
5455 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5456 * relative ordering after calling @p stable_sort().
5458 template<typename _RandomAccessIterator, typename _Compare>
5460 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5463 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5465 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5468 // concept requirements
5469 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5470 _RandomAccessIterator>)
5471 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5474 __glibcxx_requires_valid_range(__first, __last);
5476 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5478 if (__buf.begin() == 0)
5479 std::__inplace_stable_sort(__first, __last, __comp);
5481 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5482 _DistanceType(__buf.size()), __comp);
5487 * @brief Return the union of two sorted ranges.
5488 * @ingroup set_algorithms
5489 * @param first1 Start of first range.
5490 * @param last1 End of first range.
5491 * @param first2 Start of second range.
5492 * @param last2 End of second range.
5493 * @return End of the output range.
5494 * @ingroup set_algorithms
5496 * This operation iterates over both ranges, copying elements present in
5497 * each range in order to the output range. Iterators increment for each
5498 * range. When the current element of one range is less than the other,
5499 * that element is copied and the iterator advanced. If an element is
5500 * contained in both ranges, the element from the first range is copied and
5501 * both ranges advance. The output range may not overlap either input
5504 template<typename _InputIterator1, typename _InputIterator2,
5505 typename _OutputIterator>
5507 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5508 _InputIterator2 __first2, _InputIterator2 __last2,
5509 _OutputIterator __result)
5511 typedef typename iterator_traits<_InputIterator1>::value_type
5513 typedef typename iterator_traits<_InputIterator2>::value_type
5516 // concept requirements
5517 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5518 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5519 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5521 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5523 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5524 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5525 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5526 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5528 while (__first1 != __last1 && __first2 != __last2)
5530 if (*__first1 < *__first2)
5532 *__result = *__first1;
5535 else if (*__first2 < *__first1)
5537 *__result = *__first2;
5542 *__result = *__first1;
5548 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5553 * @brief Return the union of two sorted ranges using a comparison functor.
5554 * @ingroup set_algorithms
5555 * @param first1 Start of first range.
5556 * @param last1 End of first range.
5557 * @param first2 Start of second range.
5558 * @param last2 End of second range.
5559 * @param comp The comparison functor.
5560 * @return End of the output range.
5561 * @ingroup set_algorithms
5563 * This operation iterates over both ranges, copying elements present in
5564 * each range in order to the output range. Iterators increment for each
5565 * range. When the current element of one range is less than the other
5566 * according to @a comp, that element is copied and the iterator advanced.
5567 * If an equivalent element according to @a comp is contained in both
5568 * ranges, the element from the first range is copied and both ranges
5569 * advance. The output range may not overlap either input range.
5571 template<typename _InputIterator1, typename _InputIterator2,
5572 typename _OutputIterator, typename _Compare>
5574 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5575 _InputIterator2 __first2, _InputIterator2 __last2,
5576 _OutputIterator __result, _Compare __comp)
5578 typedef typename iterator_traits<_InputIterator1>::value_type
5580 typedef typename iterator_traits<_InputIterator2>::value_type
5583 // concept requirements
5584 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5585 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5586 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5588 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5590 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5591 _ValueType1, _ValueType2>)
5592 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5593 _ValueType2, _ValueType1>)
5594 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5595 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5597 while (__first1 != __last1 && __first2 != __last2)
5599 if (__comp(*__first1, *__first2))
5601 *__result = *__first1;
5604 else if (__comp(*__first2, *__first1))
5606 *__result = *__first2;
5611 *__result = *__first1;
5617 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5622 * @brief Return the intersection of two sorted ranges.
5623 * @ingroup set_algorithms
5624 * @param first1 Start of first range.
5625 * @param last1 End of first range.
5626 * @param first2 Start of second range.
5627 * @param last2 End of second range.
5628 * @return End of the output range.
5629 * @ingroup set_algorithms
5631 * This operation iterates over both ranges, copying elements present in
5632 * both ranges in order to the output range. Iterators increment for each
5633 * range. When the current element of one range is less than the other,
5634 * that iterator advances. If an element is contained in both ranges, the
5635 * element from the first range is copied and both ranges advance. The
5636 * output range may not overlap either input range.
5638 template<typename _InputIterator1, typename _InputIterator2,
5639 typename _OutputIterator>
5641 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5642 _InputIterator2 __first2, _InputIterator2 __last2,
5643 _OutputIterator __result)
5645 typedef typename iterator_traits<_InputIterator1>::value_type
5647 typedef typename iterator_traits<_InputIterator2>::value_type
5650 // concept requirements
5651 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5652 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5653 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5655 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5656 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5657 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5658 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5660 while (__first1 != __last1 && __first2 != __last2)
5661 if (*__first1 < *__first2)
5663 else if (*__first2 < *__first1)
5667 *__result = *__first1;
5676 * @brief Return the intersection of two sorted ranges using comparison
5678 * @ingroup set_algorithms
5679 * @param first1 Start of first range.
5680 * @param last1 End of first range.
5681 * @param first2 Start of second range.
5682 * @param last2 End of second range.
5683 * @param comp The comparison functor.
5684 * @return End of the output range.
5685 * @ingroup set_algorithms
5687 * This operation iterates over both ranges, copying elements present in
5688 * both ranges in order to the output range. Iterators increment for each
5689 * range. When the current element of one range is less than the other
5690 * according to @a comp, that iterator advances. If an element is
5691 * contained in both ranges according to @a comp, the element from the
5692 * first range is copied and both ranges advance. The output range may not
5693 * overlap either input range.
5695 template<typename _InputIterator1, typename _InputIterator2,
5696 typename _OutputIterator, typename _Compare>
5698 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5699 _InputIterator2 __first2, _InputIterator2 __last2,
5700 _OutputIterator __result, _Compare __comp)
5702 typedef typename iterator_traits<_InputIterator1>::value_type
5704 typedef typename iterator_traits<_InputIterator2>::value_type
5707 // concept requirements
5708 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5709 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5710 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5712 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5713 _ValueType1, _ValueType2>)
5714 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5715 _ValueType2, _ValueType1>)
5716 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5717 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5719 while (__first1 != __last1 && __first2 != __last2)
5720 if (__comp(*__first1, *__first2))
5722 else if (__comp(*__first2, *__first1))
5726 *__result = *__first1;
5735 * @brief Return the difference of two sorted ranges.
5736 * @ingroup set_algorithms
5737 * @param first1 Start of first range.
5738 * @param last1 End of first range.
5739 * @param first2 Start of second range.
5740 * @param last2 End of second range.
5741 * @return End of the output range.
5742 * @ingroup set_algorithms
5744 * This operation iterates over both ranges, copying elements present in
5745 * the first range but not the second in order to the output range.
5746 * Iterators increment for each range. When the current element of the
5747 * first range is less than the second, that element is copied and the
5748 * iterator advances. If the current element of the second range is less,
5749 * the iterator advances, but no element is copied. If an element is
5750 * contained in both ranges, no elements are copied and both ranges
5751 * advance. The output range may not overlap either input range.
5753 template<typename _InputIterator1, typename _InputIterator2,
5754 typename _OutputIterator>
5756 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5757 _InputIterator2 __first2, _InputIterator2 __last2,
5758 _OutputIterator __result)
5760 typedef typename iterator_traits<_InputIterator1>::value_type
5762 typedef typename iterator_traits<_InputIterator2>::value_type
5765 // concept requirements
5766 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5767 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5768 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5770 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5771 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5772 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5773 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5775 while (__first1 != __last1 && __first2 != __last2)
5776 if (*__first1 < *__first2)
5778 *__result = *__first1;
5782 else if (*__first2 < *__first1)
5789 return std::copy(__first1, __last1, __result);
5793 * @brief Return the difference of two sorted ranges using comparison
5795 * @ingroup set_algorithms
5796 * @param first1 Start of first range.
5797 * @param last1 End of first range.
5798 * @param first2 Start of second range.
5799 * @param last2 End of second range.
5800 * @param comp The comparison functor.
5801 * @return End of the output range.
5802 * @ingroup set_algorithms
5804 * This operation iterates over both ranges, copying elements present in
5805 * the first range but not the second in order to the output range.
5806 * Iterators increment for each range. When the current element of the
5807 * first range is less than the second according to @a comp, that element
5808 * is copied and the iterator advances. If the current element of the
5809 * second range is less, no element is copied and the iterator advances.
5810 * If an element is contained in both ranges according to @a comp, no
5811 * elements are copied and both ranges advance. The output range may not
5812 * overlap either input range.
5814 template<typename _InputIterator1, typename _InputIterator2,
5815 typename _OutputIterator, typename _Compare>
5817 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5818 _InputIterator2 __first2, _InputIterator2 __last2,
5819 _OutputIterator __result, _Compare __comp)
5821 typedef typename iterator_traits<_InputIterator1>::value_type
5823 typedef typename iterator_traits<_InputIterator2>::value_type
5826 // concept requirements
5827 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5828 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5829 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5831 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5832 _ValueType1, _ValueType2>)
5833 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5834 _ValueType2, _ValueType1>)
5835 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5836 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5838 while (__first1 != __last1 && __first2 != __last2)
5839 if (__comp(*__first1, *__first2))
5841 *__result = *__first1;
5845 else if (__comp(*__first2, *__first1))
5852 return std::copy(__first1, __last1, __result);
5856 * @brief Return the symmetric difference of two sorted ranges.
5857 * @ingroup set_algorithms
5858 * @param first1 Start of first range.
5859 * @param last1 End of first range.
5860 * @param first2 Start of second range.
5861 * @param last2 End of second range.
5862 * @return End of the output range.
5863 * @ingroup set_algorithms
5865 * This operation iterates over both ranges, copying elements present in
5866 * one range but not the other in order to the output range. Iterators
5867 * increment for each range. When the current element of one range is less
5868 * than the other, that element is copied and the iterator advances. If an
5869 * element is contained in both ranges, no elements are copied and both
5870 * ranges advance. The output range may not overlap either input range.
5872 template<typename _InputIterator1, typename _InputIterator2,
5873 typename _OutputIterator>
5875 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5876 _InputIterator2 __first2, _InputIterator2 __last2,
5877 _OutputIterator __result)
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(_OutputIteratorConcept<_OutputIterator,
5891 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5892 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5893 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5894 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5896 while (__first1 != __last1 && __first2 != __last2)
5897 if (*__first1 < *__first2)
5899 *__result = *__first1;
5903 else if (*__first2 < *__first1)
5905 *__result = *__first2;
5914 return std::copy(__first2, __last2, std::copy(__first1,
5915 __last1, __result));
5919 * @brief Return the symmetric difference of two sorted ranges using
5920 * comparison functor.
5921 * @ingroup set_algorithms
5922 * @param first1 Start of first range.
5923 * @param last1 End of first range.
5924 * @param first2 Start of second range.
5925 * @param last2 End of second range.
5926 * @param comp The comparison functor.
5927 * @return End of the output range.
5928 * @ingroup set_algorithms
5930 * This operation iterates over both ranges, copying elements present in
5931 * one range but not the other in order to the output range. Iterators
5932 * increment for each range. When the current element of one range is less
5933 * than the other according to @a comp, that element is copied and the
5934 * iterator advances. If an element is contained in both ranges according
5935 * to @a comp, no elements are copied and both ranges advance. The output
5936 * range may not overlap either input range.
5938 template<typename _InputIterator1, typename _InputIterator2,
5939 typename _OutputIterator, typename _Compare>
5941 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5942 _InputIterator2 __first2, _InputIterator2 __last2,
5943 _OutputIterator __result,
5946 typedef typename iterator_traits<_InputIterator1>::value_type
5948 typedef typename iterator_traits<_InputIterator2>::value_type
5951 // concept requirements
5952 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5953 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5954 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5956 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5958 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5959 _ValueType1, _ValueType2>)
5960 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5961 _ValueType2, _ValueType1>)
5962 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5963 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5965 while (__first1 != __last1 && __first2 != __last2)
5966 if (__comp(*__first1, *__first2))
5968 *__result = *__first1;
5972 else if (__comp(*__first2, *__first1))
5974 *__result = *__first2;
5983 return std::copy(__first2, __last2,
5984 std::copy(__first1, __last1, __result));
5989 * @brief Return the minimum element in a range.
5990 * @ingroup sorting_algorithms
5991 * @param first Start of range.
5992 * @param last End of range.
5993 * @return Iterator referencing the first instance of the smallest value.
5995 template<typename _ForwardIterator>
5997 min_element(_ForwardIterator __first, _ForwardIterator __last)
5999 // concept requirements
6000 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6001 __glibcxx_function_requires(_LessThanComparableConcept<
6002 typename iterator_traits<_ForwardIterator>::value_type>)
6003 __glibcxx_requires_valid_range(__first, __last);
6005 if (__first == __last)
6007 _ForwardIterator __result = __first;
6008 while (++__first != __last)
6009 if (*__first < *__result)
6015 * @brief Return the minimum element in a range using comparison functor.
6016 * @ingroup sorting_algorithms
6017 * @param first Start of range.
6018 * @param last End of range.
6019 * @param comp Comparison functor.
6020 * @return Iterator referencing the first instance of the smallest value
6021 * according to comp.
6023 template<typename _ForwardIterator, typename _Compare>
6025 min_element(_ForwardIterator __first, _ForwardIterator __last,
6028 // concept requirements
6029 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6030 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6031 typename iterator_traits<_ForwardIterator>::value_type,
6032 typename iterator_traits<_ForwardIterator>::value_type>)
6033 __glibcxx_requires_valid_range(__first, __last);
6035 if (__first == __last)
6037 _ForwardIterator __result = __first;
6038 while (++__first != __last)
6039 if (__comp(*__first, *__result))
6045 * @brief Return the maximum element in a range.
6046 * @ingroup sorting_algorithms
6047 * @param first Start of range.
6048 * @param last End of range.
6049 * @return Iterator referencing the first instance of the largest value.
6051 template<typename _ForwardIterator>
6053 max_element(_ForwardIterator __first, _ForwardIterator __last)
6055 // concept requirements
6056 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6057 __glibcxx_function_requires(_LessThanComparableConcept<
6058 typename iterator_traits<_ForwardIterator>::value_type>)
6059 __glibcxx_requires_valid_range(__first, __last);
6061 if (__first == __last)
6063 _ForwardIterator __result = __first;
6064 while (++__first != __last)
6065 if (*__result < *__first)
6071 * @brief Return the maximum element in a range using comparison functor.
6072 * @ingroup sorting_algorithms
6073 * @param first Start of range.
6074 * @param last End of range.
6075 * @param comp Comparison functor.
6076 * @return Iterator referencing the first instance of the largest value
6077 * according to comp.
6079 template<typename _ForwardIterator, typename _Compare>
6081 max_element(_ForwardIterator __first, _ForwardIterator __last,
6084 // concept requirements
6085 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6086 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6087 typename iterator_traits<_ForwardIterator>::value_type,
6088 typename iterator_traits<_ForwardIterator>::value_type>)
6089 __glibcxx_requires_valid_range(__first, __last);
6091 if (__first == __last) return __first;
6092 _ForwardIterator __result = __first;
6093 while (++__first != __last)
6094 if (__comp(*__result, *__first))
6099 _GLIBCXX_END_NESTED_NAMESPACE
6101 #endif /* _STL_ALGO_H */