1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef __GLIBCPP_INTERNAL_ALGO_H
62 #define __GLIBCPP_INTERNAL_ALGO_H
64 #include <bits/stl_heap.h>
65 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 // See concept_check.h for the __glibcpp_*_requires macros.
73 * @brief Find the median of three values.
77 * @return One of @p a, @p b or @p c.
79 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
80 * then the value returned will be @c m.
81 * This is an SGI extension.
82 * @ingroup SGIextensions
84 template<typename _Tp>
86 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
88 // concept requirements
89 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
106 * @brief Find the median of three values using a predicate for comparison.
110 * @param comp A binary predicate.
111 * @return One of @p a, @p b or @p c.
113 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
114 * and @p comp(m,n) are both true then the value returned will be @c m.
115 * This is an SGI extension.
116 * @ingroup SGIextensions
118 template<typename _Tp, typename _Compare>
120 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
122 // concept requirements
123 __glibcpp_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
124 if (__comp(__a, __b))
125 if (__comp(__b, __c))
127 else if (__comp(__a, __c))
131 else if (__comp(__a, __c))
133 else if (__comp(__b, __c))
140 * @brief Apply a function to every element of a sequence.
141 * @param first An input iterator.
142 * @param last An input iterator.
143 * @param f A unary function object.
146 * Applies the function object @p f to each element in the range
147 * @p [first,last). @p f must not modify the order of the sequence.
148 * If @p f has a return value it is ignored.
150 template<typename _InputIterator, typename _Function>
152 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
154 // concept requirements
155 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
156 for ( ; __first != __last; ++__first)
163 * This is an overload used by find() for the Input Iterator case.
166 template<typename _InputIterator, typename _Tp>
167 inline _InputIterator
168 find(_InputIterator __first, _InputIterator __last,
172 while (__first != __last && !(*__first == __val))
179 * This is an overload used by find_if() for the Input Iterator case.
182 template<typename _InputIterator, typename _Predicate>
183 inline _InputIterator
184 find_if(_InputIterator __first, _InputIterator __last,
188 while (__first != __last && !__pred(*__first))
195 * This is an overload used by find() for the RAI case.
198 template<typename _RandomAccessIterator, typename _Tp>
199 _RandomAccessIterator
200 find(_RandomAccessIterator __first, _RandomAccessIterator __last,
202 random_access_iterator_tag)
204 typename iterator_traits<_RandomAccessIterator>::difference_type __trip_count
205 = (__last - __first) >> 2;
207 for ( ; __trip_count > 0 ; --__trip_count) {
208 if (*__first == __val) return __first;
211 if (*__first == __val) return __first;
214 if (*__first == __val) return __first;
217 if (*__first == __val) return __first;
221 switch(__last - __first) {
223 if (*__first == __val) return __first;
226 if (*__first == __val) return __first;
229 if (*__first == __val) return __first;
239 * This is an overload used by find_if() for the RAI case.
242 template<typename _RandomAccessIterator, typename _Predicate>
243 _RandomAccessIterator
244 find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
246 random_access_iterator_tag)
248 typename iterator_traits<_RandomAccessIterator>::difference_type __trip_count
249 = (__last - __first) >> 2;
251 for ( ; __trip_count > 0 ; --__trip_count) {
252 if (__pred(*__first)) return __first;
255 if (__pred(*__first)) return __first;
258 if (__pred(*__first)) return __first;
261 if (__pred(*__first)) return __first;
265 switch(__last - __first) {
267 if (__pred(*__first)) return __first;
270 if (__pred(*__first)) return __first;
273 if (__pred(*__first)) return __first;
282 * @brief Find the first occurrence of a value in a sequence.
283 * @param first An input iterator.
284 * @param last An input iterator.
285 * @param val The value to find.
286 * @return The first iterator @c i in the range @p [first,last)
287 * such that @c *i == @p val, or @p last if no such iterator exists.
289 template<typename _InputIterator, typename _Tp>
290 inline _InputIterator
291 find(_InputIterator __first, _InputIterator __last,
294 // concept requirements
295 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
296 __glibcpp_function_requires(_EqualOpConcept<
297 typename iterator_traits<_InputIterator>::value_type, _Tp>)
298 return find(__first, __last, __val, __iterator_category(__first));
302 * @brief Find the first element in a sequence for which a predicate is true.
303 * @param first An input iterator.
304 * @param last An input iterator.
305 * @param pred A predicate.
306 * @return The first iterator @c i in the range @p [first,last)
307 * such that @p pred(*i) is true, or @p last if no such iterator exists.
309 template<typename _InputIterator, typename _Predicate>
310 inline _InputIterator
311 find_if(_InputIterator __first, _InputIterator __last,
314 // concept requirements
315 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
316 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
317 typename iterator_traits<_InputIterator>::value_type>)
318 return find_if(__first, __last, __pred, __iterator_category(__first));
322 * @brief Find two adjacent values in a sequence that are equal.
323 * @param first A forward iterator.
324 * @param last A forward iterator.
325 * @return The first iterator @c i such that @c i and @c i+1 are both
326 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
327 * or @p last if no such iterator exists.
329 template<typename _ForwardIterator>
331 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
333 // concept requirements
334 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
335 __glibcpp_function_requires(_EqualityComparableConcept<
336 typename iterator_traits<_ForwardIterator>::value_type>)
337 if (__first == __last)
339 _ForwardIterator __next = __first;
340 while(++__next != __last) {
341 if (*__first == *__next)
349 * @brief Find two adjacent values in a sequence using a predicate.
350 * @param first A forward iterator.
351 * @param last A forward iterator.
352 * @param binary_pred A binary predicate.
353 * @return The first iterator @c i such that @c i and @c i+1 are both
354 * valid iterators in @p [first,last) and such that
355 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
358 template<typename _ForwardIterator, typename _BinaryPredicate>
360 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
361 _BinaryPredicate __binary_pred)
363 // concept requirements
364 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
365 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
366 typename iterator_traits<_ForwardIterator>::value_type,
367 typename iterator_traits<_ForwardIterator>::value_type>)
368 if (__first == __last)
370 _ForwardIterator __next = __first;
371 while(++__next != __last) {
372 if (__binary_pred(*__first, *__next))
380 * @brief Count the number of copies of a value in a sequence.
381 * @param first An input iterator.
382 * @param last An input iterator.
383 * @param value The value to be counted.
384 * @return The number of iterators @c i in the range @p [first,last)
385 * for which @c *i == @p value
387 template<typename _InputIterator, typename _Tp>
388 typename iterator_traits<_InputIterator>::difference_type
389 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
391 // concept requirements
392 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
393 __glibcpp_function_requires(_EqualityComparableConcept<
394 typename iterator_traits<_InputIterator>::value_type >)
395 __glibcpp_function_requires(_EqualityComparableConcept<_Tp>)
396 typename iterator_traits<_InputIterator>::difference_type __n = 0;
397 for ( ; __first != __last; ++__first)
398 if (*__first == __value)
404 * @brief Count the elements of a sequence for which a predicate is true.
405 * @param first An input iterator.
406 * @param last An input iterator.
407 * @param pred A predicate.
408 * @return The number of iterators @c i in the range @p [first,last)
409 * for which @p pred(*i) is true.
411 template<typename _InputIterator, typename _Predicate>
412 typename iterator_traits<_InputIterator>::difference_type
413 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
415 // concept requirements
416 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
417 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
418 typename iterator_traits<_InputIterator>::value_type>)
419 typename iterator_traits<_InputIterator>::difference_type __n = 0;
420 for ( ; __first != __last; ++__first)
421 if (__pred(*__first))
428 * @brief Search a sequence for a matching sub-sequence.
429 * @param first1 A forward iterator.
430 * @param last1 A forward iterator.
431 * @param first2 A forward iterator.
432 * @param last2 A forward iterator.
433 * @return The first iterator @c i in the range
434 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
435 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
436 * such iterator exists.
438 * Searches the range @p [first1,last1) for a sub-sequence that compares
439 * equal value-by-value with the sequence given by @p [first2,last2) and
440 * returns an iterator to the first element of the sub-sequence, or
441 * @p last1 if the sub-sequence is not found.
443 * Because the sub-sequence must lie completely within the range
444 * @p [first1,last1) it must start at a position less than
445 * @p last1-(last2-first2) where @p last2-first2 is the length of the
447 * This means that the returned iterator @c i will be in the range
448 * @p [first1,last1-(last2-first2))
450 template<typename _ForwardIterator1, typename _ForwardIterator2>
452 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
453 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
455 // concept requirements
456 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
457 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
458 __glibcpp_function_requires(_EqualOpConcept<
459 typename iterator_traits<_ForwardIterator1>::value_type,
460 typename iterator_traits<_ForwardIterator2>::value_type>)
462 // Test for empty ranges
463 if (__first1 == __last1 || __first2 == __last2)
466 // Test for a pattern of length 1.
467 _ForwardIterator2 __tmp(__first2);
469 if (__tmp == __last2)
470 return find(__first1, __last1, *__first2);
474 _ForwardIterator2 __p1, __p;
476 __p1 = __first2; ++__p1;
478 _ForwardIterator1 __current = __first1;
480 while (__first1 != __last1) {
481 __first1 = find(__first1, __last1, *__first2);
482 if (__first1 == __last1)
486 __current = __first1;
487 if (++__current == __last1)
490 while (*__current == *__p) {
491 if (++__p == __last2)
493 if (++__current == __last1)
503 * @brief Search a sequence for a matching sub-sequence using a predicate.
504 * @param first1 A forward iterator.
505 * @param last1 A forward iterator.
506 * @param first2 A forward iterator.
507 * @param last2 A forward iterator.
508 * @param predicate A binary predicate.
509 * @return The first iterator @c i in the range
510 * @p [first1,last1-(last2-first2)) such that
511 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
512 * @p [0,last2-first2), or @p last1 if no such iterator exists.
514 * Searches the range @p [first1,last1) for a sub-sequence that compares
515 * equal value-by-value with the sequence given by @p [first2,last2),
516 * using @p predicate to determine equality, and returns an iterator
517 * to the first element of the sub-sequence, or @p last1 if no such
520 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
522 template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate>
524 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
525 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
526 _BinaryPredicate __predicate)
528 // concept requirements
529 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
530 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
531 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
532 typename iterator_traits<_ForwardIterator1>::value_type,
533 typename iterator_traits<_ForwardIterator2>::value_type>)
535 // Test for empty ranges
536 if (__first1 == __last1 || __first2 == __last2)
539 // Test for a pattern of length 1.
540 _ForwardIterator2 __tmp(__first2);
542 if (__tmp == __last2) {
543 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
550 _ForwardIterator2 __p1, __p;
552 __p1 = __first2; ++__p1;
554 _ForwardIterator1 __current = __first1;
556 while (__first1 != __last1) {
557 while (__first1 != __last1) {
558 if (__predicate(*__first1, *__first2))
562 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
564 if (__first1 == __last1)
568 __current = __first1;
569 if (++__current == __last1) return __last1;
571 while (__predicate(*__current, *__p)) {
572 if (++__p == __last2)
574 if (++__current == __last1)
584 * @brief Search a sequence for a number of consecutive values.
585 * @param first A forward iterator.
586 * @param last A forward iterator.
587 * @param count The number of consecutive values.
588 * @param val The value to find.
589 * @return The first iterator @c i in the range @p [first,last-count)
590 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
591 * or @p last if no such iterator exists.
593 * Searches the range @p [first,last) for @p count consecutive elements
596 template<typename _ForwardIterator, typename _Integer, typename _Tp>
598 search_n(_ForwardIterator __first, _ForwardIterator __last,
599 _Integer __count, const _Tp& __val)
601 // concept requirements
602 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
603 __glibcpp_function_requires(_EqualityComparableConcept<
604 typename iterator_traits<_ForwardIterator>::value_type>)
605 __glibcpp_function_requires(_EqualityComparableConcept<_Tp>)
610 __first = find(__first, __last, __val);
611 while (__first != __last) {
612 _Integer __n = __count - 1;
613 _ForwardIterator __i = __first;
615 while (__i != __last && __n != 0 && *__i == __val) {
622 __first = find(__i, __last, __val);
629 * @brief Search a sequence for a number of consecutive values using a
631 * @param first A forward iterator.
632 * @param last A forward iterator.
633 * @param count The number of consecutive values.
634 * @param val The value to find.
635 * @param binary_pred A binary predicate.
636 * @return The first iterator @c i in the range @p [first,last-count)
637 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
638 * range @p [0,count), or @p last if no such iterator exists.
640 * Searches the range @p [first,last) for @p count consecutive elements
641 * for which the predicate returns true.
643 template<typename _ForwardIterator, typename _Integer, typename _Tp,
644 typename _BinaryPredicate>
646 search_n(_ForwardIterator __first, _ForwardIterator __last,
647 _Integer __count, const _Tp& __val,
648 _BinaryPredicate __binary_pred)
650 // concept requirements
651 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
652 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
653 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
658 while (__first != __last) {
659 if (__binary_pred(*__first, __val))
663 while (__first != __last) {
664 _Integer __n = __count - 1;
665 _ForwardIterator __i = __first;
667 while (__i != __last && __n != 0 && __binary_pred(*__i, __val)) {
674 while (__i != __last) {
675 if (__binary_pred(*__i, __val))
687 * @brief Swap the elements of two sequences.
688 * @param first1 A forward iterator.
689 * @param last1 A forward iterator.
690 * @param first2 A forward iterator.
691 * @return An iterator equal to @p first2+(last1-first1).
693 * Swaps each element in the range @p [first1,last1) with the
694 * corresponding element in the range @p [first2,(last1-first1)).
695 * The ranges must not overlap.
697 template<typename _ForwardIterator1, typename _ForwardIterator2>
699 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
700 _ForwardIterator2 __first2)
702 // concept requirements
703 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator1>)
704 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator2>)
705 __glibcpp_function_requires(_ConvertibleConcept<
706 typename iterator_traits<_ForwardIterator1>::value_type,
707 typename iterator_traits<_ForwardIterator2>::value_type>)
708 __glibcpp_function_requires(_ConvertibleConcept<
709 typename iterator_traits<_ForwardIterator2>::value_type,
710 typename iterator_traits<_ForwardIterator1>::value_type>)
712 for ( ; __first1 != __last1; ++__first1, ++__first2)
713 iter_swap(__first1, __first2);
718 * @brief Perform an operation on a sequence.
719 * @param first An input iterator.
720 * @param last An input iterator.
721 * @param result An output iterator.
722 * @param unary_op A unary operator.
723 * @return An output iterator equal to @p result+(last-first).
725 * Applies the operator to each element in the input range and assigns
726 * the results to successive elements of the output sequence.
727 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
728 * range @p [0,last-first).
730 * @p unary_op must not alter its argument.
732 template<typename _InputIterator, typename _OutputIterator, typename _UnaryOperation>
734 transform(_InputIterator __first, _InputIterator __last,
735 _OutputIterator __result, _UnaryOperation __unary_op)
737 // concept requirements
738 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
739 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
740 // "the type returned by a _UnaryOperation"
741 __typeof__(__unary_op(*__first))>)
743 for ( ; __first != __last; ++__first, ++__result)
744 *__result = __unary_op(*__first);
749 * @brief Perform an operation on corresponding elements of two sequences.
750 * @param first1 An input iterator.
751 * @param last1 An input iterator.
752 * @param first2 An input iterator.
753 * @param result An output iterator.
754 * @param binary_op A binary operator.
755 * @return An output iterator equal to @p result+(last-first).
757 * Applies the operator to the corresponding elements in the two
758 * input ranges and assigns the results to successive elements of the
760 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
761 * @c N in the range @p [0,last1-first1).
763 * @p binary_op must not alter either of its arguments.
765 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
766 typename _BinaryOperation>
768 transform(_InputIterator1 __first1, _InputIterator1 __last1,
769 _InputIterator2 __first2, _OutputIterator __result,
770 _BinaryOperation __binary_op)
772 // concept requirements
773 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
774 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
775 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
776 // "the type returned by a _BinaryOperation"
777 __typeof__(__binary_op(*__first1,*__first2))>)
779 for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
780 *__result = __binary_op(*__first1, *__first2);
785 * @brief Replace each occurrence of one value in a sequence with another
787 * @param first A forward iterator.
788 * @param last A forward iterator.
789 * @param old_value The value to be replaced.
790 * @param new_value The replacement value.
791 * @return replace() returns no value.
793 * For each iterator @c i in the range @p [first,last) if @c *i ==
794 * @p old_value then the assignment @c *i = @p new_value is performed.
796 template<typename _ForwardIterator, typename _Tp>
798 replace(_ForwardIterator __first, _ForwardIterator __last,
799 const _Tp& __old_value, const _Tp& __new_value)
801 // concept requirements
802 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
803 __glibcpp_function_requires(_EqualOpConcept<
804 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
805 __glibcpp_function_requires(_ConvertibleConcept<_Tp,
806 typename iterator_traits<_ForwardIterator>::value_type>)
808 for ( ; __first != __last; ++__first)
809 if (*__first == __old_value)
810 *__first = __new_value;
814 * @brief Replace each value in a sequence for which a predicate returns
815 * true with another value.
816 * @param first A forward iterator.
817 * @param last A forward iterator.
818 * @param pred A predicate.
819 * @param new_value The replacement value.
820 * @return replace_if() returns no value.
822 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
823 * is true then the assignment @c *i = @p new_value is performed.
825 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
827 replace_if(_ForwardIterator __first, _ForwardIterator __last,
828 _Predicate __pred, const _Tp& __new_value)
830 // concept requirements
831 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
832 __glibcpp_function_requires(_ConvertibleConcept<_Tp,
833 typename iterator_traits<_ForwardIterator>::value_type>)
834 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
835 typename iterator_traits<_ForwardIterator>::value_type>)
837 for ( ; __first != __last; ++__first)
838 if (__pred(*__first))
839 *__first = __new_value;
843 * @brief Copy a sequence, replacing each element of one value with another
845 * @param first An input iterator.
846 * @param last An input iterator.
847 * @param result An output iterator.
848 * @param old_value The value to be replaced.
849 * @param new_value The replacement value.
850 * @return The end of the output sequence, @p result+(last-first).
852 * Copies each element in the input range @p [first,last) to the
853 * output range @p [result,result+(last-first)) replacing elements
854 * equal to @p old_value with @p new_value.
856 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
858 replace_copy(_InputIterator __first, _InputIterator __last,
859 _OutputIterator __result,
860 const _Tp& __old_value, const _Tp& __new_value)
862 // concept requirements
863 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
864 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
865 typename iterator_traits<_InputIterator>::value_type>)
866 __glibcpp_function_requires(_EqualOpConcept<
867 typename iterator_traits<_InputIterator>::value_type, _Tp>)
869 for ( ; __first != __last; ++__first, ++__result)
870 *__result = *__first == __old_value ? __new_value : *__first;
875 * @brief Copy a sequence, replacing each value for which a predicate
876 * returns true with another value.
877 * @param first An input iterator.
878 * @param last An input iterator.
879 * @param result An output iterator.
880 * @param pred A predicate.
881 * @param new_value The replacement value.
882 * @return The end of the output sequence, @p result+(last-first).
884 * Copies each element in the range @p [first,last) to the range
885 * @p [result,result+(last-first)) replacing elements for which
886 * @p pred returns true with @p new_value.
888 template<typename _InputIterator, typename _OutputIterator, typename _Predicate,
891 replace_copy_if(_InputIterator __first, _InputIterator __last,
892 _OutputIterator __result,
893 _Predicate __pred, const _Tp& __new_value)
895 // concept requirements
896 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
897 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
898 typename iterator_traits<_InputIterator>::value_type>)
899 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
900 typename iterator_traits<_InputIterator>::value_type>)
902 for ( ; __first != __last; ++__first, ++__result)
903 *__result = __pred(*__first) ? __new_value : *__first;
908 * @brief Assign the result of a function object to each value in a
910 * @param first A forward iterator.
911 * @param last A forward iterator.
912 * @param gen A function object taking no arguments.
913 * @return generate() returns no value.
915 * Performs the assignment @c *i = @p gen() for each @c i in the range
918 template<typename _ForwardIterator, typename _Generator>
920 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
922 // concept requirements
923 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
924 __glibcpp_function_requires(_GeneratorConcept<_Generator,
925 typename iterator_traits<_ForwardIterator>::value_type>)
927 for ( ; __first != __last; ++__first)
932 * @brief Assign the result of a function object to each value in a
934 * @param first A forward iterator.
935 * @param n The length of the sequence.
936 * @param gen A function object taking no arguments.
937 * @return The end of the sequence, @p first+n
939 * Performs the assignment @c *i = @p gen() for each @c i in the range
940 * @p [first,first+n).
942 template<typename _OutputIterator, typename _Size, typename _Generator>
944 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
946 // concept requirements
947 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
948 // "the type returned by a _Generator"
951 for ( ; __n > 0; --__n, ++__first)
957 * @brief Copy a sequence, removing elements of a given value.
958 * @param first An input iterator.
959 * @param last An input iterator.
960 * @param result An output iterator.
961 * @param value The value to be removed.
962 * @return An iterator designating the end of the resulting sequence.
964 * Copies each element in the range @p [first,last) not equal to @p value
965 * to the range beginning at @p result.
966 * remove_copy() is stable, so the relative order of elements that are
967 * copied is unchanged.
969 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
971 remove_copy(_InputIterator __first, _InputIterator __last,
972 _OutputIterator __result, const _Tp& __value)
974 // concept requirements
975 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
976 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
977 typename iterator_traits<_InputIterator>::value_type>)
978 __glibcpp_function_requires(_EqualOpConcept<
979 typename iterator_traits<_InputIterator>::value_type, _Tp>)
981 for ( ; __first != __last; ++__first)
982 if (!(*__first == __value)) {
983 *__result = *__first;
990 * @brief Copy a sequence, removing elements for which a predicate is true.
991 * @param first An input iterator.
992 * @param last An input iterator.
993 * @param result An output iterator.
994 * @param pred A predicate.
995 * @return An iterator designating the end of the resulting sequence.
997 * Copies each element in the range @p [first,last) for which
998 * @p pred returns true to the range beginning at @p result.
1000 * remove_copy_if() is stable, so the relative order of elements that are
1001 * copied is unchanged.
1003 template<typename _InputIterator, typename _OutputIterator, typename _Predicate>
1005 remove_copy_if(_InputIterator __first, _InputIterator __last,
1006 _OutputIterator __result, _Predicate __pred)
1008 // concept requirements
1009 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
1010 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
1011 typename iterator_traits<_InputIterator>::value_type>)
1012 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
1013 typename iterator_traits<_InputIterator>::value_type>)
1015 for ( ; __first != __last; ++__first)
1016 if (!__pred(*__first)) {
1017 *__result = *__first;
1024 * @brief Remove elements from a sequence.
1025 * @param first An input iterator.
1026 * @param last An input iterator.
1027 * @param value The value to be removed.
1028 * @return An iterator designating the end of the resulting sequence.
1030 * All elements equal to @p value are removed from the range
1033 * remove() is stable, so the relative order of elements that are
1034 * not removed is unchanged.
1036 * Elements between the end of the resulting sequence and @p last
1037 * are still present, but their value is unspecified.
1039 template<typename _ForwardIterator, typename _Tp>
1041 remove(_ForwardIterator __first, _ForwardIterator __last,
1044 // concept requirements
1045 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1046 __glibcpp_function_requires(_ConvertibleConcept<_Tp,
1047 typename iterator_traits<_ForwardIterator>::value_type>)
1048 __glibcpp_function_requires(_EqualOpConcept<
1049 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1051 __first = find(__first, __last, __value);
1052 _ForwardIterator __i = __first;
1053 return __first == __last ? __first
1054 : remove_copy(++__i, __last, __first, __value);
1058 * @brief Remove elements from a sequence using a predicate.
1059 * @param first A forward iterator.
1060 * @param last A forward iterator.
1061 * @param pred A predicate.
1062 * @return An iterator designating the end of the resulting sequence.
1064 * All elements for which @p pred returns true are removed from the range
1067 * remove_if() is stable, so the relative order of elements that are
1068 * not removed is unchanged.
1070 * Elements between the end of the resulting sequence and @p last
1071 * are still present, but their value is unspecified.
1073 template<typename _ForwardIterator, typename _Predicate>
1075 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1078 // concept requirements
1079 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1080 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
1081 typename iterator_traits<_ForwardIterator>::value_type>)
1083 __first = find_if(__first, __last, __pred);
1084 _ForwardIterator __i = __first;
1085 return __first == __last ? __first
1086 : remove_copy_if(++__i, __last, __first, __pred);
1091 * This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
1092 * overloaded for output iterators.
1095 template<typename _InputIterator, typename _OutputIterator>
1097 __unique_copy(_InputIterator __first, _InputIterator __last,
1098 _OutputIterator __result,
1099 output_iterator_tag)
1101 // concept requirements -- taken care of in dispatching function
1102 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1103 *__result = __value;
1104 while (++__first != __last)
1105 if (!(__value == *__first)) {
1107 *++__result = __value;
1114 * This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
1115 * overloaded for forward iterators.
1118 template<typename _InputIterator, typename _ForwardIterator>
1120 __unique_copy(_InputIterator __first, _InputIterator __last,
1121 _ForwardIterator __result,
1122 forward_iterator_tag)
1124 // concept requirements -- taken care of in dispatching function
1125 *__result = *__first;
1126 while (++__first != __last)
1127 if (!(*__result == *__first))
1128 *++__result = *__first;
1133 * @brief Copy a sequence, removing consecutive duplicate values.
1134 * @param first An input iterator.
1135 * @param last An input iterator.
1136 * @param result An output iterator.
1137 * @return An iterator designating the end of the resulting sequence.
1139 * Copies each element in the range @p [first,last) to the range
1140 * beginning at @p result, except that only the first element is copied
1141 * from groups of consecutive elements that compare equal.
1142 * unique_copy() is stable, so the relative order of elements that are
1143 * copied is unchanged.
1145 template<typename _InputIterator, typename _OutputIterator>
1146 inline _OutputIterator
1147 unique_copy(_InputIterator __first, _InputIterator __last,
1148 _OutputIterator __result)
1150 // concept requirements
1151 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
1152 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
1153 typename iterator_traits<_InputIterator>::value_type>)
1154 __glibcpp_function_requires(_EqualityComparableConcept<
1155 typename iterator_traits<_InputIterator>::value_type>)
1157 typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType;
1159 if (__first == __last) return __result;
1160 return __unique_copy(__first, __last, __result, _IterType());
1165 * This is an uglified
1166 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
1167 * overloaded for output iterators.
1170 template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate>
1172 __unique_copy(_InputIterator __first, _InputIterator __last,
1173 _OutputIterator __result,
1174 _BinaryPredicate __binary_pred,
1175 output_iterator_tag)
1177 // concept requirements -- iterators already checked
1178 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1179 typename iterator_traits<_InputIterator>::value_type,
1180 typename iterator_traits<_InputIterator>::value_type>)
1182 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1183 *__result = __value;
1184 while (++__first != __last)
1185 if (!__binary_pred(__value, *__first)) {
1187 *++__result = __value;
1194 * This is an uglified
1195 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
1196 * overloaded for forward iterators.
1199 template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate>
1201 __unique_copy(_InputIterator __first, _InputIterator __last,
1202 _ForwardIterator __result,
1203 _BinaryPredicate __binary_pred,
1204 forward_iterator_tag)
1206 // concept requirements -- iterators already checked
1207 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1208 typename iterator_traits<_ForwardIterator>::value_type,
1209 typename iterator_traits<_InputIterator>::value_type>)
1211 *__result = *__first;
1212 while (++__first != __last)
1213 if (!__binary_pred(*__result, *__first)) *++__result = *__first;
1218 * @brief Copy a sequence, removing consecutive values using a predicate.
1219 * @param first An input iterator.
1220 * @param last An input iterator.
1221 * @param result An output iterator.
1222 * @param binary_pred A binary predicate.
1223 * @return An iterator designating the end of the resulting sequence.
1225 * Copies each element in the range @p [first,last) to the range
1226 * beginning at @p result, except that only the first element is copied
1227 * from groups of consecutive elements for which @p binary_pred returns
1229 * unique_copy() is stable, so the relative order of elements that are
1230 * copied is unchanged.
1232 template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate>
1233 inline _OutputIterator
1234 unique_copy(_InputIterator __first, _InputIterator __last,
1235 _OutputIterator __result,
1236 _BinaryPredicate __binary_pred)
1238 // concept requirements -- predicates checked later
1239 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
1240 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
1241 typename iterator_traits<_InputIterator>::value_type>)
1243 typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType;
1245 if (__first == __last) return __result;
1246 return __unique_copy(__first, __last,
1247 __result, __binary_pred, _IterType());
1251 * @brief Remove consecutive duplicate values from a sequence.
1252 * @param first A forward iterator.
1253 * @param last A forward iterator.
1254 * @return An iterator designating the end of the resulting sequence.
1256 * Removes all but the first element from each group of consecutive
1257 * values that compare equal.
1258 * unique() is stable, so the relative order of elements that are
1259 * not removed is unchanged.
1260 * Elements between the end of the resulting sequence and @p last
1261 * are still present, but their value is unspecified.
1263 template<typename _ForwardIterator>
1265 unique(_ForwardIterator __first, _ForwardIterator __last)
1267 // concept requirements
1268 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1269 __glibcpp_function_requires(_EqualityComparableConcept<
1270 typename iterator_traits<_ForwardIterator>::value_type>)
1272 __first = adjacent_find(__first, __last);
1273 return unique_copy(__first, __last, __first);
1277 * @brief Remove consecutive values from a sequence using a predicate.
1278 * @param first A forward iterator.
1279 * @param last A forward iterator.
1280 * @param binary_pred A binary predicate.
1281 * @return An iterator designating the end of the resulting sequence.
1283 * Removes all but the first element from each group of consecutive
1284 * values for which @p binary_pred returns true.
1285 * unique() is stable, so the relative order of elements that are
1286 * not removed is unchanged.
1287 * Elements between the end of the resulting sequence and @p last
1288 * are still present, but their value is unspecified.
1290 template<typename _ForwardIterator, typename _BinaryPredicate>
1292 unique(_ForwardIterator __first, _ForwardIterator __last,
1293 _BinaryPredicate __binary_pred)
1295 // concept requirements
1296 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1297 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1298 typename iterator_traits<_ForwardIterator>::value_type,
1299 typename iterator_traits<_ForwardIterator>::value_type>)
1301 __first = adjacent_find(__first, __last, __binary_pred);
1302 return unique_copy(__first, __last, __first, __binary_pred);
1307 * This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
1308 * overloaded for bidirectional iterators.
1311 template<typename _BidirectionalIterator>
1313 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1314 bidirectional_iterator_tag)
1317 if (__first == __last || __first == --__last)
1320 iter_swap(__first++, __last);
1325 * This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
1326 * overloaded for bidirectional iterators.
1329 template<typename _RandomAccessIterator>
1331 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1332 random_access_iterator_tag)
1334 while (__first < __last)
1335 iter_swap(__first++, --__last);
1339 * @brief Reverse a sequence.
1340 * @param first A bidirectional iterator.
1341 * @param last A bidirectional iterator.
1342 * @return reverse() returns no value.
1344 * Reverses the order of the elements in the range @p [first,last),
1345 * so that the first element becomes the last etc.
1346 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1347 * swaps @p *(first+i) and @p *(last-(i+1))
1349 template<typename _BidirectionalIterator>
1351 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1353 // concept requirements
1354 __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
1355 _BidirectionalIterator>)
1356 __reverse(__first, __last, __iterator_category(__first));
1360 * @brief Copy a sequence, reversing its elements.
1361 * @param first A bidirectional iterator.
1362 * @param last A bidirectional iterator.
1363 * @param result An output iterator.
1364 * @return An iterator designating the end of the resulting sequence.
1366 * Copies the elements in the range @p [first,last) to the range
1367 * @p [result,result+(last-first)) such that the order of the
1368 * elements is reversed.
1369 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1370 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1371 * The ranges @p [first,last) and @p [result,result+(last-first))
1374 template<typename _BidirectionalIterator, typename _OutputIterator>
1376 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1377 _OutputIterator __result)
1379 // concept requirements
1380 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
1381 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
1382 typename iterator_traits<_BidirectionalIterator>::value_type>)
1384 while (__first != __last) {
1386 *__result = *__last;
1395 * This is a helper function for the rotate algorithm specialized on RAIs.
1396 * It returns the greatest common divisor of two integer values.
1399 template<typename _EuclideanRingElement>
1400 _EuclideanRingElement
1401 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1404 _EuclideanRingElement __t = __m % __n;
1413 * This is a helper function for the rotate algorithm.
1416 template<typename _ForwardIterator>
1418 __rotate(_ForwardIterator __first,
1419 _ForwardIterator __middle,
1420 _ForwardIterator __last,
1421 forward_iterator_tag)
1423 if ((__first == __middle) || (__last == __middle))
1426 _ForwardIterator __first2 = __middle;
1428 swap(*__first++, *__first2++);
1429 if (__first == __middle)
1430 __middle = __first2;
1431 } while (__first2 != __last);
1433 __first2 = __middle;
1435 while (__first2 != __last) {
1436 swap(*__first++, *__first2++);
1437 if (__first == __middle)
1438 __middle = __first2;
1439 else if (__first2 == __last)
1440 __first2 = __middle;
1446 * This is a helper function for the rotate algorithm.
1449 template<typename _BidirectionalIterator>
1451 __rotate(_BidirectionalIterator __first,
1452 _BidirectionalIterator __middle,
1453 _BidirectionalIterator __last,
1454 bidirectional_iterator_tag)
1456 // concept requirements
1457 __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
1458 _BidirectionalIterator>)
1460 if ((__first == __middle) || (__last == __middle))
1463 __reverse(__first, __middle, bidirectional_iterator_tag());
1464 __reverse(__middle, __last, bidirectional_iterator_tag());
1466 while (__first != __middle && __middle != __last)
1467 swap (*__first++, *--__last);
1469 if (__first == __middle) {
1470 __reverse(__middle, __last, bidirectional_iterator_tag());
1473 __reverse(__first, __middle, bidirectional_iterator_tag());
1479 * This is a helper function for the rotate algorithm.
1482 template<typename _RandomAccessIterator>
1484 __rotate(_RandomAccessIterator __first,
1485 _RandomAccessIterator __middle,
1486 _RandomAccessIterator __last,
1487 random_access_iterator_tag)
1489 // concept requirements
1490 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
1491 _RandomAccessIterator>)
1493 if ((__first == __middle) || (__last == __middle))
1496 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
1497 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
1499 _Distance __n = __last - __first;
1500 _Distance __k = __middle - __first;
1501 _Distance __l = __n - __k;
1504 swap_ranges(__first, __middle, __middle);
1508 _Distance __d = __gcd(__n, __k);
1510 for (_Distance __i = 0; __i < __d; __i++) {
1511 _ValueType __tmp = *__first;
1512 _RandomAccessIterator __p = __first;
1515 for (_Distance __j = 0; __j < __l/__d; __j++) {
1516 if (__p > __first + __l) {
1517 *__p = *(__p - __l);
1521 *__p = *(__p + __k);
1527 for (_Distance __j = 0; __j < __k/__d - 1; __j ++) {
1528 if (__p < __last - __k) {
1529 *__p = *(__p + __k);
1533 *__p = * (__p - __l);
1544 * @brief Rotate the elements of a sequence.
1545 * @param first A forward iterator.
1546 * @param middle A forward iterator.
1547 * @param last A forward iterator.
1550 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1551 * positions so that the element at @p middle is moved to @p first, the
1552 * element at @p middle+1 is moved to @first+1 and so on for each element
1553 * in the range @p [first,last).
1555 * This effectively swaps the ranges @p [first,middle) and
1558 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1559 * each @p n in the range @p [0,last-first).
1561 template<typename _ForwardIterator>
1563 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
1565 // concept requirements
1566 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1568 typedef typename iterator_traits<_ForwardIterator>::iterator_category _IterType;
1569 __rotate(__first, __middle, __last, _IterType());
1573 * @brief Copy a sequence, rotating its elements.
1574 * @param first A forward iterator.
1575 * @param middle A forward iterator.
1576 * @param last A forward iterator.
1577 * @param result An output iterator.
1578 * @return An iterator designating the end of the resulting sequence.
1580 * Copies the elements of the range @p [first,last) to the range
1581 * beginning at @result, rotating the copied elements by @p (middle-first)
1582 * positions so that the element at @p middle is moved to @p result, the
1583 * element at @p middle+1 is moved to @result+1 and so on for each element
1584 * in the range @p [first,last).
1586 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1587 * each @p n in the range @p [0,last-first).
1589 template<typename _ForwardIterator, typename _OutputIterator>
1591 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1592 _ForwardIterator __last, _OutputIterator __result)
1594 // concept requirements
1595 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1596 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
1597 typename iterator_traits<_ForwardIterator>::value_type>)
1599 return copy(__first, __middle, copy(__middle, __last, __result));
1605 * Return a random number in the range [0, __n). This function encapsulates
1606 * whether we're using rand (part of the standard C library) or lrand48
1607 * (not standard, but a much better choice whenever it's available).
1609 * XXX There is no corresponding encapsulation fn to seed the generator.
1612 template<typename _Distance>
1614 __random_number(_Distance __n)
1616 #ifdef _GLIBCPP_HAVE_DRAND48
1617 return lrand48() % __n;
1619 return rand() % __n;
1625 * @brief Randomly shuffle the elements of a sequence.
1626 * @param first A forward iterator.
1627 * @param last A forward iterator.
1630 * Reorder the elements in the range @p [first,last) using a random
1631 * distribution, so that every possible ordering of the sequence is
1634 template<typename _RandomAccessIterator>
1636 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
1638 // concept requirements
1639 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
1640 _RandomAccessIterator>)
1642 if (__first == __last) return;
1643 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1644 iter_swap(__i, __first + __random_number((__i - __first) + 1));
1648 * @brief Shuffle the elements of a sequence using a random number
1650 * @param first A forward iterator.
1651 * @param last A forward iterator.
1652 * @param rand The RNG functor or function.
1655 * Reorders the elements in the range @p [first,last) using @p rand to
1656 * provide a random distribution. Calling @p rand(N) for a positive
1657 * integer @p N should return a randomly chosen integer from the
1660 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
1662 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
1663 _RandomNumberGenerator& __rand)
1665 // concept requirements
1666 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
1667 _RandomAccessIterator>)
1669 if (__first == __last) return;
1670 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1671 iter_swap(__i, __first + __rand((__i - __first) + 1));
1677 * This is a helper function...
1680 template<typename _ForwardIterator, typename _Predicate>
1682 __partition(_ForwardIterator __first, _ForwardIterator __last,
1684 forward_iterator_tag)
1686 if (__first == __last) return __first;
1688 while (__pred(*__first))
1689 if (++__first == __last) return __first;
1691 _ForwardIterator __next = __first;
1693 while (++__next != __last)
1694 if (__pred(*__next)) {
1695 swap(*__first, *__next);
1704 * This is a helper function...
1707 template<typename _BidirectionalIterator, typename _Predicate>
1708 _BidirectionalIterator
1709 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1711 bidirectional_iterator_tag)
1715 if (__first == __last)
1717 else if (__pred(*__first))
1723 if (__first == __last)
1725 else if (!__pred(*__last))
1729 iter_swap(__first, __last);
1735 * @brief Move elements for which a predicate is true to the beginning
1737 * @param first A forward iterator.
1738 * @param last A forward iterator.
1739 * @param pred A predicate functor.
1740 * @return An iterator @p middle such that @p pred(i) is true for each
1741 * iterator @p i in the range @p [first,middle) and false for each @p i
1742 * in the range @p [middle,last).
1744 * @p pred must not modify its operand. @p partition() does not preserve
1745 * the relative ordering of elements in each group, use
1746 * @p stable_partition() if this is needed.
1748 template<typename _ForwardIterator, typename _Predicate>
1749 inline _ForwardIterator
1750 partition(_ForwardIterator __first, _ForwardIterator __last,
1753 // concept requirements
1754 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1755 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
1756 typename iterator_traits<_ForwardIterator>::value_type>)
1758 return __partition(__first, __last, __pred, __iterator_category(__first));
1764 * This is a helper function...
1767 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1769 __inplace_stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1770 _Predicate __pred, _Distance __len)
1773 return __pred(*__first) ? __last : __first;
1774 _ForwardIterator __middle = __first;
1775 advance(__middle, __len / 2);
1776 _ForwardIterator __begin = __inplace_stable_partition(__first, __middle,
1779 _ForwardIterator __end = __inplace_stable_partition(__middle, __last,
1782 rotate(__begin, __middle, __end);
1783 advance(__begin, std::distance(__middle, __end));
1789 * This is a helper function...
1792 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1795 __stable_partition_adaptive(_ForwardIterator __first, _ForwardIterator __last,
1796 _Predicate __pred, _Distance __len,
1798 _Distance __buffer_size)
1800 if (__len <= __buffer_size) {
1801 _ForwardIterator __result1 = __first;
1802 _Pointer __result2 = __buffer;
1803 for ( ; __first != __last ; ++__first)
1804 if (__pred(*__first)) {
1805 *__result1 = *__first;
1809 *__result2 = *__first;
1812 copy(__buffer, __result2, __result1);
1816 _ForwardIterator __middle = __first;
1817 advance(__middle, __len / 2);
1818 _ForwardIterator __begin = __stable_partition_adaptive(__first, __middle,
1821 __buffer, __buffer_size);
1822 _ForwardIterator __end = __stable_partition_adaptive( __middle, __last,
1825 __buffer, __buffer_size);
1826 rotate(__begin, __middle, __end);
1827 advance(__begin, std::distance(__middle, __end));
1833 * @brief Move elements for which a predicate is true to the beginning
1834 * of a sequence, preserving relative ordering.
1835 * @param first A forward iterator.
1836 * @param last A forward iterator.
1837 * @param pred A predicate functor.
1838 * @return An iterator @p middle such that @p pred(i) is true for each
1839 * iterator @p i in the range @p [first,middle) and false for each @p i
1840 * in the range @p [middle,last).
1842 * Performs the same function as @p partition() with the additional
1843 * guarantee that the relative ordering of elements in each group is
1844 * preserved, so any two elements @p x and @p y in the range
1845 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1846 * relative ordering after calling @p stable_partition().
1848 template<typename _ForwardIterator, typename _Predicate>
1850 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1853 // concept requirements
1854 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
1855 __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
1856 typename iterator_traits<_ForwardIterator>::value_type>)
1858 if (__first == __last)
1862 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
1863 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
1865 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last);
1866 if (__buf.size() > 0)
1867 return __stable_partition_adaptive(__first, __last, __pred,
1868 _DistanceType(__buf.requested_size()),
1869 __buf.begin(), __buf.size());
1871 return __inplace_stable_partition(__first, __last, __pred,
1872 _DistanceType(__buf.requested_size()));
1878 * This is a helper function...
1881 template<typename _RandomAccessIterator, typename _Tp>
1882 _RandomAccessIterator
1883 __unguarded_partition(_RandomAccessIterator __first, _RandomAccessIterator __last,
1887 while (*__first < __pivot)
1890 while (__pivot < *__last)
1892 if (!(__first < __last))
1894 iter_swap(__first, __last);
1901 * This is a helper function...
1904 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
1905 _RandomAccessIterator
1906 __unguarded_partition(_RandomAccessIterator __first, _RandomAccessIterator __last,
1907 _Tp __pivot, _Compare __comp)
1910 while (__comp(*__first, __pivot))
1913 while (__comp(__pivot, *__last))
1915 if (!(__first < __last))
1917 iter_swap(__first, __last);
1926 * This controls some aspect of the sort routines.
1929 enum { _S_threshold = 16 };
1933 * This is a helper function for the sort routine.
1936 template<typename _RandomAccessIterator, typename _Tp>
1938 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
1940 _RandomAccessIterator __next = __last;
1942 while (__val < *__next) {
1952 * This is a helper function for the sort routine.
1955 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
1957 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val, _Compare __comp)
1959 _RandomAccessIterator __next = __last;
1961 while (__comp(__val, *__next)) {
1971 * This is a helper function for the sort routine.
1974 template<typename _RandomAccessIterator>
1976 __insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
1978 if (__first == __last) return;
1980 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1982 typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i;
1983 if (__val < *__first) {
1984 copy_backward(__first, __i, __i + 1);
1988 __unguarded_linear_insert(__i, __val);
1994 * This is a helper function for the sort routine.
1997 template<typename _RandomAccessIterator, typename _Compare>
1999 __insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2002 if (__first == __last) return;
2004 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2006 typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i;
2007 if (__comp(__val, *__first)) {
2008 copy_backward(__first, __i, __i + 1);
2012 __unguarded_linear_insert(__i, __val, __comp);
2018 * This is a helper function for the sort routine.
2021 template<typename _RandomAccessIterator>
2023 __unguarded_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2025 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2027 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2028 __unguarded_linear_insert(__i, _ValueType(*__i));
2033 * This is a helper function for the sort routine.
2036 template<typename _RandomAccessIterator, typename _Compare>
2038 __unguarded_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2041 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2043 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2044 __unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2049 * This is a helper function for the sort routine.
2052 template<typename _RandomAccessIterator>
2054 __final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2056 if (__last - __first > _S_threshold) {
2057 __insertion_sort(__first, __first + _S_threshold);
2058 __unguarded_insertion_sort(__first + _S_threshold, __last);
2061 __insertion_sort(__first, __last);
2066 * This is a helper function for the sort routine.
2069 template<typename _RandomAccessIterator, typename _Compare>
2071 __final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2074 if (__last - __first > _S_threshold) {
2075 __insertion_sort(__first, __first + _S_threshold, __comp);
2076 __unguarded_insertion_sort(__first + _S_threshold, __last, __comp);
2079 __insertion_sort(__first, __last, __comp);
2084 * This is a helper function for the sort routine.
2087 template<typename _Size>
2092 for (__k = 0; __n != 1; __n >>= 1) ++__k;
2098 * This is a helper function for the sort routine.
2101 template<typename _RandomAccessIterator, typename _Size>
2103 __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last,
2104 _Size __depth_limit)
2106 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2108 while (__last - __first > _S_threshold) {
2109 if (__depth_limit == 0) {
2110 partial_sort(__first, __last, __last);
2114 _RandomAccessIterator __cut =
2115 __unguarded_partition(__first, __last,
2116 _ValueType(__median(*__first,
2117 *(__first + (__last - __first)/2),
2119 __introsort_loop(__cut, __last, __depth_limit);
2126 * This is a helper function for the sort routine.
2129 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2131 __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last,
2132 _Size __depth_limit, _Compare __comp)
2134 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2136 while (__last - __first > _S_threshold) {
2137 if (__depth_limit == 0) {
2138 partial_sort(__first, __last, __last, __comp);
2142 _RandomAccessIterator __cut =
2143 __unguarded_partition(__first, __last,
2144 _ValueType(__median(*__first,
2145 *(__first + (__last - __first)/2),
2146 *(__last - 1), __comp)),
2148 __introsort_loop(__cut, __last, __depth_limit, __comp);
2154 * @brief Sort the elements of a sequence.
2155 * @param first An iterator.
2156 * @param last Another iterator.
2159 * Sorts the elements in the range @p [first,last) in ascending order,
2160 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2161 * @p [first,last-1).
2163 * The relative ordering of equivalent elements is not preserved, use
2164 * @p stable_sort() if this is needed.
2166 template<typename _RandomAccessIterator>
2168 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2170 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2172 // concept requirements
2173 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2174 _RandomAccessIterator>)
2175 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
2177 if (__first != __last) {
2178 __introsort_loop(__first, __last, __lg(__last - __first) * 2);
2179 __final_insertion_sort(__first, __last);
2184 * @brief Sort the elements of a sequence using a predicate for comparison.
2185 * @param first An iterator.
2186 * @param last Another iterator.
2187 * @param comp A comparison functor.
2190 * Sorts the elements in the range @p [first,last) in ascending order,
2191 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2192 * range @p [first,last-1).
2194 * The relative ordering of equivalent elements is not preserved, use
2195 * @p stable_sort() if this is needed.
2197 template<typename _RandomAccessIterator, typename _Compare>
2199 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
2201 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2203 // concept requirements
2204 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2205 _RandomAccessIterator>)
2206 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>)
2208 if (__first != __last) {
2209 __introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp);
2210 __final_insertion_sort(__first, __last, __comp);
2217 * This is a helper function for the stable sorting routines.
2220 template<typename _RandomAccessIterator>
2222 __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2224 if (__last - __first < 15) {
2225 __insertion_sort(__first, __last);
2228 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2229 __inplace_stable_sort(__first, __middle);
2230 __inplace_stable_sort(__middle, __last);
2231 __merge_without_buffer(__first, __middle, __last,
2238 * This is a helper function for the stable sorting routines.
2241 template<typename _RandomAccessIterator, typename _Compare>
2243 __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2246 if (__last - __first < 15) {
2247 __insertion_sort(__first, __last, __comp);
2250 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2251 __inplace_stable_sort(__first, __middle, __comp);
2252 __inplace_stable_sort(__middle, __last, __comp);
2253 __merge_without_buffer(__first, __middle, __last,
2259 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2262 __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
2263 _RandomAccessIterator2 __result, _Distance __step_size)
2265 _Distance __two_step = 2 * __step_size;
2267 while (__last - __first >= __two_step) {
2268 __result = merge(__first, __first + __step_size,
2269 __first + __step_size, __first + __two_step,
2271 __first += __two_step;
2274 __step_size = std::min(_Distance(__last - __first), __step_size);
2275 merge(__first, __first + __step_size, __first + __step_size, __last,
2279 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2280 typename _Distance, typename _Compare>
2282 __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
2283 _RandomAccessIterator2 __result, _Distance __step_size,
2286 _Distance __two_step = 2 * __step_size;
2288 while (__last - __first >= __two_step) {
2289 __result = merge(__first, __first + __step_size,
2290 __first + __step_size, __first + __two_step,
2293 __first += __two_step;
2295 __step_size = std::min(_Distance(__last - __first), __step_size);
2297 merge(__first, __first + __step_size,
2298 __first + __step_size, __last,
2303 enum { _S_chunk_size = 7 };
2305 template<typename _RandomAccessIterator, typename _Distance>
2307 __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2308 _Distance __chunk_size)
2310 while (__last - __first >= __chunk_size) {
2311 __insertion_sort(__first, __first + __chunk_size);
2312 __first += __chunk_size;
2314 __insertion_sort(__first, __last);
2317 template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
2319 __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2320 _Distance __chunk_size, _Compare __comp)
2322 while (__last - __first >= __chunk_size) {
2323 __insertion_sort(__first, __first + __chunk_size, __comp);
2324 __first += __chunk_size;
2326 __insertion_sort(__first, __last, __comp);
2329 template<typename _RandomAccessIterator, typename _Pointer>
2331 __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
2334 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
2336 _Distance __len = __last - __first;
2337 _Pointer __buffer_last = __buffer + __len;
2339 _Distance __step_size = _S_chunk_size;
2340 __chunk_insertion_sort(__first, __last, __step_size);
2342 while (__step_size < __len) {
2343 __merge_sort_loop(__first, __last, __buffer, __step_size);
2345 __merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
2350 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
2352 __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
2353 _Pointer __buffer, _Compare __comp)
2355 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
2357 _Distance __len = __last - __first;
2358 _Pointer __buffer_last = __buffer + __len;
2360 _Distance __step_size = _S_chunk_size;
2361 __chunk_insertion_sort(__first, __last, __step_size, __comp);
2363 while (__step_size < __len) {
2364 __merge_sort_loop(__first, __last, __buffer, __step_size, __comp);
2366 __merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp);
2371 template<typename _RandomAccessIterator, typename _Pointer, typename _Distance>
2373 __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last,
2374 _Pointer __buffer, _Distance __buffer_size)
2376 _Distance __len = (__last - __first + 1) / 2;
2377 _RandomAccessIterator __middle = __first + __len;
2378 if (__len > __buffer_size) {
2379 __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size);
2380 __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size);
2383 __merge_sort_with_buffer(__first, __middle, __buffer);
2384 __merge_sort_with_buffer(__middle, __last, __buffer);
2386 __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
2387 _Distance(__last - __middle), __buffer, __buffer_size);
2390 template<typename _RandomAccessIterator, typename _Pointer, typename _Distance,
2393 __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last,
2394 _Pointer __buffer, _Distance __buffer_size,
2397 _Distance __len = (__last - __first + 1) / 2;
2398 _RandomAccessIterator __middle = __first + __len;
2399 if (__len > __buffer_size) {
2400 __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size,
2402 __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size,
2406 __merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2407 __merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2409 __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
2410 _Distance(__last - __middle), __buffer, __buffer_size,
2415 * @brief Sort the elements of a sequence, preserving the relative order
2416 * of equivalent elements.
2417 * @param first An iterator.
2418 * @param last Another iterator.
2421 * Sorts the elements in the range @p [first,last) in ascending order,
2422 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2423 * @p [first,last-1).
2425 * The relative ordering of equivalent elements is preserved, so any two
2426 * elements @p x and @p y in the range @p [first,last) such that
2427 * @p x<y is false and @p y<x is false will have the same relative
2428 * ordering after calling @p stable_sort().
2430 template<typename _RandomAccessIterator>
2432 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2434 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2435 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
2437 // concept requirements
2438 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2439 _RandomAccessIterator>)
2440 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
2442 _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last);
2443 if (buf.begin() == 0)
2444 __inplace_stable_sort(__first, __last);
2446 __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()));
2450 * @brief Sort the elements of a sequence using a predicate for comparison,
2451 * preserving the relative order of equivalent elements.
2452 * @param first An iterator.
2453 * @param last Another iterator.
2454 * @param comp A comparison functor.
2457 * Sorts the elements in the range @p [first,last) in ascending order,
2458 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
2459 * range @p [first,last-1).
2461 * The relative ordering of equivalent elements is preserved, so any two
2462 * elements @p x and @p y in the range @p [first,last) such that
2463 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
2464 * relative ordering after calling @p stable_sort().
2466 template<typename _RandomAccessIterator, typename _Compare>
2468 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
2470 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2471 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
2473 // concept requirements
2474 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2475 _RandomAccessIterator>)
2476 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
2477 _ValueType, _ValueType>)
2479 _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last);
2480 if (buf.begin() == 0)
2481 __inplace_stable_sort(__first, __last, __comp);
2483 __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()),
2488 * @brief Sort the smallest elements of a sequence.
2489 * @param first An iterator.
2490 * @param middle Another iterator.
2491 * @param last Another iterator.
2494 * Sorts the smallest @p (middle-first) elements in the range
2495 * @p [first,last) and moves them to the range @p [first,middle). The
2496 * order of the remaining elements in the range @p [middle,last) is
2498 * After the sort if @p i and @j are iterators in the range
2499 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2500 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2502 template<typename _RandomAccessIterator>
2504 partial_sort(_RandomAccessIterator __first,
2505 _RandomAccessIterator __middle,
2506 _RandomAccessIterator __last)
2508 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2510 // concept requirements
2511 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2512 _RandomAccessIterator>)
2513 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
2515 make_heap(__first, __middle);
2516 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2517 if (*__i < *__first)
2518 __pop_heap(__first, __middle, __i, _ValueType(*__i));
2519 sort_heap(__first, __middle);
2523 * @brief Sort the smallest elements of a sequence using a predicate
2525 * @param first An iterator.
2526 * @param middle Another iterator.
2527 * @param last Another iterator.
2528 * @param comp A comparison functor.
2531 * Sorts the smallest @p (middle-first) elements in the range
2532 * @p [first,last) and moves them to the range @p [first,middle). The
2533 * order of the remaining elements in the range @p [middle,last) is
2535 * After the sort if @p i and @j are iterators in the range
2536 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2537 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2540 template<typename _RandomAccessIterator, typename _Compare>
2542 partial_sort(_RandomAccessIterator __first,
2543 _RandomAccessIterator __middle,
2544 _RandomAccessIterator __last,
2547 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2549 // concept requirements
2550 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
2551 _RandomAccessIterator>)
2552 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
2553 _ValueType, _ValueType>)
2555 make_heap(__first, __middle, __comp);
2556 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2557 if (__comp(*__i, *__first))
2558 __pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
2559 sort_heap(__first, __middle, __comp);
2563 * @brief Copy the smallest elements of a sequence.
2564 * @param first An iterator.
2565 * @param last Another iterator.
2566 * @param result_first A random-access iterator.
2567 * @param result_last Another random-access iterator.
2568 * @return An iterator indicating the end of the resulting sequence.
2570 * Copies and sorts the smallest N values from the range @p [first,last)
2571 * to the range beginning at @p result_first, where the number of
2572 * elements to be copied, @p N, is the smaller of @p (last-first) and
2573 * @p (result_last-result_first).
2574 * After the sort if @p i and @j are iterators in the range
2575 * @p [result_first,result_first+N) such that @i precedes @j then
2576 * @p *j<*i is false.
2577 * The value returned is @p result_first+N.
2579 template<typename _InputIterator, typename _RandomAccessIterator>
2580 _RandomAccessIterator
2581 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2582 _RandomAccessIterator __result_first,
2583 _RandomAccessIterator __result_last)
2585 typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
2586 typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
2587 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
2589 // concept requirements
2590 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
2591 __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
2592 __glibcpp_function_requires(_LessThanComparableConcept<_OutputValueType>)
2593 __glibcpp_function_requires(_LessThanComparableConcept<_InputValueType>)
2595 if (__result_first == __result_last) return __result_last;
2596 _RandomAccessIterator __result_real_last = __result_first;
2597 while(__first != __last && __result_real_last != __result_last) {
2598 *__result_real_last = *__first;
2599 ++__result_real_last;
2602 make_heap(__result_first, __result_real_last);
2603 while (__first != __last) {
2604 if (*__first < *__result_first)
2605 __adjust_heap(__result_first, _DistanceType(0),
2606 _DistanceType(__result_real_last - __result_first),
2607 _InputValueType(*__first));
2610 sort_heap(__result_first, __result_real_last);
2611 return __result_real_last;
2615 * @brief Copy the smallest elements of a sequence using a predicate for
2617 * @param first An input iterator.
2618 * @param last Another input iterator.
2619 * @param result_first A random-access iterator.
2620 * @param result_last Another random-access iterator.
2621 * @param comp A comparison functor.
2622 * @return An iterator indicating the end of the resulting sequence.
2624 * Copies and sorts the smallest N values from the range @p [first,last)
2625 * to the range beginning at @p result_first, where the number of
2626 * elements to be copied, @p N, is the smaller of @p (last-first) and
2627 * @p (result_last-result_first).
2628 * After the sort if @p i and @j are iterators in the range
2629 * @p [result_first,result_first+N) such that @i precedes @j then
2630 * @p comp(*j,*i) is false.
2631 * The value returned is @p result_first+N.
2633 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2634 _RandomAccessIterator
2635 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2636 _RandomAccessIterator __result_first,
2637 _RandomAccessIterator __result_last,
2640 typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
2641 typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
2642 typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
2644 // concept requirements
2645 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
2646 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
2647 __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
2648 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
2649 _OutputValueType, _OutputValueType>)
2651 if (__result_first == __result_last) return __result_last;
2652 _RandomAccessIterator __result_real_last = __result_first;
2653 while(__first != __last && __result_real_last != __result_last) {
2654 *__result_real_last = *__first;
2655 ++__result_real_last;
2658 make_heap(__result_first, __result_real_last, __comp);
2659 while (__first != __last) {
2660 if (__comp(*__first, *__result_first))
2661 __adjust_heap(__result_first, _DistanceType(0),
2662 _DistanceType(__result_real_last - __result_first),
2663 _InputValueType(*__first),
2667 sort_heap(__result_first, __result_real_last, __comp);
2668 return __result_real_last;
2672 * @brief Sort a sequence just enough to find a particular position.
2673 * @param first An iterator.
2674 * @param nth Another iterator.
2675 * @param last Another iterator.
2678 * Rearranges the elements in the range @p [first,last) so that @p *nth
2679 * is the same element that would have been in that position had the
2680 * whole sequence been sorted.
2681 * whole sequence been sorted. The elements either side of @p *nth are
2682 * not completely sorted, but for any iterator @i in the range
2683 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
2684 * holds that @p *j<*i is false.
2686 template<typename _RandomAccessIterator>
2688 nth_element(_RandomAccessIterator __first,
2689 _RandomAccessIterator __nth,
2690 _RandomAccessIterator __last)
2692 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2694 // concept requirements
2695 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
2696 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
2698 while (__last - __first > 3) {
2699 _RandomAccessIterator __cut =
2700 __unguarded_partition(__first, __last,
2701 _ValueType(__median(*__first,
2702 *(__first + (__last - __first)/2),
2709 __insertion_sort(__first, __last);
2713 * @brief Sort a sequence just enough to find a particular position
2714 * using a predicate for comparison.
2715 * @param first An iterator.
2716 * @param nth Another iterator.
2717 * @param last Another iterator.
2718 * @param comp A comparison functor.
2721 * Rearranges the elements in the range @p [first,last) so that @p *nth
2722 * is the same element that would have been in that position had the
2723 * whole sequence been sorted. The elements either side of @p *nth are
2724 * not completely sorted, but for any iterator @i in the range
2725 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
2726 * holds that @p comp(*j,*i) is false.
2728 template<typename _RandomAccessIterator, typename _Compare>
2730 nth_element(_RandomAccessIterator __first,
2731 _RandomAccessIterator __nth,
2732 _RandomAccessIterator __last,
2735 typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
2737 // concept requirements
2738 __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
2739 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
2740 _ValueType, _ValueType>)
2742 while (__last - __first > 3) {
2743 _RandomAccessIterator __cut =
2744 __unguarded_partition(__first, __last,
2745 _ValueType(__median(*__first,
2746 *(__first + (__last - __first)/2),
2755 __insertion_sort(__first, __last, __comp);
2760 * @brief Finds the first position in which @a val could be inserted
2761 * without changing the ordering.
2762 * @param first An iterator.
2763 * @param last Another iterator.
2764 * @param val The search term.
2765 * @return An iterator pointing to the first element "not less than" @a val,
2766 * or end() if every element is less than @a val.
2767 * @ingroup binarysearch
2769 template<typename _ForwardIterator, typename _Tp>
2771 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
2773 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
2774 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
2776 // concept requirements
2777 // Note that these are slightly stricter than those of the 4-argument
2778 // version, defined next. The difference is in the strictness of the
2779 // comparison operations... so for looser checking, define your own
2780 // comparison function, as was intended.
2781 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2782 __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
2783 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
2785 _DistanceType __len = std::distance(__first, __last);
2786 _DistanceType __half;
2787 _ForwardIterator __middle;
2790 __half = __len >> 1;
2792 advance(__middle, __half);
2793 if (*__middle < __val) {
2796 __len = __len - __half - 1;
2805 * @brief Finds the first position in which @a val could be inserted
2806 * without changing the ordering.
2807 * @param first An iterator.
2808 * @param last Another iterator.
2809 * @param val The search term.
2810 * @param comp A functor to use for comparisons.
2811 * @return An iterator pointing to the first element "not less than" @a val,
2812 * or end() if every element is less than @a val.
2813 * @ingroup binarysearch
2815 * The comparison function should have the same effects on ordering as
2816 * the function used for the initial sort.
2818 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2820 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2821 const _Tp& __val, _Compare __comp)
2823 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
2824 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
2826 // concept requirements
2827 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2828 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
2830 _DistanceType __len = std::distance(__first, __last);
2831 _DistanceType __half;
2832 _ForwardIterator __middle;
2835 __half = __len >> 1;
2837 advance(__middle, __half);
2838 if (__comp(*__middle, __val)) {
2841 __len = __len - __half - 1;
2850 * @brief Finds the last position in which @a val could be inserted
2851 * without changing the ordering.
2852 * @param first An iterator.
2853 * @param last Another iterator.
2854 * @param val The search term.
2855 * @return An iterator pointing to the first element greater than @a val,
2856 * or end() if no elements are greater than @a val.
2857 * @ingroup binarysearch
2859 template<typename _ForwardIterator, typename _Tp>
2861 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
2863 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
2864 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
2866 // concept requirements
2867 // See comments on lower_bound.
2868 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2869 __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
2870 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
2872 _DistanceType __len = std::distance(__first, __last);
2873 _DistanceType __half;
2874 _ForwardIterator __middle;
2877 __half = __len >> 1;
2879 advance(__middle, __half);
2880 if (__val < *__middle)
2885 __len = __len - __half - 1;
2892 * @brief Finds the last position in which @a val could be inserted
2893 * without changing the ordering.
2894 * @param first An iterator.
2895 * @param last Another iterator.
2896 * @param val The search term.
2897 * @param comp A functor to use for comparisons.
2898 * @return An iterator pointing to the first element greater than @a val,
2899 * or end() if no elements are greater than @a val.
2900 * @ingroup binarysearch
2902 * The comparison function should have the same effects on ordering as
2903 * the function used for the initial sort.
2905 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2907 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2908 const _Tp& __val, _Compare __comp)
2910 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
2911 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
2913 // concept requirements
2914 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2915 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
2917 _DistanceType __len = std::distance(__first, __last);
2918 _DistanceType __half;
2919 _ForwardIterator __middle;
2922 __half = __len >> 1;
2924 advance(__middle, __half);
2925 if (__comp(__val, *__middle))
2930 __len = __len - __half - 1;
2937 * @brief Finds the largest subrange in which @a val could be inserted
2938 * at any place in it without changing the ordering.
2939 * @param first An iterator.
2940 * @param last Another iterator.
2941 * @param val The search term.
2942 * @return An pair of iterators defining the subrange.
2943 * @ingroup binarysearch
2945 * This is equivalent to
2947 * std::make_pair(lower_bound(first, last, val),
2948 * upper_bound(first, last, val))
2950 * but does not actually call those functions.
2952 template<typename _ForwardIterator, typename _Tp>
2953 pair<_ForwardIterator, _ForwardIterator>
2954 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
2956 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
2957 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
2959 // concept requirements
2960 // See comments on lower_bound.
2961 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2962 __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
2963 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
2965 _DistanceType __len = std::distance(__first, __last);
2966 _DistanceType __half;
2967 _ForwardIterator __middle, __left, __right;
2970 __half = __len >> 1;
2972 advance(__middle, __half);
2973 if (*__middle < __val) {
2976 __len = __len - __half - 1;
2978 else if (__val < *__middle)
2981 __left = lower_bound(__first, __middle, __val);
2982 advance(__first, __len);
2983 __right = upper_bound(++__middle, __first, __val);
2984 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2987 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2991 * @brief Finds the largest subrange in which @a val could be inserted
2992 * at any place in it without changing the ordering.
2993 * @param first An iterator.
2994 * @param last Another iterator.
2995 * @param val The search term.
2996 * @param comp A functor to use for comparisons.
2997 * @return An pair of iterators defining the subrange.
2998 * @ingroup binarysearch
3000 * This is equivalent to
3002 * std::make_pair(lower_bound(first, last, val, comp),
3003 * upper_bound(first, last, val, comp))
3005 * but does not actually call those functions.
3007 template<typename _ForwardIterator, typename _Tp, typename _Compare>
3008 pair<_ForwardIterator, _ForwardIterator>
3009 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val,
3012 typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
3013 typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
3015 // concept requirements
3016 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3017 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
3018 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
3020 _DistanceType __len = std::distance(__first, __last);
3021 _DistanceType __half;
3022 _ForwardIterator __middle, __left, __right;
3025 __half = __len >> 1;
3027 advance(__middle, __half);
3028 if (__comp(*__middle, __val)) {
3031 __len = __len - __half - 1;
3033 else if (__comp(__val, *__middle))
3036 __left = lower_bound(__first, __middle, __val, __comp);
3037 advance(__first, __len);
3038 __right = upper_bound(++__middle, __first, __val, __comp);
3039 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
3042 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3046 * @brief Determines whether an element exists in a range.
3047 * @param first An iterator.
3048 * @param last Another iterator.
3049 * @param val The search term.
3050 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3051 * @ingroup binarysearch
3053 * Note that this does not actually return an iterator to @a val. For
3054 * that, use std::find or a container's specialized find member functions.
3056 template<typename _ForwardIterator, typename _Tp>
3058 binary_search(_ForwardIterator __first, _ForwardIterator __last,
3061 // concept requirements
3062 // See comments on lower_bound.
3063 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3064 __glibcpp_function_requires(_SameTypeConcept<_Tp,
3065 typename iterator_traits<_ForwardIterator>::value_type>)
3066 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
3068 _ForwardIterator __i = lower_bound(__first, __last, __val);
3069 return __i != __last && !(__val < *__i);
3073 * @brief Determines whether an element exists in a range.
3074 * @param first An iterator.
3075 * @param last Another iterator.
3076 * @param val The search term.
3077 * @param comp A functor to use for comparisons.
3078 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3079 * @ingroup binarysearch
3081 * Note that this does not actually return an iterator to @a val. For
3082 * that, use std::find or a container's specialized find member functions.
3084 * The comparison function should have the same effects on ordering as
3085 * the function used for the initial sort.
3087 template<typename _ForwardIterator, typename _Tp, typename _Compare>
3089 binary_search(_ForwardIterator __first, _ForwardIterator __last,
3090 const _Tp& __val, _Compare __comp)
3092 // concept requirements
3093 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3094 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3095 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
3096 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
3097 typename iterator_traits<_ForwardIterator>::value_type>)
3099 _ForwardIterator __i = lower_bound(__first, __last, __val, __comp);
3100 return __i != __last && !__comp(__val, *__i);
3104 * @brief Merges two sorted ranges.
3105 * @param first1 An iterator.
3106 * @param first2 Another iterator.
3107 * @param last1 Another iterator.
3108 * @param last2 Another iterator.
3109 * @param result An iterator pointing to the end of the merged range.
3110 * @return An iterator pointing to the first element "not less than" @a val.
3112 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3113 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3114 * must be sorted, and the output range must not overlap with either of
3115 * the input ranges. The sort is @e stable, that is, for equivalent
3116 * elements in the two ranges, elements from the first range will always
3117 * come before elements from the second.
3119 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
3121 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3122 _InputIterator2 __first2, _InputIterator2 __last2,
3123 _OutputIterator __result)
3125 // concept requirements
3126 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3127 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3128 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3129 typename iterator_traits<_InputIterator1>::value_type>)
3130 __glibcpp_function_requires(_SameTypeConcept<
3131 typename iterator_traits<_InputIterator1>::value_type,
3132 typename iterator_traits<_InputIterator2>::value_type>)
3133 __glibcpp_function_requires(_LessThanComparableConcept<
3134 typename iterator_traits<_InputIterator1>::value_type>)
3136 while (__first1 != __last1 && __first2 != __last2) {
3137 if (*__first2 < *__first1) {
3138 *__result = *__first2;
3142 *__result = *__first1;
3147 return copy(__first2, __last2, copy(__first1, __last1, __result));
3151 * @brief Merges two sorted ranges.
3152 * @param first1 An iterator.
3153 * @param first2 Another iterator.
3154 * @param last1 Another iterator.
3155 * @param last2 Another iterator.
3156 * @param result An iterator pointing to the end of the merged range.
3157 * @param comp A functor to use for comparisons.
3158 * @return An iterator pointing to the first element "not less than" @a val.
3160 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3161 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3162 * must be sorted, and the output range must not overlap with either of
3163 * the input ranges. The sort is @e stable, that is, for equivalent
3164 * elements in the two ranges, elements from the first range will always
3165 * come before elements from the second.
3167 * The comparison function should have the same effects on ordering as
3168 * the function used for the initial sort.
3170 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
3173 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3174 _InputIterator2 __first2, _InputIterator2 __last2,
3175 _OutputIterator __result, _Compare __comp)
3177 // concept requirements
3178 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3179 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3180 __glibcpp_function_requires(_SameTypeConcept<
3181 typename iterator_traits<_InputIterator1>::value_type,
3182 typename iterator_traits<_InputIterator2>::value_type>)
3183 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3184 typename iterator_traits<_InputIterator1>::value_type>)
3185 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3186 typename iterator_traits<_InputIterator1>::value_type,
3187 typename iterator_traits<_InputIterator2>::value_type>)
3189 while (__first1 != __last1 && __first2 != __last2) {
3190 if (__comp(*__first2, *__first1)) {
3191 *__result = *__first2;
3195 *__result = *__first1;
3200 return copy(__first2, __last2, copy(__first1, __last1, __result));
3205 * This is a helper function for the merge routines.
3208 template<typename _BidirectionalIterator, typename _Distance>
3210 __merge_without_buffer(_BidirectionalIterator __first,
3211 _BidirectionalIterator __middle,
3212 _BidirectionalIterator __last,
3213 _Distance __len1, _Distance __len2)
3215 if (__len1 == 0 || __len2 == 0)
3217 if (__len1 + __len2 == 2) {
3218 if (*__middle < *__first)
3219 iter_swap(__first, __middle);
3222 _BidirectionalIterator __first_cut = __first;
3223 _BidirectionalIterator __second_cut = __middle;
3224 _Distance __len11 = 0;
3225 _Distance __len22 = 0;
3226 if (__len1 > __len2) {
3227 __len11 = __len1 / 2;
3228 advance(__first_cut, __len11);
3229 __second_cut = lower_bound(__middle, __last, *__first_cut);
3230 __len22 = std::distance(__middle, __second_cut);
3233 __len22 = __len2 / 2;
3234 advance(__second_cut, __len22);
3235 __first_cut = upper_bound(__first, __middle, *__second_cut);
3236 __len11 = std::distance(__first, __first_cut);
3238 rotate(__first_cut, __middle, __second_cut);
3239 _BidirectionalIterator __new_middle = __first_cut;
3240 advance(__new_middle, std::distance(__middle, __second_cut));
3241 __merge_without_buffer(__first, __first_cut, __new_middle,
3243 __merge_without_buffer(__new_middle, __second_cut, __last,
3244 __len1 - __len11, __len2 - __len22);
3249 * This is a helper function for the merge routines.
3252 template<typename _BidirectionalIterator, typename _Distance, typename _Compare>
3254 __merge_without_buffer(_BidirectionalIterator __first,
3255 _BidirectionalIterator __middle,
3256 _BidirectionalIterator __last,
3257 _Distance __len1, _Distance __len2,
3260 if (__len1 == 0 || __len2 == 0)
3262 if (__len1 + __len2 == 2) {
3263 if (__comp(*__middle, *__first))
3264 iter_swap(__first, __middle);
3267 _BidirectionalIterator __first_cut = __first;
3268 _BidirectionalIterator __second_cut = __middle;
3269 _Distance __len11 = 0;
3270 _Distance __len22 = 0;
3271 if (__len1 > __len2) {
3272 __len11 = __len1 / 2;
3273 advance(__first_cut, __len11);
3274 __second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
3275 __len22 = std::distance(__middle, __second_cut);
3278 __len22 = __len2 / 2;
3279 advance(__second_cut, __len22);
3280 __first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
3281 __len11 = std::distance(__first, __first_cut);
3283 rotate(__first_cut, __middle, __second_cut);
3284 _BidirectionalIterator __new_middle = __first_cut;
3285 advance(__new_middle, std::distance(__middle, __second_cut));
3286 __merge_without_buffer(__first, __first_cut, __new_middle,
3287 __len11, __len22, __comp);
3288 __merge_without_buffer(__new_middle, __second_cut, __last,
3289 __len1 - __len11, __len2 - __len22, __comp);
3294 * This is a helper function for the merge routines.
3297 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3299 _BidirectionalIterator1
3300 __rotate_adaptive(_BidirectionalIterator1 __first,
3301 _BidirectionalIterator1 __middle,
3302 _BidirectionalIterator1 __last,
3303 _Distance __len1, _Distance __len2,
3304 _BidirectionalIterator2 __buffer,
3305 _Distance __buffer_size)
3307 _BidirectionalIterator2 __buffer_end;
3308 if (__len1 > __len2 && __len2 <= __buffer_size) {
3309 __buffer_end = copy(__middle, __last, __buffer);
3310 copy_backward(__first, __middle, __last);
3311 return copy(__buffer, __buffer_end, __first);
3313 else if (__len1 <= __buffer_size) {
3314 __buffer_end = copy(__first, __middle, __buffer);
3315 copy(__middle, __last, __first);
3316 return copy_backward(__buffer, __buffer_end, __last);
3319 rotate(__first, __middle, __last);
3320 advance(__first, std::distance(__middle, __last));
3327 * This is a helper function for the merge routines.
3330 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3331 typename _BidirectionalIterator3>
3332 _BidirectionalIterator3
3333 __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
3334 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
3335 _BidirectionalIterator3 __result)
3337 if (__first1 == __last1)
3338 return copy_backward(__first2, __last2, __result);
3339 if (__first2 == __last2)
3340 return copy_backward(__first1, __last1, __result);
3344 if (*__last2 < *__last1) {
3345 *--__result = *__last1;
3346 if (__first1 == __last1)
3347 return copy_backward(__first2, ++__last2, __result);
3351 *--__result = *__last2;
3352 if (__first2 == __last2)
3353 return copy_backward(__first1, ++__last1, __result);
3361 * This is a helper function for the merge routines.
3364 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3365 typename _BidirectionalIterator3, typename _Compare>
3366 _BidirectionalIterator3
3367 __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
3368 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
3369 _BidirectionalIterator3 __result,
3372 if (__first1 == __last1)
3373 return copy_backward(__first2, __last2, __result);
3374 if (__first2 == __last2)
3375 return copy_backward(__first1, __last1, __result);
3379 if (__comp(*__last2, *__last1)) {
3380 *--__result = *__last1;
3381 if (__first1 == __last1)
3382 return copy_backward(__first2, ++__last2, __result);
3386 *--__result = *__last2;
3387 if (__first2 == __last2)
3388 return copy_backward(__first1, ++__last1, __result);
3396 * This is a helper function for the merge routines.
3399 template<typename _BidirectionalIterator, typename _Distance, typename _Pointer>
3401 __merge_adaptive(_BidirectionalIterator __first,
3402 _BidirectionalIterator __middle,
3403 _BidirectionalIterator __last,
3404 _Distance __len1, _Distance __len2,
3405 _Pointer __buffer, _Distance __buffer_size)
3407 if (__len1 <= __len2 && __len1 <= __buffer_size) {
3408 _Pointer __buffer_end = copy(__first, __middle, __buffer);
3409 merge(__buffer, __buffer_end, __middle, __last, __first);
3411 else if (__len2 <= __buffer_size) {
3412 _Pointer __buffer_end = copy(__middle, __last, __buffer);
3413 __merge_backward(__first, __middle, __buffer, __buffer_end, __last);
3416 _BidirectionalIterator __first_cut = __first;
3417 _BidirectionalIterator __second_cut = __middle;
3418 _Distance __len11 = 0;
3419 _Distance __len22 = 0;
3420 if (__len1 > __len2) {
3421 __len11 = __len1 / 2;
3422 advance(__first_cut, __len11);
3423 __second_cut = lower_bound(__middle, __last, *__first_cut);
3424 __len22 = std::distance(__middle, __second_cut);
3427 __len22 = __len2 / 2;
3428 advance(__second_cut, __len22);
3429 __first_cut = upper_bound(__first, __middle, *__second_cut);
3430 __len11 = std::distance(__first, __first_cut);
3432 _BidirectionalIterator __new_middle =
3433 __rotate_adaptive(__first_cut, __middle, __second_cut,
3434 __len1 - __len11, __len22, __buffer,
3436 __merge_adaptive(__first, __first_cut, __new_middle, __len11,
3437 __len22, __buffer, __buffer_size);
3438 __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11,
3439 __len2 - __len22, __buffer, __buffer_size);
3445 * This is a helper function for the merge routines.
3448 template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
3451 __merge_adaptive(_BidirectionalIterator __first,
3452 _BidirectionalIterator __middle,
3453 _BidirectionalIterator __last,
3454 _Distance __len1, _Distance __len2,
3455 _Pointer __buffer, _Distance __buffer_size,
3458 if (__len1 <= __len2 && __len1 <= __buffer_size) {
3459 _Pointer __buffer_end = copy(__first, __middle, __buffer);
3460 merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
3462 else if (__len2 <= __buffer_size) {
3463 _Pointer __buffer_end = copy(__middle, __last, __buffer);
3464 __merge_backward(__first, __middle, __buffer, __buffer_end, __last,
3468 _BidirectionalIterator __first_cut = __first;
3469 _BidirectionalIterator __second_cut = __middle;
3470 _Distance __len11 = 0;
3471 _Distance __len22 = 0;
3472 if (__len1 > __len2) {
3473 __len11 = __len1 / 2;
3474 advance(__first_cut, __len11);
3475 __second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
3476 __len22 = std::distance(__middle, __second_cut);
3479 __len22 = __len2 / 2;
3480 advance(__second_cut, __len22);
3481 __first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
3482 __len11 = std::distance(__first, __first_cut);
3484 _BidirectionalIterator __new_middle =
3485 __rotate_adaptive(__first_cut, __middle, __second_cut,
3486 __len1 - __len11, __len22, __buffer,
3488 __merge_adaptive(__first, __first_cut, __new_middle, __len11,
3489 __len22, __buffer, __buffer_size, __comp);
3490 __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11,
3491 __len2 - __len22, __buffer, __buffer_size, __comp);
3496 * @brief Merges two sorted ranges in place.
3497 * @param first An iterator.
3498 * @param middle Another iterator.
3499 * @param last Another iterator.
3502 * Merges two sorted and consecutive ranges, [first,middle) and
3503 * [middle,last), and puts the result in [first,last). The output will
3504 * be sorted. The sort is @e stable, that is, for equivalent
3505 * elements in the two ranges, elements from the first range will always
3506 * come before elements from the second.
3508 * If enough additional memory is available, this takes (last-first)-1
3509 * comparisons. Otherwise an NlogN algorithm is used, where N is
3510 * distance(first,last).
3512 template<typename _BidirectionalIterator>
3514 inplace_merge(_BidirectionalIterator __first,
3515 _BidirectionalIterator __middle,
3516 _BidirectionalIterator __last)
3518 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3520 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3523 // concept requirements
3524 __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
3525 _BidirectionalIterator>)
3526 __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
3528 if (__first == __middle || __middle == __last)
3531 _DistanceType __len1 = std::distance(__first, __middle);
3532 _DistanceType __len2 = std::distance(__middle, __last);
3534 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
3535 if (__buf.begin() == 0)
3536 __merge_without_buffer(__first, __middle, __last, __len1, __len2);
3538 __merge_adaptive(__first, __middle, __last, __len1, __len2,
3539 __buf.begin(), _DistanceType(__buf.size()));
3543 * @brief Merges two sorted ranges in place.
3544 * @param first An iterator.
3545 * @param middle Another iterator.
3546 * @param last Another iterator.
3547 * @param comp A functor to use for comparisons.
3550 * Merges two sorted and consecutive ranges, [first,middle) and
3551 * [middle,last), and puts the result in [first,last). The output will
3552 * be sorted. The sort is @e stable, that is, for equivalent
3553 * elements in the two ranges, elements from the first range will always
3554 * come before elements from the second.
3556 * If enough additional memory is available, this takes (last-first)-1
3557 * comparisons. Otherwise an NlogN algorithm is used, where N is
3558 * distance(first,last).
3560 * The comparison function should have the same effects on ordering as
3561 * the function used for the initial sort.
3563 template<typename _BidirectionalIterator, typename _Compare>
3565 inplace_merge(_BidirectionalIterator __first,
3566 _BidirectionalIterator __middle,
3567 _BidirectionalIterator __last,
3570 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3572 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3575 // concept requirements
3576 __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
3577 _BidirectionalIterator>)
3578 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3579 _ValueType, _ValueType>)
3581 if (__first == __middle || __middle == __last)
3584 _DistanceType __len1 = std::distance(__first, __middle);
3585 _DistanceType __len2 = std::distance(__middle, __last);
3587 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
3588 if (__buf.begin() == 0)
3589 __merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
3591 __merge_adaptive(__first, __middle, __last, __len1, __len2,
3592 __buf.begin(), _DistanceType(__buf.size()),
3596 // Set algorithms: includes, set_union, set_intersection, set_difference,
3597 // set_symmetric_difference. All of these algorithms have the precondition
3598 // that their input ranges are sorted and the postcondition that their output
3599 // ranges are sorted.
3601 template<typename _InputIterator1, typename _InputIterator2>
3603 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3604 _InputIterator2 __first2, _InputIterator2 __last2)
3606 // concept requirements
3607 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3608 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3609 __glibcpp_function_requires(_SameTypeConcept<
3610 typename iterator_traits<_InputIterator1>::value_type,
3611 typename iterator_traits<_InputIterator2>::value_type>)
3612 __glibcpp_function_requires(_LessThanComparableConcept<
3613 typename iterator_traits<_InputIterator1>::value_type>)
3615 while (__first1 != __last1 && __first2 != __last2)
3616 if (*__first2 < *__first1)
3618 else if(*__first1 < *__first2)
3621 ++__first1, ++__first2;
3623 return __first2 == __last2;
3626 template<typename _InputIterator1, typename _InputIterator2, typename _Compare>
3628 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3629 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
3631 // concept requirements
3632 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3633 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3634 __glibcpp_function_requires(_SameTypeConcept<
3635 typename iterator_traits<_InputIterator1>::value_type,
3636 typename iterator_traits<_InputIterator2>::value_type>)
3637 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3638 typename iterator_traits<_InputIterator1>::value_type,
3639 typename iterator_traits<_InputIterator2>::value_type>)
3641 while (__first1 != __last1 && __first2 != __last2)
3642 if (__comp(*__first2, *__first1))
3644 else if(__comp(*__first1, *__first2))
3647 ++__first1, ++__first2;
3649 return __first2 == __last2;
3652 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
3654 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
3655 _InputIterator2 __first2, _InputIterator2 __last2,
3656 _OutputIterator __result)
3658 // concept requirements
3659 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3660 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3661 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3662 typename iterator_traits<_InputIterator1>::value_type>)
3663 __glibcpp_function_requires(_SameTypeConcept<
3664 typename iterator_traits<_InputIterator1>::value_type,
3665 typename iterator_traits<_InputIterator2>::value_type>)
3666 __glibcpp_function_requires(_LessThanComparableConcept<
3667 typename iterator_traits<_InputIterator1>::value_type>)
3669 while (__first1 != __last1 && __first2 != __last2) {
3670 if (*__first1 < *__first2) {
3671 *__result = *__first1;
3674 else if (*__first2 < *__first1) {
3675 *__result = *__first2;
3679 *__result = *__first1;
3685 return copy(__first2, __last2, copy(__first1, __last1, __result));
3688 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
3691 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
3692 _InputIterator2 __first2, _InputIterator2 __last2,
3693 _OutputIterator __result, _Compare __comp)
3695 // concept requirements
3696 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3697 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3698 __glibcpp_function_requires(_SameTypeConcept<
3699 typename iterator_traits<_InputIterator1>::value_type,
3700 typename iterator_traits<_InputIterator2>::value_type>)
3701 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3702 typename iterator_traits<_InputIterator1>::value_type>)
3703 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3704 typename iterator_traits<_InputIterator1>::value_type,
3705 typename iterator_traits<_InputIterator2>::value_type>)
3707 while (__first1 != __last1 && __first2 != __last2) {
3708 if (__comp(*__first1, *__first2)) {
3709 *__result = *__first1;
3712 else if (__comp(*__first2, *__first1)) {
3713 *__result = *__first2;
3717 *__result = *__first1;
3723 return copy(__first2, __last2, copy(__first1, __last1, __result));
3726 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
3728 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
3729 _InputIterator2 __first2, _InputIterator2 __last2,
3730 _OutputIterator __result)
3732 // concept requirements
3733 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3734 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3735 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3736 typename iterator_traits<_InputIterator1>::value_type>)
3737 __glibcpp_function_requires(_SameTypeConcept<
3738 typename iterator_traits<_InputIterator1>::value_type,
3739 typename iterator_traits<_InputIterator2>::value_type>)
3740 __glibcpp_function_requires(_LessThanComparableConcept<
3741 typename iterator_traits<_InputIterator1>::value_type>)
3743 while (__first1 != __last1 && __first2 != __last2)
3744 if (*__first1 < *__first2)
3746 else if (*__first2 < *__first1)
3749 *__result = *__first1;
3757 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
3760 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
3761 _InputIterator2 __first2, _InputIterator2 __last2,
3762 _OutputIterator __result, _Compare __comp)
3764 // concept requirements
3765 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3766 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3767 __glibcpp_function_requires(_SameTypeConcept<
3768 typename iterator_traits<_InputIterator1>::value_type,
3769 typename iterator_traits<_InputIterator2>::value_type>)
3770 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3771 typename iterator_traits<_InputIterator1>::value_type>)
3772 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3773 typename iterator_traits<_InputIterator1>::value_type,
3774 typename iterator_traits<_InputIterator2>::value_type>)
3776 while (__first1 != __last1 && __first2 != __last2)
3777 if (__comp(*__first1, *__first2))
3779 else if (__comp(*__first2, *__first1))
3782 *__result = *__first1;
3790 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
3792 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
3793 _InputIterator2 __first2, _InputIterator2 __last2,
3794 _OutputIterator __result)
3796 // concept requirements
3797 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3798 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3799 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3800 typename iterator_traits<_InputIterator1>::value_type>)
3801 __glibcpp_function_requires(_SameTypeConcept<
3802 typename iterator_traits<_InputIterator1>::value_type,
3803 typename iterator_traits<_InputIterator2>::value_type>)
3804 __glibcpp_function_requires(_LessThanComparableConcept<
3805 typename iterator_traits<_InputIterator1>::value_type>)
3807 while (__first1 != __last1 && __first2 != __last2)
3808 if (*__first1 < *__first2) {
3809 *__result = *__first1;
3813 else if (*__first2 < *__first1)
3819 return copy(__first1, __last1, __result);
3822 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
3825 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
3826 _InputIterator2 __first2, _InputIterator2 __last2,
3827 _OutputIterator __result, _Compare __comp)
3829 // concept requirements
3830 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3831 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3832 __glibcpp_function_requires(_SameTypeConcept<
3833 typename iterator_traits<_InputIterator1>::value_type,
3834 typename iterator_traits<_InputIterator2>::value_type>)
3835 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3836 typename iterator_traits<_InputIterator1>::value_type>)
3837 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3838 typename iterator_traits<_InputIterator1>::value_type,
3839 typename iterator_traits<_InputIterator2>::value_type>)
3841 while (__first1 != __last1 && __first2 != __last2)
3842 if (__comp(*__first1, *__first2)) {
3843 *__result = *__first1;
3847 else if (__comp(*__first2, *__first1))
3853 return copy(__first1, __last1, __result);
3856 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
3858 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
3859 _InputIterator2 __first2, _InputIterator2 __last2,
3860 _OutputIterator __result)
3862 // concept requirements
3863 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3864 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3865 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3866 typename iterator_traits<_InputIterator1>::value_type>)
3867 __glibcpp_function_requires(_SameTypeConcept<
3868 typename iterator_traits<_InputIterator1>::value_type,
3869 typename iterator_traits<_InputIterator2>::value_type>)
3870 __glibcpp_function_requires(_LessThanComparableConcept<
3871 typename iterator_traits<_InputIterator1>::value_type>)
3873 while (__first1 != __last1 && __first2 != __last2)
3874 if (*__first1 < *__first2) {
3875 *__result = *__first1;
3879 else if (*__first2 < *__first1) {
3880 *__result = *__first2;
3888 return copy(__first2, __last2, copy(__first1, __last1, __result));
3891 template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
3894 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
3895 _InputIterator2 __first2, _InputIterator2 __last2,
3896 _OutputIterator __result,
3899 // concept requirements
3900 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
3901 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
3902 __glibcpp_function_requires(_SameTypeConcept<
3903 typename iterator_traits<_InputIterator1>::value_type,
3904 typename iterator_traits<_InputIterator2>::value_type>)
3905 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
3906 typename iterator_traits<_InputIterator1>::value_type>)
3907 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3908 typename iterator_traits<_InputIterator1>::value_type,
3909 typename iterator_traits<_InputIterator2>::value_type>)
3911 while (__first1 != __last1 && __first2 != __last2)
3912 if (__comp(*__first1, *__first2)) {
3913 *__result = *__first1;
3917 else if (__comp(*__first2, *__first1)) {
3918 *__result = *__first2;
3926 return copy(__first2, __last2, copy(__first1, __last1, __result));
3929 // min_element and max_element, with and without an explicitly supplied
3930 // comparison function.
3932 template<typename _ForwardIterator>
3934 max_element(_ForwardIterator __first, _ForwardIterator __last)
3936 // concept requirements
3937 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3938 __glibcpp_function_requires(_LessThanComparableConcept<
3939 typename iterator_traits<_ForwardIterator>::value_type>)
3941 if (__first == __last) return __first;
3942 _ForwardIterator __result = __first;
3943 while (++__first != __last)
3944 if (*__result < *__first)
3949 template<typename _ForwardIterator, typename _Compare>
3951 max_element(_ForwardIterator __first, _ForwardIterator __last,
3954 // concept requirements
3955 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3956 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3957 typename iterator_traits<_ForwardIterator>::value_type,
3958 typename iterator_traits<_ForwardIterator>::value_type>)
3960 if (__first == __last) return __first;
3961 _ForwardIterator __result = __first;
3962 while (++__first != __last)
3963 if (__comp(*__result, *__first)) __result = __first;
3967 template<typename _ForwardIterator>
3969 min_element(_ForwardIterator __first, _ForwardIterator __last)
3971 // concept requirements
3972 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3973 __glibcpp_function_requires(_LessThanComparableConcept<
3974 typename iterator_traits<_ForwardIterator>::value_type>)
3976 if (__first == __last) return __first;
3977 _ForwardIterator __result = __first;
3978 while (++__first != __last)
3979 if (*__first < *__result)
3984 template<typename _ForwardIterator, typename _Compare>
3986 min_element(_ForwardIterator __first, _ForwardIterator __last,
3989 // concept requirements
3990 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3991 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
3992 typename iterator_traits<_ForwardIterator>::value_type,
3993 typename iterator_traits<_ForwardIterator>::value_type>)
3995 if (__first == __last) return __first;
3996 _ForwardIterator __result = __first;
3997 while (++__first != __last)
3998 if (__comp(*__first, *__result))
4003 // next_permutation and prev_permutation, with and without an explicitly
4004 // supplied comparison function.
4006 template<typename _BidirectionalIterator>
4008 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
4010 // concept requirements
4011 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
4012 __glibcpp_function_requires(_LessThanComparableConcept<
4013 typename iterator_traits<_BidirectionalIterator>::value_type>)
4015 if (__first == __last)
4017 _BidirectionalIterator __i = __first;
4025 _BidirectionalIterator __ii = __i;
4028 _BidirectionalIterator __j = __last;
4029 while (!(*__i < *--__j))
4031 iter_swap(__i, __j);
4032 reverse(__ii, __last);
4035 if (__i == __first) {
4036 reverse(__first, __last);
4042 template<typename _BidirectionalIterator, typename _Compare>
4044 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
4047 // concept requirements
4048 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
4049 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
4050 typename iterator_traits<_BidirectionalIterator>::value_type,
4051 typename iterator_traits<_BidirectionalIterator>::value_type>)
4053 if (__first == __last)
4055 _BidirectionalIterator __i = __first;
4063 _BidirectionalIterator __ii = __i;
4065 if (__comp(*__i, *__ii)) {
4066 _BidirectionalIterator __j = __last;
4067 while (!__comp(*__i, *--__j))
4069 iter_swap(__i, __j);
4070 reverse(__ii, __last);
4073 if (__i == __first) {
4074 reverse(__first, __last);
4080 template<typename _BidirectionalIterator>
4082 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
4084 // concept requirements
4085 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
4086 __glibcpp_function_requires(_LessThanComparableConcept<
4087 typename iterator_traits<_BidirectionalIterator>::value_type>)
4089 if (__first == __last)
4091 _BidirectionalIterator __i = __first;
4099 _BidirectionalIterator __ii = __i;
4102 _BidirectionalIterator __j = __last;
4103 while (!(*--__j < *__i))
4105 iter_swap(__i, __j);
4106 reverse(__ii, __last);
4109 if (__i == __first) {
4110 reverse(__first, __last);
4116 template<typename _BidirectionalIterator, typename _Compare>
4118 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
4121 // concept requirements
4122 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
4123 __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
4124 typename iterator_traits<_BidirectionalIterator>::value_type,
4125 typename iterator_traits<_BidirectionalIterator>::value_type>)
4127 if (__first == __last)
4129 _BidirectionalIterator __i = __first;
4137 _BidirectionalIterator __ii = __i;
4139 if (__comp(*__ii, *__i)) {
4140 _BidirectionalIterator __j = __last;
4141 while (!__comp(*--__j, *__i))
4143 iter_swap(__i, __j);
4144 reverse(__ii, __last);
4147 if (__i == __first) {
4148 reverse(__first, __last);
4154 // find_first_of, with and without an explicitly supplied comparison function.
4156 template<typename _InputIterator, typename _ForwardIterator>
4158 find_first_of(_InputIterator __first1, _InputIterator __last1,
4159 _ForwardIterator __first2, _ForwardIterator __last2)
4161 // concept requirements
4162 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
4163 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4164 __glibcpp_function_requires(_EqualOpConcept<
4165 typename iterator_traits<_InputIterator>::value_type,
4166 typename iterator_traits<_ForwardIterator>::value_type>)
4168 for ( ; __first1 != __last1; ++__first1)
4169 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4170 if (*__first1 == *__iter)
4175 template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate>
4177 find_first_of(_InputIterator __first1, _InputIterator __last1,
4178 _ForwardIterator __first2, _ForwardIterator __last2,
4179 _BinaryPredicate __comp)
4181 // concept requirements
4182 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
4183 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4184 __glibcpp_function_requires(_EqualOpConcept<
4185 typename iterator_traits<_InputIterator>::value_type,
4186 typename iterator_traits<_ForwardIterator>::value_type>)
4187 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4188 typename iterator_traits<_InputIterator>::value_type,
4189 typename iterator_traits<_ForwardIterator>::value_type>)
4191 for ( ; __first1 != __last1; ++__first1)
4192 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4193 if (__comp(*__first1, *__iter))
4199 // find_end, with and without an explicitly supplied comparison function.
4200 // Search [first2, last2) as a subsequence in [first1, last1), and return
4201 // the *last* possible match. Note that find_end for bidirectional iterators
4202 // is much faster than for forward iterators.
4204 // find_end for forward iterators.
4205 template<typename _ForwardIterator1, typename _ForwardIterator2>
4207 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4208 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4209 forward_iterator_tag, forward_iterator_tag)
4211 if (__first2 == __last2)
4214 _ForwardIterator1 __result = __last1;
4216 _ForwardIterator1 __new_result
4217 = search(__first1, __last1, __first2, __last2);
4218 if (__new_result == __last1)
4221 __result = __new_result;
4222 __first1 = __new_result;
4229 template<typename _ForwardIterator1, typename _ForwardIterator2,
4230 typename _BinaryPredicate>
4232 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4233 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4234 forward_iterator_tag, forward_iterator_tag,
4235 _BinaryPredicate __comp)
4237 if (__first2 == __last2)
4240 _ForwardIterator1 __result = __last1;
4242 _ForwardIterator1 __new_result
4243 = search(__first1, __last1, __first2, __last2, __comp);
4244 if (__new_result == __last1)
4247 __result = __new_result;
4248 __first1 = __new_result;
4255 // find_end for bidirectional iterators. Requires partial specialization.
4256 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
4257 _BidirectionalIterator1
4258 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
4259 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
4260 bidirectional_iterator_tag, bidirectional_iterator_tag)
4262 // concept requirements
4263 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator1>)
4264 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator2>)
4266 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
4267 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
4269 _RevIterator1 __rlast1(__first1);
4270 _RevIterator2 __rlast2(__first2);
4271 _RevIterator1 __rresult = search(_RevIterator1(__last1), __rlast1,
4272 _RevIterator2(__last2), __rlast2);
4274 if (__rresult == __rlast1)
4277 _BidirectionalIterator1 __result = __rresult.base();
4278 advance(__result, -std::distance(__first2, __last2));
4283 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
4284 typename _BinaryPredicate>
4285 _BidirectionalIterator1
4286 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
4287 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
4288 bidirectional_iterator_tag, bidirectional_iterator_tag,
4289 _BinaryPredicate __comp)
4291 // concept requirements
4292 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator1>)
4293 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator2>)
4295 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
4296 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
4298 _RevIterator1 __rlast1(__first1);
4299 _RevIterator2 __rlast2(__first2);
4300 _RevIterator1 __rresult = search(_RevIterator1(__last1), __rlast1,
4301 _RevIterator2(__last2), __rlast2,
4304 if (__rresult == __rlast1)
4307 _BidirectionalIterator1 __result = __rresult.base();
4308 advance(__result, -std::distance(__first2, __last2));
4313 // Dispatching functions for find_end.
4315 template<typename _ForwardIterator1, typename _ForwardIterator2>
4316 inline _ForwardIterator1
4317 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4318 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4320 // concept requirements
4321 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4322 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4323 __glibcpp_function_requires(_EqualOpConcept<
4324 typename iterator_traits<_ForwardIterator1>::value_type,
4325 typename iterator_traits<_ForwardIterator2>::value_type>)
4327 return __find_end(__first1, __last1, __first2, __last2,
4328 __iterator_category(__first1),
4329 __iterator_category(__first2));
4332 template<typename _ForwardIterator1, typename _ForwardIterator2,
4333 typename _BinaryPredicate>
4334 inline _ForwardIterator1
4335 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4336 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4337 _BinaryPredicate __comp)
4339 // concept requirements
4340 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4341 __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4342 __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4343 typename iterator_traits<_ForwardIterator1>::value_type,
4344 typename iterator_traits<_ForwardIterator2>::value_type>)
4346 return __find_end(__first1, __last1, __first2, __last2,
4347 __iterator_category(__first1),
4348 __iterator_category(__first2),
4354 #endif /* __GLIBCPP_INTERNAL_ALGO_H */