1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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.
64 #include <bits/stl_heap.h>
65 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
66 #include <debug/debug.h>
68 // See concept_check.h for the __glibcxx_*_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 __glibcxx_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 __glibcxx_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 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
156 __glibcxx_requires_valid_range(__first, __last);
157 for ( ; __first != __last; ++__first)
164 * This is an overload used by find() for the Input Iterator case.
167 template<typename _InputIterator, typename _Tp>
168 inline _InputIterator
169 find(_InputIterator __first, _InputIterator __last,
170 const _Tp& __val, input_iterator_tag)
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,
185 _Predicate __pred, input_iterator_tag)
187 while (__first != __last && !__pred(*__first))
194 * This is an overload used by find() for the RAI case.
197 template<typename _RandomAccessIterator, typename _Tp>
198 _RandomAccessIterator
199 find(_RandomAccessIterator __first, _RandomAccessIterator __last,
200 const _Tp& __val, random_access_iterator_tag)
202 typename iterator_traits<_RandomAccessIterator>::difference_type
203 __trip_count = (__last - __first) >> 2;
205 for ( ; __trip_count > 0 ; --__trip_count)
207 if (*__first == __val)
211 if (*__first == __val)
215 if (*__first == __val)
219 if (*__first == __val)
224 switch (__last - __first)
227 if (*__first == __val)
231 if (*__first == __val)
235 if (*__first == __val)
246 * This is an overload used by find_if() for the RAI case.
249 template<typename _RandomAccessIterator, typename _Predicate>
250 _RandomAccessIterator
251 find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
252 _Predicate __pred, random_access_iterator_tag)
254 typename iterator_traits<_RandomAccessIterator>::difference_type
255 __trip_count = (__last - __first) >> 2;
257 for ( ; __trip_count > 0 ; --__trip_count)
259 if (__pred(*__first))
263 if (__pred(*__first))
267 if (__pred(*__first))
271 if (__pred(*__first))
276 switch (__last - __first)
279 if (__pred(*__first))
283 if (__pred(*__first))
287 if (__pred(*__first))
297 * @brief Find the first occurrence of a value in a sequence.
298 * @param first An input iterator.
299 * @param last An input iterator.
300 * @param val The value to find.
301 * @return The first iterator @c i in the range @p [first,last)
302 * such that @c *i == @p val, or @p last if no such iterator exists.
304 template<typename _InputIterator, typename _Tp>
305 inline _InputIterator
306 find(_InputIterator __first, _InputIterator __last,
309 // concept requirements
310 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
311 __glibcxx_function_requires(_EqualOpConcept<
312 typename iterator_traits<_InputIterator>::value_type, _Tp>)
313 __glibcxx_requires_valid_range(__first, __last);
314 return std::find(__first, __last, __val,
315 std::__iterator_category(__first));
319 * @brief Find the first element in a sequence for which a predicate is true.
320 * @param first An input iterator.
321 * @param last An input iterator.
322 * @param pred A predicate.
323 * @return The first iterator @c i in the range @p [first,last)
324 * such that @p pred(*i) is true, or @p last if no such iterator exists.
326 template<typename _InputIterator, typename _Predicate>
327 inline _InputIterator
328 find_if(_InputIterator __first, _InputIterator __last,
331 // concept requirements
332 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
333 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
334 typename iterator_traits<_InputIterator>::value_type>)
335 __glibcxx_requires_valid_range(__first, __last);
336 return std::find_if(__first, __last, __pred,
337 std::__iterator_category(__first));
341 * @brief Find two adjacent values in a sequence that are equal.
342 * @param first A forward iterator.
343 * @param last A forward iterator.
344 * @return The first iterator @c i such that @c i and @c i+1 are both
345 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
346 * or @p last if no such iterator exists.
348 template<typename _ForwardIterator>
350 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
352 // concept requirements
353 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
354 __glibcxx_function_requires(_EqualityComparableConcept<
355 typename iterator_traits<_ForwardIterator>::value_type>)
356 __glibcxx_requires_valid_range(__first, __last);
357 if (__first == __last)
359 _ForwardIterator __next = __first;
360 while(++__next != __last)
362 if (*__first == *__next)
370 * @brief Find two adjacent values in a sequence using a predicate.
371 * @param first A forward iterator.
372 * @param last A forward iterator.
373 * @param binary_pred A binary predicate.
374 * @return The first iterator @c i such that @c i and @c i+1 are both
375 * valid iterators in @p [first,last) and such that
376 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
379 template<typename _ForwardIterator, typename _BinaryPredicate>
381 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
382 _BinaryPredicate __binary_pred)
384 // concept requirements
385 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
386 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
387 typename iterator_traits<_ForwardIterator>::value_type,
388 typename iterator_traits<_ForwardIterator>::value_type>)
389 __glibcxx_requires_valid_range(__first, __last);
390 if (__first == __last)
392 _ForwardIterator __next = __first;
393 while(++__next != __last)
395 if (__binary_pred(*__first, *__next))
403 * @brief Count the number of copies of a value in a sequence.
404 * @param first An input iterator.
405 * @param last An input iterator.
406 * @param value The value to be counted.
407 * @return The number of iterators @c i in the range @p [first,last)
408 * for which @c *i == @p value
410 template<typename _InputIterator, typename _Tp>
411 typename iterator_traits<_InputIterator>::difference_type
412 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
414 // concept requirements
415 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
416 __glibcxx_function_requires(_EqualOpConcept<
417 typename iterator_traits<_InputIterator>::value_type, _Tp>)
418 __glibcxx_requires_valid_range(__first, __last);
419 typename iterator_traits<_InputIterator>::difference_type __n = 0;
420 for ( ; __first != __last; ++__first)
421 if (*__first == __value)
427 * @brief Count the elements of a sequence for which a predicate is true.
428 * @param first An input iterator.
429 * @param last An input iterator.
430 * @param pred A predicate.
431 * @return The number of iterators @c i in the range @p [first,last)
432 * for which @p pred(*i) is true.
434 template<typename _InputIterator, typename _Predicate>
435 typename iterator_traits<_InputIterator>::difference_type
436 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
438 // concept requirements
439 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
440 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
441 typename iterator_traits<_InputIterator>::value_type>)
442 __glibcxx_requires_valid_range(__first, __last);
443 typename iterator_traits<_InputIterator>::difference_type __n = 0;
444 for ( ; __first != __last; ++__first)
445 if (__pred(*__first))
451 * @brief Search a sequence for a matching sub-sequence.
452 * @param first1 A forward iterator.
453 * @param last1 A forward iterator.
454 * @param first2 A forward iterator.
455 * @param last2 A forward iterator.
456 * @return The first iterator @c i in the range
457 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
458 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
459 * such iterator exists.
461 * Searches the range @p [first1,last1) for a sub-sequence that compares
462 * equal value-by-value with the sequence given by @p [first2,last2) and
463 * returns an iterator to the first element of the sub-sequence, or
464 * @p last1 if the sub-sequence is not found.
466 * Because the sub-sequence must lie completely within the range
467 * @p [first1,last1) it must start at a position less than
468 * @p last1-(last2-first2) where @p last2-first2 is the length of the
470 * This means that the returned iterator @c i will be in the range
471 * @p [first1,last1-(last2-first2))
473 template<typename _ForwardIterator1, typename _ForwardIterator2>
475 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
476 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
478 // concept requirements
479 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
481 __glibcxx_function_requires(_EqualOpConcept<
482 typename iterator_traits<_ForwardIterator1>::value_type,
483 typename iterator_traits<_ForwardIterator2>::value_type>)
484 __glibcxx_requires_valid_range(__first1, __last1);
485 __glibcxx_requires_valid_range(__first2, __last2);
486 // Test for empty ranges
487 if (__first1 == __last1 || __first2 == __last2)
490 // Test for a pattern of length 1.
491 _ForwardIterator2 __tmp(__first2);
493 if (__tmp == __last2)
494 return std::find(__first1, __last1, *__first2);
497 _ForwardIterator2 __p1, __p;
498 __p1 = __first2; ++__p1;
499 _ForwardIterator1 __current = __first1;
501 while (__first1 != __last1)
503 __first1 = std::find(__first1, __last1, *__first2);
504 if (__first1 == __last1)
508 __current = __first1;
509 if (++__current == __last1)
512 while (*__current == *__p)
514 if (++__p == __last2)
516 if (++__current == __last1)
525 * @brief Search a sequence for a matching sub-sequence using a predicate.
526 * @param first1 A forward iterator.
527 * @param last1 A forward iterator.
528 * @param first2 A forward iterator.
529 * @param last2 A forward iterator.
530 * @param predicate A binary predicate.
531 * @return The first iterator @c i in the range
532 * @p [first1,last1-(last2-first2)) such that
533 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
534 * @p [0,last2-first2), or @p last1 if no such iterator exists.
536 * Searches the range @p [first1,last1) for a sub-sequence that compares
537 * equal value-by-value with the sequence given by @p [first2,last2),
538 * using @p predicate to determine equality, and returns an iterator
539 * to the first element of the sub-sequence, or @p last1 if no such
542 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
544 template<typename _ForwardIterator1, typename _ForwardIterator2,
545 typename _BinaryPredicate>
547 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
548 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
549 _BinaryPredicate __predicate)
551 // concept requirements
552 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
553 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
554 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
555 typename iterator_traits<_ForwardIterator1>::value_type,
556 typename iterator_traits<_ForwardIterator2>::value_type>)
557 __glibcxx_requires_valid_range(__first1, __last1);
558 __glibcxx_requires_valid_range(__first2, __last2);
560 // Test for empty ranges
561 if (__first1 == __last1 || __first2 == __last2)
564 // Test for a pattern of length 1.
565 _ForwardIterator2 __tmp(__first2);
567 if (__tmp == __last2)
569 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
575 _ForwardIterator2 __p1, __p;
576 __p1 = __first2; ++__p1;
577 _ForwardIterator1 __current = __first1;
579 while (__first1 != __last1)
581 while (__first1 != __last1)
583 if (__predicate(*__first1, *__first2))
587 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
589 if (__first1 == __last1)
593 __current = __first1;
594 if (++__current == __last1)
597 while (__predicate(*__current, *__p))
599 if (++__p == __last2)
601 if (++__current == __last1)
610 * @brief Search a sequence for a number of consecutive values.
611 * @param first A forward iterator.
612 * @param last A forward iterator.
613 * @param count The number of consecutive values.
614 * @param val The value to find.
615 * @return The first iterator @c i in the range @p [first,last-count)
616 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
617 * or @p last if no such iterator exists.
619 * Searches the range @p [first,last) for @p count consecutive elements
622 template<typename _ForwardIterator, typename _Integer, typename _Tp>
624 search_n(_ForwardIterator __first, _ForwardIterator __last,
625 _Integer __count, const _Tp& __val)
627 // concept requirements
628 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
629 __glibcxx_function_requires(_EqualOpConcept<
630 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
631 __glibcxx_requires_valid_range(__first, __last);
637 __first = std::find(__first, __last, __val);
638 while (__first != __last)
640 typename iterator_traits<_ForwardIterator>::difference_type
642 _ForwardIterator __i = __first;
644 while (__i != __last && __n != 1 && *__i == __val)
652 __first = std::find(__i, __last, __val);
659 * @brief Search a sequence for a number of consecutive values using a
661 * @param first A forward iterator.
662 * @param last A forward iterator.
663 * @param count The number of consecutive values.
664 * @param val The value to find.
665 * @param binary_pred A binary predicate.
666 * @return The first iterator @c i in the range @p [first,last-count)
667 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
668 * range @p [0,count), or @p last if no such iterator exists.
670 * Searches the range @p [first,last) for @p count consecutive elements
671 * for which the predicate returns true.
673 template<typename _ForwardIterator, typename _Integer, typename _Tp,
674 typename _BinaryPredicate>
676 search_n(_ForwardIterator __first, _ForwardIterator __last,
677 _Integer __count, const _Tp& __val,
678 _BinaryPredicate __binary_pred)
680 // concept requirements
681 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
682 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
683 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
684 __glibcxx_requires_valid_range(__first, __last);
690 while (__first != __last)
692 if (__binary_pred(*__first, __val))
696 while (__first != __last)
698 typename iterator_traits<_ForwardIterator>::difference_type
700 _ForwardIterator __i = __first;
702 while (__i != __last && __n != 1 && __binary_pred(*__i, __val))
711 while (__i != __last)
713 if (__binary_pred(*__i, __val))
725 * @brief Swap the elements of two sequences.
726 * @param first1 A forward iterator.
727 * @param last1 A forward iterator.
728 * @param first2 A forward iterator.
729 * @return An iterator equal to @p first2+(last1-first1).
731 * Swaps each element in the range @p [first1,last1) with the
732 * corresponding element in the range @p [first2,(last1-first1)).
733 * The ranges must not overlap.
735 template<typename _ForwardIterator1, typename _ForwardIterator2>
737 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
738 _ForwardIterator2 __first2)
740 // concept requirements
741 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
743 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
745 __glibcxx_function_requires(_ConvertibleConcept<
746 typename iterator_traits<_ForwardIterator1>::value_type,
747 typename iterator_traits<_ForwardIterator2>::value_type>)
748 __glibcxx_function_requires(_ConvertibleConcept<
749 typename iterator_traits<_ForwardIterator2>::value_type,
750 typename iterator_traits<_ForwardIterator1>::value_type>)
751 __glibcxx_requires_valid_range(__first1, __last1);
753 for ( ; __first1 != __last1; ++__first1, ++__first2)
754 std::iter_swap(__first1, __first2);
759 * @brief Perform an operation on a sequence.
760 * @param first An input iterator.
761 * @param last An input iterator.
762 * @param result An output iterator.
763 * @param unary_op A unary operator.
764 * @return An output iterator equal to @p result+(last-first).
766 * Applies the operator to each element in the input range and assigns
767 * the results to successive elements of the output sequence.
768 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
769 * range @p [0,last-first).
771 * @p unary_op must not alter its argument.
773 template<typename _InputIterator, typename _OutputIterator,
774 typename _UnaryOperation>
776 transform(_InputIterator __first, _InputIterator __last,
777 _OutputIterator __result, _UnaryOperation __unary_op)
779 // concept requirements
780 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
781 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
782 // "the type returned by a _UnaryOperation"
783 __typeof__(__unary_op(*__first))>)
784 __glibcxx_requires_valid_range(__first, __last);
786 for ( ; __first != __last; ++__first, ++__result)
787 *__result = __unary_op(*__first);
792 * @brief Perform an operation on corresponding elements of two sequences.
793 * @param first1 An input iterator.
794 * @param last1 An input iterator.
795 * @param first2 An input iterator.
796 * @param result An output iterator.
797 * @param binary_op A binary operator.
798 * @return An output iterator equal to @p result+(last-first).
800 * Applies the operator to the corresponding elements in the two
801 * input ranges and assigns the results to successive elements of the
803 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
804 * @c N in the range @p [0,last1-first1).
806 * @p binary_op must not alter either of its arguments.
808 template<typename _InputIterator1, typename _InputIterator2,
809 typename _OutputIterator, typename _BinaryOperation>
811 transform(_InputIterator1 __first1, _InputIterator1 __last1,
812 _InputIterator2 __first2, _OutputIterator __result,
813 _BinaryOperation __binary_op)
815 // concept requirements
816 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
817 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
818 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
819 // "the type returned by a _BinaryOperation"
820 __typeof__(__binary_op(*__first1,*__first2))>)
821 __glibcxx_requires_valid_range(__first1, __last1);
823 for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
824 *__result = __binary_op(*__first1, *__first2);
829 * @brief Replace each occurrence of one value in a sequence with another
831 * @param first A forward iterator.
832 * @param last A forward iterator.
833 * @param old_value The value to be replaced.
834 * @param new_value The replacement value.
835 * @return replace() returns no value.
837 * For each iterator @c i in the range @p [first,last) if @c *i ==
838 * @p old_value then the assignment @c *i = @p new_value is performed.
840 template<typename _ForwardIterator, typename _Tp>
842 replace(_ForwardIterator __first, _ForwardIterator __last,
843 const _Tp& __old_value, const _Tp& __new_value)
845 // concept requirements
846 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
848 __glibcxx_function_requires(_EqualOpConcept<
849 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
850 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
851 typename iterator_traits<_ForwardIterator>::value_type>)
852 __glibcxx_requires_valid_range(__first, __last);
854 for ( ; __first != __last; ++__first)
855 if (*__first == __old_value)
856 *__first = __new_value;
860 * @brief Replace each value in a sequence for which a predicate returns
861 * true with another value.
862 * @param first A forward iterator.
863 * @param last A forward iterator.
864 * @param pred A predicate.
865 * @param new_value The replacement value.
866 * @return replace_if() returns no value.
868 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
869 * is true then the assignment @c *i = @p new_value is performed.
871 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
873 replace_if(_ForwardIterator __first, _ForwardIterator __last,
874 _Predicate __pred, const _Tp& __new_value)
876 // concept requirements
877 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
879 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
880 typename iterator_traits<_ForwardIterator>::value_type>)
881 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
882 typename iterator_traits<_ForwardIterator>::value_type>)
883 __glibcxx_requires_valid_range(__first, __last);
885 for ( ; __first != __last; ++__first)
886 if (__pred(*__first))
887 *__first = __new_value;
891 * @brief Copy a sequence, replacing each element of one value with another
893 * @param first An input iterator.
894 * @param last An input iterator.
895 * @param result An output iterator.
896 * @param old_value The value to be replaced.
897 * @param new_value The replacement value.
898 * @return The end of the output sequence, @p result+(last-first).
900 * Copies each element in the input range @p [first,last) to the
901 * output range @p [result,result+(last-first)) replacing elements
902 * equal to @p old_value with @p new_value.
904 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
906 replace_copy(_InputIterator __first, _InputIterator __last,
907 _OutputIterator __result,
908 const _Tp& __old_value, const _Tp& __new_value)
910 // concept requirements
911 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
912 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
913 typename iterator_traits<_InputIterator>::value_type>)
914 __glibcxx_function_requires(_EqualOpConcept<
915 typename iterator_traits<_InputIterator>::value_type, _Tp>)
916 __glibcxx_requires_valid_range(__first, __last);
918 for ( ; __first != __last; ++__first, ++__result)
919 *__result = *__first == __old_value ? __new_value : *__first;
924 * @brief Copy a sequence, replacing each value for which a predicate
925 * returns true with another value.
926 * @param first An input iterator.
927 * @param last An input iterator.
928 * @param result An output iterator.
929 * @param pred A predicate.
930 * @param new_value The replacement value.
931 * @return The end of the output sequence, @p result+(last-first).
933 * Copies each element in the range @p [first,last) to the range
934 * @p [result,result+(last-first)) replacing elements for which
935 * @p pred returns true with @p new_value.
937 template<typename _InputIterator, typename _OutputIterator,
938 typename _Predicate, typename _Tp>
940 replace_copy_if(_InputIterator __first, _InputIterator __last,
941 _OutputIterator __result,
942 _Predicate __pred, const _Tp& __new_value)
944 // concept requirements
945 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
946 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
947 typename iterator_traits<_InputIterator>::value_type>)
948 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
949 typename iterator_traits<_InputIterator>::value_type>)
950 __glibcxx_requires_valid_range(__first, __last);
952 for ( ; __first != __last; ++__first, ++__result)
953 *__result = __pred(*__first) ? __new_value : *__first;
958 * @brief Assign the result of a function object to each value in a
960 * @param first A forward iterator.
961 * @param last A forward iterator.
962 * @param gen A function object taking no arguments.
963 * @return generate() returns no value.
965 * Performs the assignment @c *i = @p gen() for each @c i in the range
968 template<typename _ForwardIterator, typename _Generator>
970 generate(_ForwardIterator __first, _ForwardIterator __last,
973 // concept requirements
974 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
975 __glibcxx_function_requires(_GeneratorConcept<_Generator,
976 typename iterator_traits<_ForwardIterator>::value_type>)
977 __glibcxx_requires_valid_range(__first, __last);
979 for ( ; __first != __last; ++__first)
984 * @brief Assign the result of a function object to each value in a
986 * @param first A forward iterator.
987 * @param n The length of the sequence.
988 * @param gen A function object taking no arguments.
989 * @return The end of the sequence, @p first+n
991 * Performs the assignment @c *i = @p gen() for each @c i in the range
992 * @p [first,first+n).
994 template<typename _OutputIterator, typename _Size, typename _Generator>
996 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
998 // concept requirements
999 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1000 // "the type returned by a _Generator"
1001 __typeof__(__gen())>)
1003 for ( ; __n > 0; --__n, ++__first)
1009 * @brief Copy a sequence, removing elements of a given value.
1010 * @param first An input iterator.
1011 * @param last An input iterator.
1012 * @param result An output iterator.
1013 * @param value The value to be removed.
1014 * @return An iterator designating the end of the resulting sequence.
1016 * Copies each element in the range @p [first,last) not equal to @p value
1017 * to the range beginning at @p result.
1018 * remove_copy() is stable, so the relative order of elements that are
1019 * copied is unchanged.
1021 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
1023 remove_copy(_InputIterator __first, _InputIterator __last,
1024 _OutputIterator __result, const _Tp& __value)
1026 // concept requirements
1027 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1028 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1029 typename iterator_traits<_InputIterator>::value_type>)
1030 __glibcxx_function_requires(_EqualOpConcept<
1031 typename iterator_traits<_InputIterator>::value_type, _Tp>)
1032 __glibcxx_requires_valid_range(__first, __last);
1034 for ( ; __first != __last; ++__first)
1035 if (!(*__first == __value))
1037 *__result = *__first;
1044 * @brief Copy a sequence, removing elements for which a predicate is true.
1045 * @param first An input iterator.
1046 * @param last An input iterator.
1047 * @param result An output iterator.
1048 * @param pred A predicate.
1049 * @return An iterator designating the end of the resulting sequence.
1051 * Copies each element in the range @p [first,last) for which
1052 * @p pred returns true to the range beginning at @p result.
1054 * remove_copy_if() is stable, so the relative order of elements that are
1055 * copied is unchanged.
1057 template<typename _InputIterator, typename _OutputIterator,
1058 typename _Predicate>
1060 remove_copy_if(_InputIterator __first, _InputIterator __last,
1061 _OutputIterator __result, _Predicate __pred)
1063 // concept requirements
1064 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1065 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1066 typename iterator_traits<_InputIterator>::value_type>)
1067 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1068 typename iterator_traits<_InputIterator>::value_type>)
1069 __glibcxx_requires_valid_range(__first, __last);
1071 for ( ; __first != __last; ++__first)
1072 if (!__pred(*__first))
1074 *__result = *__first;
1081 * @brief Remove elements from a sequence.
1082 * @param first An input iterator.
1083 * @param last An input iterator.
1084 * @param value The value to be removed.
1085 * @return An iterator designating the end of the resulting sequence.
1087 * All elements equal to @p value are removed from the range
1090 * remove() is stable, so the relative order of elements that are
1091 * not removed is unchanged.
1093 * Elements between the end of the resulting sequence and @p last
1094 * are still present, but their value is unspecified.
1096 template<typename _ForwardIterator, typename _Tp>
1098 remove(_ForwardIterator __first, _ForwardIterator __last,
1101 // concept requirements
1102 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1104 __glibcxx_function_requires(_EqualOpConcept<
1105 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1106 __glibcxx_requires_valid_range(__first, __last);
1108 __first = std::find(__first, __last, __value);
1109 _ForwardIterator __i = __first;
1110 return __first == __last ? __first
1111 : std::remove_copy(++__i, __last,
1116 * @brief Remove elements from a sequence using a predicate.
1117 * @param first A forward iterator.
1118 * @param last A forward iterator.
1119 * @param pred A predicate.
1120 * @return An iterator designating the end of the resulting sequence.
1122 * All elements for which @p pred returns true are removed from the range
1125 * remove_if() is stable, so the relative order of elements that are
1126 * not removed is unchanged.
1128 * Elements between the end of the resulting sequence and @p last
1129 * are still present, but their value is unspecified.
1131 template<typename _ForwardIterator, typename _Predicate>
1133 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1136 // concept requirements
1137 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1139 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1140 typename iterator_traits<_ForwardIterator>::value_type>)
1141 __glibcxx_requires_valid_range(__first, __last);
1143 __first = std::find_if(__first, __last, __pred);
1144 _ForwardIterator __i = __first;
1145 return __first == __last ? __first
1146 : std::remove_copy_if(++__i, __last,
1152 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1154 * overloaded for output iterators.
1157 template<typename _InputIterator, typename _OutputIterator>
1159 __unique_copy(_InputIterator __first, _InputIterator __last,
1160 _OutputIterator __result,
1161 output_iterator_tag)
1163 // concept requirements -- taken care of in dispatching function
1164 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1165 *__result = __value;
1166 while (++__first != __last)
1167 if (!(__value == *__first))
1170 *++__result = __value;
1177 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1179 * overloaded for forward iterators.
1182 template<typename _InputIterator, typename _ForwardIterator>
1184 __unique_copy(_InputIterator __first, _InputIterator __last,
1185 _ForwardIterator __result,
1186 forward_iterator_tag)
1188 // concept requirements -- taken care of in dispatching function
1189 *__result = *__first;
1190 while (++__first != __last)
1191 if (!(*__result == *__first))
1192 *++__result = *__first;
1198 * This is an uglified
1199 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1201 * overloaded for output iterators.
1204 template<typename _InputIterator, typename _OutputIterator,
1205 typename _BinaryPredicate>
1207 __unique_copy(_InputIterator __first, _InputIterator __last,
1208 _OutputIterator __result,
1209 _BinaryPredicate __binary_pred,
1210 output_iterator_tag)
1212 // concept requirements -- iterators already checked
1213 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1214 typename iterator_traits<_InputIterator>::value_type,
1215 typename iterator_traits<_InputIterator>::value_type>)
1217 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1218 *__result = __value;
1219 while (++__first != __last)
1220 if (!__binary_pred(__value, *__first))
1223 *++__result = __value;
1230 * This is an uglified
1231 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1233 * overloaded for forward iterators.
1236 template<typename _InputIterator, typename _ForwardIterator,
1237 typename _BinaryPredicate>
1239 __unique_copy(_InputIterator __first, _InputIterator __last,
1240 _ForwardIterator __result,
1241 _BinaryPredicate __binary_pred,
1242 forward_iterator_tag)
1244 // concept requirements -- iterators already checked
1245 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1246 typename iterator_traits<_ForwardIterator>::value_type,
1247 typename iterator_traits<_InputIterator>::value_type>)
1249 *__result = *__first;
1250 while (++__first != __last)
1251 if (!__binary_pred(*__result, *__first)) *++__result = *__first;
1256 * @brief Copy a sequence, removing consecutive duplicate values.
1257 * @param first An input iterator.
1258 * @param last An input iterator.
1259 * @param result An output iterator.
1260 * @return An iterator designating the end of the resulting sequence.
1262 * Copies each element in the range @p [first,last) to the range
1263 * beginning at @p result, except that only the first element is copied
1264 * from groups of consecutive elements that compare equal.
1265 * unique_copy() is stable, so the relative order of elements that are
1266 * copied is unchanged.
1268 template<typename _InputIterator, typename _OutputIterator>
1269 inline _OutputIterator
1270 unique_copy(_InputIterator __first, _InputIterator __last,
1271 _OutputIterator __result)
1273 // concept requirements
1274 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1275 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1276 typename iterator_traits<_InputIterator>::value_type>)
1277 __glibcxx_function_requires(_EqualityComparableConcept<
1278 typename iterator_traits<_InputIterator>::value_type>)
1279 __glibcxx_requires_valid_range(__first, __last);
1281 typedef typename iterator_traits<_OutputIterator>::iterator_category
1284 if (__first == __last) return __result;
1285 return std::__unique_copy(__first, __last, __result, _IterType());
1289 * @brief Copy a sequence, removing consecutive values using a predicate.
1290 * @param first An input iterator.
1291 * @param last An input iterator.
1292 * @param result An output iterator.
1293 * @param binary_pred A binary predicate.
1294 * @return An iterator designating the end of the resulting sequence.
1296 * Copies each element in the range @p [first,last) to the range
1297 * beginning at @p result, except that only the first element is copied
1298 * from groups of consecutive elements for which @p binary_pred returns
1300 * unique_copy() is stable, so the relative order of elements that are
1301 * copied is unchanged.
1303 template<typename _InputIterator, typename _OutputIterator,
1304 typename _BinaryPredicate>
1305 inline _OutputIterator
1306 unique_copy(_InputIterator __first, _InputIterator __last,
1307 _OutputIterator __result,
1308 _BinaryPredicate __binary_pred)
1310 // concept requirements -- predicates checked later
1311 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1312 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1313 typename iterator_traits<_InputIterator>::value_type>)
1314 __glibcxx_requires_valid_range(__first, __last);
1316 typedef typename iterator_traits<_OutputIterator>::iterator_category
1319 if (__first == __last) return __result;
1320 return std::__unique_copy(__first, __last, __result,
1321 __binary_pred, _IterType());
1325 * @brief Remove consecutive duplicate values from a sequence.
1326 * @param first A forward iterator.
1327 * @param last A forward iterator.
1328 * @return An iterator designating the end of the resulting sequence.
1330 * Removes all but the first element from each group of consecutive
1331 * values that compare equal.
1332 * unique() is stable, so the relative order of elements that are
1333 * not removed is unchanged.
1334 * Elements between the end of the resulting sequence and @p last
1335 * are still present, but their value is unspecified.
1337 template<typename _ForwardIterator>
1339 unique(_ForwardIterator __first, _ForwardIterator __last)
1341 // concept requirements
1342 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1344 __glibcxx_function_requires(_EqualityComparableConcept<
1345 typename iterator_traits<_ForwardIterator>::value_type>)
1346 __glibcxx_requires_valid_range(__first, __last);
1348 // Skip the beginning, if already unique.
1349 __first = std::adjacent_find(__first, __last);
1350 if (__first == __last)
1353 // Do the real copy work.
1354 _ForwardIterator __dest = __first;
1356 while (++__first != __last)
1357 if (!(*__dest == *__first))
1358 *++__dest = *__first;
1363 * @brief Remove consecutive values from a sequence using a predicate.
1364 * @param first A forward iterator.
1365 * @param last A forward iterator.
1366 * @param binary_pred A binary predicate.
1367 * @return An iterator designating the end of the resulting sequence.
1369 * Removes all but the first element from each group of consecutive
1370 * values for which @p binary_pred returns true.
1371 * unique() is stable, so the relative order of elements that are
1372 * not removed is unchanged.
1373 * Elements between the end of the resulting sequence and @p last
1374 * are still present, but their value is unspecified.
1376 template<typename _ForwardIterator, typename _BinaryPredicate>
1378 unique(_ForwardIterator __first, _ForwardIterator __last,
1379 _BinaryPredicate __binary_pred)
1381 // concept requirements
1382 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1384 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1385 typename iterator_traits<_ForwardIterator>::value_type,
1386 typename iterator_traits<_ForwardIterator>::value_type>)
1387 __glibcxx_requires_valid_range(__first, __last);
1389 // Skip the beginning, if already unique.
1390 __first = std::adjacent_find(__first, __last, __binary_pred);
1391 if (__first == __last)
1394 // Do the real copy work.
1395 _ForwardIterator __dest = __first;
1397 while (++__first != __last)
1398 if (!__binary_pred(*__dest, *__first))
1399 *++__dest = *__first;
1405 * This is an uglified reverse(_BidirectionalIterator,
1406 * _BidirectionalIterator)
1407 * overloaded for bidirectional iterators.
1410 template<typename _BidirectionalIterator>
1412 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1413 bidirectional_iterator_tag)
1416 if (__first == __last || __first == --__last)
1420 std::iter_swap(__first, __last);
1427 * This is an uglified reverse(_BidirectionalIterator,
1428 * _BidirectionalIterator)
1429 * overloaded for random access iterators.
1432 template<typename _RandomAccessIterator>
1434 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1435 random_access_iterator_tag)
1437 if (__first == __last)
1440 while (__first < __last)
1442 std::iter_swap(__first, __last);
1449 * @brief Reverse a sequence.
1450 * @param first A bidirectional iterator.
1451 * @param last A bidirectional iterator.
1452 * @return reverse() returns no value.
1454 * Reverses the order of the elements in the range @p [first,last),
1455 * so that the first element becomes the last etc.
1456 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1457 * swaps @p *(first+i) and @p *(last-(i+1))
1459 template<typename _BidirectionalIterator>
1461 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1463 // concept requirements
1464 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1465 _BidirectionalIterator>)
1466 __glibcxx_requires_valid_range(__first, __last);
1467 std::__reverse(__first, __last, std::__iterator_category(__first));
1471 * @brief Copy a sequence, reversing its elements.
1472 * @param first A bidirectional iterator.
1473 * @param last A bidirectional iterator.
1474 * @param result An output iterator.
1475 * @return An iterator designating the end of the resulting sequence.
1477 * Copies the elements in the range @p [first,last) to the range
1478 * @p [result,result+(last-first)) such that the order of the
1479 * elements is reversed.
1480 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1481 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1482 * The ranges @p [first,last) and @p [result,result+(last-first))
1485 template<typename _BidirectionalIterator, typename _OutputIterator>
1487 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1488 _OutputIterator __result)
1490 // concept requirements
1491 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1492 _BidirectionalIterator>)
1493 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1494 typename iterator_traits<_BidirectionalIterator>::value_type>)
1495 __glibcxx_requires_valid_range(__first, __last);
1497 while (__first != __last)
1500 *__result = *__last;
1509 * This is a helper function for the rotate algorithm specialized on RAIs.
1510 * It returns the greatest common divisor of two integer values.
1513 template<typename _EuclideanRingElement>
1514 _EuclideanRingElement
1515 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1519 _EuclideanRingElement __t = __m % __n;
1528 * This is a helper function for the rotate algorithm.
1531 template<typename _ForwardIterator>
1533 __rotate(_ForwardIterator __first,
1534 _ForwardIterator __middle,
1535 _ForwardIterator __last,
1536 forward_iterator_tag)
1538 if (__first == __middle || __last == __middle)
1541 _ForwardIterator __first2 = __middle;
1544 swap(*__first, *__first2);
1547 if (__first == __middle)
1548 __middle = __first2;
1550 while (__first2 != __last);
1552 __first2 = __middle;
1554 while (__first2 != __last)
1556 swap(*__first, *__first2);
1559 if (__first == __middle)
1560 __middle = __first2;
1561 else if (__first2 == __last)
1562 __first2 = __middle;
1568 * This is a helper function for the rotate algorithm.
1571 template<typename _BidirectionalIterator>
1573 __rotate(_BidirectionalIterator __first,
1574 _BidirectionalIterator __middle,
1575 _BidirectionalIterator __last,
1576 bidirectional_iterator_tag)
1578 // concept requirements
1579 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1580 _BidirectionalIterator>)
1582 if (__first == __middle || __last == __middle)
1585 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1586 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1588 while (__first != __middle && __middle != __last)
1590 swap(*__first, *--__last);
1594 if (__first == __middle)
1595 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1597 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1602 * This is a helper function for the rotate algorithm.
1605 template<typename _RandomAccessIterator>
1607 __rotate(_RandomAccessIterator __first,
1608 _RandomAccessIterator __middle,
1609 _RandomAccessIterator __last,
1610 random_access_iterator_tag)
1612 // concept requirements
1613 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1614 _RandomAccessIterator>)
1616 if (__first == __middle || __last == __middle)
1619 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1621 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1624 const _Distance __n = __last - __first;
1625 const _Distance __k = __middle - __first;
1626 const _Distance __l = __n - __k;
1630 std::swap_ranges(__first, __middle, __middle);
1634 const _Distance __d = __gcd(__n, __k);
1636 for (_Distance __i = 0; __i < __d; __i++)
1638 _ValueType __tmp = *__first;
1639 _RandomAccessIterator __p = __first;
1643 for (_Distance __j = 0; __j < __l / __d; __j++)
1645 if (__p > __first + __l)
1647 *__p = *(__p - __l);
1651 *__p = *(__p + __k);
1657 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
1659 if (__p < __last - __k)
1661 *__p = *(__p + __k);
1664 *__p = * (__p - __l);
1675 * @brief Rotate the elements of a sequence.
1676 * @param first A forward iterator.
1677 * @param middle A forward iterator.
1678 * @param last A forward iterator.
1681 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1682 * positions so that the element at @p middle is moved to @p first, the
1683 * element at @p middle+1 is moved to @first+1 and so on for each element
1684 * in the range @p [first,last).
1686 * This effectively swaps the ranges @p [first,middle) and
1689 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1690 * each @p n in the range @p [0,last-first).
1692 template<typename _ForwardIterator>
1694 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1695 _ForwardIterator __last)
1697 // concept requirements
1698 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1700 __glibcxx_requires_valid_range(__first, __middle);
1701 __glibcxx_requires_valid_range(__middle, __last);
1703 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1705 std::__rotate(__first, __middle, __last, _IterType());
1709 * @brief Copy a sequence, rotating its elements.
1710 * @param first A forward iterator.
1711 * @param middle A forward iterator.
1712 * @param last A forward iterator.
1713 * @param result An output iterator.
1714 * @return An iterator designating the end of the resulting sequence.
1716 * Copies the elements of the range @p [first,last) to the range
1717 * beginning at @result, rotating the copied elements by @p (middle-first)
1718 * positions so that the element at @p middle is moved to @p result, the
1719 * element at @p middle+1 is moved to @result+1 and so on for each element
1720 * in the range @p [first,last).
1722 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1723 * each @p n in the range @p [0,last-first).
1725 template<typename _ForwardIterator, typename _OutputIterator>
1727 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1728 _ForwardIterator __last, _OutputIterator __result)
1730 // concept requirements
1731 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1732 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1733 typename iterator_traits<_ForwardIterator>::value_type>)
1734 __glibcxx_requires_valid_range(__first, __middle);
1735 __glibcxx_requires_valid_range(__middle, __last);
1737 return std::copy(__first, __middle,
1738 std::copy(__middle, __last, __result));
1742 * @brief Randomly shuffle the elements of a sequence.
1743 * @param first A forward iterator.
1744 * @param last A forward iterator.
1747 * Reorder the elements in the range @p [first,last) using a random
1748 * distribution, so that every possible ordering of the sequence is
1751 template<typename _RandomAccessIterator>
1753 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
1755 // concept requirements
1756 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1757 _RandomAccessIterator>)
1758 __glibcxx_requires_valid_range(__first, __last);
1760 if (__first != __last)
1761 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1762 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
1766 * @brief Shuffle the elements of a sequence using a random number
1768 * @param first A forward iterator.
1769 * @param last A forward iterator.
1770 * @param rand The RNG functor or function.
1773 * Reorders the elements in the range @p [first,last) using @p rand to
1774 * provide a random distribution. Calling @p rand(N) for a positive
1775 * integer @p N should return a randomly chosen integer from the
1778 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
1780 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
1781 _RandomNumberGenerator& __rand)
1783 // concept requirements
1784 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1785 _RandomAccessIterator>)
1786 __glibcxx_requires_valid_range(__first, __last);
1788 if (__first == __last)
1790 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1791 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
1797 * This is a helper function...
1800 template<typename _ForwardIterator, typename _Predicate>
1802 __partition(_ForwardIterator __first, _ForwardIterator __last,
1804 forward_iterator_tag)
1806 if (__first == __last)
1809 while (__pred(*__first))
1810 if (++__first == __last)
1813 _ForwardIterator __next = __first;
1815 while (++__next != __last)
1816 if (__pred(*__next))
1818 swap(*__first, *__next);
1827 * This is a helper function...
1830 template<typename _BidirectionalIterator, typename _Predicate>
1831 _BidirectionalIterator
1832 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1834 bidirectional_iterator_tag)
1839 if (__first == __last)
1841 else if (__pred(*__first))
1847 if (__first == __last)
1849 else if (!__pred(*__last))
1853 std::iter_swap(__first, __last);
1859 * @brief Move elements for which a predicate is true to the beginning
1861 * @param first A forward iterator.
1862 * @param last A forward iterator.
1863 * @param pred A predicate functor.
1864 * @return An iterator @p middle such that @p pred(i) is true for each
1865 * iterator @p i in the range @p [first,middle) and false for each @p i
1866 * in the range @p [middle,last).
1868 * @p pred must not modify its operand. @p partition() does not preserve
1869 * the relative ordering of elements in each group, use
1870 * @p stable_partition() if this is needed.
1872 template<typename _ForwardIterator, typename _Predicate>
1873 inline _ForwardIterator
1874 partition(_ForwardIterator __first, _ForwardIterator __last,
1877 // concept requirements
1878 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1880 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1881 typename iterator_traits<_ForwardIterator>::value_type>)
1882 __glibcxx_requires_valid_range(__first, __last);
1884 return std::__partition(__first, __last, __pred,
1885 std::__iterator_category(__first));
1891 * This is a helper function...
1894 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1896 __inplace_stable_partition(_ForwardIterator __first,
1897 _ForwardIterator __last,
1898 _Predicate __pred, _Distance __len)
1901 return __pred(*__first) ? __last : __first;
1902 _ForwardIterator __middle = __first;
1903 std::advance(__middle, __len / 2);
1904 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1908 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1912 std::rotate(__begin, __middle, __end);
1913 std::advance(__begin, std::distance(__middle, __end));
1919 * This is a helper function...
1922 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1925 __stable_partition_adaptive(_ForwardIterator __first,
1926 _ForwardIterator __last,
1927 _Predicate __pred, _Distance __len,
1929 _Distance __buffer_size)
1931 if (__len <= __buffer_size)
1933 _ForwardIterator __result1 = __first;
1934 _Pointer __result2 = __buffer;
1935 for ( ; __first != __last ; ++__first)
1936 if (__pred(*__first))
1938 *__result1 = *__first;
1943 *__result2 = *__first;
1946 std::copy(__buffer, __result2, __result1);
1951 _ForwardIterator __middle = __first;
1952 std::advance(__middle, __len / 2);
1953 _ForwardIterator __begin =
1954 std::__stable_partition_adaptive(__first, __middle, __pred,
1955 __len / 2, __buffer,
1957 _ForwardIterator __end =
1958 std::__stable_partition_adaptive(__middle, __last, __pred,
1960 __buffer, __buffer_size);
1961 std::rotate(__begin, __middle, __end);
1962 std::advance(__begin, std::distance(__middle, __end));
1968 * @brief Move elements for which a predicate is true to the beginning
1969 * of a sequence, preserving relative ordering.
1970 * @param first A forward iterator.
1971 * @param last A forward iterator.
1972 * @param pred A predicate functor.
1973 * @return An iterator @p middle such that @p pred(i) is true for each
1974 * iterator @p i in the range @p [first,middle) and false for each @p i
1975 * in the range @p [middle,last).
1977 * Performs the same function as @p partition() with the additional
1978 * guarantee that the relative ordering of elements in each group is
1979 * preserved, so any two elements @p x and @p y in the range
1980 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1981 * relative ordering after calling @p stable_partition().
1983 template<typename _ForwardIterator, typename _Predicate>
1985 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1988 // concept requirements
1989 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1991 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1992 typename iterator_traits<_ForwardIterator>::value_type>)
1993 __glibcxx_requires_valid_range(__first, __last);
1995 if (__first == __last)
1999 typedef typename iterator_traits<_ForwardIterator>::value_type
2001 typedef typename iterator_traits<_ForwardIterator>::difference_type
2004 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
2006 if (__buf.size() > 0)
2008 std::__stable_partition_adaptive(__first, __last, __pred,
2009 _DistanceType(__buf.requested_size()),
2010 __buf.begin(), __buf.size());
2013 std::__inplace_stable_partition(__first, __last, __pred,
2014 _DistanceType(__buf.requested_size()));
2020 * This is a helper function...
2023 template<typename _RandomAccessIterator, typename _Tp>
2024 _RandomAccessIterator
2025 __unguarded_partition(_RandomAccessIterator __first,
2026 _RandomAccessIterator __last, _Tp __pivot)
2030 while (*__first < __pivot)
2033 while (__pivot < *__last)
2035 if (!(__first < __last))
2037 std::iter_swap(__first, __last);
2044 * This is a helper function...
2047 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2048 _RandomAccessIterator
2049 __unguarded_partition(_RandomAccessIterator __first,
2050 _RandomAccessIterator __last,
2051 _Tp __pivot, _Compare __comp)
2055 while (__comp(*__first, __pivot))
2058 while (__comp(__pivot, *__last))
2060 if (!(__first < __last))
2062 std::iter_swap(__first, __last);
2070 * This controls some aspect of the sort routines.
2073 enum { _S_threshold = 16 };
2077 * This is a helper function for the sort routine.
2080 template<typename _RandomAccessIterator, typename _Tp>
2082 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
2084 _RandomAccessIterator __next = __last;
2086 while (__val < *__next)
2097 * This is a helper function for the sort routine.
2100 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2102 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
2105 _RandomAccessIterator __next = __last;
2107 while (__comp(__val, *__next))
2118 * This is a helper function for the sort routine.
2121 template<typename _RandomAccessIterator>
2123 __insertion_sort(_RandomAccessIterator __first,
2124 _RandomAccessIterator __last)
2126 if (__first == __last)
2129 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2131 typename iterator_traits<_RandomAccessIterator>::value_type
2133 if (__val < *__first)
2135 std::copy_backward(__first, __i, __i + 1);
2139 std::__unguarded_linear_insert(__i, __val);
2145 * This is a helper function for the sort routine.
2148 template<typename _RandomAccessIterator, typename _Compare>
2150 __insertion_sort(_RandomAccessIterator __first,
2151 _RandomAccessIterator __last, _Compare __comp)
2153 if (__first == __last) return;
2155 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2157 typename iterator_traits<_RandomAccessIterator>::value_type
2159 if (__comp(__val, *__first))
2161 std::copy_backward(__first, __i, __i + 1);
2165 std::__unguarded_linear_insert(__i, __val, __comp);
2171 * This is a helper function for the sort routine.
2174 template<typename _RandomAccessIterator>
2176 __unguarded_insertion_sort(_RandomAccessIterator __first,
2177 _RandomAccessIterator __last)
2179 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2182 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2183 std::__unguarded_linear_insert(__i, _ValueType(*__i));
2188 * This is a helper function for the sort routine.
2191 template<typename _RandomAccessIterator, typename _Compare>
2193 __unguarded_insertion_sort(_RandomAccessIterator __first,
2194 _RandomAccessIterator __last, _Compare __comp)
2196 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2199 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2200 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2205 * This is a helper function for the sort routine.
2208 template<typename _RandomAccessIterator>
2210 __final_insertion_sort(_RandomAccessIterator __first,
2211 _RandomAccessIterator __last)
2213 if (__last - __first > int(_S_threshold))
2215 std::__insertion_sort(__first, __first + int(_S_threshold));
2216 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2219 std::__insertion_sort(__first, __last);
2224 * This is a helper function for the sort routine.
2227 template<typename _RandomAccessIterator, typename _Compare>
2229 __final_insertion_sort(_RandomAccessIterator __first,
2230 _RandomAccessIterator __last, _Compare __comp)
2232 if (__last - __first > int(_S_threshold))
2234 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2235 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2239 std::__insertion_sort(__first, __last, __comp);
2244 * This is a helper function for the sort routine.
2247 template<typename _Size>
2252 for (__k = 0; __n != 1; __n >>= 1)
2258 * @brief Sort the smallest elements of a sequence.
2259 * @param first An iterator.
2260 * @param middle Another iterator.
2261 * @param last Another iterator.
2264 * Sorts the smallest @p (middle-first) elements in the range
2265 * @p [first,last) and moves them to the range @p [first,middle). The
2266 * order of the remaining elements in the range @p [middle,last) is
2268 * After the sort if @p i and @j are iterators in the range
2269 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2270 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2272 template<typename _RandomAccessIterator>
2274 partial_sort(_RandomAccessIterator __first,
2275 _RandomAccessIterator __middle,
2276 _RandomAccessIterator __last)
2278 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2281 // concept requirements
2282 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2283 _RandomAccessIterator>)
2284 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2285 __glibcxx_requires_valid_range(__first, __middle);
2286 __glibcxx_requires_valid_range(__middle, __last);
2288 std::make_heap(__first, __middle);
2289 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2290 if (*__i < *__first)
2291 std::__pop_heap(__first, __middle, __i, _ValueType(*__i));
2292 std::sort_heap(__first, __middle);
2296 * @brief Sort the smallest elements of a sequence using a predicate
2298 * @param first An iterator.
2299 * @param middle Another iterator.
2300 * @param last Another iterator.
2301 * @param comp A comparison functor.
2304 * Sorts the smallest @p (middle-first) elements in the range
2305 * @p [first,last) and moves them to the range @p [first,middle). The
2306 * order of the remaining elements in the range @p [middle,last) is
2308 * After the sort if @p i and @j are iterators in the range
2309 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2310 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2313 template<typename _RandomAccessIterator, typename _Compare>
2315 partial_sort(_RandomAccessIterator __first,
2316 _RandomAccessIterator __middle,
2317 _RandomAccessIterator __last,
2320 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2323 // concept requirements
2324 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2325 _RandomAccessIterator>)
2326 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2327 _ValueType, _ValueType>)
2328 __glibcxx_requires_valid_range(__first, __middle);
2329 __glibcxx_requires_valid_range(__middle, __last);
2331 std::make_heap(__first, __middle, __comp);
2332 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2333 if (__comp(*__i, *__first))
2334 std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
2335 std::sort_heap(__first, __middle, __comp);
2339 * @brief Copy the smallest elements of a sequence.
2340 * @param first An iterator.
2341 * @param last Another iterator.
2342 * @param result_first A random-access iterator.
2343 * @param result_last Another random-access iterator.
2344 * @return An iterator indicating the end of the resulting sequence.
2346 * Copies and sorts the smallest N values from the range @p [first,last)
2347 * to the range beginning at @p result_first, where the number of
2348 * elements to be copied, @p N, is the smaller of @p (last-first) and
2349 * @p (result_last-result_first).
2350 * After the sort if @p i and @j are iterators in the range
2351 * @p [result_first,result_first+N) such that @i precedes @j then
2352 * @p *j<*i is false.
2353 * The value returned is @p result_first+N.
2355 template<typename _InputIterator, typename _RandomAccessIterator>
2356 _RandomAccessIterator
2357 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2358 _RandomAccessIterator __result_first,
2359 _RandomAccessIterator __result_last)
2361 typedef typename iterator_traits<_InputIterator>::value_type
2363 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2365 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2368 // concept requirements
2369 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2370 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2372 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
2373 __glibcxx_function_requires(_LessThanComparableConcept<_InputValueType>)
2374 __glibcxx_requires_valid_range(__first, __last);
2375 __glibcxx_requires_valid_range(__result_first, __result_last);
2377 if (__result_first == __result_last)
2378 return __result_last;
2379 _RandomAccessIterator __result_real_last = __result_first;
2380 while(__first != __last && __result_real_last != __result_last)
2382 *__result_real_last = *__first;
2383 ++__result_real_last;
2386 std::make_heap(__result_first, __result_real_last);
2387 while (__first != __last)
2389 if (*__first < *__result_first)
2390 std::__adjust_heap(__result_first, _DistanceType(0),
2391 _DistanceType(__result_real_last
2393 _InputValueType(*__first));
2396 std::sort_heap(__result_first, __result_real_last);
2397 return __result_real_last;
2401 * @brief Copy the smallest elements of a sequence using a predicate for
2403 * @param first An input iterator.
2404 * @param last Another input iterator.
2405 * @param result_first A random-access iterator.
2406 * @param result_last Another random-access iterator.
2407 * @param comp A comparison functor.
2408 * @return An iterator indicating the end of the resulting sequence.
2410 * Copies and sorts the smallest N values from the range @p [first,last)
2411 * to the range beginning at @p result_first, where the number of
2412 * elements to be copied, @p N, is the smaller of @p (last-first) and
2413 * @p (result_last-result_first).
2414 * After the sort if @p i and @j are iterators in the range
2415 * @p [result_first,result_first+N) such that @i precedes @j then
2416 * @p comp(*j,*i) is false.
2417 * The value returned is @p result_first+N.
2419 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2420 _RandomAccessIterator
2421 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2422 _RandomAccessIterator __result_first,
2423 _RandomAccessIterator __result_last,
2426 typedef typename iterator_traits<_InputIterator>::value_type
2428 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2430 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2433 // concept requirements
2434 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2435 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2436 _RandomAccessIterator>)
2437 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2439 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2440 _OutputValueType, _OutputValueType>)
2441 __glibcxx_requires_valid_range(__first, __last);
2442 __glibcxx_requires_valid_range(__result_first, __result_last);
2444 if (__result_first == __result_last)
2445 return __result_last;
2446 _RandomAccessIterator __result_real_last = __result_first;
2447 while(__first != __last && __result_real_last != __result_last)
2449 *__result_real_last = *__first;
2450 ++__result_real_last;
2453 std::make_heap(__result_first, __result_real_last, __comp);
2454 while (__first != __last)
2456 if (__comp(*__first, *__result_first))
2457 std::__adjust_heap(__result_first, _DistanceType(0),
2458 _DistanceType(__result_real_last
2460 _InputValueType(*__first),
2464 std::sort_heap(__result_first, __result_real_last, __comp);
2465 return __result_real_last;
2470 * This is a helper function for the sort routine.
2473 template<typename _RandomAccessIterator, typename _Size>
2475 __introsort_loop(_RandomAccessIterator __first,
2476 _RandomAccessIterator __last,
2477 _Size __depth_limit)
2479 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2482 while (__last - __first > int(_S_threshold))
2484 if (__depth_limit == 0)
2486 std::partial_sort(__first, __last, __last);
2490 _RandomAccessIterator __cut =
2491 std::__unguarded_partition(__first, __last,
2492 _ValueType(std::__median(*__first,
2499 std::__introsort_loop(__cut, __last, __depth_limit);
2506 * This is a helper function for the sort routine.
2509 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2511 __introsort_loop(_RandomAccessIterator __first,
2512 _RandomAccessIterator __last,
2513 _Size __depth_limit, _Compare __comp)
2515 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2518 while (__last - __first > int(_S_threshold))
2520 if (__depth_limit == 0)
2522 std::partial_sort(__first, __last, __last, __comp);
2526 _RandomAccessIterator __cut =
2527 std::__unguarded_partition(__first, __last,
2528 _ValueType(std::__median(*__first,
2536 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2542 * @brief Sort the elements of a sequence.
2543 * @param first An iterator.
2544 * @param last Another iterator.
2547 * Sorts the elements in the range @p [first,last) in ascending order,
2548 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2549 * @p [first,last-1).
2551 * The relative ordering of equivalent elements is not preserved, use
2552 * @p stable_sort() if this is needed.
2554 template<typename _RandomAccessIterator>
2556 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2558 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2561 // concept requirements
2562 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2563 _RandomAccessIterator>)
2564 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2565 __glibcxx_requires_valid_range(__first, __last);
2567 if (__first != __last)
2569 std::__introsort_loop(__first, __last, __lg(__last - __first) * 2);
2570 std::__final_insertion_sort(__first, __last);
2575 * @brief Sort the elements of a sequence using a predicate for comparison.
2576 * @param first An iterator.
2577 * @param last Another iterator.
2578 * @param comp A comparison functor.
2581 * Sorts the elements in the range @p [first,last) in ascending order,
2582 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2583 * range @p [first,last-1).
2585 * The relative ordering of equivalent elements is not preserved, use
2586 * @p stable_sort() if this is needed.
2588 template<typename _RandomAccessIterator, typename _Compare>
2590 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2593 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2596 // concept requirements
2597 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2598 _RandomAccessIterator>)
2599 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
2601 __glibcxx_requires_valid_range(__first, __last);
2603 if (__first != __last)
2605 std::__introsort_loop(__first, __last, __lg(__last - __first) * 2,
2607 std::__final_insertion_sort(__first, __last, __comp);
2612 * @brief Finds the first position in which @a val could be inserted
2613 * without changing the ordering.
2614 * @param first An iterator.
2615 * @param last Another iterator.
2616 * @param val The search term.
2617 * @return An iterator pointing to the first element "not less than" @a val,
2618 * or end() if every element is less than @a val.
2619 * @ingroup binarysearch
2621 template<typename _ForwardIterator, typename _Tp>
2623 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2626 typedef typename iterator_traits<_ForwardIterator>::value_type
2628 typedef typename iterator_traits<_ForwardIterator>::difference_type
2631 // concept requirements
2632 // Note that these are slightly stricter than those of the 4-argument
2633 // version, defined next. The difference is in the strictness of the
2634 // comparison operations... so for looser checking, define your own
2635 // comparison function, as was intended.
2636 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2637 __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
2638 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
2639 __glibcxx_requires_partitioned(__first, __last, __val);
2641 _DistanceType __len = std::distance(__first, __last);
2642 _DistanceType __half;
2643 _ForwardIterator __middle;
2647 __half = __len >> 1;
2649 std::advance(__middle, __half);
2650 if (*__middle < __val)
2654 __len = __len - __half - 1;
2663 * @brief Finds the first position in which @a val could be inserted
2664 * without changing the ordering.
2665 * @param first An iterator.
2666 * @param last Another iterator.
2667 * @param val The search term.
2668 * @param comp A functor to use for comparisons.
2669 * @return An iterator pointing to the first element "not less than" @a val,
2670 * or end() if every element is less than @a val.
2671 * @ingroup binarysearch
2673 * The comparison function should have the same effects on ordering as
2674 * the function used for the initial sort.
2676 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2678 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2679 const _Tp& __val, _Compare __comp)
2681 typedef typename iterator_traits<_ForwardIterator>::value_type
2683 typedef typename iterator_traits<_ForwardIterator>::difference_type
2686 // concept requirements
2687 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2688 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2690 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2692 _DistanceType __len = std::distance(__first, __last);
2693 _DistanceType __half;
2694 _ForwardIterator __middle;
2698 __half = __len >> 1;
2700 std::advance(__middle, __half);
2701 if (__comp(*__middle, __val))
2705 __len = __len - __half - 1;
2714 * @brief Finds the last position in which @a val could be inserted
2715 * without changing the ordering.
2716 * @param first An iterator.
2717 * @param last Another iterator.
2718 * @param val The search term.
2719 * @return An iterator pointing to the first element greater than @a val,
2720 * or end() if no elements are greater than @a val.
2721 * @ingroup binarysearch
2723 template<typename _ForwardIterator, typename _Tp>
2725 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2728 typedef typename iterator_traits<_ForwardIterator>::value_type
2730 typedef typename iterator_traits<_ForwardIterator>::difference_type
2733 // concept requirements
2734 // See comments on lower_bound.
2735 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2736 __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
2737 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
2738 __glibcxx_requires_partitioned(__first, __last, __val);
2740 _DistanceType __len = std::distance(__first, __last);
2741 _DistanceType __half;
2742 _ForwardIterator __middle;
2746 __half = __len >> 1;
2748 std::advance(__middle, __half);
2749 if (__val < *__middle)
2755 __len = __len - __half - 1;
2762 * @brief Finds the last position in which @a val could be inserted
2763 * without changing the ordering.
2764 * @param first An iterator.
2765 * @param last Another iterator.
2766 * @param val The search term.
2767 * @param comp A functor to use for comparisons.
2768 * @return An iterator pointing to the first element greater than @a val,
2769 * or end() if no elements are greater than @a val.
2770 * @ingroup binarysearch
2772 * The comparison function should have the same effects on ordering as
2773 * the function used for the initial sort.
2775 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2777 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2778 const _Tp& __val, _Compare __comp)
2780 typedef typename iterator_traits<_ForwardIterator>::value_type
2782 typedef typename iterator_traits<_ForwardIterator>::difference_type
2785 // concept requirements
2786 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2787 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2789 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2791 _DistanceType __len = std::distance(__first, __last);
2792 _DistanceType __half;
2793 _ForwardIterator __middle;
2797 __half = __len >> 1;
2799 std::advance(__middle, __half);
2800 if (__comp(__val, *__middle))
2806 __len = __len - __half - 1;
2814 * This is a helper function for the merge routines.
2817 template<typename _BidirectionalIterator, typename _Distance>
2819 __merge_without_buffer(_BidirectionalIterator __first,
2820 _BidirectionalIterator __middle,
2821 _BidirectionalIterator __last,
2822 _Distance __len1, _Distance __len2)
2824 if (__len1 == 0 || __len2 == 0)
2826 if (__len1 + __len2 == 2)
2828 if (*__middle < *__first)
2829 std::iter_swap(__first, __middle);
2832 _BidirectionalIterator __first_cut = __first;
2833 _BidirectionalIterator __second_cut = __middle;
2834 _Distance __len11 = 0;
2835 _Distance __len22 = 0;
2836 if (__len1 > __len2)
2838 __len11 = __len1 / 2;
2839 std::advance(__first_cut, __len11);
2840 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
2841 __len22 = std::distance(__middle, __second_cut);
2845 __len22 = __len2 / 2;
2846 std::advance(__second_cut, __len22);
2847 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
2848 __len11 = std::distance(__first, __first_cut);
2850 std::rotate(__first_cut, __middle, __second_cut);
2851 _BidirectionalIterator __new_middle = __first_cut;
2852 std::advance(__new_middle, std::distance(__middle, __second_cut));
2853 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2855 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2856 __len1 - __len11, __len2 - __len22);
2861 * This is a helper function for the merge routines.
2864 template<typename _BidirectionalIterator, typename _Distance,
2867 __merge_without_buffer(_BidirectionalIterator __first,
2868 _BidirectionalIterator __middle,
2869 _BidirectionalIterator __last,
2870 _Distance __len1, _Distance __len2,
2873 if (__len1 == 0 || __len2 == 0)
2875 if (__len1 + __len2 == 2)
2877 if (__comp(*__middle, *__first))
2878 std::iter_swap(__first, __middle);
2881 _BidirectionalIterator __first_cut = __first;
2882 _BidirectionalIterator __second_cut = __middle;
2883 _Distance __len11 = 0;
2884 _Distance __len22 = 0;
2885 if (__len1 > __len2)
2887 __len11 = __len1 / 2;
2888 std::advance(__first_cut, __len11);
2889 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2891 __len22 = std::distance(__middle, __second_cut);
2895 __len22 = __len2 / 2;
2896 std::advance(__second_cut, __len22);
2897 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2899 __len11 = std::distance(__first, __first_cut);
2901 std::rotate(__first_cut, __middle, __second_cut);
2902 _BidirectionalIterator __new_middle = __first_cut;
2903 std::advance(__new_middle, std::distance(__middle, __second_cut));
2904 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2905 __len11, __len22, __comp);
2906 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2907 __len1 - __len11, __len2 - __len22, __comp);
2912 * This is a helper function for the stable sorting routines.
2915 template<typename _RandomAccessIterator>
2917 __inplace_stable_sort(_RandomAccessIterator __first,
2918 _RandomAccessIterator __last)
2920 if (__last - __first < 15)
2922 std::__insertion_sort(__first, __last);
2925 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2926 std::__inplace_stable_sort(__first, __middle);
2927 std::__inplace_stable_sort(__middle, __last);
2928 std::__merge_without_buffer(__first, __middle, __last,
2935 * This is a helper function for the stable sorting routines.
2938 template<typename _RandomAccessIterator, typename _Compare>
2940 __inplace_stable_sort(_RandomAccessIterator __first,
2941 _RandomAccessIterator __last, _Compare __comp)
2943 if (__last - __first < 15)
2945 std::__insertion_sort(__first, __last, __comp);
2948 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2949 std::__inplace_stable_sort(__first, __middle, __comp);
2950 std::__inplace_stable_sort(__middle, __last, __comp);
2951 std::__merge_without_buffer(__first, __middle, __last,
2958 * @brief Merges two sorted ranges.
2959 * @param first1 An iterator.
2960 * @param first2 Another iterator.
2961 * @param last1 Another iterator.
2962 * @param last2 Another iterator.
2963 * @param result An iterator pointing to the end of the merged range.
2964 * @return An iterator pointing to the first element "not less than" @a val.
2966 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2967 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2968 * must be sorted, and the output range must not overlap with either of
2969 * the input ranges. The sort is @e stable, that is, for equivalent
2970 * elements in the two ranges, elements from the first range will always
2971 * come before elements from the second.
2973 template<typename _InputIterator1, typename _InputIterator2,
2974 typename _OutputIterator>
2976 merge(_InputIterator1 __first1, _InputIterator1 __last1,
2977 _InputIterator2 __first2, _InputIterator2 __last2,
2978 _OutputIterator __result)
2980 // concept requirements
2981 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2982 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2983 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
2984 typename iterator_traits<_InputIterator1>::value_type>)
2985 __glibcxx_function_requires(_SameTypeConcept<
2986 typename iterator_traits<_InputIterator1>::value_type,
2987 typename iterator_traits<_InputIterator2>::value_type>)
2988 __glibcxx_function_requires(_LessThanComparableConcept<
2989 typename iterator_traits<_InputIterator1>::value_type>)
2990 __glibcxx_requires_sorted(__first1, __last1);
2991 __glibcxx_requires_sorted(__first2, __last2);
2993 while (__first1 != __last1 && __first2 != __last2)
2995 if (*__first2 < *__first1)
2997 *__result = *__first2;
3002 *__result = *__first1;
3007 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3012 * @brief Merges two sorted ranges.
3013 * @param first1 An iterator.
3014 * @param first2 Another iterator.
3015 * @param last1 Another iterator.
3016 * @param last2 Another iterator.
3017 * @param result An iterator pointing to the end of the merged range.
3018 * @param comp A functor to use for comparisons.
3019 * @return An iterator pointing to the first element "not less than" @a val.
3021 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3022 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3023 * must be sorted, and the output range must not overlap with either of
3024 * the input ranges. The sort is @e stable, that is, for equivalent
3025 * elements in the two ranges, elements from the first range will always
3026 * come before elements from the second.
3028 * The comparison function should have the same effects on ordering as
3029 * the function used for the initial sort.
3031 template<typename _InputIterator1, typename _InputIterator2,
3032 typename _OutputIterator, typename _Compare>
3034 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3035 _InputIterator2 __first2, _InputIterator2 __last2,
3036 _OutputIterator __result, _Compare __comp)
3038 // concept requirements
3039 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3040 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3041 __glibcxx_function_requires(_SameTypeConcept<
3042 typename iterator_traits<_InputIterator1>::value_type,
3043 typename iterator_traits<_InputIterator2>::value_type>)
3044 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3045 typename iterator_traits<_InputIterator1>::value_type>)
3046 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3047 typename iterator_traits<_InputIterator1>::value_type,
3048 typename iterator_traits<_InputIterator2>::value_type>)
3049 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
3050 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
3052 while (__first1 != __last1 && __first2 != __last2)
3054 if (__comp(*__first2, *__first1))
3056 *__result = *__first2;
3061 *__result = *__first1;
3066 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3070 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3073 __merge_sort_loop(_RandomAccessIterator1 __first,
3074 _RandomAccessIterator1 __last,
3075 _RandomAccessIterator2 __result,
3076 _Distance __step_size)
3078 const _Distance __two_step = 2 * __step_size;
3080 while (__last - __first >= __two_step)
3082 __result = std::merge(__first, __first + __step_size,
3083 __first + __step_size, __first + __two_step,
3085 __first += __two_step;
3088 __step_size = std::min(_Distance(__last - __first), __step_size);
3089 std::merge(__first, __first + __step_size, __first + __step_size, __last,
3093 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3094 typename _Distance, typename _Compare>
3096 __merge_sort_loop(_RandomAccessIterator1 __first,
3097 _RandomAccessIterator1 __last,
3098 _RandomAccessIterator2 __result, _Distance __step_size,
3101 const _Distance __two_step = 2 * __step_size;
3103 while (__last - __first >= __two_step)
3105 __result = std::merge(__first, __first + __step_size,
3106 __first + __step_size, __first + __two_step,
3109 __first += __two_step;
3111 __step_size = std::min(_Distance(__last - __first), __step_size);
3113 std::merge(__first, __first + __step_size,
3114 __first + __step_size, __last,
3119 enum { _S_chunk_size = 7 };
3121 template<typename _RandomAccessIterator, typename _Distance>
3123 __chunk_insertion_sort(_RandomAccessIterator __first,
3124 _RandomAccessIterator __last,
3125 _Distance __chunk_size)
3127 while (__last - __first >= __chunk_size)
3129 std::__insertion_sort(__first, __first + __chunk_size);
3130 __first += __chunk_size;
3132 std::__insertion_sort(__first, __last);
3135 template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
3137 __chunk_insertion_sort(_RandomAccessIterator __first,
3138 _RandomAccessIterator __last,
3139 _Distance __chunk_size, _Compare __comp)
3141 while (__last - __first >= __chunk_size)
3143 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3144 __first += __chunk_size;
3146 std::__insertion_sort(__first, __last, __comp);
3149 template<typename _RandomAccessIterator, typename _Pointer>
3151 __merge_sort_with_buffer(_RandomAccessIterator __first,
3152 _RandomAccessIterator __last,
3155 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3158 const _Distance __len = __last - __first;
3159 const _Pointer __buffer_last = __buffer + __len;
3161 _Distance __step_size = _S_chunk_size;
3162 std::__chunk_insertion_sort(__first, __last, __step_size);
3164 while (__step_size < __len)
3166 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3168 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3173 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3175 __merge_sort_with_buffer(_RandomAccessIterator __first,
3176 _RandomAccessIterator __last,
3177 _Pointer __buffer, _Compare __comp)
3179 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3182 const _Distance __len = __last - __first;
3183 const _Pointer __buffer_last = __buffer + __len;
3185 _Distance __step_size = _S_chunk_size;
3186 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3188 while (__step_size < __len)
3190 std::__merge_sort_loop(__first, __last, __buffer,
3191 __step_size, __comp);
3193 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3194 __step_size, __comp);
3201 * This is a helper function for the merge routines.
3204 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3205 typename _BidirectionalIterator3>
3206 _BidirectionalIterator3
3207 __merge_backward(_BidirectionalIterator1 __first1,
3208 _BidirectionalIterator1 __last1,
3209 _BidirectionalIterator2 __first2,
3210 _BidirectionalIterator2 __last2,
3211 _BidirectionalIterator3 __result)
3213 if (__first1 == __last1)
3214 return std::copy_backward(__first2, __last2, __result);
3215 if (__first2 == __last2)
3216 return std::copy_backward(__first1, __last1, __result);
3221 if (*__last2 < *__last1)
3223 *--__result = *__last1;
3224 if (__first1 == __last1)
3225 return std::copy_backward(__first2, ++__last2, __result);
3230 *--__result = *__last2;
3231 if (__first2 == __last2)
3232 return std::copy_backward(__first1, ++__last1, __result);
3240 * This is a helper function for the merge routines.
3243 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3244 typename _BidirectionalIterator3, typename _Compare>
3245 _BidirectionalIterator3
3246 __merge_backward(_BidirectionalIterator1 __first1,
3247 _BidirectionalIterator1 __last1,
3248 _BidirectionalIterator2 __first2,
3249 _BidirectionalIterator2 __last2,
3250 _BidirectionalIterator3 __result,
3253 if (__first1 == __last1)
3254 return std::copy_backward(__first2, __last2, __result);
3255 if (__first2 == __last2)
3256 return std::copy_backward(__first1, __last1, __result);
3261 if (__comp(*__last2, *__last1))
3263 *--__result = *__last1;
3264 if (__first1 == __last1)
3265 return std::copy_backward(__first2, ++__last2, __result);
3270 *--__result = *__last2;
3271 if (__first2 == __last2)
3272 return std::copy_backward(__first1, ++__last1, __result);
3280 * This is a helper function for the merge routines.
3283 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3285 _BidirectionalIterator1
3286 __rotate_adaptive(_BidirectionalIterator1 __first,
3287 _BidirectionalIterator1 __middle,
3288 _BidirectionalIterator1 __last,
3289 _Distance __len1, _Distance __len2,
3290 _BidirectionalIterator2 __buffer,
3291 _Distance __buffer_size)
3293 _BidirectionalIterator2 __buffer_end;
3294 if (__len1 > __len2 && __len2 <= __buffer_size)
3296 __buffer_end = std::copy(__middle, __last, __buffer);
3297 std::copy_backward(__first, __middle, __last);
3298 return std::copy(__buffer, __buffer_end, __first);
3300 else if (__len1 <= __buffer_size)
3302 __buffer_end = std::copy(__first, __middle, __buffer);
3303 std::copy(__middle, __last, __first);
3304 return std::copy_backward(__buffer, __buffer_end, __last);
3308 std::rotate(__first, __middle, __last);
3309 std::advance(__first, std::distance(__middle, __last));
3316 * This is a helper function for the merge routines.
3319 template<typename _BidirectionalIterator, typename _Distance,
3322 __merge_adaptive(_BidirectionalIterator __first,
3323 _BidirectionalIterator __middle,
3324 _BidirectionalIterator __last,
3325 _Distance __len1, _Distance __len2,
3326 _Pointer __buffer, _Distance __buffer_size)
3328 if (__len1 <= __len2 && __len1 <= __buffer_size)
3330 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3331 std::merge(__buffer, __buffer_end, __middle, __last, __first);
3333 else if (__len2 <= __buffer_size)
3335 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3336 std::__merge_backward(__first, __middle, __buffer,
3337 __buffer_end, __last);
3341 _BidirectionalIterator __first_cut = __first;
3342 _BidirectionalIterator __second_cut = __middle;
3343 _Distance __len11 = 0;
3344 _Distance __len22 = 0;
3345 if (__len1 > __len2)
3347 __len11 = __len1 / 2;
3348 std::advance(__first_cut, __len11);
3349 __second_cut = std::lower_bound(__middle, __last,
3351 __len22 = std::distance(__middle, __second_cut);
3355 __len22 = __len2 / 2;
3356 std::advance(__second_cut, __len22);
3357 __first_cut = std::upper_bound(__first, __middle,
3359 __len11 = std::distance(__first, __first_cut);
3361 _BidirectionalIterator __new_middle =
3362 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3363 __len1 - __len11, __len22, __buffer,
3365 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3366 __len22, __buffer, __buffer_size);
3367 std::__merge_adaptive(__new_middle, __second_cut, __last,
3369 __len2 - __len22, __buffer, __buffer_size);
3375 * This is a helper function for the merge routines.
3378 template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
3381 __merge_adaptive(_BidirectionalIterator __first,
3382 _BidirectionalIterator __middle,
3383 _BidirectionalIterator __last,
3384 _Distance __len1, _Distance __len2,
3385 _Pointer __buffer, _Distance __buffer_size,
3388 if (__len1 <= __len2 && __len1 <= __buffer_size)
3390 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3391 std::merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
3393 else if (__len2 <= __buffer_size)
3395 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3396 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
3401 _BidirectionalIterator __first_cut = __first;
3402 _BidirectionalIterator __second_cut = __middle;
3403 _Distance __len11 = 0;
3404 _Distance __len22 = 0;
3405 if (__len1 > __len2)
3407 __len11 = __len1 / 2;
3408 std::advance(__first_cut, __len11);
3409 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3411 __len22 = std::distance(__middle, __second_cut);
3415 __len22 = __len2 / 2;
3416 std::advance(__second_cut, __len22);
3417 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3419 __len11 = std::distance(__first, __first_cut);
3421 _BidirectionalIterator __new_middle =
3422 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3423 __len1 - __len11, __len22, __buffer,
3425 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3426 __len22, __buffer, __buffer_size, __comp);
3427 std::__merge_adaptive(__new_middle, __second_cut, __last,
3429 __len2 - __len22, __buffer,
3430 __buffer_size, __comp);
3435 * @brief Merges two sorted ranges in place.
3436 * @param first An iterator.
3437 * @param middle Another iterator.
3438 * @param last Another iterator.
3441 * Merges two sorted and consecutive ranges, [first,middle) and
3442 * [middle,last), and puts the result in [first,last). The output will
3443 * be sorted. The sort is @e stable, that is, for equivalent
3444 * elements in the two ranges, elements from the first range will always
3445 * come before elements from the second.
3447 * If enough additional memory is available, this takes (last-first)-1
3448 * comparisons. Otherwise an NlogN algorithm is used, where N is
3449 * distance(first,last).
3451 template<typename _BidirectionalIterator>
3453 inplace_merge(_BidirectionalIterator __first,
3454 _BidirectionalIterator __middle,
3455 _BidirectionalIterator __last)
3457 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3459 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3462 // concept requirements
3463 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3464 _BidirectionalIterator>)
3465 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3466 __glibcxx_requires_sorted(__first, __middle);
3467 __glibcxx_requires_sorted(__middle, __last);
3469 if (__first == __middle || __middle == __last)
3472 _DistanceType __len1 = std::distance(__first, __middle);
3473 _DistanceType __len2 = std::distance(__middle, __last);
3475 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3477 if (__buf.begin() == 0)
3478 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3480 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3481 __buf.begin(), _DistanceType(__buf.size()));
3485 * @brief Merges two sorted ranges in place.
3486 * @param first An iterator.
3487 * @param middle Another iterator.
3488 * @param last Another iterator.
3489 * @param comp A functor to use for comparisons.
3492 * Merges two sorted and consecutive ranges, [first,middle) and
3493 * [middle,last), and puts the result in [first,last). The output will
3494 * be sorted. The sort is @e stable, that is, for equivalent
3495 * elements in the two ranges, elements from the first range will always
3496 * come before elements from the second.
3498 * If enough additional memory is available, this takes (last-first)-1
3499 * comparisons. Otherwise an NlogN algorithm is used, where N is
3500 * distance(first,last).
3502 * The comparison function should have the same effects on ordering as
3503 * the function used for the initial sort.
3505 template<typename _BidirectionalIterator, typename _Compare>
3507 inplace_merge(_BidirectionalIterator __first,
3508 _BidirectionalIterator __middle,
3509 _BidirectionalIterator __last,
3512 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3514 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3517 // concept requirements
3518 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3519 _BidirectionalIterator>)
3520 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3521 _ValueType, _ValueType>)
3522 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3523 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3525 if (__first == __middle || __middle == __last)
3528 const _DistanceType __len1 = std::distance(__first, __middle);
3529 const _DistanceType __len2 = std::distance(__middle, __last);
3531 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3533 if (__buf.begin() == 0)
3534 std::__merge_without_buffer(__first, __middle, __last, __len1,
3537 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3538 __buf.begin(), _DistanceType(__buf.size()),
3542 template<typename _RandomAccessIterator, typename _Pointer,
3545 __stable_sort_adaptive(_RandomAccessIterator __first,
3546 _RandomAccessIterator __last,
3547 _Pointer __buffer, _Distance __buffer_size)
3549 const _Distance __len = (__last - __first + 1) / 2;
3550 const _RandomAccessIterator __middle = __first + __len;
3551 if (__len > __buffer_size)
3553 std::__stable_sort_adaptive(__first, __middle,
3554 __buffer, __buffer_size);
3555 std::__stable_sort_adaptive(__middle, __last,
3556 __buffer, __buffer_size);
3560 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3561 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3563 std::__merge_adaptive(__first, __middle, __last,
3564 _Distance(__middle - __first),
3565 _Distance(__last - __middle),
3566 __buffer, __buffer_size);
3569 template<typename _RandomAccessIterator, typename _Pointer,
3570 typename _Distance, typename _Compare>
3572 __stable_sort_adaptive(_RandomAccessIterator __first,
3573 _RandomAccessIterator __last,
3574 _Pointer __buffer, _Distance __buffer_size,
3577 const _Distance __len = (__last - __first + 1) / 2;
3578 const _RandomAccessIterator __middle = __first + __len;
3579 if (__len > __buffer_size)
3581 std::__stable_sort_adaptive(__first, __middle, __buffer,
3582 __buffer_size, __comp);
3583 std::__stable_sort_adaptive(__middle, __last, __buffer,
3584 __buffer_size, __comp);
3588 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3589 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3591 std::__merge_adaptive(__first, __middle, __last,
3592 _Distance(__middle - __first),
3593 _Distance(__last - __middle),
3594 __buffer, __buffer_size,
3599 * @brief Sort the elements of a sequence, preserving the relative order
3600 * of equivalent elements.
3601 * @param first An iterator.
3602 * @param last Another iterator.
3605 * Sorts the elements in the range @p [first,last) in ascending order,
3606 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3607 * @p [first,last-1).
3609 * The relative ordering of equivalent elements is preserved, so any two
3610 * elements @p x and @p y in the range @p [first,last) such that
3611 * @p x<y is false and @p y<x is false will have the same relative
3612 * ordering after calling @p stable_sort().
3614 template<typename _RandomAccessIterator>
3616 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3618 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3620 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3623 // concept requirements
3624 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3625 _RandomAccessIterator>)
3626 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3627 __glibcxx_requires_valid_range(__first, __last);
3629 _Temporary_buffer<_RandomAccessIterator, _ValueType>
3630 buf(__first, __last);
3631 if (buf.begin() == 0)
3632 std::__inplace_stable_sort(__first, __last);
3634 std::__stable_sort_adaptive(__first, __last, buf.begin(),
3635 _DistanceType(buf.size()));
3639 * @brief Sort the elements of a sequence using a predicate for comparison,
3640 * preserving the relative order of equivalent elements.
3641 * @param first An iterator.
3642 * @param last Another iterator.
3643 * @param comp A comparison functor.
3646 * Sorts the elements in the range @p [first,last) in ascending order,
3647 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3648 * range @p [first,last-1).
3650 * The relative ordering of equivalent elements is preserved, so any two
3651 * elements @p x and @p y in the range @p [first,last) such that
3652 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3653 * relative ordering after calling @p stable_sort().
3655 template<typename _RandomAccessIterator, typename _Compare>
3657 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
3660 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3662 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3665 // concept requirements
3666 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3667 _RandomAccessIterator>)
3668 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3671 __glibcxx_requires_valid_range(__first, __last);
3673 _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last);
3674 if (buf.begin() == 0)
3675 std::__inplace_stable_sort(__first, __last, __comp);
3677 std::__stable_sort_adaptive(__first, __last, buf.begin(),
3678 _DistanceType(buf.size()), __comp);
3682 * @brief Sort a sequence just enough to find a particular position.
3683 * @param first An iterator.
3684 * @param nth Another iterator.
3685 * @param last Another iterator.
3688 * Rearranges the elements in the range @p [first,last) so that @p *nth
3689 * is the same element that would have been in that position had the
3690 * whole sequence been sorted.
3691 * whole sequence been sorted. The elements either side of @p *nth are
3692 * not completely sorted, but for any iterator @i in the range
3693 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3694 * holds that @p *j<*i is false.
3696 template<typename _RandomAccessIterator>
3698 nth_element(_RandomAccessIterator __first,
3699 _RandomAccessIterator __nth,
3700 _RandomAccessIterator __last)
3702 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3705 // concept requirements
3706 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3707 _RandomAccessIterator>)
3708 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3709 __glibcxx_requires_valid_range(__first, __nth);
3710 __glibcxx_requires_valid_range(__nth, __last);
3712 while (__last - __first > 3)
3714 _RandomAccessIterator __cut =
3715 std::__unguarded_partition(__first, __last,
3716 _ValueType(std::__median(*__first,
3728 std::__insertion_sort(__first, __last);
3732 * @brief Sort a sequence just enough to find a particular position
3733 * using a predicate for comparison.
3734 * @param first An iterator.
3735 * @param nth Another iterator.
3736 * @param last Another iterator.
3737 * @param comp A comparison functor.
3740 * Rearranges the elements in the range @p [first,last) so that @p *nth
3741 * is the same element that would have been in that position had the
3742 * whole sequence been sorted. The elements either side of @p *nth are
3743 * not completely sorted, but for any iterator @i in the range
3744 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3745 * holds that @p comp(*j,*i) is false.
3747 template<typename _RandomAccessIterator, typename _Compare>
3749 nth_element(_RandomAccessIterator __first,
3750 _RandomAccessIterator __nth,
3751 _RandomAccessIterator __last,
3754 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3757 // concept requirements
3758 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3759 _RandomAccessIterator>)
3760 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3761 _ValueType, _ValueType>)
3762 __glibcxx_requires_valid_range(__first, __nth);
3763 __glibcxx_requires_valid_range(__nth, __last);
3765 while (__last - __first > 3)
3767 _RandomAccessIterator __cut =
3768 std::__unguarded_partition(__first, __last,
3769 _ValueType(std::__median(*__first,
3781 std::__insertion_sort(__first, __last, __comp);
3785 * @brief Finds the largest subrange in which @a val could be inserted
3786 * at any place in it without changing the ordering.
3787 * @param first An iterator.
3788 * @param last Another iterator.
3789 * @param val The search term.
3790 * @return An pair of iterators defining the subrange.
3791 * @ingroup binarysearch
3793 * This is equivalent to
3795 * std::make_pair(lower_bound(first, last, val),
3796 * upper_bound(first, last, val))
3798 * but does not actually call those functions.
3800 template<typename _ForwardIterator, typename _Tp>
3801 pair<_ForwardIterator, _ForwardIterator>
3802 equal_range(_ForwardIterator __first, _ForwardIterator __last,
3805 typedef typename iterator_traits<_ForwardIterator>::value_type
3807 typedef typename iterator_traits<_ForwardIterator>::difference_type
3810 // concept requirements
3811 // See comments on lower_bound.
3812 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3813 __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
3814 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3815 __glibcxx_requires_partitioned(__first, __last, __val);
3817 _DistanceType __len = std::distance(__first, __last);
3818 _DistanceType __half;
3819 _ForwardIterator __middle, __left, __right;
3823 __half = __len >> 1;
3825 std::advance(__middle, __half);
3826 if (*__middle < __val)
3830 __len = __len - __half - 1;
3832 else if (__val < *__middle)
3836 __left = std::lower_bound(__first, __middle, __val);
3837 std::advance(__first, __len);
3838 __right = std::upper_bound(++__middle, __first, __val);
3839 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
3842 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3846 * @brief Finds the largest subrange in which @a val could be inserted
3847 * at any place in it without changing the ordering.
3848 * @param first An iterator.
3849 * @param last Another iterator.
3850 * @param val The search term.
3851 * @param comp A functor to use for comparisons.
3852 * @return An pair of iterators defining the subrange.
3853 * @ingroup binarysearch
3855 * This is equivalent to
3857 * std::make_pair(lower_bound(first, last, val, comp),
3858 * upper_bound(first, last, val, comp))
3860 * but does not actually call those functions.
3862 template<typename _ForwardIterator, typename _Tp, typename _Compare>
3863 pair<_ForwardIterator, _ForwardIterator>
3864 equal_range(_ForwardIterator __first, _ForwardIterator __last,
3868 typedef typename iterator_traits<_ForwardIterator>::value_type
3870 typedef typename iterator_traits<_ForwardIterator>::difference_type
3873 // concept requirements
3874 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3875 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3877 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3879 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
3881 _DistanceType __len = std::distance(__first, __last);
3882 _DistanceType __half;
3883 _ForwardIterator __middle, __left, __right;
3887 __half = __len >> 1;
3889 std::advance(__middle, __half);
3890 if (__comp(*__middle, __val))
3894 __len = __len - __half - 1;
3896 else if (__comp(__val, *__middle))
3900 __left = std::lower_bound(__first, __middle, __val, __comp);
3901 std::advance(__first, __len);
3902 __right = std::upper_bound(++__middle, __first, __val, __comp);
3903 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
3906 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3910 * @brief Determines whether an element exists in a range.
3911 * @param first An iterator.
3912 * @param last Another iterator.
3913 * @param val The search term.
3914 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3915 * @ingroup binarysearch
3917 * Note that this does not actually return an iterator to @a val. For
3918 * that, use std::find or a container's specialized find member functions.
3920 template<typename _ForwardIterator, typename _Tp>
3922 binary_search(_ForwardIterator __first, _ForwardIterator __last,
3925 // concept requirements
3926 // See comments on lower_bound.
3927 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3928 __glibcxx_function_requires(_SameTypeConcept<_Tp,
3929 typename iterator_traits<_ForwardIterator>::value_type>)
3930 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3931 __glibcxx_requires_partitioned(__first, __last, __val);
3933 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
3934 return __i != __last && !(__val < *__i);
3938 * @brief Determines whether an element exists in a range.
3939 * @param first An iterator.
3940 * @param last Another iterator.
3941 * @param val The search term.
3942 * @param comp A functor to use for comparisons.
3943 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3944 * @ingroup binarysearch
3946 * Note that this does not actually return an iterator to @a val. For
3947 * that, use std::find or a container's specialized find member functions.
3949 * The comparison function should have the same effects on ordering as
3950 * the function used for the initial sort.
3952 template<typename _ForwardIterator, typename _Tp, typename _Compare>
3954 binary_search(_ForwardIterator __first, _ForwardIterator __last,
3955 const _Tp& __val, _Compare __comp)
3957 // concept requirements
3958 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3959 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3960 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
3961 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
3962 typename iterator_traits<_ForwardIterator>::value_type>)
3963 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
3965 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
3966 return __i != __last && !__comp(__val, *__i);
3969 // Set algorithms: includes, set_union, set_intersection, set_difference,
3970 // set_symmetric_difference. All of these algorithms have the precondition
3971 // that their input ranges are sorted and the postcondition that their output
3972 // ranges are sorted.
3975 * @brief Determines whether all elements of a sequence exists in a range.
3976 * @param first1 Start of search range.
3977 * @param last1 End of search range.
3978 * @param first2 Start of sequence
3979 * @param last2 End of sequence.
3980 * @return True if each element in [first2,last2) is contained in order
3981 * within [first1,last1). False otherwise.
3982 * @ingroup setoperations
3984 * This operation expects both [first1,last1) and [first2,last2) to be
3985 * sorted. Searches for the presence of each element in [first2,last2)
3986 * within [first1,last1). The iterators over each range only move forward,
3987 * so this is a linear algorithm. If an element in [first2,last2) is not
3988 * found before the search iterator reaches @a last2, false is returned.
3990 template<typename _InputIterator1, typename _InputIterator2>
3992 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3993 _InputIterator2 __first2, _InputIterator2 __last2)
3995 // concept requirements
3996 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3997 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3998 __glibcxx_function_requires(_SameTypeConcept<
3999 typename iterator_traits<_InputIterator1>::value_type,
4000 typename iterator_traits<_InputIterator2>::value_type>)
4001 __glibcxx_function_requires(_LessThanComparableConcept<
4002 typename iterator_traits<_InputIterator1>::value_type>)
4003 __glibcxx_requires_sorted(__first1, __last1);
4004 __glibcxx_requires_sorted(__first2, __last2);
4006 while (__first1 != __last1 && __first2 != __last2)
4007 if (*__first2 < *__first1)
4009 else if(*__first1 < *__first2)
4012 ++__first1, ++__first2;
4014 return __first2 == __last2;
4018 * @brief Determines whether all elements of a sequence exists in a range
4020 * @param first1 Start of search range.
4021 * @param last1 End of search range.
4022 * @param first2 Start of sequence
4023 * @param last2 End of sequence.
4024 * @param comp Comparison function to use.
4025 * @return True if each element in [first2,last2) is contained in order
4026 * within [first1,last1) according to comp. False otherwise.
4027 * @ingroup setoperations
4029 * This operation expects both [first1,last1) and [first2,last2) to be
4030 * sorted. Searches for the presence of each element in [first2,last2)
4031 * within [first1,last1), using comp to decide. The iterators over each
4032 * range only move forward, so this is a linear algorithm. If an element
4033 * in [first2,last2) is not found before the search iterator reaches @a
4034 * last2, false is returned.
4036 template<typename _InputIterator1, typename _InputIterator2,
4039 includes(_InputIterator1 __first1, _InputIterator1 __last1,
4040 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
4042 // concept requirements
4043 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4044 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4045 __glibcxx_function_requires(_SameTypeConcept<
4046 typename iterator_traits<_InputIterator1>::value_type,
4047 typename iterator_traits<_InputIterator2>::value_type>)
4048 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4049 typename iterator_traits<_InputIterator1>::value_type,
4050 typename iterator_traits<_InputIterator2>::value_type>)
4051 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4052 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4054 while (__first1 != __last1 && __first2 != __last2)
4055 if (__comp(*__first2, *__first1))
4057 else if(__comp(*__first1, *__first2))
4060 ++__first1, ++__first2;
4062 return __first2 == __last2;
4066 * @brief Return the union of two sorted ranges.
4067 * @param first1 Start of first range.
4068 * @param last1 End of first range.
4069 * @param first2 Start of second range.
4070 * @param last2 End of second range.
4071 * @return End of the output range.
4072 * @ingroup setoperations
4074 * This operation iterates over both ranges, copying elements present in
4075 * each range in order to the output range. Iterators increment for each
4076 * range. When the current element of one range is less than the other,
4077 * that element is copied and the iterator advanced. If an element is
4078 * contained in both ranges, the element from the first range is copied and
4079 * both ranges advance. The output range may not overlap either input
4082 template<typename _InputIterator1, typename _InputIterator2,
4083 typename _OutputIterator>
4085 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4086 _InputIterator2 __first2, _InputIterator2 __last2,
4087 _OutputIterator __result)
4089 // concept requirements
4090 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4091 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4092 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4093 typename iterator_traits<_InputIterator1>::value_type>)
4094 __glibcxx_function_requires(_SameTypeConcept<
4095 typename iterator_traits<_InputIterator1>::value_type,
4096 typename iterator_traits<_InputIterator2>::value_type>)
4097 __glibcxx_function_requires(_LessThanComparableConcept<
4098 typename iterator_traits<_InputIterator1>::value_type>)
4099 __glibcxx_requires_sorted(__first1, __last1);
4100 __glibcxx_requires_sorted(__first2, __last2);
4102 while (__first1 != __last1 && __first2 != __last2)
4104 if (*__first1 < *__first2)
4106 *__result = *__first1;
4109 else if (*__first2 < *__first1)
4111 *__result = *__first2;
4116 *__result = *__first1;
4122 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4127 * @brief Return the union of two sorted ranges using a comparison functor.
4128 * @param first1 Start of first range.
4129 * @param last1 End of first range.
4130 * @param first2 Start of second range.
4131 * @param last2 End of second range.
4132 * @param comp The comparison functor.
4133 * @return End of the output range.
4134 * @ingroup setoperations
4136 * This operation iterates over both ranges, copying elements present in
4137 * each range in order to the output range. Iterators increment for each
4138 * range. When the current element of one range is less than the other
4139 * according to @a comp, that element is copied and the iterator advanced.
4140 * If an equivalent element according to @a comp is contained in both
4141 * ranges, the element from the first range is copied and both ranges
4142 * advance. The output range may not overlap either input range.
4144 template<typename _InputIterator1, typename _InputIterator2,
4145 typename _OutputIterator, typename _Compare>
4147 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4148 _InputIterator2 __first2, _InputIterator2 __last2,
4149 _OutputIterator __result, _Compare __comp)
4151 // concept requirements
4152 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4153 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4154 __glibcxx_function_requires(_SameTypeConcept<
4155 typename iterator_traits<_InputIterator1>::value_type,
4156 typename iterator_traits<_InputIterator2>::value_type>)
4157 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4158 typename iterator_traits<_InputIterator1>::value_type>)
4159 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4160 typename iterator_traits<_InputIterator1>::value_type,
4161 typename iterator_traits<_InputIterator2>::value_type>)
4162 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4163 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4165 while (__first1 != __last1 && __first2 != __last2)
4167 if (__comp(*__first1, *__first2))
4169 *__result = *__first1;
4172 else if (__comp(*__first2, *__first1))
4174 *__result = *__first2;
4179 *__result = *__first1;
4185 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4190 * @brief Return the intersection of two sorted ranges.
4191 * @param first1 Start of first range.
4192 * @param last1 End of first range.
4193 * @param first2 Start of second range.
4194 * @param last2 End of second range.
4195 * @return End of the output range.
4196 * @ingroup setoperations
4198 * This operation iterates over both ranges, copying elements present in
4199 * both ranges in order to the output range. Iterators increment for each
4200 * range. When the current element of one range is less than the other,
4201 * that iterator advances. If an element is contained in both ranges, the
4202 * element from the first range is copied and both ranges advance. The
4203 * output range may not overlap either input range.
4205 template<typename _InputIterator1, typename _InputIterator2,
4206 typename _OutputIterator>
4208 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4209 _InputIterator2 __first2, _InputIterator2 __last2,
4210 _OutputIterator __result)
4212 // concept requirements
4213 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4214 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4215 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4216 typename iterator_traits<_InputIterator1>::value_type>)
4217 __glibcxx_function_requires(_SameTypeConcept<
4218 typename iterator_traits<_InputIterator1>::value_type,
4219 typename iterator_traits<_InputIterator2>::value_type>)
4220 __glibcxx_function_requires(_LessThanComparableConcept<
4221 typename iterator_traits<_InputIterator1>::value_type>)
4222 __glibcxx_requires_sorted(__first1, __last1);
4223 __glibcxx_requires_sorted(__first2, __last2);
4225 while (__first1 != __last1 && __first2 != __last2)
4226 if (*__first1 < *__first2)
4228 else if (*__first2 < *__first1)
4232 *__result = *__first1;
4241 * @brief Return the intersection of two sorted ranges using comparison
4243 * @param first1 Start of first range.
4244 * @param last1 End of first range.
4245 * @param first2 Start of second range.
4246 * @param last2 End of second range.
4247 * @param comp The comparison functor.
4248 * @return End of the output range.
4249 * @ingroup setoperations
4251 * This operation iterates over both ranges, copying elements present in
4252 * both ranges in order to the output range. Iterators increment for each
4253 * range. When the current element of one range is less than the other
4254 * according to @a comp, that iterator advances. If an element is
4255 * contained in both ranges according to @a comp, the element from the
4256 * first range is copied and both ranges advance. The output range may not
4257 * overlap either input range.
4259 template<typename _InputIterator1, typename _InputIterator2,
4260 typename _OutputIterator, typename _Compare>
4262 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4263 _InputIterator2 __first2, _InputIterator2 __last2,
4264 _OutputIterator __result, _Compare __comp)
4266 // concept requirements
4267 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4268 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4269 __glibcxx_function_requires(_SameTypeConcept<
4270 typename iterator_traits<_InputIterator1>::value_type,
4271 typename iterator_traits<_InputIterator2>::value_type>)
4272 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4273 typename iterator_traits<_InputIterator1>::value_type>)
4274 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4275 typename iterator_traits<_InputIterator1>::value_type,
4276 typename iterator_traits<_InputIterator2>::value_type>)
4277 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4278 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4280 while (__first1 != __last1 && __first2 != __last2)
4281 if (__comp(*__first1, *__first2))
4283 else if (__comp(*__first2, *__first1))
4287 *__result = *__first1;
4296 * @brief Return the difference of two sorted ranges.
4297 * @param first1 Start of first range.
4298 * @param last1 End of first range.
4299 * @param first2 Start of second range.
4300 * @param last2 End of second range.
4301 * @return End of the output range.
4302 * @ingroup setoperations
4304 * This operation iterates over both ranges, copying elements present in
4305 * the first range but not the second in order to the output range.
4306 * Iterators increment for each range. When the current element of the
4307 * first range is less than the second, that element is copied and the
4308 * iterator advances. If the current element of the second range is less,
4309 * the iterator advances, but no element is copied. If an element is
4310 * contained in both ranges, no elements are copied and both ranges
4311 * advance. The output range may not overlap either input range.
4313 template<typename _InputIterator1, typename _InputIterator2,
4314 typename _OutputIterator>
4316 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4317 _InputIterator2 __first2, _InputIterator2 __last2,
4318 _OutputIterator __result)
4320 // concept requirements
4321 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4322 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4323 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4324 typename iterator_traits<_InputIterator1>::value_type>)
4325 __glibcxx_function_requires(_SameTypeConcept<
4326 typename iterator_traits<_InputIterator1>::value_type,
4327 typename iterator_traits<_InputIterator2>::value_type>)
4328 __glibcxx_function_requires(_LessThanComparableConcept<
4329 typename iterator_traits<_InputIterator1>::value_type>)
4330 __glibcxx_requires_sorted(__first1, __last1);
4331 __glibcxx_requires_sorted(__first2, __last2);
4333 while (__first1 != __last1 && __first2 != __last2)
4334 if (*__first1 < *__first2)
4336 *__result = *__first1;
4340 else if (*__first2 < *__first1)
4347 return std::copy(__first1, __last1, __result);
4351 * @brief Return the difference of two sorted ranges using comparison
4353 * @param first1 Start of first range.
4354 * @param last1 End of first range.
4355 * @param first2 Start of second range.
4356 * @param last2 End of second range.
4357 * @param comp The comparison functor.
4358 * @return End of the output range.
4359 * @ingroup setoperations
4361 * This operation iterates over both ranges, copying elements present in
4362 * the first range but not the second in order to the output range.
4363 * Iterators increment for each range. When the current element of the
4364 * first range is less than the second according to @a comp, that element
4365 * is copied and the iterator advances. If the current element of the
4366 * second range is less, no element is copied and the iterator advances.
4367 * If an element is contained in both ranges according to @a comp, no
4368 * elements are copied and both ranges advance. The output range may not
4369 * overlap either input range.
4371 template<typename _InputIterator1, typename _InputIterator2,
4372 typename _OutputIterator, typename _Compare>
4374 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4375 _InputIterator2 __first2, _InputIterator2 __last2,
4376 _OutputIterator __result, _Compare __comp)
4378 // concept requirements
4379 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4380 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4381 __glibcxx_function_requires(_SameTypeConcept<
4382 typename iterator_traits<_InputIterator1>::value_type,
4383 typename iterator_traits<_InputIterator2>::value_type>)
4384 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4385 typename iterator_traits<_InputIterator1>::value_type>)
4386 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4387 typename iterator_traits<_InputIterator1>::value_type,
4388 typename iterator_traits<_InputIterator2>::value_type>)
4389 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4390 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4392 while (__first1 != __last1 && __first2 != __last2)
4393 if (__comp(*__first1, *__first2))
4395 *__result = *__first1;
4399 else if (__comp(*__first2, *__first1))
4406 return std::copy(__first1, __last1, __result);
4410 * @brief Return the symmetric difference of two sorted ranges.
4411 * @param first1 Start of first range.
4412 * @param last1 End of first range.
4413 * @param first2 Start of second range.
4414 * @param last2 End of second range.
4415 * @return End of the output range.
4416 * @ingroup setoperations
4418 * This operation iterates over both ranges, copying elements present in
4419 * one range but not the other in order to the output range. Iterators
4420 * increment for each range. When the current element of one range is less
4421 * than the other, that element is copied and the iterator advances. If an
4422 * element is contained in both ranges, no elements are copied and both
4423 * ranges advance. The output range may not overlap either input range.
4425 template<typename _InputIterator1, typename _InputIterator2,
4426 typename _OutputIterator>
4428 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4429 _InputIterator2 __first2, _InputIterator2 __last2,
4430 _OutputIterator __result)
4432 // concept requirements
4433 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4434 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4435 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4436 typename iterator_traits<_InputIterator1>::value_type>)
4437 __glibcxx_function_requires(_SameTypeConcept<
4438 typename iterator_traits<_InputIterator1>::value_type,
4439 typename iterator_traits<_InputIterator2>::value_type>)
4440 __glibcxx_function_requires(_LessThanComparableConcept<
4441 typename iterator_traits<_InputIterator1>::value_type>)
4442 __glibcxx_requires_sorted(__first1, __last1);
4443 __glibcxx_requires_sorted(__first2, __last2);
4445 while (__first1 != __last1 && __first2 != __last2)
4446 if (*__first1 < *__first2)
4448 *__result = *__first1;
4452 else if (*__first2 < *__first1)
4454 *__result = *__first2;
4463 return std::copy(__first2, __last2, std::copy(__first1,
4464 __last1, __result));
4468 * @brief Return the symmetric difference of two sorted ranges using
4469 * comparison functor.
4470 * @param first1 Start of first range.
4471 * @param last1 End of first range.
4472 * @param first2 Start of second range.
4473 * @param last2 End of second range.
4474 * @param comp The comparison functor.
4475 * @return End of the output range.
4476 * @ingroup setoperations
4478 * This operation iterates over both ranges, copying elements present in
4479 * one range but not the other in order to the output range. Iterators
4480 * increment for each range. When the current element of one range is less
4481 * than the other according to @a comp, that element is copied and the
4482 * iterator advances. If an element is contained in both ranges according
4483 * to @a comp, no elements are copied and both ranges advance. The output
4484 * range may not overlap either input range.
4486 template<typename _InputIterator1, typename _InputIterator2,
4487 typename _OutputIterator, typename _Compare>
4489 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4490 _InputIterator2 __first2, _InputIterator2 __last2,
4491 _OutputIterator __result,
4494 // concept requirements
4495 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4496 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4497 __glibcxx_function_requires(_SameTypeConcept<
4498 typename iterator_traits<_InputIterator1>::value_type,
4499 typename iterator_traits<_InputIterator2>::value_type>)
4500 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4501 typename iterator_traits<_InputIterator1>::value_type>)
4502 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4503 typename iterator_traits<_InputIterator1>::value_type,
4504 typename iterator_traits<_InputIterator2>::value_type>)
4505 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4506 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4508 while (__first1 != __last1 && __first2 != __last2)
4509 if (__comp(*__first1, *__first2))
4511 *__result = *__first1;
4515 else if (__comp(*__first2, *__first1))
4517 *__result = *__first2;
4526 return std::copy(__first2, __last2, std::copy(__first1,
4527 __last1, __result));
4530 // min_element and max_element, with and without an explicitly supplied
4531 // comparison function.
4534 * @brief Return the maximum element in a range.
4535 * @param first Start of range.
4536 * @param last End of range.
4537 * @return Iterator referencing the first instance of the largest value.
4539 template<typename _ForwardIterator>
4541 max_element(_ForwardIterator __first, _ForwardIterator __last)
4543 // concept requirements
4544 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4545 __glibcxx_function_requires(_LessThanComparableConcept<
4546 typename iterator_traits<_ForwardIterator>::value_type>)
4547 __glibcxx_requires_valid_range(__first, __last);
4549 if (__first == __last)
4551 _ForwardIterator __result = __first;
4552 while (++__first != __last)
4553 if (*__result < *__first)
4559 * @brief Return the maximum element in a range using comparison functor.
4560 * @param first Start of range.
4561 * @param last End of range.
4562 * @param comp Comparison functor.
4563 * @return Iterator referencing the first instance of the largest value
4564 * according to comp.
4566 template<typename _ForwardIterator, typename _Compare>
4568 max_element(_ForwardIterator __first, _ForwardIterator __last,
4571 // concept requirements
4572 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4573 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4574 typename iterator_traits<_ForwardIterator>::value_type,
4575 typename iterator_traits<_ForwardIterator>::value_type>)
4576 __glibcxx_requires_valid_range(__first, __last);
4578 if (__first == __last) return __first;
4579 _ForwardIterator __result = __first;
4580 while (++__first != __last)
4581 if (__comp(*__result, *__first)) __result = __first;
4586 * @brief Return the minimum element in a range.
4587 * @param first Start of range.
4588 * @param last End of range.
4589 * @return Iterator referencing the first instance of the smallest value.
4591 template<typename _ForwardIterator>
4593 min_element(_ForwardIterator __first, _ForwardIterator __last)
4595 // concept requirements
4596 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4597 __glibcxx_function_requires(_LessThanComparableConcept<
4598 typename iterator_traits<_ForwardIterator>::value_type>)
4599 __glibcxx_requires_valid_range(__first, __last);
4601 if (__first == __last)
4603 _ForwardIterator __result = __first;
4604 while (++__first != __last)
4605 if (*__first < *__result)
4611 * @brief Return the minimum element in a range using comparison functor.
4612 * @param first Start of range.
4613 * @param last End of range.
4614 * @param comp Comparison functor.
4615 * @return Iterator referencing the first instance of the smallest value
4616 * according to comp.
4618 template<typename _ForwardIterator, typename _Compare>
4620 min_element(_ForwardIterator __first, _ForwardIterator __last,
4623 // concept requirements
4624 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4625 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4626 typename iterator_traits<_ForwardIterator>::value_type,
4627 typename iterator_traits<_ForwardIterator>::value_type>)
4628 __glibcxx_requires_valid_range(__first, __last);
4630 if (__first == __last)
4632 _ForwardIterator __result = __first;
4633 while (++__first != __last)
4634 if (__comp(*__first, *__result))
4639 // next_permutation and prev_permutation, with and without an explicitly
4640 // supplied comparison function.
4643 * @brief Permute range into the next "dictionary" ordering.
4644 * @param first Start of range.
4645 * @param last End of range.
4646 * @return False if wrapped to first permutation, true otherwise.
4648 * Treats all permutations of the range as a set of "dictionary" sorted
4649 * sequences. Permutes the current sequence into the next one of this set.
4650 * Returns true if there are more sequences to generate. If the sequence
4651 * is the largest of the set, the smallest is generated and false returned.
4653 template<typename _BidirectionalIterator>
4655 next_permutation(_BidirectionalIterator __first,
4656 _BidirectionalIterator __last)
4658 // concept requirements
4659 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4660 _BidirectionalIterator>)
4661 __glibcxx_function_requires(_LessThanComparableConcept<
4662 typename iterator_traits<_BidirectionalIterator>::value_type>)
4663 __glibcxx_requires_valid_range(__first, __last);
4665 if (__first == __last)
4667 _BidirectionalIterator __i = __first;
4676 _BidirectionalIterator __ii = __i;
4680 _BidirectionalIterator __j = __last;
4681 while (!(*__i < *--__j))
4683 std::iter_swap(__i, __j);
4684 std::reverse(__ii, __last);
4689 std::reverse(__first, __last);
4696 * @brief Permute range into the next "dictionary" ordering using
4697 * comparison functor.
4698 * @param first Start of range.
4699 * @param last End of range.
4701 * @return False if wrapped to first permutation, true otherwise.
4703 * Treats all permutations of the range [first,last) as a set of
4704 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4705 * sequence into the next one of this set. Returns true if there are more
4706 * sequences to generate. If the sequence is the largest of the set, the
4707 * smallest is generated and false returned.
4709 template<typename _BidirectionalIterator, typename _Compare>
4711 next_permutation(_BidirectionalIterator __first,
4712 _BidirectionalIterator __last, _Compare __comp)
4714 // concept requirements
4715 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4716 _BidirectionalIterator>)
4717 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4718 typename iterator_traits<_BidirectionalIterator>::value_type,
4719 typename iterator_traits<_BidirectionalIterator>::value_type>)
4720 __glibcxx_requires_valid_range(__first, __last);
4722 if (__first == __last)
4724 _BidirectionalIterator __i = __first;
4733 _BidirectionalIterator __ii = __i;
4735 if (__comp(*__i, *__ii))
4737 _BidirectionalIterator __j = __last;
4738 while (!__comp(*__i, *--__j))
4740 std::iter_swap(__i, __j);
4741 std::reverse(__ii, __last);
4746 std::reverse(__first, __last);
4753 * @brief Permute range into the previous "dictionary" ordering.
4754 * @param first Start of range.
4755 * @param last End of range.
4756 * @return False if wrapped to last permutation, true otherwise.
4758 * Treats all permutations of the range as a set of "dictionary" sorted
4759 * sequences. Permutes the current sequence into the previous one of this
4760 * set. Returns true if there are more sequences to generate. If the
4761 * sequence is the smallest of the set, the largest is generated and false
4764 template<typename _BidirectionalIterator>
4766 prev_permutation(_BidirectionalIterator __first,
4767 _BidirectionalIterator __last)
4769 // concept requirements
4770 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4771 _BidirectionalIterator>)
4772 __glibcxx_function_requires(_LessThanComparableConcept<
4773 typename iterator_traits<_BidirectionalIterator>::value_type>)
4774 __glibcxx_requires_valid_range(__first, __last);
4776 if (__first == __last)
4778 _BidirectionalIterator __i = __first;
4787 _BidirectionalIterator __ii = __i;
4791 _BidirectionalIterator __j = __last;
4792 while (!(*--__j < *__i))
4794 std::iter_swap(__i, __j);
4795 std::reverse(__ii, __last);
4800 std::reverse(__first, __last);
4807 * @brief Permute range into the previous "dictionary" ordering using
4808 * comparison functor.
4809 * @param first Start of range.
4810 * @param last End of range.
4812 * @return False if wrapped to last permutation, true otherwise.
4814 * Treats all permutations of the range [first,last) as a set of
4815 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4816 * sequence into the previous one of this set. Returns true if there are
4817 * more sequences to generate. If the sequence is the smallest of the set,
4818 * the largest is generated and false returned.
4820 template<typename _BidirectionalIterator, typename _Compare>
4822 prev_permutation(_BidirectionalIterator __first,
4823 _BidirectionalIterator __last, _Compare __comp)
4825 // concept requirements
4826 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4827 _BidirectionalIterator>)
4828 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4829 typename iterator_traits<_BidirectionalIterator>::value_type,
4830 typename iterator_traits<_BidirectionalIterator>::value_type>)
4831 __glibcxx_requires_valid_range(__first, __last);
4833 if (__first == __last)
4835 _BidirectionalIterator __i = __first;
4844 _BidirectionalIterator __ii = __i;
4846 if (__comp(*__ii, *__i))
4848 _BidirectionalIterator __j = __last;
4849 while (!__comp(*--__j, *__i))
4851 std::iter_swap(__i, __j);
4852 std::reverse(__ii, __last);
4857 std::reverse(__first, __last);
4863 // find_first_of, with and without an explicitly supplied comparison function.
4866 * @brief Find element from a set in a sequence.
4867 * @param first1 Start of range to search.
4868 * @param last1 End of range to search.
4869 * @param first2 Start of match candidates.
4870 * @param last2 End of match candidates.
4871 * @return The first iterator @c i in the range
4872 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4873 * interator in [first2,last2), or @p last1 if no such iterator exists.
4875 * Searches the range @p [first1,last1) for an element that is equal to
4876 * some element in the range [first2,last2). If found, returns an iterator
4877 * in the range [first1,last1), otherwise returns @p last1.
4879 template<typename _InputIterator, typename _ForwardIterator>
4881 find_first_of(_InputIterator __first1, _InputIterator __last1,
4882 _ForwardIterator __first2, _ForwardIterator __last2)
4884 // concept requirements
4885 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4886 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4887 __glibcxx_function_requires(_EqualOpConcept<
4888 typename iterator_traits<_InputIterator>::value_type,
4889 typename iterator_traits<_ForwardIterator>::value_type>)
4890 __glibcxx_requires_valid_range(__first1, __last1);
4891 __glibcxx_requires_valid_range(__first2, __last2);
4893 for ( ; __first1 != __last1; ++__first1)
4894 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4895 if (*__first1 == *__iter)
4901 * @brief Find element from a set in a sequence using a predicate.
4902 * @param first1 Start of range to search.
4903 * @param last1 End of range to search.
4904 * @param first2 Start of match candidates.
4905 * @param last2 End of match candidates.
4906 * @param comp Predicate to use.
4907 * @return The first iterator @c i in the range
4908 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4909 * interator in [first2,last2), or @p last1 if no such iterator exists.
4911 * Searches the range @p [first1,last1) for an element that is equal to
4912 * some element in the range [first2,last2). If found, returns an iterator in
4913 * the range [first1,last1), otherwise returns @p last1.
4915 template<typename _InputIterator, typename _ForwardIterator,
4916 typename _BinaryPredicate>
4918 find_first_of(_InputIterator __first1, _InputIterator __last1,
4919 _ForwardIterator __first2, _ForwardIterator __last2,
4920 _BinaryPredicate __comp)
4922 // concept requirements
4923 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4924 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4925 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4926 typename iterator_traits<_InputIterator>::value_type,
4927 typename iterator_traits<_ForwardIterator>::value_type>)
4928 __glibcxx_requires_valid_range(__first1, __last1);
4929 __glibcxx_requires_valid_range(__first2, __last2);
4931 for ( ; __first1 != __last1; ++__first1)
4932 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4933 if (__comp(*__first1, *__iter))
4939 // find_end, with and without an explicitly supplied comparison function.
4940 // Search [first2, last2) as a subsequence in [first1, last1), and return
4941 // the *last* possible match. Note that find_end for bidirectional iterators
4942 // is much faster than for forward iterators.
4944 // find_end for forward iterators.
4945 template<typename _ForwardIterator1, typename _ForwardIterator2>
4947 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4948 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4949 forward_iterator_tag, forward_iterator_tag)
4951 if (__first2 == __last2)
4955 _ForwardIterator1 __result = __last1;
4958 _ForwardIterator1 __new_result
4959 = std::search(__first1, __last1, __first2, __last2);
4960 if (__new_result == __last1)
4964 __result = __new_result;
4965 __first1 = __new_result;
4972 template<typename _ForwardIterator1, typename _ForwardIterator2,
4973 typename _BinaryPredicate>
4975 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4976 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4977 forward_iterator_tag, forward_iterator_tag,
4978 _BinaryPredicate __comp)
4980 if (__first2 == __last2)
4984 _ForwardIterator1 __result = __last1;
4987 _ForwardIterator1 __new_result
4988 = std::search(__first1, __last1, __first2, __last2, __comp);
4989 if (__new_result == __last1)
4993 __result = __new_result;
4994 __first1 = __new_result;
5001 // find_end for bidirectional iterators. Requires partial specialization.
5002 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
5003 _BidirectionalIterator1
5004 __find_end(_BidirectionalIterator1 __first1,
5005 _BidirectionalIterator1 __last1,
5006 _BidirectionalIterator2 __first2,
5007 _BidirectionalIterator2 __last2,
5008 bidirectional_iterator_tag, bidirectional_iterator_tag)
5010 // concept requirements
5011 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5012 _BidirectionalIterator1>)
5013 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5014 _BidirectionalIterator2>)
5016 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5017 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5019 _RevIterator1 __rlast1(__first1);
5020 _RevIterator2 __rlast2(__first2);
5021 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5022 _RevIterator2(__last2), __rlast2);
5024 if (__rresult == __rlast1)
5028 _BidirectionalIterator1 __result = __rresult.base();
5029 std::advance(__result, -std::distance(__first2, __last2));
5034 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
5035 typename _BinaryPredicate>
5036 _BidirectionalIterator1
5037 __find_end(_BidirectionalIterator1 __first1,
5038 _BidirectionalIterator1 __last1,
5039 _BidirectionalIterator2 __first2,
5040 _BidirectionalIterator2 __last2,
5041 bidirectional_iterator_tag, bidirectional_iterator_tag,
5042 _BinaryPredicate __comp)
5044 // concept requirements
5045 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5046 _BidirectionalIterator1>)
5047 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5048 _BidirectionalIterator2>)
5050 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5051 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5053 _RevIterator1 __rlast1(__first1);
5054 _RevIterator2 __rlast2(__first2);
5055 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5056 _RevIterator2(__last2), __rlast2,
5059 if (__rresult == __rlast1)
5063 _BidirectionalIterator1 __result = __rresult.base();
5064 std::advance(__result, -std::distance(__first2, __last2));
5069 // Dispatching functions for find_end.
5072 * @brief Find last matching subsequence in a sequence.
5073 * @param first1 Start of range to search.
5074 * @param last1 End of range to search.
5075 * @param first2 Start of sequence to match.
5076 * @param last2 End of sequence to match.
5077 * @return The last iterator @c i in the range
5078 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5079 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5080 * such iterator exists.
5082 * Searches the range @p [first1,last1) for a sub-sequence that compares
5083 * equal value-by-value with the sequence given by @p [first2,last2) and
5084 * returns an iterator to the first element of the sub-sequence, or
5085 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5086 * last such subsequence contained in [first,last1).
5088 * Because the sub-sequence must lie completely within the range
5089 * @p [first1,last1) it must start at a position less than
5090 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5092 * This means that the returned iterator @c i will be in the range
5093 * @p [first1,last1-(last2-first2))
5095 template<typename _ForwardIterator1, typename _ForwardIterator2>
5096 inline _ForwardIterator1
5097 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5098 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
5100 // concept requirements
5101 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5102 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5103 __glibcxx_function_requires(_EqualOpConcept<
5104 typename iterator_traits<_ForwardIterator1>::value_type,
5105 typename iterator_traits<_ForwardIterator2>::value_type>)
5106 __glibcxx_requires_valid_range(__first1, __last1);
5107 __glibcxx_requires_valid_range(__first2, __last2);
5109 return std::__find_end(__first1, __last1, __first2, __last2,
5110 std::__iterator_category(__first1),
5111 std::__iterator_category(__first2));
5115 * @brief Find last matching subsequence in a sequence using a predicate.
5116 * @param first1 Start of range to search.
5117 * @param last1 End of range to search.
5118 * @param first2 Start of sequence to match.
5119 * @param last2 End of sequence to match.
5120 * @param comp The predicate to use.
5121 * @return The last iterator @c i in the range
5122 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5123 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5124 * @p last1 if no such iterator exists.
5126 * Searches the range @p [first1,last1) for a sub-sequence that compares
5127 * equal value-by-value with the sequence given by @p [first2,last2) using
5128 * comp as a predicate and returns an iterator to the first element of the
5129 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5130 * sub-sequence will be the last such subsequence contained in
5133 * Because the sub-sequence must lie completely within the range
5134 * @p [first1,last1) it must start at a position less than
5135 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5137 * This means that the returned iterator @c i will be in the range
5138 * @p [first1,last1-(last2-first2))
5140 template<typename _ForwardIterator1, typename _ForwardIterator2,
5141 typename _BinaryPredicate>
5142 inline _ForwardIterator1
5143 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5144 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5145 _BinaryPredicate __comp)
5147 // concept requirements
5148 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5149 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5150 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
5151 typename iterator_traits<_ForwardIterator1>::value_type,
5152 typename iterator_traits<_ForwardIterator2>::value_type>)
5153 __glibcxx_requires_valid_range(__first1, __last1);
5154 __glibcxx_requires_valid_range(__first2, __last2);
5156 return std::__find_end(__first1, __last1, __first2, __last2,
5157 std::__iterator_category(__first1),
5158 std::__iterator_category(__first2),
5164 #endif /* _ALGO_H */