1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <cstdlib> // for rand
66 #include <bits/stl_heap.h>
67 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
68 #include <bits/algorithmfwd.h>
69 #include <debug/debug.h>
71 // See concept_check.h for the __glibcxx_*_requires macros.
73 _GLIBCXX_BEGIN_NAMESPACE(std)
76 * @brief Find the median of three values.
80 * @return One of @p a, @p b or @p c.
82 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
83 * then the value returned will be @c m.
84 * This is an SGI extension.
85 * @ingroup SGIextensions
87 template<typename _Tp>
89 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
91 // concept requirements
92 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
109 * @brief Find the median of three values using a predicate for comparison.
113 * @param comp A binary predicate.
114 * @return One of @p a, @p b or @p c.
116 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
117 * and @p comp(m,n) are both true then the value returned will be @c m.
118 * This is an SGI extension.
119 * @ingroup SGIextensions
121 template<typename _Tp, typename _Compare>
123 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
125 // concept requirements
126 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
127 if (__comp(__a, __b))
128 if (__comp(__b, __c))
130 else if (__comp(__a, __c))
134 else if (__comp(__a, __c))
136 else if (__comp(__b, __c))
146 * This is an overload used by find() for the Input Iterator case.
149 template<typename _InputIterator, typename _Tp>
150 inline _InputIterator
151 __find(_InputIterator __first, _InputIterator __last,
152 const _Tp& __val, input_iterator_tag)
154 while (__first != __last && !(*__first == __val))
161 * This is an overload used by find_if() for the Input Iterator case.
164 template<typename _InputIterator, typename _Predicate>
165 inline _InputIterator
166 __find_if(_InputIterator __first, _InputIterator __last,
167 _Predicate __pred, input_iterator_tag)
169 while (__first != __last && !bool(__pred(*__first)))
176 * This is an overload used by find() for the RAI case.
179 template<typename _RandomAccessIterator, typename _Tp>
180 _RandomAccessIterator
181 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
182 const _Tp& __val, random_access_iterator_tag)
184 typename iterator_traits<_RandomAccessIterator>::difference_type
185 __trip_count = (__last - __first) >> 2;
187 for (; __trip_count > 0; --__trip_count)
189 if (*__first == __val)
193 if (*__first == __val)
197 if (*__first == __val)
201 if (*__first == __val)
206 switch (__last - __first)
209 if (*__first == __val)
213 if (*__first == __val)
217 if (*__first == __val)
228 * This is an overload used by find_if() for the RAI case.
231 template<typename _RandomAccessIterator, typename _Predicate>
232 _RandomAccessIterator
233 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
234 _Predicate __pred, random_access_iterator_tag)
236 typename iterator_traits<_RandomAccessIterator>::difference_type
237 __trip_count = (__last - __first) >> 2;
239 for (; __trip_count > 0; --__trip_count)
241 if (__pred(*__first))
245 if (__pred(*__first))
249 if (__pred(*__first))
253 if (__pred(*__first))
258 switch (__last - __first)
261 if (__pred(*__first))
265 if (__pred(*__first))
269 if (__pred(*__first))
280 // set_symmetric_difference
293 * This is an uglified
294 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
295 * overloaded for forward iterators.
298 template<typename _ForwardIterator, typename _Integer, typename _Tp>
300 __search_n(_ForwardIterator __first, _ForwardIterator __last,
301 _Integer __count, const _Tp& __val,
302 std::forward_iterator_tag)
304 __first = _GLIBCXX_STD_P::find(__first, __last, __val);
305 while (__first != __last)
307 typename iterator_traits<_ForwardIterator>::difference_type
309 _ForwardIterator __i = __first;
311 while (__i != __last && __n != 1 && *__i == __val)
320 __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
327 * This is an uglified
328 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
329 * overloaded for random access iterators.
332 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
334 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
335 _Integer __count, const _Tp& __val,
336 std::random_access_iterator_tag)
339 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
342 _DistanceType __tailSize = __last - __first;
343 const _DistanceType __pattSize = __count;
345 if (__tailSize < __pattSize)
348 const _DistanceType __skipOffset = __pattSize - 1;
349 _RandomAccessIter __lookAhead = __first + __skipOffset;
350 __tailSize -= __pattSize;
352 while (1) // the main loop...
354 // __lookAhead here is always pointing to the last element of next
356 while (!(*__lookAhead == __val)) // the skip loop...
358 if (__tailSize < __pattSize)
359 return __last; // Failure
360 __lookAhead += __pattSize;
361 __tailSize -= __pattSize;
363 _DistanceType __remainder = __skipOffset;
364 for (_RandomAccessIter __backTrack = __lookAhead - 1;
365 *__backTrack == __val; --__backTrack)
367 if (--__remainder == 0)
368 return (__lookAhead - __skipOffset); // Success
370 if (__remainder > __tailSize)
371 return __last; // Failure
372 __lookAhead += __remainder;
373 __tailSize -= __remainder;
381 * This is an uglified
382 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
384 * overloaded for forward iterators.
387 template<typename _ForwardIterator, typename _Integer, typename _Tp,
388 typename _BinaryPredicate>
390 __search_n(_ForwardIterator __first, _ForwardIterator __last,
391 _Integer __count, const _Tp& __val,
392 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
394 while (__first != __last && !bool(__binary_pred(*__first, __val)))
397 while (__first != __last)
399 typename iterator_traits<_ForwardIterator>::difference_type
401 _ForwardIterator __i = __first;
403 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
413 while (__first != __last
414 && !bool(__binary_pred(*__first, __val)))
422 * This is an uglified
423 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
425 * overloaded for random access iterators.
428 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
429 typename _BinaryPredicate>
431 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
432 _Integer __count, const _Tp& __val,
433 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
436 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
439 _DistanceType __tailSize = __last - __first;
440 const _DistanceType __pattSize = __count;
442 if (__tailSize < __pattSize)
445 const _DistanceType __skipOffset = __pattSize - 1;
446 _RandomAccessIter __lookAhead = __first + __skipOffset;
447 __tailSize -= __pattSize;
449 while (1) // the main loop...
451 // __lookAhead here is always pointing to the last element of next
453 while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
455 if (__tailSize < __pattSize)
456 return __last; // Failure
457 __lookAhead += __pattSize;
458 __tailSize -= __pattSize;
460 _DistanceType __remainder = __skipOffset;
461 for (_RandomAccessIter __backTrack = __lookAhead - 1;
462 __binary_pred(*__backTrack, __val); --__backTrack)
464 if (--__remainder == 0)
465 return (__lookAhead - __skipOffset); // Success
467 if (__remainder > __tailSize)
468 return __last; // Failure
469 __lookAhead += __remainder;
470 __tailSize -= __remainder;
474 // find_end for forward iterators.
475 template<typename _ForwardIterator1, typename _ForwardIterator2>
477 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
478 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
479 forward_iterator_tag, forward_iterator_tag)
481 if (__first2 == __last2)
485 _ForwardIterator1 __result = __last1;
488 _ForwardIterator1 __new_result
489 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
490 if (__new_result == __last1)
494 __result = __new_result;
495 __first1 = __new_result;
502 template<typename _ForwardIterator1, typename _ForwardIterator2,
503 typename _BinaryPredicate>
505 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
506 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
507 forward_iterator_tag, forward_iterator_tag,
508 _BinaryPredicate __comp)
510 if (__first2 == __last2)
514 _ForwardIterator1 __result = __last1;
517 _ForwardIterator1 __new_result
518 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2, __comp);
519 if (__new_result == __last1)
523 __result = __new_result;
524 __first1 = __new_result;
531 // find_end for bidirectional iterators (much faster).
532 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
533 _BidirectionalIterator1
534 __find_end(_BidirectionalIterator1 __first1,
535 _BidirectionalIterator1 __last1,
536 _BidirectionalIterator2 __first2,
537 _BidirectionalIterator2 __last2,
538 bidirectional_iterator_tag, bidirectional_iterator_tag)
540 // concept requirements
541 __glibcxx_function_requires(_BidirectionalIteratorConcept<
542 _BidirectionalIterator1>)
543 __glibcxx_function_requires(_BidirectionalIteratorConcept<
544 _BidirectionalIterator2>)
546 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
547 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
549 _RevIterator1 __rlast1(__first1);
550 _RevIterator2 __rlast2(__first2);
551 _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1), __rlast1, _RevIterator2(__last2), __rlast2);
553 if (__rresult == __rlast1)
557 _BidirectionalIterator1 __result = __rresult.base();
558 std::advance(__result, -std::distance(__first2, __last2));
563 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
564 typename _BinaryPredicate>
565 _BidirectionalIterator1
566 __find_end(_BidirectionalIterator1 __first1,
567 _BidirectionalIterator1 __last1,
568 _BidirectionalIterator2 __first2,
569 _BidirectionalIterator2 __last2,
570 bidirectional_iterator_tag, bidirectional_iterator_tag,
571 _BinaryPredicate __comp)
573 // concept requirements
574 __glibcxx_function_requires(_BidirectionalIteratorConcept<
575 _BidirectionalIterator1>)
576 __glibcxx_function_requires(_BidirectionalIteratorConcept<
577 _BidirectionalIterator2>)
579 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
580 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
582 _RevIterator1 __rlast1(__first1);
583 _RevIterator2 __rlast2(__first2);
584 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
585 _RevIterator2(__last2), __rlast2,
588 if (__rresult == __rlast1)
592 _BidirectionalIterator1 __result = __rresult.base();
593 std::advance(__result, -std::distance(__first2, __last2));
599 * @brief Find last matching subsequence in a sequence.
600 * @param first1 Start of range to search.
601 * @param last1 End of range to search.
602 * @param first2 Start of sequence to match.
603 * @param last2 End of sequence to match.
604 * @return The last iterator @c i in the range
605 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
606 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
607 * such iterator exists.
609 * Searches the range @p [first1,last1) for a sub-sequence that compares
610 * equal value-by-value with the sequence given by @p [first2,last2) and
611 * returns an iterator to the first element of the sub-sequence, or
612 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
613 * last such subsequence contained in [first,last1).
615 * Because the sub-sequence must lie completely within the range
616 * @p [first1,last1) it must start at a position less than
617 * @p last1-(last2-first2) where @p last2-first2 is the length of the
619 * This means that the returned iterator @c i will be in the range
620 * @p [first1,last1-(last2-first2))
622 template<typename _ForwardIterator1, typename _ForwardIterator2>
623 inline _ForwardIterator1
624 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
625 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
627 // concept requirements
628 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
629 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
630 __glibcxx_function_requires(_EqualOpConcept<
631 typename iterator_traits<_ForwardIterator1>::value_type,
632 typename iterator_traits<_ForwardIterator2>::value_type>)
633 __glibcxx_requires_valid_range(__first1, __last1);
634 __glibcxx_requires_valid_range(__first2, __last2);
636 return std::__find_end(__first1, __last1, __first2, __last2,
637 std::__iterator_category(__first1),
638 std::__iterator_category(__first2));
642 * @brief Find last matching subsequence in a sequence using a predicate.
643 * @param first1 Start of range to search.
644 * @param last1 End of range to search.
645 * @param first2 Start of sequence to match.
646 * @param last2 End of sequence to match.
647 * @param comp The predicate to use.
648 * @return The last iterator @c i in the range
649 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
650 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
651 * @p last1 if no such iterator exists.
653 * Searches the range @p [first1,last1) for a sub-sequence that compares
654 * equal value-by-value with the sequence given by @p [first2,last2) using
655 * comp as a predicate and returns an iterator to the first element of the
656 * sub-sequence, or @p last1 if the sub-sequence is not found. The
657 * sub-sequence will be the last such subsequence contained in
660 * Because the sub-sequence must lie completely within the range
661 * @p [first1,last1) it must start at a position less than
662 * @p last1-(last2-first2) where @p last2-first2 is the length of the
664 * This means that the returned iterator @c i will be in the range
665 * @p [first1,last1-(last2-first2))
667 template<typename _ForwardIterator1, typename _ForwardIterator2,
668 typename _BinaryPredicate>
669 inline _ForwardIterator1
670 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
671 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
672 _BinaryPredicate __comp)
674 // concept requirements
675 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
676 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
677 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
678 typename iterator_traits<_ForwardIterator1>::value_type,
679 typename iterator_traits<_ForwardIterator2>::value_type>)
680 __glibcxx_requires_valid_range(__first1, __last1);
681 __glibcxx_requires_valid_range(__first2, __last2);
683 return std::__find_end(__first1, __last1, __first2, __last2,
684 std::__iterator_category(__first1),
685 std::__iterator_category(__first2),
691 * @brief Copy a sequence, removing elements of a given value.
692 * @param first An input iterator.
693 * @param last An input iterator.
694 * @param result An output iterator.
695 * @param value The value to be removed.
696 * @return An iterator designating the end of the resulting sequence.
698 * Copies each element in the range @p [first,last) not equal to @p value
699 * to the range beginning at @p result.
700 * remove_copy() is stable, so the relative order of elements that are
701 * copied is unchanged.
703 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
705 remove_copy(_InputIterator __first, _InputIterator __last,
706 _OutputIterator __result, const _Tp& __value)
708 // concept requirements
709 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
710 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
711 typename iterator_traits<_InputIterator>::value_type>)
712 __glibcxx_function_requires(_EqualOpConcept<
713 typename iterator_traits<_InputIterator>::value_type, _Tp>)
714 __glibcxx_requires_valid_range(__first, __last);
716 for (; __first != __last; ++__first)
717 if (!(*__first == __value))
719 *__result = *__first;
726 * @brief Copy a sequence, removing elements for which a predicate is true.
727 * @param first An input iterator.
728 * @param last An input iterator.
729 * @param result An output iterator.
730 * @param pred A predicate.
731 * @return An iterator designating the end of the resulting sequence.
733 * Copies each element in the range @p [first,last) for which
734 * @p pred returns true to the range beginning at @p result.
736 * remove_copy_if() is stable, so the relative order of elements that are
737 * copied is unchanged.
739 template<typename _InputIterator, typename _OutputIterator,
742 remove_copy_if(_InputIterator __first, _InputIterator __last,
743 _OutputIterator __result, _Predicate __pred)
745 // concept requirements
746 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
747 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
748 typename iterator_traits<_InputIterator>::value_type>)
749 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
750 typename iterator_traits<_InputIterator>::value_type>)
751 __glibcxx_requires_valid_range(__first, __last);
753 for (; __first != __last; ++__first)
754 if (!bool(__pred(*__first)))
756 *__result = *__first;
763 * @brief Remove elements from a sequence.
764 * @param first An input iterator.
765 * @param last An input iterator.
766 * @param value The value to be removed.
767 * @return An iterator designating the end of the resulting sequence.
769 * All elements equal to @p value are removed from the range
772 * remove() is stable, so the relative order of elements that are
773 * not removed is unchanged.
775 * Elements between the end of the resulting sequence and @p last
776 * are still present, but their value is unspecified.
778 template<typename _ForwardIterator, typename _Tp>
780 remove(_ForwardIterator __first, _ForwardIterator __last,
783 // concept requirements
784 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
786 __glibcxx_function_requires(_EqualOpConcept<
787 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
788 __glibcxx_requires_valid_range(__first, __last);
790 __first = _GLIBCXX_STD_P::find(__first, __last, __value);
791 _ForwardIterator __i = __first;
792 return __first == __last ? __first
793 : std::remove_copy(++__i, __last, __first, __value);
797 * @brief Remove elements from a sequence using a predicate.
798 * @param first A forward iterator.
799 * @param last A forward iterator.
800 * @param pred A predicate.
801 * @return An iterator designating the end of the resulting sequence.
803 * All elements for which @p pred returns true are removed from the range
806 * remove_if() is stable, so the relative order of elements that are
807 * not removed is unchanged.
809 * Elements between the end of the resulting sequence and @p last
810 * are still present, but their value is unspecified.
812 template<typename _ForwardIterator, typename _Predicate>
814 remove_if(_ForwardIterator __first, _ForwardIterator __last,
817 // concept requirements
818 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
820 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
821 typename iterator_traits<_ForwardIterator>::value_type>)
822 __glibcxx_requires_valid_range(__first, __last);
824 __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
825 _ForwardIterator __i = __first;
826 return __first == __last ? __first
827 : std::remove_copy_if(++__i, __last,
832 * @brief Remove consecutive duplicate values from a sequence.
833 * @param first A forward iterator.
834 * @param last A forward iterator.
835 * @return An iterator designating the end of the resulting sequence.
837 * Removes all but the first element from each group of consecutive
838 * values that compare equal.
839 * unique() is stable, so the relative order of elements that are
840 * not removed is unchanged.
841 * Elements between the end of the resulting sequence and @p last
842 * are still present, but their value is unspecified.
844 template<typename _ForwardIterator>
846 unique(_ForwardIterator __first, _ForwardIterator __last)
848 // concept requirements
849 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
851 __glibcxx_function_requires(_EqualityComparableConcept<
852 typename iterator_traits<_ForwardIterator>::value_type>)
853 __glibcxx_requires_valid_range(__first, __last);
855 // Skip the beginning, if already unique.
856 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
857 if (__first == __last)
860 // Do the real copy work.
861 _ForwardIterator __dest = __first;
863 while (++__first != __last)
864 if (!(*__dest == *__first))
865 *++__dest = *__first;
870 * @brief Remove consecutive values from a sequence using a predicate.
871 * @param first A forward iterator.
872 * @param last A forward iterator.
873 * @param binary_pred A binary predicate.
874 * @return An iterator designating the end of the resulting sequence.
876 * Removes all but the first element from each group of consecutive
877 * values for which @p binary_pred returns true.
878 * unique() is stable, so the relative order of elements that are
879 * not removed is unchanged.
880 * Elements between the end of the resulting sequence and @p last
881 * are still present, but their value is unspecified.
883 template<typename _ForwardIterator, typename _BinaryPredicate>
885 unique(_ForwardIterator __first, _ForwardIterator __last,
886 _BinaryPredicate __binary_pred)
888 // concept requirements
889 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
891 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
892 typename iterator_traits<_ForwardIterator>::value_type,
893 typename iterator_traits<_ForwardIterator>::value_type>)
894 __glibcxx_requires_valid_range(__first, __last);
896 // Skip the beginning, if already unique.
897 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
898 if (__first == __last)
901 // Do the real copy work.
902 _ForwardIterator __dest = __first;
904 while (++__first != __last)
905 if (!bool(__binary_pred(*__dest, *__first)))
906 *++__dest = *__first;
912 * This is an uglified unique_copy(_InputIterator, _InputIterator,
914 * overloaded for forward iterators and output iterator as result.
917 template<typename _ForwardIterator, typename _OutputIterator>
919 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
920 _OutputIterator __result,
921 forward_iterator_tag, output_iterator_tag)
923 // concept requirements -- taken care of in dispatching function
924 _ForwardIterator __next = __first;
925 *__result = *__first;
926 while (++__next != __last)
927 if (!(*__first == *__next))
930 *++__result = *__first;
937 * This is an uglified unique_copy(_InputIterator, _InputIterator,
939 * overloaded for input iterators and output iterator as result.
942 template<typename _InputIterator, typename _OutputIterator>
944 __unique_copy(_InputIterator __first, _InputIterator __last,
945 _OutputIterator __result,
946 input_iterator_tag, output_iterator_tag)
948 // concept requirements -- taken care of in dispatching function
949 typename iterator_traits<_InputIterator>::value_type __value = *__first;
951 while (++__first != __last)
952 if (!(__value == *__first))
955 *++__result = __value;
962 * This is an uglified unique_copy(_InputIterator, _InputIterator,
964 * overloaded for input iterators and forward iterator as result.
967 template<typename _InputIterator, typename _ForwardIterator>
969 __unique_copy(_InputIterator __first, _InputIterator __last,
970 _ForwardIterator __result,
971 input_iterator_tag, forward_iterator_tag)
973 // concept requirements -- taken care of in dispatching function
974 *__result = *__first;
975 while (++__first != __last)
976 if (!(*__result == *__first))
977 *++__result = *__first;
983 * This is an uglified
984 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
986 * overloaded for forward iterators and output iterator as result.
989 template<typename _ForwardIterator, typename _OutputIterator,
990 typename _BinaryPredicate>
992 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
993 _OutputIterator __result, _BinaryPredicate __binary_pred,
994 forward_iterator_tag, output_iterator_tag)
996 // concept requirements -- iterators already checked
997 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
998 typename iterator_traits<_ForwardIterator>::value_type,
999 typename iterator_traits<_ForwardIterator>::value_type>)
1001 _ForwardIterator __next = __first;
1002 *__result = *__first;
1003 while (++__next != __last)
1004 if (!bool(__binary_pred(*__first, *__next)))
1007 *++__result = *__first;
1014 * This is an uglified
1015 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1017 * overloaded for input iterators and output iterator as result.
1020 template<typename _InputIterator, typename _OutputIterator,
1021 typename _BinaryPredicate>
1023 __unique_copy(_InputIterator __first, _InputIterator __last,
1024 _OutputIterator __result, _BinaryPredicate __binary_pred,
1025 input_iterator_tag, output_iterator_tag)
1027 // concept requirements -- iterators already checked
1028 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1029 typename iterator_traits<_InputIterator>::value_type,
1030 typename iterator_traits<_InputIterator>::value_type>)
1032 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1033 *__result = __value;
1034 while (++__first != __last)
1035 if (!bool(__binary_pred(__value, *__first)))
1038 *++__result = __value;
1045 * This is an uglified
1046 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1048 * overloaded for input iterators and forward iterator as result.
1051 template<typename _InputIterator, typename _ForwardIterator,
1052 typename _BinaryPredicate>
1054 __unique_copy(_InputIterator __first, _InputIterator __last,
1055 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1056 input_iterator_tag, forward_iterator_tag)
1058 // concept requirements -- iterators already checked
1059 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1060 typename iterator_traits<_ForwardIterator>::value_type,
1061 typename iterator_traits<_InputIterator>::value_type>)
1063 *__result = *__first;
1064 while (++__first != __last)
1065 if (!bool(__binary_pred(*__result, *__first)))
1066 *++__result = *__first;
1072 * This is an uglified reverse(_BidirectionalIterator,
1073 * _BidirectionalIterator)
1074 * overloaded for bidirectional iterators.
1077 template<typename _BidirectionalIterator>
1079 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1080 bidirectional_iterator_tag)
1083 if (__first == __last || __first == --__last)
1087 std::iter_swap(__first, __last);
1094 * This is an uglified reverse(_BidirectionalIterator,
1095 * _BidirectionalIterator)
1096 * overloaded for random access iterators.
1099 template<typename _RandomAccessIterator>
1101 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1102 random_access_iterator_tag)
1104 if (__first == __last)
1107 while (__first < __last)
1109 std::iter_swap(__first, __last);
1116 * @brief Reverse a sequence.
1117 * @param first A bidirectional iterator.
1118 * @param last A bidirectional iterator.
1119 * @return reverse() returns no value.
1121 * Reverses the order of the elements in the range @p [first,last),
1122 * so that the first element becomes the last etc.
1123 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1124 * swaps @p *(first+i) and @p *(last-(i+1))
1126 template<typename _BidirectionalIterator>
1128 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1130 // concept requirements
1131 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1132 _BidirectionalIterator>)
1133 __glibcxx_requires_valid_range(__first, __last);
1134 std::__reverse(__first, __last, std::__iterator_category(__first));
1138 * @brief Copy a sequence, reversing its elements.
1139 * @param first A bidirectional iterator.
1140 * @param last A bidirectional iterator.
1141 * @param result An output iterator.
1142 * @return An iterator designating the end of the resulting sequence.
1144 * Copies the elements in the range @p [first,last) to the range
1145 * @p [result,result+(last-first)) such that the order of the
1146 * elements is reversed.
1147 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1148 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1149 * The ranges @p [first,last) and @p [result,result+(last-first))
1152 template<typename _BidirectionalIterator, typename _OutputIterator>
1154 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1155 _OutputIterator __result)
1157 // concept requirements
1158 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1159 _BidirectionalIterator>)
1160 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1161 typename iterator_traits<_BidirectionalIterator>::value_type>)
1162 __glibcxx_requires_valid_range(__first, __last);
1164 while (__first != __last)
1167 *__result = *__last;
1175 * This is a helper function for the rotate algorithm specialized on RAIs.
1176 * It returns the greatest common divisor of two integer values.
1179 template<typename _EuclideanRingElement>
1180 _EuclideanRingElement
1181 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1185 _EuclideanRingElement __t = __m % __n;
1194 * This is a helper function for the rotate algorithm.
1197 template<typename _ForwardIterator>
1199 __rotate(_ForwardIterator __first,
1200 _ForwardIterator __middle,
1201 _ForwardIterator __last,
1202 forward_iterator_tag)
1204 if (__first == __middle || __last == __middle)
1207 _ForwardIterator __first2 = __middle;
1210 swap(*__first, *__first2);
1213 if (__first == __middle)
1214 __middle = __first2;
1216 while (__first2 != __last);
1218 __first2 = __middle;
1220 while (__first2 != __last)
1222 swap(*__first, *__first2);
1225 if (__first == __middle)
1226 __middle = __first2;
1227 else if (__first2 == __last)
1228 __first2 = __middle;
1234 * This is a helper function for the rotate algorithm.
1237 template<typename _BidirectionalIterator>
1239 __rotate(_BidirectionalIterator __first,
1240 _BidirectionalIterator __middle,
1241 _BidirectionalIterator __last,
1242 bidirectional_iterator_tag)
1244 // concept requirements
1245 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1246 _BidirectionalIterator>)
1248 if (__first == __middle || __last == __middle)
1251 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1252 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1254 while (__first != __middle && __middle != __last)
1256 swap(*__first, *--__last);
1260 if (__first == __middle)
1261 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1263 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1268 * This is a helper function for the rotate algorithm.
1271 template<typename _RandomAccessIterator>
1273 __rotate(_RandomAccessIterator __first,
1274 _RandomAccessIterator __middle,
1275 _RandomAccessIterator __last,
1276 random_access_iterator_tag)
1278 // concept requirements
1279 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1280 _RandomAccessIterator>)
1282 if (__first == __middle || __last == __middle)
1285 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1287 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1290 const _Distance __n = __last - __first;
1291 const _Distance __k = __middle - __first;
1292 const _Distance __l = __n - __k;
1296 std::swap_ranges(__first, __middle, __middle);
1300 const _Distance __d = std::__gcd(__n, __k);
1302 for (_Distance __i = 0; __i < __d; __i++)
1304 _ValueType __tmp = *__first;
1305 _RandomAccessIterator __p = __first;
1309 for (_Distance __j = 0; __j < __l / __d; __j++)
1311 if (__p > __first + __l)
1313 *__p = *(__p - __l);
1317 *__p = *(__p + __k);
1323 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
1325 if (__p < __last - __k)
1327 *__p = *(__p + __k);
1330 *__p = * (__p - __l);
1341 * @brief Rotate the elements of a sequence.
1342 * @param first A forward iterator.
1343 * @param middle A forward iterator.
1344 * @param last A forward iterator.
1347 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1348 * positions so that the element at @p middle is moved to @p first, the
1349 * element at @p middle+1 is moved to @first+1 and so on for each element
1350 * in the range @p [first,last).
1352 * This effectively swaps the ranges @p [first,middle) and
1355 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1356 * each @p n in the range @p [0,last-first).
1358 template<typename _ForwardIterator>
1360 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1361 _ForwardIterator __last)
1363 // concept requirements
1364 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1366 __glibcxx_requires_valid_range(__first, __middle);
1367 __glibcxx_requires_valid_range(__middle, __last);
1369 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1371 std::__rotate(__first, __middle, __last, _IterType());
1375 * @brief Copy a sequence, rotating its elements.
1376 * @param first A forward iterator.
1377 * @param middle A forward iterator.
1378 * @param last A forward iterator.
1379 * @param result An output iterator.
1380 * @return An iterator designating the end of the resulting sequence.
1382 * Copies the elements of the range @p [first,last) to the range
1383 * beginning at @result, rotating the copied elements by @p (middle-first)
1384 * positions so that the element at @p middle is moved to @p result, the
1385 * element at @p middle+1 is moved to @result+1 and so on for each element
1386 * in the range @p [first,last).
1388 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1389 * each @p n in the range @p [0,last-first).
1391 template<typename _ForwardIterator, typename _OutputIterator>
1393 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1394 _ForwardIterator __last, _OutputIterator __result)
1396 // concept requirements
1397 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1398 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1399 typename iterator_traits<_ForwardIterator>::value_type>)
1400 __glibcxx_requires_valid_range(__first, __middle);
1401 __glibcxx_requires_valid_range(__middle, __last);
1403 return std::copy(__first, __middle,
1404 std::copy(__middle, __last, __result));
1409 * This is a helper function...
1412 template<typename _ForwardIterator, typename _Predicate>
1414 __partition(_ForwardIterator __first, _ForwardIterator __last,
1416 forward_iterator_tag)
1418 if (__first == __last)
1421 while (__pred(*__first))
1422 if (++__first == __last)
1425 _ForwardIterator __next = __first;
1427 while (++__next != __last)
1428 if (__pred(*__next))
1430 swap(*__first, *__next);
1439 * This is a helper function...
1442 template<typename _BidirectionalIterator, typename _Predicate>
1443 _BidirectionalIterator
1444 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1446 bidirectional_iterator_tag)
1451 if (__first == __last)
1453 else if (__pred(*__first))
1459 if (__first == __last)
1461 else if (!bool(__pred(*__last)))
1465 std::iter_swap(__first, __last);
1474 * This is a helper function...
1477 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1479 __inplace_stable_partition(_ForwardIterator __first,
1480 _ForwardIterator __last,
1481 _Predicate __pred, _Distance __len)
1484 return __pred(*__first) ? __last : __first;
1485 _ForwardIterator __middle = __first;
1486 std::advance(__middle, __len / 2);
1487 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1491 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1495 std::rotate(__begin, __middle, __end);
1496 std::advance(__begin, std::distance(__middle, __end));
1502 * This is a helper function...
1505 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1508 __stable_partition_adaptive(_ForwardIterator __first,
1509 _ForwardIterator __last,
1510 _Predicate __pred, _Distance __len,
1512 _Distance __buffer_size)
1514 if (__len <= __buffer_size)
1516 _ForwardIterator __result1 = __first;
1517 _Pointer __result2 = __buffer;
1518 for (; __first != __last; ++__first)
1519 if (__pred(*__first))
1521 *__result1 = *__first;
1526 *__result2 = *__first;
1529 std::copy(__buffer, __result2, __result1);
1534 _ForwardIterator __middle = __first;
1535 std::advance(__middle, __len / 2);
1536 _ForwardIterator __begin =
1537 std::__stable_partition_adaptive(__first, __middle, __pred,
1538 __len / 2, __buffer,
1540 _ForwardIterator __end =
1541 std::__stable_partition_adaptive(__middle, __last, __pred,
1543 __buffer, __buffer_size);
1544 std::rotate(__begin, __middle, __end);
1545 std::advance(__begin, std::distance(__middle, __end));
1551 * @brief Move elements for which a predicate is true to the beginning
1552 * of a sequence, preserving relative ordering.
1553 * @param first A forward iterator.
1554 * @param last A forward iterator.
1555 * @param pred A predicate functor.
1556 * @return An iterator @p middle such that @p pred(i) is true for each
1557 * iterator @p i in the range @p [first,middle) and false for each @p i
1558 * in the range @p [middle,last).
1560 * Performs the same function as @p partition() with the additional
1561 * guarantee that the relative ordering of elements in each group is
1562 * preserved, so any two elements @p x and @p y in the range
1563 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1564 * relative ordering after calling @p stable_partition().
1566 template<typename _ForwardIterator, typename _Predicate>
1568 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1571 // concept requirements
1572 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1574 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1575 typename iterator_traits<_ForwardIterator>::value_type>)
1576 __glibcxx_requires_valid_range(__first, __last);
1578 if (__first == __last)
1582 typedef typename iterator_traits<_ForwardIterator>::value_type
1584 typedef typename iterator_traits<_ForwardIterator>::difference_type
1587 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1589 if (__buf.size() > 0)
1591 std::__stable_partition_adaptive(__first, __last, __pred,
1592 _DistanceType(__buf.requested_size()),
1594 _DistanceType(__buf.size()));
1597 std::__inplace_stable_partition(__first, __last, __pred,
1598 _DistanceType(__buf.requested_size()));
1604 * This is a helper function for the sort routines.
1607 template<typename _RandomAccessIterator>
1609 __heap_select(_RandomAccessIterator __first,
1610 _RandomAccessIterator __middle,
1611 _RandomAccessIterator __last)
1613 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1616 std::make_heap(__first, __middle);
1617 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1618 if (*__i < *__first)
1619 std::__pop_heap(__first, __middle, __i, _ValueType(*__i));
1624 * This is a helper function for the sort routines.
1627 template<typename _RandomAccessIterator, typename _Compare>
1629 __heap_select(_RandomAccessIterator __first,
1630 _RandomAccessIterator __middle,
1631 _RandomAccessIterator __last, _Compare __comp)
1633 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1636 std::make_heap(__first, __middle, __comp);
1637 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1638 if (__comp(*__i, *__first))
1639 std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
1645 * @brief Copy the smallest elements of a sequence.
1646 * @param first An iterator.
1647 * @param last Another iterator.
1648 * @param result_first A random-access iterator.
1649 * @param result_last Another random-access iterator.
1650 * @return An iterator indicating the end of the resulting sequence.
1652 * Copies and sorts the smallest N values from the range @p [first,last)
1653 * to the range beginning at @p result_first, where the number of
1654 * elements to be copied, @p N, is the smaller of @p (last-first) and
1655 * @p (result_last-result_first).
1656 * After the sort if @p i and @j are iterators in the range
1657 * @p [result_first,result_first+N) such that @i precedes @j then
1658 * @p *j<*i is false.
1659 * The value returned is @p result_first+N.
1661 template<typename _InputIterator, typename _RandomAccessIterator>
1662 _RandomAccessIterator
1663 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1664 _RandomAccessIterator __result_first,
1665 _RandomAccessIterator __result_last)
1667 typedef typename iterator_traits<_InputIterator>::value_type
1669 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1671 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1674 // concept requirements
1675 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1676 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1678 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1680 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1681 __glibcxx_requires_valid_range(__first, __last);
1682 __glibcxx_requires_valid_range(__result_first, __result_last);
1684 if (__result_first == __result_last)
1685 return __result_last;
1686 _RandomAccessIterator __result_real_last = __result_first;
1687 while(__first != __last && __result_real_last != __result_last)
1689 *__result_real_last = *__first;
1690 ++__result_real_last;
1693 std::make_heap(__result_first, __result_real_last);
1694 while (__first != __last)
1696 if (*__first < *__result_first)
1697 std::__adjust_heap(__result_first, _DistanceType(0),
1698 _DistanceType(__result_real_last
1700 _InputValueType(*__first));
1703 std::sort_heap(__result_first, __result_real_last);
1704 return __result_real_last;
1708 * @brief Copy the smallest elements of a sequence using a predicate for
1710 * @param first An input iterator.
1711 * @param last Another input iterator.
1712 * @param result_first A random-access iterator.
1713 * @param result_last Another random-access iterator.
1714 * @param comp A comparison functor.
1715 * @return An iterator indicating the end of the resulting sequence.
1717 * Copies and sorts the smallest N values from the range @p [first,last)
1718 * to the range beginning at @p result_first, where the number of
1719 * elements to be copied, @p N, is the smaller of @p (last-first) and
1720 * @p (result_last-result_first).
1721 * After the sort if @p i and @j are iterators in the range
1722 * @p [result_first,result_first+N) such that @i precedes @j then
1723 * @p comp(*j,*i) is false.
1724 * The value returned is @p result_first+N.
1726 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
1727 _RandomAccessIterator
1728 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1729 _RandomAccessIterator __result_first,
1730 _RandomAccessIterator __result_last,
1733 typedef typename iterator_traits<_InputIterator>::value_type
1735 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1737 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1740 // concept requirements
1741 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1742 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1743 _RandomAccessIterator>)
1744 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1746 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1747 _InputValueType, _OutputValueType>)
1748 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1749 _OutputValueType, _OutputValueType>)
1750 __glibcxx_requires_valid_range(__first, __last);
1751 __glibcxx_requires_valid_range(__result_first, __result_last);
1753 if (__result_first == __result_last)
1754 return __result_last;
1755 _RandomAccessIterator __result_real_last = __result_first;
1756 while(__first != __last && __result_real_last != __result_last)
1758 *__result_real_last = *__first;
1759 ++__result_real_last;
1762 std::make_heap(__result_first, __result_real_last, __comp);
1763 while (__first != __last)
1765 if (__comp(*__first, *__result_first))
1766 std::__adjust_heap(__result_first, _DistanceType(0),
1767 _DistanceType(__result_real_last
1769 _InputValueType(*__first),
1773 std::sort_heap(__result_first, __result_real_last, __comp);
1774 return __result_real_last;
1779 * This is a helper function for the sort routine.
1782 template<typename _RandomAccessIterator, typename _Tp>
1784 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
1786 _RandomAccessIterator __next = __last;
1788 while (__val < *__next)
1799 * This is a helper function for the sort routine.
1802 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
1804 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
1807 _RandomAccessIterator __next = __last;
1809 while (__comp(__val, *__next))
1820 * This is a helper function for the sort routine.
1823 template<typename _RandomAccessIterator>
1825 __insertion_sort(_RandomAccessIterator __first,
1826 _RandomAccessIterator __last)
1828 if (__first == __last)
1831 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1833 typename iterator_traits<_RandomAccessIterator>::value_type
1835 if (__val < *__first)
1837 std::copy_backward(__first, __i, __i + 1);
1841 std::__unguarded_linear_insert(__i, __val);
1847 * This is a helper function for the sort routine.
1850 template<typename _RandomAccessIterator, typename _Compare>
1852 __insertion_sort(_RandomAccessIterator __first,
1853 _RandomAccessIterator __last, _Compare __comp)
1855 if (__first == __last) return;
1857 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1859 typename iterator_traits<_RandomAccessIterator>::value_type
1861 if (__comp(__val, *__first))
1863 std::copy_backward(__first, __i, __i + 1);
1867 std::__unguarded_linear_insert(__i, __val, __comp);
1873 * This is a helper function for the sort routine.
1876 template<typename _RandomAccessIterator>
1878 __unguarded_insertion_sort(_RandomAccessIterator __first,
1879 _RandomAccessIterator __last)
1881 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1884 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
1885 std::__unguarded_linear_insert(__i, _ValueType(*__i));
1890 * This is a helper function for the sort routine.
1893 template<typename _RandomAccessIterator, typename _Compare>
1895 __unguarded_insertion_sort(_RandomAccessIterator __first,
1896 _RandomAccessIterator __last, _Compare __comp)
1898 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1901 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
1902 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
1908 * This controls some aspect of the sort routines.
1911 enum { _S_threshold = 16 };
1915 * This is a helper function for the sort routine.
1918 template<typename _RandomAccessIterator>
1920 __final_insertion_sort(_RandomAccessIterator __first,
1921 _RandomAccessIterator __last)
1923 if (__last - __first > int(_S_threshold))
1925 std::__insertion_sort(__first, __first + int(_S_threshold));
1926 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
1929 std::__insertion_sort(__first, __last);
1934 * This is a helper function for the sort routine.
1937 template<typename _RandomAccessIterator, typename _Compare>
1939 __final_insertion_sort(_RandomAccessIterator __first,
1940 _RandomAccessIterator __last, _Compare __comp)
1942 if (__last - __first > int(_S_threshold))
1944 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
1945 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
1949 std::__insertion_sort(__first, __last, __comp);
1954 * This is a helper function...
1957 template<typename _RandomAccessIterator, typename _Tp>
1958 _RandomAccessIterator
1959 __unguarded_partition(_RandomAccessIterator __first,
1960 _RandomAccessIterator __last, _Tp __pivot)
1964 while (*__first < __pivot)
1967 while (__pivot < *__last)
1969 if (!(__first < __last))
1971 std::iter_swap(__first, __last);
1978 * This is a helper function...
1981 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
1982 _RandomAccessIterator
1983 __unguarded_partition(_RandomAccessIterator __first,
1984 _RandomAccessIterator __last,
1985 _Tp __pivot, _Compare __comp)
1989 while (__comp(*__first, __pivot))
1992 while (__comp(__pivot, *__last))
1994 if (!(__first < __last))
1996 std::iter_swap(__first, __last);
2003 * This is a helper function for the sort routine.
2006 template<typename _RandomAccessIterator, typename _Size>
2008 __introsort_loop(_RandomAccessIterator __first,
2009 _RandomAccessIterator __last,
2010 _Size __depth_limit)
2012 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2015 while (__last - __first > int(_S_threshold))
2017 if (__depth_limit == 0)
2019 _GLIBCXX_STD_P:partial_sort(__first, __last, __last);
2023 _RandomAccessIterator __cut =
2024 std::__unguarded_partition(__first, __last,
2025 _ValueType(std::__median(*__first,
2032 std::__introsort_loop(__cut, __last, __depth_limit);
2039 * This is a helper function for the sort routine.
2042 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2044 __introsort_loop(_RandomAccessIterator __first,
2045 _RandomAccessIterator __last,
2046 _Size __depth_limit, _Compare __comp)
2048 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2051 while (__last - __first > int(_S_threshold))
2053 if (__depth_limit == 0)
2055 _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
2059 _RandomAccessIterator __cut =
2060 std::__unguarded_partition(__first, __last,
2061 _ValueType(std::__median(*__first,
2069 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2076 * This is a helper function for the sort routines.
2079 template<typename _Size>
2084 for (__k = 0; __n != 1; __n >>= 1)
2091 template<typename _RandomAccessIterator, typename _Size>
2093 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2094 _RandomAccessIterator __last, _Size __depth_limit)
2096 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2099 while (__last - __first > 3)
2101 if (__depth_limit == 0)
2103 std::__heap_select(__first, __nth + 1, __last);
2104 // Place the nth largest element in its final position.
2105 std::iter_swap(__first, __nth);
2109 _RandomAccessIterator __cut =
2110 std::__unguarded_partition(__first, __last,
2111 _ValueType(std::__median(*__first,
2123 std::__insertion_sort(__first, __last);
2126 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2128 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2129 _RandomAccessIterator __last, _Size __depth_limit,
2132 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2135 while (__last - __first > 3)
2137 if (__depth_limit == 0)
2139 std::__heap_select(__first, __nth + 1, __last, __comp);
2140 // Place the nth largest element in its final position.
2141 std::iter_swap(__first, __nth);
2145 _RandomAccessIterator __cut =
2146 std::__unguarded_partition(__first, __last,
2147 _ValueType(std::__median(*__first,
2160 std::__insertion_sort(__first, __last, __comp);
2166 * @brief Finds the first position in which @a val could be inserted
2167 * without changing the ordering.
2168 * @param first An iterator.
2169 * @param last Another iterator.
2170 * @param val The search term.
2171 * @return An iterator pointing to the first element "not less
2172 * than" @a val, or end() if every element is less than
2174 * @ingroup binarysearch
2176 template<typename _ForwardIterator, typename _Tp>
2178 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2181 typedef typename iterator_traits<_ForwardIterator>::value_type
2183 typedef typename iterator_traits<_ForwardIterator>::difference_type
2186 // concept requirements
2187 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2188 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2189 __glibcxx_requires_partitioned(__first, __last, __val);
2191 _DistanceType __len = std::distance(__first, __last);
2192 _DistanceType __half;
2193 _ForwardIterator __middle;
2197 __half = __len >> 1;
2199 std::advance(__middle, __half);
2200 if (*__middle < __val)
2204 __len = __len - __half - 1;
2213 * @brief Finds the first position in which @a val could be inserted
2214 * without changing the ordering.
2215 * @param first An iterator.
2216 * @param last Another iterator.
2217 * @param val The search term.
2218 * @param comp A functor to use for comparisons.
2219 * @return An iterator pointing to the first element "not less than" @a val,
2220 * or end() if every element is less than @a val.
2221 * @ingroup binarysearch
2223 * The comparison function should have the same effects on ordering as
2224 * the function used for the initial sort.
2226 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2228 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2229 const _Tp& __val, _Compare __comp)
2231 typedef typename iterator_traits<_ForwardIterator>::value_type
2233 typedef typename iterator_traits<_ForwardIterator>::difference_type
2236 // concept requirements
2237 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2238 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2240 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2242 _DistanceType __len = std::distance(__first, __last);
2243 _DistanceType __half;
2244 _ForwardIterator __middle;
2248 __half = __len >> 1;
2250 std::advance(__middle, __half);
2251 if (__comp(*__middle, __val))
2255 __len = __len - __half - 1;
2264 * @brief Finds the last position in which @a val could be inserted
2265 * without changing the ordering.
2266 * @param first An iterator.
2267 * @param last Another iterator.
2268 * @param val The search term.
2269 * @return An iterator pointing to the first element greater than @a val,
2270 * or end() if no elements are greater than @a val.
2271 * @ingroup binarysearch
2273 template<typename _ForwardIterator, typename _Tp>
2275 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2278 typedef typename iterator_traits<_ForwardIterator>::value_type
2280 typedef typename iterator_traits<_ForwardIterator>::difference_type
2283 // concept requirements
2284 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2285 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2286 __glibcxx_requires_partitioned(__first, __last, __val);
2288 _DistanceType __len = std::distance(__first, __last);
2289 _DistanceType __half;
2290 _ForwardIterator __middle;
2294 __half = __len >> 1;
2296 std::advance(__middle, __half);
2297 if (__val < *__middle)
2303 __len = __len - __half - 1;
2310 * @brief Finds the last position in which @a val could be inserted
2311 * without changing the ordering.
2312 * @param first An iterator.
2313 * @param last Another iterator.
2314 * @param val The search term.
2315 * @param comp A functor to use for comparisons.
2316 * @return An iterator pointing to the first element greater than @a val,
2317 * or end() if no elements are greater than @a val.
2318 * @ingroup binarysearch
2320 * The comparison function should have the same effects on ordering as
2321 * the function used for the initial sort.
2323 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2325 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2326 const _Tp& __val, _Compare __comp)
2328 typedef typename iterator_traits<_ForwardIterator>::value_type
2330 typedef typename iterator_traits<_ForwardIterator>::difference_type
2333 // concept requirements
2334 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2335 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2337 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2339 _DistanceType __len = std::distance(__first, __last);
2340 _DistanceType __half;
2341 _ForwardIterator __middle;
2345 __half = __len >> 1;
2347 std::advance(__middle, __half);
2348 if (__comp(__val, *__middle))
2354 __len = __len - __half - 1;
2361 * @brief Finds the largest subrange in which @a val could be inserted
2362 * at any place in it without changing the ordering.
2363 * @param first An iterator.
2364 * @param last Another iterator.
2365 * @param val The search term.
2366 * @return An pair of iterators defining the subrange.
2367 * @ingroup binarysearch
2369 * This is equivalent to
2371 * std::make_pair(lower_bound(first, last, val),
2372 * upper_bound(first, last, val))
2374 * but does not actually call those functions.
2376 template<typename _ForwardIterator, typename _Tp>
2377 pair<_ForwardIterator, _ForwardIterator>
2378 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2381 typedef typename iterator_traits<_ForwardIterator>::value_type
2383 typedef typename iterator_traits<_ForwardIterator>::difference_type
2386 // concept requirements
2387 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2388 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2389 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2390 __glibcxx_requires_partitioned(__first, __last, __val);
2392 _DistanceType __len = std::distance(__first, __last);
2393 _DistanceType __half;
2394 _ForwardIterator __middle, __left, __right;
2398 __half = __len >> 1;
2400 std::advance(__middle, __half);
2401 if (*__middle < __val)
2405 __len = __len - __half - 1;
2407 else if (__val < *__middle)
2411 __left = std::lower_bound(__first, __middle, __val);
2412 std::advance(__first, __len);
2413 __right = std::upper_bound(++__middle, __first, __val);
2414 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2417 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2421 * @brief Finds the largest subrange in which @a val could be inserted
2422 * at any place in it without changing the ordering.
2423 * @param first An iterator.
2424 * @param last Another iterator.
2425 * @param val The search term.
2426 * @param comp A functor to use for comparisons.
2427 * @return An pair of iterators defining the subrange.
2428 * @ingroup binarysearch
2430 * This is equivalent to
2432 * std::make_pair(lower_bound(first, last, val, comp),
2433 * upper_bound(first, last, val, comp))
2435 * but does not actually call those functions.
2437 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2438 pair<_ForwardIterator, _ForwardIterator>
2439 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2443 typedef typename iterator_traits<_ForwardIterator>::value_type
2445 typedef typename iterator_traits<_ForwardIterator>::difference_type
2448 // concept requirements
2449 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2450 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2452 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2454 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2456 _DistanceType __len = std::distance(__first, __last);
2457 _DistanceType __half;
2458 _ForwardIterator __middle, __left, __right;
2462 __half = __len >> 1;
2464 std::advance(__middle, __half);
2465 if (__comp(*__middle, __val))
2469 __len = __len - __half - 1;
2471 else if (__comp(__val, *__middle))
2475 __left = std::lower_bound(__first, __middle, __val, __comp);
2476 std::advance(__first, __len);
2477 __right = std::upper_bound(++__middle, __first, __val, __comp);
2478 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2481 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2485 * @brief Determines whether an element exists in a range.
2486 * @param first An iterator.
2487 * @param last Another iterator.
2488 * @param val The search term.
2489 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2490 * @ingroup binarysearch
2492 * Note that this does not actually return an iterator to @a val. For
2493 * that, use std::find or a container's specialized find member functions.
2495 template<typename _ForwardIterator, typename _Tp>
2497 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2500 typedef typename iterator_traits<_ForwardIterator>::value_type
2503 // concept requirements
2504 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2505 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2506 __glibcxx_requires_partitioned(__first, __last, __val);
2508 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2509 return __i != __last && !(__val < *__i);
2513 * @brief Determines whether an element exists in a range.
2514 * @param first An iterator.
2515 * @param last Another iterator.
2516 * @param val The search term.
2517 * @param comp A functor to use for comparisons.
2518 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
2519 * @ingroup binarysearch
2521 * Note that this does not actually return an iterator to @a val. For
2522 * that, use std::find or a container's specialized find member functions.
2524 * The comparison function should have the same effects on ordering as
2525 * the function used for the initial sort.
2527 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2529 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2530 const _Tp& __val, _Compare __comp)
2532 typedef typename iterator_traits<_ForwardIterator>::value_type
2535 // concept requirements
2536 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2537 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2539 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2541 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2542 return __i != __last && !bool(__comp(__val, *__i));
2549 * This is a helper function for the merge routines.
2552 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2553 typename _BidirectionalIterator3>
2554 _BidirectionalIterator3
2555 __merge_backward(_BidirectionalIterator1 __first1,
2556 _BidirectionalIterator1 __last1,
2557 _BidirectionalIterator2 __first2,
2558 _BidirectionalIterator2 __last2,
2559 _BidirectionalIterator3 __result)
2561 if (__first1 == __last1)
2562 return std::copy_backward(__first2, __last2, __result);
2563 if (__first2 == __last2)
2564 return std::copy_backward(__first1, __last1, __result);
2569 if (*__last2 < *__last1)
2571 *--__result = *__last1;
2572 if (__first1 == __last1)
2573 return std::copy_backward(__first2, ++__last2, __result);
2578 *--__result = *__last2;
2579 if (__first2 == __last2)
2580 return std::copy_backward(__first1, ++__last1, __result);
2588 * This is a helper function for the merge routines.
2591 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2592 typename _BidirectionalIterator3, typename _Compare>
2593 _BidirectionalIterator3
2594 __merge_backward(_BidirectionalIterator1 __first1,
2595 _BidirectionalIterator1 __last1,
2596 _BidirectionalIterator2 __first2,
2597 _BidirectionalIterator2 __last2,
2598 _BidirectionalIterator3 __result,
2601 if (__first1 == __last1)
2602 return std::copy_backward(__first2, __last2, __result);
2603 if (__first2 == __last2)
2604 return std::copy_backward(__first1, __last1, __result);
2609 if (__comp(*__last2, *__last1))
2611 *--__result = *__last1;
2612 if (__first1 == __last1)
2613 return std::copy_backward(__first2, ++__last2, __result);
2618 *--__result = *__last2;
2619 if (__first2 == __last2)
2620 return std::copy_backward(__first1, ++__last1, __result);
2628 * This is a helper function for the merge routines.
2631 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2633 _BidirectionalIterator1
2634 __rotate_adaptive(_BidirectionalIterator1 __first,
2635 _BidirectionalIterator1 __middle,
2636 _BidirectionalIterator1 __last,
2637 _Distance __len1, _Distance __len2,
2638 _BidirectionalIterator2 __buffer,
2639 _Distance __buffer_size)
2641 _BidirectionalIterator2 __buffer_end;
2642 if (__len1 > __len2 && __len2 <= __buffer_size)
2644 __buffer_end = std::copy(__middle, __last, __buffer);
2645 std::copy_backward(__first, __middle, __last);
2646 return std::copy(__buffer, __buffer_end, __first);
2648 else if (__len1 <= __buffer_size)
2650 __buffer_end = std::copy(__first, __middle, __buffer);
2651 std::copy(__middle, __last, __first);
2652 return std::copy_backward(__buffer, __buffer_end, __last);
2656 std::rotate(__first, __middle, __last);
2657 std::advance(__first, std::distance(__middle, __last));
2664 * This is a helper function for the merge routines.
2667 template<typename _BidirectionalIterator, typename _Distance,
2670 __merge_adaptive(_BidirectionalIterator __first,
2671 _BidirectionalIterator __middle,
2672 _BidirectionalIterator __last,
2673 _Distance __len1, _Distance __len2,
2674 _Pointer __buffer, _Distance __buffer_size)
2676 if (__len1 <= __len2 && __len1 <= __buffer_size)
2678 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
2679 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
2682 else if (__len2 <= __buffer_size)
2684 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
2685 std::__merge_backward(__first, __middle, __buffer,
2686 __buffer_end, __last);
2690 _BidirectionalIterator __first_cut = __first;
2691 _BidirectionalIterator __second_cut = __middle;
2692 _Distance __len11 = 0;
2693 _Distance __len22 = 0;
2694 if (__len1 > __len2)
2696 __len11 = __len1 / 2;
2697 std::advance(__first_cut, __len11);
2698 __second_cut = std::lower_bound(__middle, __last,
2700 __len22 = std::distance(__middle, __second_cut);
2704 __len22 = __len2 / 2;
2705 std::advance(__second_cut, __len22);
2706 __first_cut = std::upper_bound(__first, __middle,
2708 __len11 = std::distance(__first, __first_cut);
2710 _BidirectionalIterator __new_middle =
2711 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2712 __len1 - __len11, __len22, __buffer,
2714 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2715 __len22, __buffer, __buffer_size);
2716 std::__merge_adaptive(__new_middle, __second_cut, __last,
2718 __len2 - __len22, __buffer, __buffer_size);
2724 * This is a helper function for the merge routines.
2727 template<typename _BidirectionalIterator, typename _Distance,
2728 typename _Pointer, typename _Compare>
2730 __merge_adaptive(_BidirectionalIterator __first,
2731 _BidirectionalIterator __middle,
2732 _BidirectionalIterator __last,
2733 _Distance __len1, _Distance __len2,
2734 _Pointer __buffer, _Distance __buffer_size,
2737 if (__len1 <= __len2 && __len1 <= __buffer_size)
2739 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
2740 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
2742 else if (__len2 <= __buffer_size)
2744 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
2745 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
2750 _BidirectionalIterator __first_cut = __first;
2751 _BidirectionalIterator __second_cut = __middle;
2752 _Distance __len11 = 0;
2753 _Distance __len22 = 0;
2754 if (__len1 > __len2)
2756 __len11 = __len1 / 2;
2757 std::advance(__first_cut, __len11);
2758 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2760 __len22 = std::distance(__middle, __second_cut);
2764 __len22 = __len2 / 2;
2765 std::advance(__second_cut, __len22);
2766 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2768 __len11 = std::distance(__first, __first_cut);
2770 _BidirectionalIterator __new_middle =
2771 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2772 __len1 - __len11, __len22, __buffer,
2774 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2775 __len22, __buffer, __buffer_size, __comp);
2776 std::__merge_adaptive(__new_middle, __second_cut, __last,
2778 __len2 - __len22, __buffer,
2779 __buffer_size, __comp);
2785 * This is a helper function for the merge routines.
2788 template<typename _BidirectionalIterator, typename _Distance>
2790 __merge_without_buffer(_BidirectionalIterator __first,
2791 _BidirectionalIterator __middle,
2792 _BidirectionalIterator __last,
2793 _Distance __len1, _Distance __len2)
2795 if (__len1 == 0 || __len2 == 0)
2797 if (__len1 + __len2 == 2)
2799 if (*__middle < *__first)
2800 std::iter_swap(__first, __middle);
2803 _BidirectionalIterator __first_cut = __first;
2804 _BidirectionalIterator __second_cut = __middle;
2805 _Distance __len11 = 0;
2806 _Distance __len22 = 0;
2807 if (__len1 > __len2)
2809 __len11 = __len1 / 2;
2810 std::advance(__first_cut, __len11);
2811 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
2812 __len22 = std::distance(__middle, __second_cut);
2816 __len22 = __len2 / 2;
2817 std::advance(__second_cut, __len22);
2818 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
2819 __len11 = std::distance(__first, __first_cut);
2821 std::rotate(__first_cut, __middle, __second_cut);
2822 _BidirectionalIterator __new_middle = __first_cut;
2823 std::advance(__new_middle, std::distance(__middle, __second_cut));
2824 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2826 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2827 __len1 - __len11, __len2 - __len22);
2832 * This is a helper function for the merge routines.
2835 template<typename _BidirectionalIterator, typename _Distance,
2838 __merge_without_buffer(_BidirectionalIterator __first,
2839 _BidirectionalIterator __middle,
2840 _BidirectionalIterator __last,
2841 _Distance __len1, _Distance __len2,
2844 if (__len1 == 0 || __len2 == 0)
2846 if (__len1 + __len2 == 2)
2848 if (__comp(*__middle, *__first))
2849 std::iter_swap(__first, __middle);
2852 _BidirectionalIterator __first_cut = __first;
2853 _BidirectionalIterator __second_cut = __middle;
2854 _Distance __len11 = 0;
2855 _Distance __len22 = 0;
2856 if (__len1 > __len2)
2858 __len11 = __len1 / 2;
2859 std::advance(__first_cut, __len11);
2860 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2862 __len22 = std::distance(__middle, __second_cut);
2866 __len22 = __len2 / 2;
2867 std::advance(__second_cut, __len22);
2868 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2870 __len11 = std::distance(__first, __first_cut);
2872 std::rotate(__first_cut, __middle, __second_cut);
2873 _BidirectionalIterator __new_middle = __first_cut;
2874 std::advance(__new_middle, std::distance(__middle, __second_cut));
2875 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2876 __len11, __len22, __comp);
2877 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2878 __len1 - __len11, __len2 - __len22, __comp);
2882 * @brief Merges two sorted ranges in place.
2883 * @param first An iterator.
2884 * @param middle Another iterator.
2885 * @param last Another iterator.
2888 * Merges two sorted and consecutive ranges, [first,middle) and
2889 * [middle,last), and puts the result in [first,last). The output will
2890 * be sorted. The sort is @e stable, that is, for equivalent
2891 * elements in the two ranges, elements from the first range will always
2892 * come before elements from the second.
2894 * If enough additional memory is available, this takes (last-first)-1
2895 * comparisons. Otherwise an NlogN algorithm is used, where N is
2896 * distance(first,last).
2898 template<typename _BidirectionalIterator>
2900 inplace_merge(_BidirectionalIterator __first,
2901 _BidirectionalIterator __middle,
2902 _BidirectionalIterator __last)
2904 typedef typename iterator_traits<_BidirectionalIterator>::value_type
2906 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
2909 // concept requirements
2910 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2911 _BidirectionalIterator>)
2912 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2913 __glibcxx_requires_sorted(__first, __middle);
2914 __glibcxx_requires_sorted(__middle, __last);
2916 if (__first == __middle || __middle == __last)
2919 _DistanceType __len1 = std::distance(__first, __middle);
2920 _DistanceType __len2 = std::distance(__middle, __last);
2922 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
2924 if (__buf.begin() == 0)
2925 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
2927 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
2928 __buf.begin(), _DistanceType(__buf.size()));
2932 * @brief Merges two sorted ranges in place.
2933 * @param first An iterator.
2934 * @param middle Another iterator.
2935 * @param last Another iterator.
2936 * @param comp A functor to use for comparisons.
2939 * Merges two sorted and consecutive ranges, [first,middle) and
2940 * [middle,last), and puts the result in [first,last). The output will
2941 * be sorted. The sort is @e stable, that is, for equivalent
2942 * elements in the two ranges, elements from the first range will always
2943 * come before elements from the second.
2945 * If enough additional memory is available, this takes (last-first)-1
2946 * comparisons. Otherwise an NlogN algorithm is used, where N is
2947 * distance(first,last).
2949 * The comparison function should have the same effects on ordering as
2950 * the function used for the initial sort.
2952 template<typename _BidirectionalIterator, typename _Compare>
2954 inplace_merge(_BidirectionalIterator __first,
2955 _BidirectionalIterator __middle,
2956 _BidirectionalIterator __last,
2959 typedef typename iterator_traits<_BidirectionalIterator>::value_type
2961 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
2964 // concept requirements
2965 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2966 _BidirectionalIterator>)
2967 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2968 _ValueType, _ValueType>)
2969 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
2970 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
2972 if (__first == __middle || __middle == __last)
2975 const _DistanceType __len1 = std::distance(__first, __middle);
2976 const _DistanceType __len2 = std::distance(__middle, __last);
2978 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
2980 if (__buf.begin() == 0)
2981 std::__merge_without_buffer(__first, __middle, __last, __len1,
2984 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
2985 __buf.begin(), _DistanceType(__buf.size()),
2989 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2992 __merge_sort_loop(_RandomAccessIterator1 __first,
2993 _RandomAccessIterator1 __last,
2994 _RandomAccessIterator2 __result,
2995 _Distance __step_size)
2997 const _Distance __two_step = 2 * __step_size;
2999 while (__last - __first >= __two_step)
3001 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3002 __first + __step_size, __first + __two_step,
3004 __first += __two_step;
3007 __step_size = std::min(_Distance(__last - __first), __step_size);
3008 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3009 __first + __step_size, __last,
3013 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3014 typename _Distance, typename _Compare>
3016 __merge_sort_loop(_RandomAccessIterator1 __first,
3017 _RandomAccessIterator1 __last,
3018 _RandomAccessIterator2 __result, _Distance __step_size,
3021 const _Distance __two_step = 2 * __step_size;
3023 while (__last - __first >= __two_step)
3025 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3026 __first + __step_size, __first + __two_step,
3029 __first += __two_step;
3031 __step_size = std::min(_Distance(__last - __first), __step_size);
3033 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3034 __first + __step_size, __last, __result, __comp);
3037 template<typename _RandomAccessIterator, typename _Distance>
3039 __chunk_insertion_sort(_RandomAccessIterator __first,
3040 _RandomAccessIterator __last,
3041 _Distance __chunk_size)
3043 while (__last - __first >= __chunk_size)
3045 std::__insertion_sort(__first, __first + __chunk_size);
3046 __first += __chunk_size;
3048 std::__insertion_sort(__first, __last);
3051 template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
3053 __chunk_insertion_sort(_RandomAccessIterator __first,
3054 _RandomAccessIterator __last,
3055 _Distance __chunk_size, _Compare __comp)
3057 while (__last - __first >= __chunk_size)
3059 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3060 __first += __chunk_size;
3062 std::__insertion_sort(__first, __last, __comp);
3065 enum { _S_chunk_size = 7 };
3067 template<typename _RandomAccessIterator, typename _Pointer>
3069 __merge_sort_with_buffer(_RandomAccessIterator __first,
3070 _RandomAccessIterator __last,
3073 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3076 const _Distance __len = __last - __first;
3077 const _Pointer __buffer_last = __buffer + __len;
3079 _Distance __step_size = _S_chunk_size;
3080 std::__chunk_insertion_sort(__first, __last, __step_size);
3082 while (__step_size < __len)
3084 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3086 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3091 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3093 __merge_sort_with_buffer(_RandomAccessIterator __first,
3094 _RandomAccessIterator __last,
3095 _Pointer __buffer, _Compare __comp)
3097 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3100 const _Distance __len = __last - __first;
3101 const _Pointer __buffer_last = __buffer + __len;
3103 _Distance __step_size = _S_chunk_size;
3104 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3106 while (__step_size < __len)
3108 std::__merge_sort_loop(__first, __last, __buffer,
3109 __step_size, __comp);
3111 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3112 __step_size, __comp);
3117 template<typename _RandomAccessIterator, typename _Pointer,
3120 __stable_sort_adaptive(_RandomAccessIterator __first,
3121 _RandomAccessIterator __last,
3122 _Pointer __buffer, _Distance __buffer_size)
3124 const _Distance __len = (__last - __first + 1) / 2;
3125 const _RandomAccessIterator __middle = __first + __len;
3126 if (__len > __buffer_size)
3128 std::__stable_sort_adaptive(__first, __middle,
3129 __buffer, __buffer_size);
3130 std::__stable_sort_adaptive(__middle, __last,
3131 __buffer, __buffer_size);
3135 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3136 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3138 std::__merge_adaptive(__first, __middle, __last,
3139 _Distance(__middle - __first),
3140 _Distance(__last - __middle),
3141 __buffer, __buffer_size);
3144 template<typename _RandomAccessIterator, typename _Pointer,
3145 typename _Distance, typename _Compare>
3147 __stable_sort_adaptive(_RandomAccessIterator __first,
3148 _RandomAccessIterator __last,
3149 _Pointer __buffer, _Distance __buffer_size,
3152 const _Distance __len = (__last - __first + 1) / 2;
3153 const _RandomAccessIterator __middle = __first + __len;
3154 if (__len > __buffer_size)
3156 std::__stable_sort_adaptive(__first, __middle, __buffer,
3157 __buffer_size, __comp);
3158 std::__stable_sort_adaptive(__middle, __last, __buffer,
3159 __buffer_size, __comp);
3163 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3164 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3166 std::__merge_adaptive(__first, __middle, __last,
3167 _Distance(__middle - __first),
3168 _Distance(__last - __middle),
3169 __buffer, __buffer_size,
3175 * This is a helper function for the stable sorting routines.
3178 template<typename _RandomAccessIterator>
3180 __inplace_stable_sort(_RandomAccessIterator __first,
3181 _RandomAccessIterator __last)
3183 if (__last - __first < 15)
3185 std::__insertion_sort(__first, __last);
3188 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3189 std::__inplace_stable_sort(__first, __middle);
3190 std::__inplace_stable_sort(__middle, __last);
3191 std::__merge_without_buffer(__first, __middle, __last,
3198 * This is a helper function for the stable sorting routines.
3201 template<typename _RandomAccessIterator, typename _Compare>
3203 __inplace_stable_sort(_RandomAccessIterator __first,
3204 _RandomAccessIterator __last, _Compare __comp)
3206 if (__last - __first < 15)
3208 std::__insertion_sort(__first, __last, __comp);
3211 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3212 std::__inplace_stable_sort(__first, __middle, __comp);
3213 std::__inplace_stable_sort(__middle, __last, __comp);
3214 std::__merge_without_buffer(__first, __middle, __last,
3222 // Set algorithms: includes, set_union, set_intersection, set_difference,
3223 // set_symmetric_difference. All of these algorithms have the precondition
3224 // that their input ranges are sorted and the postcondition that their output
3225 // ranges are sorted.
3228 * @brief Determines whether all elements of a sequence exists in a range.
3229 * @param first1 Start of search range.
3230 * @param last1 End of search range.
3231 * @param first2 Start of sequence
3232 * @param last2 End of sequence.
3233 * @return True if each element in [first2,last2) is contained in order
3234 * within [first1,last1). False otherwise.
3235 * @ingroup setoperations
3237 * This operation expects both [first1,last1) and [first2,last2) to be
3238 * sorted. Searches for the presence of each element in [first2,last2)
3239 * within [first1,last1). The iterators over each range only move forward,
3240 * so this is a linear algorithm. If an element in [first2,last2) is not
3241 * found before the search iterator reaches @a last2, false is returned.
3243 template<typename _InputIterator1, typename _InputIterator2>
3245 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3246 _InputIterator2 __first2, _InputIterator2 __last2)
3248 typedef typename iterator_traits<_InputIterator1>::value_type
3250 typedef typename iterator_traits<_InputIterator2>::value_type
3253 // concept requirements
3254 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3255 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3256 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3257 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3258 __glibcxx_requires_sorted(__first1, __last1);
3259 __glibcxx_requires_sorted(__first2, __last2);
3261 while (__first1 != __last1 && __first2 != __last2)
3262 if (*__first2 < *__first1)
3264 else if(*__first1 < *__first2)
3267 ++__first1, ++__first2;
3269 return __first2 == __last2;
3273 * @brief Determines whether all elements of a sequence exists in a range
3275 * @param first1 Start of search range.
3276 * @param last1 End of search range.
3277 * @param first2 Start of sequence
3278 * @param last2 End of sequence.
3279 * @param comp Comparison function to use.
3280 * @return True if each element in [first2,last2) is contained in order
3281 * within [first1,last1) according to comp. False otherwise.
3282 * @ingroup setoperations
3284 * This operation expects both [first1,last1) and [first2,last2) to be
3285 * sorted. Searches for the presence of each element in [first2,last2)
3286 * within [first1,last1), using comp to decide. The iterators over each
3287 * range only move forward, so this is a linear algorithm. If an element
3288 * in [first2,last2) is not found before the search iterator reaches @a
3289 * last2, false is returned.
3291 template<typename _InputIterator1, typename _InputIterator2,
3294 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3295 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
3297 typedef typename iterator_traits<_InputIterator1>::value_type
3299 typedef typename iterator_traits<_InputIterator2>::value_type
3302 // concept requirements
3303 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3304 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3305 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3306 _ValueType1, _ValueType2>)
3307 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3308 _ValueType2, _ValueType1>)
3309 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
3310 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
3312 while (__first1 != __last1 && __first2 != __last2)
3313 if (__comp(*__first2, *__first1))
3315 else if(__comp(*__first1, *__first2))
3318 ++__first1, ++__first2;
3320 return __first2 == __last2;
3329 // set_symmetric_difference
3334 * @brief Permute range into the next "dictionary" ordering.
3335 * @param first Start of range.
3336 * @param last End of range.
3337 * @return False if wrapped to first permutation, true otherwise.
3339 * Treats all permutations of the range as a set of "dictionary" sorted
3340 * sequences. Permutes the current sequence into the next one of this set.
3341 * Returns true if there are more sequences to generate. If the sequence
3342 * is the largest of the set, the smallest is generated and false returned.
3344 template<typename _BidirectionalIterator>
3346 next_permutation(_BidirectionalIterator __first,
3347 _BidirectionalIterator __last)
3349 // concept requirements
3350 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3351 _BidirectionalIterator>)
3352 __glibcxx_function_requires(_LessThanComparableConcept<
3353 typename iterator_traits<_BidirectionalIterator>::value_type>)
3354 __glibcxx_requires_valid_range(__first, __last);
3356 if (__first == __last)
3358 _BidirectionalIterator __i = __first;
3367 _BidirectionalIterator __ii = __i;
3371 _BidirectionalIterator __j = __last;
3372 while (!(*__i < *--__j))
3374 std::iter_swap(__i, __j);
3375 std::reverse(__ii, __last);
3380 std::reverse(__first, __last);
3387 * @brief Permute range into the next "dictionary" ordering using
3388 * comparison functor.
3389 * @param first Start of range.
3390 * @param last End of range.
3392 * @return False if wrapped to first permutation, true otherwise.
3394 * Treats all permutations of the range [first,last) as a set of
3395 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3396 * sequence into the next one of this set. Returns true if there are more
3397 * sequences to generate. If the sequence is the largest of the set, the
3398 * smallest is generated and false returned.
3400 template<typename _BidirectionalIterator, typename _Compare>
3402 next_permutation(_BidirectionalIterator __first,
3403 _BidirectionalIterator __last, _Compare __comp)
3405 // concept requirements
3406 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3407 _BidirectionalIterator>)
3408 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3409 typename iterator_traits<_BidirectionalIterator>::value_type,
3410 typename iterator_traits<_BidirectionalIterator>::value_type>)
3411 __glibcxx_requires_valid_range(__first, __last);
3413 if (__first == __last)
3415 _BidirectionalIterator __i = __first;
3424 _BidirectionalIterator __ii = __i;
3426 if (__comp(*__i, *__ii))
3428 _BidirectionalIterator __j = __last;
3429 while (!bool(__comp(*__i, *--__j)))
3431 std::iter_swap(__i, __j);
3432 std::reverse(__ii, __last);
3437 std::reverse(__first, __last);
3444 * @brief Permute range into the previous "dictionary" ordering.
3445 * @param first Start of range.
3446 * @param last End of range.
3447 * @return False if wrapped to last permutation, true otherwise.
3449 * Treats all permutations of the range as a set of "dictionary" sorted
3450 * sequences. Permutes the current sequence into the previous one of this
3451 * set. Returns true if there are more sequences to generate. If the
3452 * sequence is the smallest of the set, the largest is generated and false
3455 template<typename _BidirectionalIterator>
3457 prev_permutation(_BidirectionalIterator __first,
3458 _BidirectionalIterator __last)
3460 // concept requirements
3461 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3462 _BidirectionalIterator>)
3463 __glibcxx_function_requires(_LessThanComparableConcept<
3464 typename iterator_traits<_BidirectionalIterator>::value_type>)
3465 __glibcxx_requires_valid_range(__first, __last);
3467 if (__first == __last)
3469 _BidirectionalIterator __i = __first;
3478 _BidirectionalIterator __ii = __i;
3482 _BidirectionalIterator __j = __last;
3483 while (!(*--__j < *__i))
3485 std::iter_swap(__i, __j);
3486 std::reverse(__ii, __last);
3491 std::reverse(__first, __last);
3498 * @brief Permute range into the previous "dictionary" ordering using
3499 * comparison functor.
3500 * @param first Start of range.
3501 * @param last End of range.
3503 * @return False if wrapped to last permutation, true otherwise.
3505 * Treats all permutations of the range [first,last) as a set of
3506 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3507 * sequence into the previous one of this set. Returns true if there are
3508 * more sequences to generate. If the sequence is the smallest of the set,
3509 * the largest is generated and false returned.
3511 template<typename _BidirectionalIterator, typename _Compare>
3513 prev_permutation(_BidirectionalIterator __first,
3514 _BidirectionalIterator __last, _Compare __comp)
3516 // concept requirements
3517 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3518 _BidirectionalIterator>)
3519 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3520 typename iterator_traits<_BidirectionalIterator>::value_type,
3521 typename iterator_traits<_BidirectionalIterator>::value_type>)
3522 __glibcxx_requires_valid_range(__first, __last);
3524 if (__first == __last)
3526 _BidirectionalIterator __i = __first;
3535 _BidirectionalIterator __ii = __i;
3537 if (__comp(*__ii, *__i))
3539 _BidirectionalIterator __j = __last;
3540 while (!bool(__comp(*--__j, *__i)))
3542 std::iter_swap(__i, __j);
3543 std::reverse(__ii, __last);
3548 std::reverse(__first, __last);
3558 * @brief Copy a sequence, replacing each element of one value with another
3560 * @param first An input iterator.
3561 * @param last An input iterator.
3562 * @param result An output iterator.
3563 * @param old_value The value to be replaced.
3564 * @param new_value The replacement value.
3565 * @return The end of the output sequence, @p result+(last-first).
3567 * Copies each element in the input range @p [first,last) to the
3568 * output range @p [result,result+(last-first)) replacing elements
3569 * equal to @p old_value with @p new_value.
3571 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3573 replace_copy(_InputIterator __first, _InputIterator __last,
3574 _OutputIterator __result,
3575 const _Tp& __old_value, const _Tp& __new_value)
3577 // concept requirements
3578 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3579 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3580 typename iterator_traits<_InputIterator>::value_type>)
3581 __glibcxx_function_requires(_EqualOpConcept<
3582 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3583 __glibcxx_requires_valid_range(__first, __last);
3585 for (; __first != __last; ++__first, ++__result)
3586 if (*__first == __old_value)
3587 *__result = __new_value;
3589 *__result = *__first;
3594 * @brief Copy a sequence, replacing each value for which a predicate
3595 * returns true with another value.
3596 * @param first An input iterator.
3597 * @param last An input iterator.
3598 * @param result An output iterator.
3599 * @param pred A predicate.
3600 * @param new_value The replacement value.
3601 * @return The end of the output sequence, @p result+(last-first).
3603 * Copies each element in the range @p [first,last) to the range
3604 * @p [result,result+(last-first)) replacing elements for which
3605 * @p pred returns true with @p new_value.
3607 template<typename _InputIterator, typename _OutputIterator,
3608 typename _Predicate, typename _Tp>
3610 replace_copy_if(_InputIterator __first, _InputIterator __last,
3611 _OutputIterator __result,
3612 _Predicate __pred, const _Tp& __new_value)
3614 // concept requirements
3615 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3616 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3617 typename iterator_traits<_InputIterator>::value_type>)
3618 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3619 typename iterator_traits<_InputIterator>::value_type>)
3620 __glibcxx_requires_valid_range(__first, __last);
3622 for (; __first != __last; ++__first, ++__result)
3623 if (__pred(*__first))
3624 *__result = __new_value;
3626 *__result = *__first;
3630 _GLIBCXX_END_NAMESPACE
3632 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
3635 * @brief Apply a function to every element of a sequence.
3636 * @param first An input iterator.
3637 * @param last An input iterator.
3638 * @param f A unary function object.
3641 * Applies the function object @p f to each element in the range
3642 * @p [first,last). @p f must not modify the order of the sequence.
3643 * If @p f has a return value it is ignored.
3645 template<typename _InputIterator, typename _Function>
3647 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3649 // concept requirements
3650 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3651 __glibcxx_requires_valid_range(__first, __last);
3652 for (; __first != __last; ++__first)
3658 * @brief Find the first occurrence of a value in a sequence.
3659 * @param first An input iterator.
3660 * @param last An input iterator.
3661 * @param val The value to find.
3662 * @return The first iterator @c i in the range @p [first,last)
3663 * such that @c *i == @p val, or @p last if no such iterator exists.
3665 template<typename _InputIterator, typename _Tp>
3666 inline _InputIterator
3667 find(_InputIterator __first, _InputIterator __last,
3670 // concept requirements
3671 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3672 __glibcxx_function_requires(_EqualOpConcept<
3673 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3674 __glibcxx_requires_valid_range(__first, __last);
3675 return std::__find(__first, __last, __val,
3676 std::__iterator_category(__first));
3680 * @brief Find the first element in a sequence for which a
3681 * predicate is true.
3682 * @param first An input iterator.
3683 * @param last An input iterator.
3684 * @param pred A predicate.
3685 * @return The first iterator @c i in the range @p [first,last)
3686 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3688 template<typename _InputIterator, typename _Predicate>
3689 inline _InputIterator
3690 find_if(_InputIterator __first, _InputIterator __last,
3693 // concept requirements
3694 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3695 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3696 typename iterator_traits<_InputIterator>::value_type>)
3697 __glibcxx_requires_valid_range(__first, __last);
3698 return std::__find_if(__first, __last, __pred,
3699 std::__iterator_category(__first));
3703 * @brief Find element from a set in a sequence.
3704 * @param first1 Start of range to search.
3705 * @param last1 End of range to search.
3706 * @param first2 Start of match candidates.
3707 * @param last2 End of match candidates.
3708 * @return The first iterator @c i in the range
3709 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
3710 * interator in [first2,last2), or @p last1 if no such iterator exists.
3712 * Searches the range @p [first1,last1) for an element that is equal to
3713 * some element in the range [first2,last2). If found, returns an iterator
3714 * in the range [first1,last1), otherwise returns @p last1.
3716 template<typename _InputIterator, typename _ForwardIterator>
3718 find_first_of(_InputIterator __first1, _InputIterator __last1,
3719 _ForwardIterator __first2, _ForwardIterator __last2)
3721 // concept requirements
3722 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3723 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3724 __glibcxx_function_requires(_EqualOpConcept<
3725 typename iterator_traits<_InputIterator>::value_type,
3726 typename iterator_traits<_ForwardIterator>::value_type>)
3727 __glibcxx_requires_valid_range(__first1, __last1);
3728 __glibcxx_requires_valid_range(__first2, __last2);
3730 for (; __first1 != __last1; ++__first1)
3731 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3732 if (*__first1 == *__iter)
3738 * @brief Find element from a set in a sequence using a predicate.
3739 * @param first1 Start of range to search.
3740 * @param last1 End of range to search.
3741 * @param first2 Start of match candidates.
3742 * @param last2 End of match candidates.
3743 * @param comp Predicate to use.
3744 * @return The first iterator @c i in the range
3745 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
3746 * interator in [first2,last2), or @p last1 if no such iterator exists.
3749 * Searches the range @p [first1,last1) for an element that is
3750 * equal to some element in the range [first2,last2). If found,
3751 * returns an iterator in the range [first1,last1), otherwise
3754 template<typename _InputIterator, typename _ForwardIterator,
3755 typename _BinaryPredicate>
3757 find_first_of(_InputIterator __first1, _InputIterator __last1,
3758 _ForwardIterator __first2, _ForwardIterator __last2,
3759 _BinaryPredicate __comp)
3761 // concept requirements
3762 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3763 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3764 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3765 typename iterator_traits<_InputIterator>::value_type,
3766 typename iterator_traits<_ForwardIterator>::value_type>)
3767 __glibcxx_requires_valid_range(__first1, __last1);
3768 __glibcxx_requires_valid_range(__first2, __last2);
3770 for (; __first1 != __last1; ++__first1)
3771 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3772 if (__comp(*__first1, *__iter))
3778 * @brief Find two adjacent values in a sequence that are equal.
3779 * @param first A forward iterator.
3780 * @param last A forward iterator.
3781 * @return The first iterator @c i such that @c i and @c i+1 are both
3782 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
3783 * or @p last if no such iterator exists.
3785 template<typename _ForwardIterator>
3787 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
3789 // concept requirements
3790 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3791 __glibcxx_function_requires(_EqualityComparableConcept<
3792 typename iterator_traits<_ForwardIterator>::value_type>)
3793 __glibcxx_requires_valid_range(__first, __last);
3794 if (__first == __last)
3796 _ForwardIterator __next = __first;
3797 while(++__next != __last)
3799 if (*__first == *__next)
3807 * @brief Find two adjacent values in a sequence using a predicate.
3808 * @param first A forward iterator.
3809 * @param last A forward iterator.
3810 * @param binary_pred A binary predicate.
3811 * @return The first iterator @c i such that @c i and @c i+1 are both
3812 * valid iterators in @p [first,last) and such that
3813 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
3816 template<typename _ForwardIterator, typename _BinaryPredicate>
3818 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
3819 _BinaryPredicate __binary_pred)
3821 // concept requirements
3822 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3823 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3824 typename iterator_traits<_ForwardIterator>::value_type,
3825 typename iterator_traits<_ForwardIterator>::value_type>)
3826 __glibcxx_requires_valid_range(__first, __last);
3827 if (__first == __last)
3829 _ForwardIterator __next = __first;
3830 while(++__next != __last)
3832 if (__binary_pred(*__first, *__next))
3840 * @brief Count the number of copies of a value in a sequence.
3841 * @param first An input iterator.
3842 * @param last An input iterator.
3843 * @param value The value to be counted.
3844 * @return The number of iterators @c i in the range @p [first,last)
3845 * for which @c *i == @p value
3847 template<typename _InputIterator, typename _Tp>
3848 typename iterator_traits<_InputIterator>::difference_type
3849 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
3851 // concept requirements
3852 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3853 __glibcxx_function_requires(_EqualOpConcept<
3854 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3855 __glibcxx_requires_valid_range(__first, __last);
3856 typename iterator_traits<_InputIterator>::difference_type __n = 0;
3857 for (; __first != __last; ++__first)
3858 if (*__first == __value)
3864 * @brief Count the elements of a sequence for which a predicate is true.
3865 * @param first An input iterator.
3866 * @param last An input iterator.
3867 * @param pred A predicate.
3868 * @return The number of iterators @c i in the range @p [first,last)
3869 * for which @p pred(*i) is true.
3871 template<typename _InputIterator, typename _Predicate>
3872 typename iterator_traits<_InputIterator>::difference_type
3873 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3875 // concept requirements
3876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3877 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3878 typename iterator_traits<_InputIterator>::value_type>)
3879 __glibcxx_requires_valid_range(__first, __last);
3880 typename iterator_traits<_InputIterator>::difference_type __n = 0;
3881 for (; __first != __last; ++__first)
3882 if (__pred(*__first))
3888 * @brief Search a sequence for a matching sub-sequence.
3889 * @param first1 A forward iterator.
3890 * @param last1 A forward iterator.
3891 * @param first2 A forward iterator.
3892 * @param last2 A forward iterator.
3893 * @return The first iterator @c i in the range
3894 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
3895 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
3896 * such iterator exists.
3898 * Searches the range @p [first1,last1) for a sub-sequence that compares
3899 * equal value-by-value with the sequence given by @p [first2,last2) and
3900 * returns an iterator to the first element of the sub-sequence, or
3901 * @p last1 if the sub-sequence is not found.
3903 * Because the sub-sequence must lie completely within the range
3904 * @p [first1,last1) it must start at a position less than
3905 * @p last1-(last2-first2) where @p last2-first2 is the length of the
3907 * This means that the returned iterator @c i will be in the range
3908 * @p [first1,last1-(last2-first2))
3910 template<typename _ForwardIterator1, typename _ForwardIterator2>
3912 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3913 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3915 // concept requirements
3916 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3917 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3918 __glibcxx_function_requires(_EqualOpConcept<
3919 typename iterator_traits<_ForwardIterator1>::value_type,
3920 typename iterator_traits<_ForwardIterator2>::value_type>)
3921 __glibcxx_requires_valid_range(__first1, __last1);
3922 __glibcxx_requires_valid_range(__first2, __last2);
3924 // Test for empty ranges
3925 if (__first1 == __last1 || __first2 == __last2)
3928 // Test for a pattern of length 1.
3929 _ForwardIterator2 __p1(__first2);
3930 if (++__p1 == __last2)
3931 return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
3934 _ForwardIterator2 __p;
3935 _ForwardIterator1 __current = __first1;
3939 __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
3940 if (__first1 == __last1)
3944 __current = __first1;
3945 if (++__current == __last1)
3948 while (*__current == *__p)
3950 if (++__p == __last2)
3952 if (++__current == __last1)
3961 * @brief Search a sequence for a matching sub-sequence using a predicate.
3962 * @param first1 A forward iterator.
3963 * @param last1 A forward iterator.
3964 * @param first2 A forward iterator.
3965 * @param last2 A forward iterator.
3966 * @param predicate A binary predicate.
3967 * @return The first iterator @c i in the range
3968 * @p [first1,last1-(last2-first2)) such that
3969 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
3970 * @p [0,last2-first2), or @p last1 if no such iterator exists.
3972 * Searches the range @p [first1,last1) for a sub-sequence that compares
3973 * equal value-by-value with the sequence given by @p [first2,last2),
3974 * using @p predicate to determine equality, and returns an iterator
3975 * to the first element of the sub-sequence, or @p last1 if no such
3978 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
3980 template<typename _ForwardIterator1, typename _ForwardIterator2,
3981 typename _BinaryPredicate>
3983 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3984 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3985 _BinaryPredicate __predicate)
3987 // concept requirements
3988 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3989 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3990 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3991 typename iterator_traits<_ForwardIterator1>::value_type,
3992 typename iterator_traits<_ForwardIterator2>::value_type>)
3993 __glibcxx_requires_valid_range(__first1, __last1);
3994 __glibcxx_requires_valid_range(__first2, __last2);
3996 // Test for empty ranges
3997 if (__first1 == __last1 || __first2 == __last2)
4000 // Test for a pattern of length 1.
4001 _ForwardIterator2 __p1(__first2);
4002 if (++__p1 == __last2)
4004 while (__first1 != __last1
4005 && !bool(__predicate(*__first1, *__first2)))
4011 _ForwardIterator2 __p;
4012 _ForwardIterator1 __current = __first1;
4016 while (__first1 != __last1
4017 && !bool(__predicate(*__first1, *__first2)))
4019 if (__first1 == __last1)
4023 __current = __first1;
4024 if (++__current == __last1)
4027 while (__predicate(*__current, *__p))
4029 if (++__p == __last2)
4031 if (++__current == __last1)
4041 * @brief Search a sequence for a number of consecutive values.
4042 * @param first A forward iterator.
4043 * @param last A forward iterator.
4044 * @param count The number of consecutive values.
4045 * @param val The value to find.
4046 * @return The first iterator @c i in the range @p [first,last-count)
4047 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4048 * or @p last if no such iterator exists.
4050 * Searches the range @p [first,last) for @p count consecutive elements
4053 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4055 search_n(_ForwardIterator __first, _ForwardIterator __last,
4056 _Integer __count, const _Tp& __val)
4058 // concept requirements
4059 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4060 __glibcxx_function_requires(_EqualOpConcept<
4061 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4062 __glibcxx_requires_valid_range(__first, __last);
4067 return _GLIBCXX_STD_P::find(__first, __last, __val);
4068 return std::__search_n(__first, __last, __count, __val,
4069 std::__iterator_category(__first));
4074 * @brief Search a sequence for a number of consecutive values using a
4076 * @param first A forward iterator.
4077 * @param last A forward iterator.
4078 * @param count The number of consecutive values.
4079 * @param val The value to find.
4080 * @param binary_pred A binary predicate.
4081 * @return The first iterator @c i in the range @p [first,last-count)
4082 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4083 * range @p [0,count), or @p last if no such iterator exists.
4085 * Searches the range @p [first,last) for @p count consecutive elements
4086 * for which the predicate returns true.
4088 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4089 typename _BinaryPredicate>
4091 search_n(_ForwardIterator __first, _ForwardIterator __last,
4092 _Integer __count, const _Tp& __val,
4093 _BinaryPredicate __binary_pred)
4095 // concept requirements
4096 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4097 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4098 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4099 __glibcxx_requires_valid_range(__first, __last);
4105 while (__first != __last && !bool(__binary_pred(*__first, __val)))
4109 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4110 std::__iterator_category(__first));
4115 * @brief Perform an operation on a sequence.
4116 * @param first An input iterator.
4117 * @param last An input iterator.
4118 * @param result An output iterator.
4119 * @param unary_op A unary operator.
4120 * @return An output iterator equal to @p result+(last-first).
4122 * Applies the operator to each element in the input range and assigns
4123 * the results to successive elements of the output sequence.
4124 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4125 * range @p [0,last-first).
4127 * @p unary_op must not alter its argument.
4129 template<typename _InputIterator, typename _OutputIterator,
4130 typename _UnaryOperation>
4132 transform(_InputIterator __first, _InputIterator __last,
4133 _OutputIterator __result, _UnaryOperation __unary_op)
4135 // concept requirements
4136 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4137 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4138 // "the type returned by a _UnaryOperation"
4139 __typeof__(__unary_op(*__first))>)
4140 __glibcxx_requires_valid_range(__first, __last);
4142 for (; __first != __last; ++__first, ++__result)
4143 *__result = __unary_op(*__first);
4148 * @brief Perform an operation on corresponding elements of two sequences.
4149 * @param first1 An input iterator.
4150 * @param last1 An input iterator.
4151 * @param first2 An input iterator.
4152 * @param result An output iterator.
4153 * @param binary_op A binary operator.
4154 * @return An output iterator equal to @p result+(last-first).
4156 * Applies the operator to the corresponding elements in the two
4157 * input ranges and assigns the results to successive elements of the
4159 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4160 * @c N in the range @p [0,last1-first1).
4162 * @p binary_op must not alter either of its arguments.
4164 template<typename _InputIterator1, typename _InputIterator2,
4165 typename _OutputIterator, typename _BinaryOperation>
4167 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4168 _InputIterator2 __first2, _OutputIterator __result,
4169 _BinaryOperation __binary_op)
4171 // concept requirements
4172 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4173 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4174 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4175 // "the type returned by a _BinaryOperation"
4176 __typeof__(__binary_op(*__first1,*__first2))>)
4177 __glibcxx_requires_valid_range(__first1, __last1);
4179 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4180 *__result = __binary_op(*__first1, *__first2);
4185 * @brief Replace each occurrence of one value in a sequence with another
4187 * @param first A forward iterator.
4188 * @param last A forward iterator.
4189 * @param old_value The value to be replaced.
4190 * @param new_value The replacement value.
4191 * @return replace() returns no value.
4193 * For each iterator @c i in the range @p [first,last) if @c *i ==
4194 * @p old_value then the assignment @c *i = @p new_value is performed.
4196 template<typename _ForwardIterator, typename _Tp>
4198 replace(_ForwardIterator __first, _ForwardIterator __last,
4199 const _Tp& __old_value, const _Tp& __new_value)
4201 // concept requirements
4202 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4204 __glibcxx_function_requires(_EqualOpConcept<
4205 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4206 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4207 typename iterator_traits<_ForwardIterator>::value_type>)
4208 __glibcxx_requires_valid_range(__first, __last);
4210 for (; __first != __last; ++__first)
4211 if (*__first == __old_value)
4212 *__first = __new_value;
4216 * @brief Replace each value in a sequence for which a predicate returns
4217 * true with another value.
4218 * @param first A forward iterator.
4219 * @param last A forward iterator.
4220 * @param pred A predicate.
4221 * @param new_value The replacement value.
4222 * @return replace_if() returns no value.
4224 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4225 * is true then the assignment @c *i = @p new_value is performed.
4227 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4229 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4230 _Predicate __pred, const _Tp& __new_value)
4232 // concept requirements
4233 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4235 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4236 typename iterator_traits<_ForwardIterator>::value_type>)
4237 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4238 typename iterator_traits<_ForwardIterator>::value_type>)
4239 __glibcxx_requires_valid_range(__first, __last);
4241 for (; __first != __last; ++__first)
4242 if (__pred(*__first))
4243 *__first = __new_value;
4247 * @brief Assign the result of a function object to each value in a
4249 * @param first A forward iterator.
4250 * @param last A forward iterator.
4251 * @param gen A function object taking no arguments and returning
4252 * std::iterator_traits<_ForwardIterator>::value_type
4253 * @return generate() returns no value.
4255 * Performs the assignment @c *i = @p gen() for each @c i in the range
4258 template<typename _ForwardIterator, typename _Generator>
4260 generate(_ForwardIterator __first, _ForwardIterator __last,
4263 // concept requirements
4264 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4265 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4266 typename iterator_traits<_ForwardIterator>::value_type>)
4267 __glibcxx_requires_valid_range(__first, __last);
4269 for (; __first != __last; ++__first)
4274 * @brief Assign the result of a function object to each value in a
4276 * @param first A forward iterator.
4277 * @param n The length of the sequence.
4278 * @param gen A function object taking no arguments and returning
4279 * std::iterator_traits<_ForwardIterator>::value_type
4280 * @return The end of the sequence, @p first+n
4282 * Performs the assignment @c *i = @p gen() for each @c i in the range
4283 * @p [first,first+n).
4285 template<typename _OutputIterator, typename _Size, typename _Generator>
4287 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4289 // concept requirements
4290 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4291 // "the type returned by a _Generator"
4292 __typeof__(__gen())>)
4294 for (; __n > 0; --__n, ++__first)
4301 * @brief Copy a sequence, removing consecutive duplicate values.
4302 * @param first An input iterator.
4303 * @param last An input iterator.
4304 * @param result An output iterator.
4305 * @return An iterator designating the end of the resulting sequence.
4307 * Copies each element in the range @p [first,last) to the range
4308 * beginning at @p result, except that only the first element is copied
4309 * from groups of consecutive elements that compare equal.
4310 * unique_copy() is stable, so the relative order of elements that are
4311 * copied is unchanged.
4314 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4315 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4317 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4318 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4322 template<typename _InputIterator, typename _OutputIterator>
4323 inline _OutputIterator
4324 unique_copy(_InputIterator __first, _InputIterator __last,
4325 _OutputIterator __result)
4327 // concept requirements
4328 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4329 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4330 typename iterator_traits<_InputIterator>::value_type>)
4331 __glibcxx_function_requires(_EqualityComparableConcept<
4332 typename iterator_traits<_InputIterator>::value_type>)
4333 __glibcxx_requires_valid_range(__first, __last);
4335 if (__first == __last)
4337 return std::__unique_copy(__first, __last, __result,
4338 std::__iterator_category(__first),
4339 std::__iterator_category(__result));
4343 * @brief Copy a sequence, removing consecutive values using a predicate.
4344 * @param first An input iterator.
4345 * @param last An input iterator.
4346 * @param result An output iterator.
4347 * @param binary_pred A binary predicate.
4348 * @return An iterator designating the end of the resulting sequence.
4350 * Copies each element in the range @p [first,last) to the range
4351 * beginning at @p result, except that only the first element is copied
4352 * from groups of consecutive elements for which @p binary_pred returns
4354 * unique_copy() is stable, so the relative order of elements that are
4355 * copied is unchanged.
4358 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4359 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4362 template<typename _InputIterator, typename _OutputIterator,
4363 typename _BinaryPredicate>
4364 inline _OutputIterator
4365 unique_copy(_InputIterator __first, _InputIterator __last,
4366 _OutputIterator __result,
4367 _BinaryPredicate __binary_pred)
4369 // concept requirements -- predicates checked later
4370 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4371 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4372 typename iterator_traits<_InputIterator>::value_type>)
4373 __glibcxx_requires_valid_range(__first, __last);
4375 if (__first == __last)
4377 return std::__unique_copy(__first, __last, __result, __binary_pred,
4378 std::__iterator_category(__first),
4379 std::__iterator_category(__result));
4384 * @brief Randomly shuffle the elements of a sequence.
4385 * @param first A forward iterator.
4386 * @param last A forward iterator.
4389 * Reorder the elements in the range @p [first,last) using a random
4390 * distribution, so that every possible ordering of the sequence is
4393 template<typename _RandomAccessIterator>
4395 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4397 // concept requirements
4398 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4399 _RandomAccessIterator>)
4400 __glibcxx_requires_valid_range(__first, __last);
4402 if (__first != __last)
4403 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4404 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4408 * @brief Shuffle the elements of a sequence using a random number
4410 * @param first A forward iterator.
4411 * @param last A forward iterator.
4412 * @param rand The RNG functor or function.
4415 * Reorders the elements in the range @p [first,last) using @p rand to
4416 * provide a random distribution. Calling @p rand(N) for a positive
4417 * integer @p N should return a randomly chosen integer from the
4420 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4422 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4423 _RandomNumberGenerator& __rand)
4425 // concept requirements
4426 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4427 _RandomAccessIterator>)
4428 __glibcxx_requires_valid_range(__first, __last);
4430 if (__first == __last)
4432 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4433 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
4438 * @brief Move elements for which a predicate is true to the beginning
4440 * @param first A forward iterator.
4441 * @param last A forward iterator.
4442 * @param pred A predicate functor.
4443 * @return An iterator @p middle such that @p pred(i) is true for each
4444 * iterator @p i in the range @p [first,middle) and false for each @p i
4445 * in the range @p [middle,last).
4447 * @p pred must not modify its operand. @p partition() does not preserve
4448 * the relative ordering of elements in each group, use
4449 * @p stable_partition() if this is needed.
4451 template<typename _ForwardIterator, typename _Predicate>
4452 inline _ForwardIterator
4453 partition(_ForwardIterator __first, _ForwardIterator __last,
4456 // concept requirements
4457 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4459 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4460 typename iterator_traits<_ForwardIterator>::value_type>)
4461 __glibcxx_requires_valid_range(__first, __last);
4463 return std::__partition(__first, __last, __pred,
4464 std::__iterator_category(__first));
4470 * @brief Sort the smallest elements of a sequence.
4471 * @param first An iterator.
4472 * @param middle Another iterator.
4473 * @param last Another iterator.
4476 * Sorts the smallest @p (middle-first) elements in the range
4477 * @p [first,last) and moves them to the range @p [first,middle). The
4478 * order of the remaining elements in the range @p [middle,last) is
4480 * After the sort if @p i and @j are iterators in the range
4481 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4482 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4484 template<typename _RandomAccessIterator>
4486 partial_sort(_RandomAccessIterator __first,
4487 _RandomAccessIterator __middle,
4488 _RandomAccessIterator __last)
4490 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4493 // concept requirements
4494 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4495 _RandomAccessIterator>)
4496 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4497 __glibcxx_requires_valid_range(__first, __middle);
4498 __glibcxx_requires_valid_range(__middle, __last);
4500 std::__heap_select(__first, __middle, __last);
4501 std::sort_heap(__first, __middle);
4505 * @brief Sort the smallest elements of a sequence using a predicate
4507 * @param first An iterator.
4508 * @param middle Another iterator.
4509 * @param last Another iterator.
4510 * @param comp A comparison functor.
4513 * Sorts the smallest @p (middle-first) elements in the range
4514 * @p [first,last) and moves them to the range @p [first,middle). The
4515 * order of the remaining elements in the range @p [middle,last) is
4517 * After the sort if @p i and @j are iterators in the range
4518 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4519 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4522 template<typename _RandomAccessIterator, typename _Compare>
4524 partial_sort(_RandomAccessIterator __first,
4525 _RandomAccessIterator __middle,
4526 _RandomAccessIterator __last,
4529 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4532 // concept requirements
4533 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4534 _RandomAccessIterator>)
4535 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4536 _ValueType, _ValueType>)
4537 __glibcxx_requires_valid_range(__first, __middle);
4538 __glibcxx_requires_valid_range(__middle, __last);
4540 std::__heap_select(__first, __middle, __last, __comp);
4541 std::sort_heap(__first, __middle, __comp);
4545 * @brief Sort a sequence just enough to find a particular position.
4546 * @param first An iterator.
4547 * @param nth Another iterator.
4548 * @param last Another iterator.
4551 * Rearranges the elements in the range @p [first,last) so that @p *nth
4552 * is the same element that would have been in that position had the
4553 * whole sequence been sorted.
4554 * whole sequence been sorted. The elements either side of @p *nth are
4555 * not completely sorted, but for any iterator @i in the range
4556 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4557 * holds that @p *j<*i is false.
4559 template<typename _RandomAccessIterator>
4561 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4562 _RandomAccessIterator __last)
4564 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4567 // concept requirements
4568 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4569 _RandomAccessIterator>)
4570 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4571 __glibcxx_requires_valid_range(__first, __nth);
4572 __glibcxx_requires_valid_range(__nth, __last);
4574 if (__first == __last || __nth == __last)
4577 std::__introselect(__first, __nth, __last,
4578 std::__lg(__last - __first) * 2);
4582 * @brief Sort a sequence just enough to find a particular position
4583 * using a predicate for comparison.
4584 * @param first An iterator.
4585 * @param nth Another iterator.
4586 * @param last Another iterator.
4587 * @param comp A comparison functor.
4590 * Rearranges the elements in the range @p [first,last) so that @p *nth
4591 * is the same element that would have been in that position had the
4592 * whole sequence been sorted. The elements either side of @p *nth are
4593 * not completely sorted, but for any iterator @i in the range
4594 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4595 * holds that @p comp(*j,*i) is false.
4597 template<typename _RandomAccessIterator, typename _Compare>
4599 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4600 _RandomAccessIterator __last, _Compare __comp)
4602 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4605 // concept requirements
4606 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4607 _RandomAccessIterator>)
4608 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4609 _ValueType, _ValueType>)
4610 __glibcxx_requires_valid_range(__first, __nth);
4611 __glibcxx_requires_valid_range(__nth, __last);
4613 if (__first == __last || __nth == __last)
4616 std::__introselect(__first, __nth, __last,
4617 std::__lg(__last - __first) * 2, __comp);
4622 * @brief Sort the elements of a sequence.
4623 * @param first An iterator.
4624 * @param last Another iterator.
4627 * Sorts the elements in the range @p [first,last) in ascending order,
4628 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4629 * @p [first,last-1).
4631 * The relative ordering of equivalent elements is not preserved, use
4632 * @p stable_sort() if this is needed.
4634 template<typename _RandomAccessIterator>
4636 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4638 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4641 // concept requirements
4642 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4643 _RandomAccessIterator>)
4644 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4645 __glibcxx_requires_valid_range(__first, __last);
4647 if (__first != __last)
4649 std::__introsort_loop(__first, __last,
4650 std::__lg(__last - __first) * 2);
4651 std::__final_insertion_sort(__first, __last);
4656 * @brief Sort the elements of a sequence using a predicate for comparison.
4657 * @param first An iterator.
4658 * @param last Another iterator.
4659 * @param comp A comparison functor.
4662 * Sorts the elements in the range @p [first,last) in ascending order,
4663 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4664 * range @p [first,last-1).
4666 * The relative ordering of equivalent elements is not preserved, use
4667 * @p stable_sort() if this is needed.
4669 template<typename _RandomAccessIterator, typename _Compare>
4671 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4674 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4677 // concept requirements
4678 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4679 _RandomAccessIterator>)
4680 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
4682 __glibcxx_requires_valid_range(__first, __last);
4684 if (__first != __last)
4686 std::__introsort_loop(__first, __last,
4687 std::__lg(__last - __first) * 2, __comp);
4688 std::__final_insertion_sort(__first, __last, __comp);
4693 * @brief Merges two sorted ranges.
4694 * @param first1 An iterator.
4695 * @param first2 Another iterator.
4696 * @param last1 Another iterator.
4697 * @param last2 Another iterator.
4698 * @param result An iterator pointing to the end of the merged range.
4699 * @return An iterator pointing to the first element "not less
4702 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4703 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4704 * must be sorted, and the output range must not overlap with either of
4705 * the input ranges. The sort is @e stable, that is, for equivalent
4706 * elements in the two ranges, elements from the first range will always
4707 * come before elements from the second.
4709 template<typename _InputIterator1, typename _InputIterator2,
4710 typename _OutputIterator>
4712 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4713 _InputIterator2 __first2, _InputIterator2 __last2,
4714 _OutputIterator __result)
4716 typedef typename iterator_traits<_InputIterator1>::value_type
4718 typedef typename iterator_traits<_InputIterator2>::value_type
4721 // concept requirements
4722 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4723 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4724 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4726 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4728 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4729 __glibcxx_requires_sorted(__first1, __last1);
4730 __glibcxx_requires_sorted(__first2, __last2);
4732 while (__first1 != __last1 && __first2 != __last2)
4734 if (*__first2 < *__first1)
4736 *__result = *__first2;
4741 *__result = *__first1;
4746 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4751 * @brief Merges two sorted ranges.
4752 * @param first1 An iterator.
4753 * @param first2 Another iterator.
4754 * @param last1 Another iterator.
4755 * @param last2 Another iterator.
4756 * @param result An iterator pointing to the end of the merged range.
4757 * @param comp A functor to use for comparisons.
4758 * @return An iterator pointing to the first element "not less
4761 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
4762 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
4763 * must be sorted, and the output range must not overlap with either of
4764 * the input ranges. The sort is @e stable, that is, for equivalent
4765 * elements in the two ranges, elements from the first range will always
4766 * come before elements from the second.
4768 * The comparison function should have the same effects on ordering as
4769 * the function used for the initial sort.
4771 template<typename _InputIterator1, typename _InputIterator2,
4772 typename _OutputIterator, typename _Compare>
4774 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4775 _InputIterator2 __first2, _InputIterator2 __last2,
4776 _OutputIterator __result, _Compare __comp)
4778 typedef typename iterator_traits<_InputIterator1>::value_type
4780 typedef typename iterator_traits<_InputIterator2>::value_type
4783 // concept requirements
4784 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4785 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4786 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4788 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4790 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4791 _ValueType2, _ValueType1>)
4792 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4793 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4795 while (__first1 != __last1 && __first2 != __last2)
4797 if (__comp(*__first2, *__first1))
4799 *__result = *__first2;
4804 *__result = *__first1;
4809 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4815 * @brief Sort the elements of a sequence, preserving the relative order
4816 * of equivalent elements.
4817 * @param first An iterator.
4818 * @param last Another iterator.
4821 * Sorts the elements in the range @p [first,last) in ascending order,
4822 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4823 * @p [first,last-1).
4825 * The relative ordering of equivalent elements is preserved, so any two
4826 * elements @p x and @p y in the range @p [first,last) such that
4827 * @p x<y is false and @p y<x is false will have the same relative
4828 * ordering after calling @p stable_sort().
4830 template<typename _RandomAccessIterator>
4832 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4834 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4836 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4839 // concept requirements
4840 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4841 _RandomAccessIterator>)
4842 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4843 __glibcxx_requires_valid_range(__first, __last);
4845 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
4847 if (__buf.begin() == 0)
4848 std::__inplace_stable_sort(__first, __last);
4850 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
4851 _DistanceType(__buf.size()));
4855 * @brief Sort the elements of a sequence using a predicate for comparison,
4856 * preserving the relative order of equivalent elements.
4857 * @param first An iterator.
4858 * @param last Another iterator.
4859 * @param comp A comparison functor.
4862 * Sorts the elements in the range @p [first,last) in ascending order,
4863 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4864 * range @p [first,last-1).
4866 * The relative ordering of equivalent elements is preserved, so any two
4867 * elements @p x and @p y in the range @p [first,last) such that
4868 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
4869 * relative ordering after calling @p stable_sort().
4871 template<typename _RandomAccessIterator, typename _Compare>
4873 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4876 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4878 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4881 // concept requirements
4882 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4883 _RandomAccessIterator>)
4884 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4887 __glibcxx_requires_valid_range(__first, __last);
4889 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
4891 if (__buf.begin() == 0)
4892 std::__inplace_stable_sort(__first, __last, __comp);
4894 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
4895 _DistanceType(__buf.size()), __comp);
4900 * @brief Return the union of two sorted ranges.
4901 * @param first1 Start of first range.
4902 * @param last1 End of first range.
4903 * @param first2 Start of second range.
4904 * @param last2 End of second range.
4905 * @return End of the output range.
4906 * @ingroup setoperations
4908 * This operation iterates over both ranges, copying elements present in
4909 * each range in order to the output range. Iterators increment for each
4910 * range. When the current element of one range is less than the other,
4911 * that element is copied and the iterator advanced. If an element is
4912 * contained in both ranges, the element from the first range is copied and
4913 * both ranges advance. The output range may not overlap either input
4916 template<typename _InputIterator1, typename _InputIterator2,
4917 typename _OutputIterator>
4919 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4920 _InputIterator2 __first2, _InputIterator2 __last2,
4921 _OutputIterator __result)
4923 typedef typename iterator_traits<_InputIterator1>::value_type
4925 typedef typename iterator_traits<_InputIterator2>::value_type
4928 // concept requirements
4929 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4930 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4931 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4933 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4935 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4936 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4937 __glibcxx_requires_sorted(__first1, __last1);
4938 __glibcxx_requires_sorted(__first2, __last2);
4940 while (__first1 != __last1 && __first2 != __last2)
4942 if (*__first1 < *__first2)
4944 *__result = *__first1;
4947 else if (*__first2 < *__first1)
4949 *__result = *__first2;
4954 *__result = *__first1;
4960 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4965 * @brief Return the union of two sorted ranges using a comparison functor.
4966 * @param first1 Start of first range.
4967 * @param last1 End of first range.
4968 * @param first2 Start of second range.
4969 * @param last2 End of second range.
4970 * @param comp The comparison functor.
4971 * @return End of the output range.
4972 * @ingroup setoperations
4974 * This operation iterates over both ranges, copying elements present in
4975 * each range in order to the output range. Iterators increment for each
4976 * range. When the current element of one range is less than the other
4977 * according to @a comp, that element is copied and the iterator advanced.
4978 * If an equivalent element according to @a comp is contained in both
4979 * ranges, the element from the first range is copied and both ranges
4980 * advance. The output range may not overlap either input range.
4982 template<typename _InputIterator1, typename _InputIterator2,
4983 typename _OutputIterator, typename _Compare>
4985 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4986 _InputIterator2 __first2, _InputIterator2 __last2,
4987 _OutputIterator __result, _Compare __comp)
4989 typedef typename iterator_traits<_InputIterator1>::value_type
4991 typedef typename iterator_traits<_InputIterator2>::value_type
4994 // concept requirements
4995 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4996 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4997 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4999 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5001 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5002 _ValueType1, _ValueType2>)
5003 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5004 _ValueType2, _ValueType1>)
5005 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
5006 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
5008 while (__first1 != __last1 && __first2 != __last2)
5010 if (__comp(*__first1, *__first2))
5012 *__result = *__first1;
5015 else if (__comp(*__first2, *__first1))
5017 *__result = *__first2;
5022 *__result = *__first1;
5028 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5033 * @brief Return the intersection of two sorted ranges.
5034 * @param first1 Start of first range.
5035 * @param last1 End of first range.
5036 * @param first2 Start of second range.
5037 * @param last2 End of second range.
5038 * @return End of the output range.
5039 * @ingroup setoperations
5041 * This operation iterates over both ranges, copying elements present in
5042 * both ranges in order to the output range. Iterators increment for each
5043 * range. When the current element of one range is less than the other,
5044 * that iterator advances. If an element is contained in both ranges, the
5045 * element from the first range is copied and both ranges advance. The
5046 * output range may not overlap either input range.
5048 template<typename _InputIterator1, typename _InputIterator2,
5049 typename _OutputIterator>
5051 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5052 _InputIterator2 __first2, _InputIterator2 __last2,
5053 _OutputIterator __result)
5055 typedef typename iterator_traits<_InputIterator1>::value_type
5057 typedef typename iterator_traits<_InputIterator2>::value_type
5060 // concept requirements
5061 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5062 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5063 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5065 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5066 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5067 __glibcxx_requires_sorted(__first1, __last1);
5068 __glibcxx_requires_sorted(__first2, __last2);
5070 while (__first1 != __last1 && __first2 != __last2)
5071 if (*__first1 < *__first2)
5073 else if (*__first2 < *__first1)
5077 *__result = *__first1;
5086 * @brief Return the intersection of two sorted ranges using comparison
5088 * @param first1 Start of first range.
5089 * @param last1 End of first range.
5090 * @param first2 Start of second range.
5091 * @param last2 End of second range.
5092 * @param comp The comparison functor.
5093 * @return End of the output range.
5094 * @ingroup setoperations
5096 * This operation iterates over both ranges, copying elements present in
5097 * both ranges in order to the output range. Iterators increment for each
5098 * range. When the current element of one range is less than the other
5099 * according to @a comp, that iterator advances. If an element is
5100 * contained in both ranges according to @a comp, the element from the
5101 * first range is copied and both ranges advance. The output range may not
5102 * overlap either input range.
5104 template<typename _InputIterator1, typename _InputIterator2,
5105 typename _OutputIterator, typename _Compare>
5107 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5108 _InputIterator2 __first2, _InputIterator2 __last2,
5109 _OutputIterator __result, _Compare __comp)
5111 typedef typename iterator_traits<_InputIterator1>::value_type
5113 typedef typename iterator_traits<_InputIterator2>::value_type
5116 // concept requirements
5117 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5118 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5119 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5121 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5122 _ValueType1, _ValueType2>)
5123 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5124 _ValueType2, _ValueType1>)
5125 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
5126 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
5128 while (__first1 != __last1 && __first2 != __last2)
5129 if (__comp(*__first1, *__first2))
5131 else if (__comp(*__first2, *__first1))
5135 *__result = *__first1;
5144 * @brief Return the difference of two sorted ranges.
5145 * @param first1 Start of first range.
5146 * @param last1 End of first range.
5147 * @param first2 Start of second range.
5148 * @param last2 End of second range.
5149 * @return End of the output range.
5150 * @ingroup setoperations
5152 * This operation iterates over both ranges, copying elements present in
5153 * the first range but not the second in order to the output range.
5154 * Iterators increment for each range. When the current element of the
5155 * first range is less than the second, that element is copied and the
5156 * iterator advances. If the current element of the second range is less,
5157 * the iterator advances, but no element is copied. If an element is
5158 * contained in both ranges, no elements are copied and both ranges
5159 * advance. The output range may not overlap either input range.
5161 template<typename _InputIterator1, typename _InputIterator2,
5162 typename _OutputIterator>
5164 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5165 _InputIterator2 __first2, _InputIterator2 __last2,
5166 _OutputIterator __result)
5168 typedef typename iterator_traits<_InputIterator1>::value_type
5170 typedef typename iterator_traits<_InputIterator2>::value_type
5173 // concept requirements
5174 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5175 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5176 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5178 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5179 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5180 __glibcxx_requires_sorted(__first1, __last1);
5181 __glibcxx_requires_sorted(__first2, __last2);
5183 while (__first1 != __last1 && __first2 != __last2)
5184 if (*__first1 < *__first2)
5186 *__result = *__first1;
5190 else if (*__first2 < *__first1)
5197 return std::copy(__first1, __last1, __result);
5201 * @brief Return the difference of two sorted ranges using comparison
5203 * @param first1 Start of first range.
5204 * @param last1 End of first range.
5205 * @param first2 Start of second range.
5206 * @param last2 End of second range.
5207 * @param comp The comparison functor.
5208 * @return End of the output range.
5209 * @ingroup setoperations
5211 * This operation iterates over both ranges, copying elements present in
5212 * the first range but not the second in order to the output range.
5213 * Iterators increment for each range. When the current element of the
5214 * first range is less than the second according to @a comp, that element
5215 * is copied and the iterator advances. If the current element of the
5216 * second range is less, no element is copied and the iterator advances.
5217 * If an element is contained in both ranges according to @a comp, no
5218 * elements are copied and both ranges advance. The output range may not
5219 * overlap either input range.
5221 template<typename _InputIterator1, typename _InputIterator2,
5222 typename _OutputIterator, typename _Compare>
5224 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5225 _InputIterator2 __first2, _InputIterator2 __last2,
5226 _OutputIterator __result, _Compare __comp)
5228 typedef typename iterator_traits<_InputIterator1>::value_type
5230 typedef typename iterator_traits<_InputIterator2>::value_type
5233 // concept requirements
5234 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5235 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5236 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5238 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5239 _ValueType1, _ValueType2>)
5240 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5241 _ValueType2, _ValueType1>)
5242 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
5243 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
5245 while (__first1 != __last1 && __first2 != __last2)
5246 if (__comp(*__first1, *__first2))
5248 *__result = *__first1;
5252 else if (__comp(*__first2, *__first1))
5259 return std::copy(__first1, __last1, __result);
5263 * @brief Return the symmetric difference of two sorted ranges.
5264 * @param first1 Start of first range.
5265 * @param last1 End of first range.
5266 * @param first2 Start of second range.
5267 * @param last2 End of second range.
5268 * @return End of the output range.
5269 * @ingroup setoperations
5271 * This operation iterates over both ranges, copying elements present in
5272 * one range but not the other in order to the output range. Iterators
5273 * increment for each range. When the current element of one range is less
5274 * than the other, that element is copied and the iterator advances. If an
5275 * element is contained in both ranges, no elements are copied and both
5276 * ranges advance. The output range may not overlap either input range.
5278 template<typename _InputIterator1, typename _InputIterator2,
5279 typename _OutputIterator>
5281 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5282 _InputIterator2 __first2, _InputIterator2 __last2,
5283 _OutputIterator __result)
5285 typedef typename iterator_traits<_InputIterator1>::value_type
5287 typedef typename iterator_traits<_InputIterator2>::value_type
5290 // concept requirements
5291 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5292 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5293 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5295 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5297 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5298 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5299 __glibcxx_requires_sorted(__first1, __last1);
5300 __glibcxx_requires_sorted(__first2, __last2);
5302 while (__first1 != __last1 && __first2 != __last2)
5303 if (*__first1 < *__first2)
5305 *__result = *__first1;
5309 else if (*__first2 < *__first1)
5311 *__result = *__first2;
5320 return std::copy(__first2, __last2, std::copy(__first1,
5321 __last1, __result));
5325 * @brief Return the symmetric difference of two sorted ranges using
5326 * comparison functor.
5327 * @param first1 Start of first range.
5328 * @param last1 End of first range.
5329 * @param first2 Start of second range.
5330 * @param last2 End of second range.
5331 * @param comp The comparison functor.
5332 * @return End of the output range.
5333 * @ingroup setoperations
5335 * This operation iterates over both ranges, copying elements present in
5336 * one range but not the other in order to the output range. Iterators
5337 * increment for each range. When the current element of one range is less
5338 * than the other according to @a comp, that element is copied and the
5339 * iterator advances. If an element is contained in both ranges according
5340 * to @a comp, no elements are copied and both ranges advance. The output
5341 * range may not overlap either input range.
5343 template<typename _InputIterator1, typename _InputIterator2,
5344 typename _OutputIterator, typename _Compare>
5346 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5347 _InputIterator2 __first2, _InputIterator2 __last2,
5348 _OutputIterator __result,
5351 typedef typename iterator_traits<_InputIterator1>::value_type
5353 typedef typename iterator_traits<_InputIterator2>::value_type
5356 // concept requirements
5357 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5358 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5359 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5361 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5363 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5364 _ValueType1, _ValueType2>)
5365 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5366 _ValueType2, _ValueType1>)
5367 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
5368 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
5370 while (__first1 != __last1 && __first2 != __last2)
5371 if (__comp(*__first1, *__first2))
5373 *__result = *__first1;
5377 else if (__comp(*__first2, *__first1))
5379 *__result = *__first2;
5388 return std::copy(__first2, __last2,
5389 std::copy(__first1, __last1, __result));
5394 * @brief Return the minimum element in a range.
5395 * @param first Start of range.
5396 * @param last End of range.
5397 * @return Iterator referencing the first instance of the smallest value.
5399 template<typename _ForwardIterator>
5401 min_element(_ForwardIterator __first, _ForwardIterator __last)
5403 // concept requirements
5404 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5405 __glibcxx_function_requires(_LessThanComparableConcept<
5406 typename iterator_traits<_ForwardIterator>::value_type>)
5407 __glibcxx_requires_valid_range(__first, __last);
5409 if (__first == __last)
5411 _ForwardIterator __result = __first;
5412 while (++__first != __last)
5413 if (*__first < *__result)
5419 * @brief Return the minimum element in a range using comparison functor.
5420 * @param first Start of range.
5421 * @param last End of range.
5422 * @param comp Comparison functor.
5423 * @return Iterator referencing the first instance of the smallest value
5424 * according to comp.
5426 template<typename _ForwardIterator, typename _Compare>
5428 min_element(_ForwardIterator __first, _ForwardIterator __last,
5431 // concept requirements
5432 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5433 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5434 typename iterator_traits<_ForwardIterator>::value_type,
5435 typename iterator_traits<_ForwardIterator>::value_type>)
5436 __glibcxx_requires_valid_range(__first, __last);
5438 if (__first == __last)
5440 _ForwardIterator __result = __first;
5441 while (++__first != __last)
5442 if (__comp(*__first, *__result))
5448 * @brief Return the maximum element in a range.
5449 * @param first Start of range.
5450 * @param last End of range.
5451 * @return Iterator referencing the first instance of the largest value.
5453 template<typename _ForwardIterator>
5455 max_element(_ForwardIterator __first, _ForwardIterator __last)
5457 // concept requirements
5458 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5459 __glibcxx_function_requires(_LessThanComparableConcept<
5460 typename iterator_traits<_ForwardIterator>::value_type>)
5461 __glibcxx_requires_valid_range(__first, __last);
5463 if (__first == __last)
5465 _ForwardIterator __result = __first;
5466 while (++__first != __last)
5467 if (*__result < *__first)
5473 * @brief Return the maximum element in a range using comparison functor.
5474 * @param first Start of range.
5475 * @param last End of range.
5476 * @param comp Comparison functor.
5477 * @return Iterator referencing the first instance of the largest value
5478 * according to comp.
5480 template<typename _ForwardIterator, typename _Compare>
5482 max_element(_ForwardIterator __first, _ForwardIterator __last,
5485 // concept requirements
5486 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5487 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5488 typename iterator_traits<_ForwardIterator>::value_type,
5489 typename iterator_traits<_ForwardIterator>::value_type>)
5490 __glibcxx_requires_valid_range(__first, __last);
5492 if (__first == __last) return __first;
5493 _ForwardIterator __result = __first;
5494 while (++__first != __last)
5495 if (__comp(*__result, *__first))
5500 _GLIBCXX_END_NESTED_NAMESPACE
5502 #endif /* _STL_ALGO_H */