1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_algo.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
59 #include <cstdlib> // for rand
60 #include <bits/algorithmfwd.h>
61 #include <bits/stl_heap.h>
62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
63 #include <bits/predefined_ops.h>
65 #if __cplusplus >= 201103L
66 #include <random> // for std::uniform_int_distribution
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 namespace std _GLIBCXX_VISIBILITY(default)
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
75 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result
76 template<typename _Iterator, typename _Compare>
78 __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b,
79 _Iterator __c, _Compare __comp)
84 std::iter_swap(__result, __b);
85 else if (__comp(__a, __c))
86 std::iter_swap(__result, __c);
88 std::iter_swap(__result, __a);
90 else if (__comp(__a, __c))
91 std::iter_swap(__result, __a);
92 else if (__comp(__b, __c))
93 std::iter_swap(__result, __c);
95 std::iter_swap(__result, __b);
98 /// This is an overload used by find algos for the Input Iterator case.
99 template<typename _InputIterator, typename _Predicate>
100 inline _InputIterator
101 __find_if(_InputIterator __first, _InputIterator __last,
102 _Predicate __pred, input_iterator_tag)
104 while (__first != __last && !__pred(__first))
109 /// This is an overload used by find algos for the RAI case.
110 template<typename _RandomAccessIterator, typename _Predicate>
111 _RandomAccessIterator
112 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
113 _Predicate __pred, random_access_iterator_tag)
115 typename iterator_traits<_RandomAccessIterator>::difference_type
116 __trip_count = (__last - __first) >> 2;
118 for (; __trip_count > 0; --__trip_count)
137 switch (__last - __first)
157 template<typename _Iterator, typename _Predicate>
159 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
161 return __find_if(__first, __last, __pred,
162 std::__iterator_category(__first));
165 /// Provided for stable_partition to use.
166 template<typename _InputIterator, typename _Predicate>
167 inline _InputIterator
168 __find_if_not(_InputIterator __first, _InputIterator __last,
171 return std::__find_if(__first, __last,
172 __gnu_cxx::__ops::__negate(__pred),
173 std::__iterator_category(__first));
176 /// Like find_if_not(), but uses and updates a count of the
177 /// remaining range length instead of comparing against an end
179 template<typename _InputIterator, typename _Predicate, typename _Distance>
181 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
183 for (; __len; --__len, ++__first)
184 if (!__pred(__first))
191 // set_symmetric_difference
202 template<typename _ForwardIterator1, typename _ForwardIterator2,
203 typename _BinaryPredicate>
205 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
206 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
207 _BinaryPredicate __predicate)
209 // Test for empty ranges
210 if (__first1 == __last1 || __first2 == __last2)
213 // Test for a pattern of length 1.
214 _ForwardIterator2 __p1(__first2);
215 if (++__p1 == __last2)
216 return std::__find_if(__first1, __last1,
217 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
220 _ForwardIterator2 __p;
221 _ForwardIterator1 __current = __first1;
226 std::__find_if(__first1, __last1,
227 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
229 if (__first1 == __last1)
233 __current = __first1;
234 if (++__current == __last1)
237 while (__predicate(__current, __p))
239 if (++__p == __last2)
241 if (++__current == __last1)
252 * This is an helper function for search_n overloaded for forward iterators.
254 template<typename _ForwardIterator, typename _Integer,
255 typename _UnaryPredicate>
257 __search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
258 _Integer __count, _UnaryPredicate __unary_pred,
259 std::forward_iterator_tag)
261 __first = std::__find_if(__first, __last, __unary_pred);
262 while (__first != __last)
264 typename iterator_traits<_ForwardIterator>::difference_type
266 _ForwardIterator __i = __first;
268 while (__i != __last && __n != 1 && __unary_pred(__i))
277 __first = std::__find_if(++__i, __last, __unary_pred);
283 * This is an helper function for search_n overloaded for random access
286 template<typename _RandomAccessIter, typename _Integer,
287 typename _UnaryPredicate>
289 __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
290 _Integer __count, _UnaryPredicate __unary_pred,
291 std::random_access_iterator_tag)
293 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
296 _DistanceType __tailSize = __last - __first;
297 _DistanceType __remainder = __count;
299 while (__remainder <= __tailSize) // the main loop...
301 __first += __remainder;
302 __tailSize -= __remainder;
303 // __first here is always pointing to one past the last element of
304 // next possible match.
305 _RandomAccessIter __backTrack = __first;
306 while (__unary_pred(--__backTrack))
308 if (--__remainder == 0)
309 return (__first - __count); // Success
311 __remainder = __count + 1 - (__first - __backTrack);
313 return __last; // Failure
316 template<typename _ForwardIterator, typename _Integer,
317 typename _UnaryPredicate>
319 __search_n(_ForwardIterator __first, _ForwardIterator __last,
321 _UnaryPredicate __unary_pred)
327 return std::__find_if(__first, __last, __unary_pred);
329 return std::__search_n_aux(__first, __last, __count, __unary_pred,
330 std::__iterator_category(__first));
333 // find_end for forward iterators.
334 template<typename _ForwardIterator1, typename _ForwardIterator2,
335 typename _BinaryPredicate>
337 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
338 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
339 forward_iterator_tag, forward_iterator_tag,
340 _BinaryPredicate __comp)
342 if (__first2 == __last2)
345 _ForwardIterator1 __result = __last1;
348 _ForwardIterator1 __new_result
349 = std::__search(__first1, __last1, __first2, __last2, __comp);
350 if (__new_result == __last1)
354 __result = __new_result;
355 __first1 = __new_result;
361 // find_end for bidirectional iterators (much faster).
362 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
363 typename _BinaryPredicate>
364 _BidirectionalIterator1
365 __find_end(_BidirectionalIterator1 __first1,
366 _BidirectionalIterator1 __last1,
367 _BidirectionalIterator2 __first2,
368 _BidirectionalIterator2 __last2,
369 bidirectional_iterator_tag, bidirectional_iterator_tag,
370 _BinaryPredicate __comp)
372 // concept requirements
373 __glibcxx_function_requires(_BidirectionalIteratorConcept<
374 _BidirectionalIterator1>)
375 __glibcxx_function_requires(_BidirectionalIteratorConcept<
376 _BidirectionalIterator2>)
378 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
379 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
381 _RevIterator1 __rlast1(__first1);
382 _RevIterator2 __rlast2(__first2);
383 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
384 _RevIterator2(__last2), __rlast2,
387 if (__rresult == __rlast1)
391 _BidirectionalIterator1 __result = __rresult.base();
392 std::advance(__result, -std::distance(__first2, __last2));
398 * @brief Find last matching subsequence in a sequence.
399 * @ingroup non_mutating_algorithms
400 * @param __first1 Start of range to search.
401 * @param __last1 End of range to search.
402 * @param __first2 Start of sequence to match.
403 * @param __last2 End of sequence to match.
404 * @return The last iterator @c i in the range
405 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
406 * @p *(__first2+N) for each @c N in the range @p
407 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
409 * Searches the range @p [__first1,__last1) for a sub-sequence that
410 * compares equal value-by-value with the sequence given by @p
411 * [__first2,__last2) and returns an iterator to the __first
412 * element of the sub-sequence, or @p __last1 if the sub-sequence
413 * is not found. The sub-sequence will be the last such
414 * subsequence contained in [__first1,__last1).
416 * Because the sub-sequence must lie completely within the range @p
417 * [__first1,__last1) it must start at a position less than @p
418 * __last1-(__last2-__first2) where @p __last2-__first2 is the
419 * length of the sub-sequence. This means that the returned
420 * iterator @c i will be in the range @p
421 * [__first1,__last1-(__last2-__first2))
423 template<typename _ForwardIterator1, typename _ForwardIterator2>
424 inline _ForwardIterator1
425 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
426 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
428 // concept requirements
429 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
430 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
431 __glibcxx_function_requires(_EqualOpConcept<
432 typename iterator_traits<_ForwardIterator1>::value_type,
433 typename iterator_traits<_ForwardIterator2>::value_type>)
434 __glibcxx_requires_valid_range(__first1, __last1);
435 __glibcxx_requires_valid_range(__first2, __last2);
437 return std::__find_end(__first1, __last1, __first2, __last2,
438 std::__iterator_category(__first1),
439 std::__iterator_category(__first2),
440 __gnu_cxx::__ops::__iter_equal_to_iter());
444 * @brief Find last matching subsequence in a sequence using a predicate.
445 * @ingroup non_mutating_algorithms
446 * @param __first1 Start of range to search.
447 * @param __last1 End of range to search.
448 * @param __first2 Start of sequence to match.
449 * @param __last2 End of sequence to match.
450 * @param __comp The predicate to use.
451 * @return The last iterator @c i in the range @p
452 * [__first1,__last1-(__last2-__first2)) such that @c
453 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
454 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
457 * Searches the range @p [__first1,__last1) for a sub-sequence that
458 * compares equal value-by-value with the sequence given by @p
459 * [__first2,__last2) using comp as a predicate and returns an
460 * iterator to the first element of the sub-sequence, or @p __last1
461 * if the sub-sequence is not found. The sub-sequence will be the
462 * last such subsequence contained in [__first,__last1).
464 * Because the sub-sequence must lie completely within the range @p
465 * [__first1,__last1) it must start at a position less than @p
466 * __last1-(__last2-__first2) where @p __last2-__first2 is the
467 * length of the sub-sequence. This means that the returned
468 * iterator @c i will be in the range @p
469 * [__first1,__last1-(__last2-__first2))
471 template<typename _ForwardIterator1, typename _ForwardIterator2,
472 typename _BinaryPredicate>
473 inline _ForwardIterator1
474 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
475 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
476 _BinaryPredicate __comp)
478 // concept requirements
479 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
481 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
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);
487 return std::__find_end(__first1, __last1, __first2, __last2,
488 std::__iterator_category(__first1),
489 std::__iterator_category(__first2),
490 __gnu_cxx::__ops::__iter_comp_iter(__comp));
493 #if __cplusplus >= 201103L
495 * @brief Checks that a predicate is true for all the elements
497 * @ingroup non_mutating_algorithms
498 * @param __first An input iterator.
499 * @param __last An input iterator.
500 * @param __pred A predicate.
501 * @return True if the check is true, false otherwise.
503 * Returns true if @p __pred is true for each element in the range
504 * @p [__first,__last), and false otherwise.
506 template<typename _InputIterator, typename _Predicate>
508 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
509 { return __last == std::find_if_not(__first, __last, __pred); }
512 * @brief Checks that a predicate is false for all the elements
514 * @ingroup non_mutating_algorithms
515 * @param __first An input iterator.
516 * @param __last An input iterator.
517 * @param __pred A predicate.
518 * @return True if the check is true, false otherwise.
520 * Returns true if @p __pred is false for each element in the range
521 * @p [__first,__last), and false otherwise.
523 template<typename _InputIterator, typename _Predicate>
525 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
526 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
529 * @brief Checks that a predicate is false for at least an element
531 * @ingroup non_mutating_algorithms
532 * @param __first An input iterator.
533 * @param __last An input iterator.
534 * @param __pred A predicate.
535 * @return True if the check is true, false otherwise.
537 * Returns true if an element exists in the range @p
538 * [__first,__last) such that @p __pred is true, and false
541 template<typename _InputIterator, typename _Predicate>
543 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
544 { return !std::none_of(__first, __last, __pred); }
547 * @brief Find the first element in a sequence for which a
548 * predicate is false.
549 * @ingroup non_mutating_algorithms
550 * @param __first An input iterator.
551 * @param __last An input iterator.
552 * @param __pred A predicate.
553 * @return The first iterator @c i in the range @p [__first,__last)
554 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
556 template<typename _InputIterator, typename _Predicate>
557 inline _InputIterator
558 find_if_not(_InputIterator __first, _InputIterator __last,
561 // concept requirements
562 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
563 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
564 typename iterator_traits<_InputIterator>::value_type>)
565 __glibcxx_requires_valid_range(__first, __last);
566 return std::__find_if_not(__first, __last,
567 __gnu_cxx::__ops::__pred_iter(__pred));
571 * @brief Checks whether the sequence is partitioned.
572 * @ingroup mutating_algorithms
573 * @param __first An input iterator.
574 * @param __last An input iterator.
575 * @param __pred A predicate.
576 * @return True if the range @p [__first,__last) is partioned by @p __pred,
577 * i.e. if all elements that satisfy @p __pred appear before those that
580 template<typename _InputIterator, typename _Predicate>
582 is_partitioned(_InputIterator __first, _InputIterator __last,
585 __first = std::find_if_not(__first, __last, __pred);
586 return std::none_of(__first, __last, __pred);
590 * @brief Find the partition point of a partitioned range.
591 * @ingroup mutating_algorithms
592 * @param __first An iterator.
593 * @param __last Another iterator.
594 * @param __pred A predicate.
595 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
596 * and @p none_of(mid, __last, __pred) are both true.
598 template<typename _ForwardIterator, typename _Predicate>
600 partition_point(_ForwardIterator __first, _ForwardIterator __last,
603 // concept requirements
604 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
605 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
606 typename iterator_traits<_ForwardIterator>::value_type>)
608 // A specific debug-mode test will be necessary...
609 __glibcxx_requires_valid_range(__first, __last);
611 typedef typename iterator_traits<_ForwardIterator>::difference_type
614 _DistanceType __len = std::distance(__first, __last);
615 _DistanceType __half;
616 _ForwardIterator __middle;
622 std::advance(__middle, __half);
623 if (__pred(*__middle))
627 __len = __len - __half - 1;
636 template<typename _InputIterator, typename _OutputIterator,
639 __remove_copy_if(_InputIterator __first, _InputIterator __last,
640 _OutputIterator __result, _Predicate __pred)
642 for (; __first != __last; ++__first)
643 if (!__pred(__first))
645 *__result = *__first;
652 * @brief Copy a sequence, removing elements of a given value.
653 * @ingroup mutating_algorithms
654 * @param __first An input iterator.
655 * @param __last An input iterator.
656 * @param __result An output iterator.
657 * @param __value The value to be removed.
658 * @return An iterator designating the end of the resulting sequence.
660 * Copies each element in the range @p [__first,__last) not equal
661 * to @p __value to the range beginning at @p __result.
662 * remove_copy() is stable, so the relative order of elements that
663 * are copied is unchanged.
665 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
666 inline _OutputIterator
667 remove_copy(_InputIterator __first, _InputIterator __last,
668 _OutputIterator __result, const _Tp& __value)
670 // concept requirements
671 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
672 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
673 typename iterator_traits<_InputIterator>::value_type>)
674 __glibcxx_function_requires(_EqualOpConcept<
675 typename iterator_traits<_InputIterator>::value_type, _Tp>)
676 __glibcxx_requires_valid_range(__first, __last);
678 return std::__remove_copy_if(__first, __last, __result,
679 __gnu_cxx::__ops::__iter_equals_val(__value));
683 * @brief Copy a sequence, removing elements for which a predicate is true.
684 * @ingroup mutating_algorithms
685 * @param __first An input iterator.
686 * @param __last An input iterator.
687 * @param __result An output iterator.
688 * @param __pred A predicate.
689 * @return An iterator designating the end of the resulting sequence.
691 * Copies each element in the range @p [__first,__last) for which
692 * @p __pred returns false to the range beginning at @p __result.
694 * remove_copy_if() is stable, so the relative order of elements that are
695 * copied is unchanged.
697 template<typename _InputIterator, typename _OutputIterator,
699 inline _OutputIterator
700 remove_copy_if(_InputIterator __first, _InputIterator __last,
701 _OutputIterator __result, _Predicate __pred)
703 // concept requirements
704 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
705 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
706 typename iterator_traits<_InputIterator>::value_type>)
707 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
708 typename iterator_traits<_InputIterator>::value_type>)
709 __glibcxx_requires_valid_range(__first, __last);
711 return std::__remove_copy_if(__first, __last, __result,
712 __gnu_cxx::__ops::__pred_iter(__pred));
715 #if __cplusplus >= 201103L
717 * @brief Copy the elements of a sequence for which a predicate is true.
718 * @ingroup mutating_algorithms
719 * @param __first An input iterator.
720 * @param __last An input iterator.
721 * @param __result An output iterator.
722 * @param __pred A predicate.
723 * @return An iterator designating the end of the resulting sequence.
725 * Copies each element in the range @p [__first,__last) for which
726 * @p __pred returns true to the range beginning at @p __result.
728 * copy_if() is stable, so the relative order of elements that are
729 * copied is unchanged.
731 template<typename _InputIterator, typename _OutputIterator,
734 copy_if(_InputIterator __first, _InputIterator __last,
735 _OutputIterator __result, _Predicate __pred)
737 // concept requirements
738 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
739 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
740 typename iterator_traits<_InputIterator>::value_type>)
741 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
742 typename iterator_traits<_InputIterator>::value_type>)
743 __glibcxx_requires_valid_range(__first, __last);
745 for (; __first != __last; ++__first)
746 if (__pred(*__first))
748 *__result = *__first;
754 template<typename _InputIterator, typename _Size, typename _OutputIterator>
756 __copy_n(_InputIterator __first, _Size __n,
757 _OutputIterator __result, input_iterator_tag)
763 *__result = *__first;
774 template<typename _RandomAccessIterator, typename _Size,
775 typename _OutputIterator>
776 inline _OutputIterator
777 __copy_n(_RandomAccessIterator __first, _Size __n,
778 _OutputIterator __result, random_access_iterator_tag)
779 { return std::copy(__first, __first + __n, __result); }
782 * @brief Copies the range [first,first+n) into [result,result+n).
783 * @ingroup mutating_algorithms
784 * @param __first An input iterator.
785 * @param __n The number of elements to copy.
786 * @param __result An output iterator.
789 * This inline function will boil down to a call to @c memmove whenever
790 * possible. Failing that, if random access iterators are passed, then the
791 * loop count will be known (and therefore a candidate for compiler
792 * optimizations such as unrolling).
794 template<typename _InputIterator, typename _Size, typename _OutputIterator>
795 inline _OutputIterator
796 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
798 // concept requirements
799 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
800 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
801 typename iterator_traits<_InputIterator>::value_type>)
803 return std::__copy_n(__first, __n, __result,
804 std::__iterator_category(__first));
808 * @brief Copy the elements of a sequence to separate output sequences
809 * depending on the truth value of a predicate.
810 * @ingroup mutating_algorithms
811 * @param __first An input iterator.
812 * @param __last An input iterator.
813 * @param __out_true An output iterator.
814 * @param __out_false An output iterator.
815 * @param __pred A predicate.
816 * @return A pair designating the ends of the resulting sequences.
818 * Copies each element in the range @p [__first,__last) for which
819 * @p __pred returns true to the range beginning at @p out_true
820 * and each element for which @p __pred returns false to @p __out_false.
822 template<typename _InputIterator, typename _OutputIterator1,
823 typename _OutputIterator2, typename _Predicate>
824 pair<_OutputIterator1, _OutputIterator2>
825 partition_copy(_InputIterator __first, _InputIterator __last,
826 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
829 // concept requirements
830 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
831 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
832 typename iterator_traits<_InputIterator>::value_type>)
833 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
834 typename iterator_traits<_InputIterator>::value_type>)
835 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
836 typename iterator_traits<_InputIterator>::value_type>)
837 __glibcxx_requires_valid_range(__first, __last);
839 for (; __first != __last; ++__first)
840 if (__pred(*__first))
842 *__out_true = *__first;
847 *__out_false = *__first;
851 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
855 template<typename _ForwardIterator, typename _Predicate>
857 __remove_if(_ForwardIterator __first, _ForwardIterator __last,
860 __first = std::__find_if(__first, __last, __pred);
861 if (__first == __last)
863 _ForwardIterator __result = __first;
865 for (; __first != __last; ++__first)
866 if (!__pred(__first))
868 *__result = _GLIBCXX_MOVE(*__first);
875 * @brief Remove elements from a sequence.
876 * @ingroup mutating_algorithms
877 * @param __first An input iterator.
878 * @param __last An input iterator.
879 * @param __value The value to be removed.
880 * @return An iterator designating the end of the resulting sequence.
882 * All elements equal to @p __value are removed from the range
883 * @p [__first,__last).
885 * remove() is stable, so the relative order of elements that are
886 * not removed is unchanged.
888 * Elements between the end of the resulting sequence and @p __last
889 * are still present, but their value is unspecified.
891 template<typename _ForwardIterator, typename _Tp>
892 inline _ForwardIterator
893 remove(_ForwardIterator __first, _ForwardIterator __last,
896 // concept requirements
897 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
899 __glibcxx_function_requires(_EqualOpConcept<
900 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
901 __glibcxx_requires_valid_range(__first, __last);
903 return std::__remove_if(__first, __last,
904 __gnu_cxx::__ops::__iter_equals_val(__value));
908 * @brief Remove elements from a sequence using a predicate.
909 * @ingroup mutating_algorithms
910 * @param __first A forward iterator.
911 * @param __last A forward iterator.
912 * @param __pred A predicate.
913 * @return An iterator designating the end of the resulting sequence.
915 * All elements for which @p __pred returns true are removed from the range
916 * @p [__first,__last).
918 * remove_if() is stable, so the relative order of elements that are
919 * not removed is unchanged.
921 * Elements between the end of the resulting sequence and @p __last
922 * are still present, but their value is unspecified.
924 template<typename _ForwardIterator, typename _Predicate>
925 inline _ForwardIterator
926 remove_if(_ForwardIterator __first, _ForwardIterator __last,
929 // concept requirements
930 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
932 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
933 typename iterator_traits<_ForwardIterator>::value_type>)
934 __glibcxx_requires_valid_range(__first, __last);
936 return std::__remove_if(__first, __last,
937 __gnu_cxx::__ops::__pred_iter(__pred));
940 template<typename _ForwardIterator, typename _BinaryPredicate>
942 __adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
943 _BinaryPredicate __binary_pred)
945 if (__first == __last)
947 _ForwardIterator __next = __first;
948 while (++__next != __last)
950 if (__binary_pred(__first, __next))
957 template<typename _ForwardIterator, typename _BinaryPredicate>
959 __unique(_ForwardIterator __first, _ForwardIterator __last,
960 _BinaryPredicate __binary_pred)
962 // Skip the beginning, if already unique.
963 __first = std::__adjacent_find(__first, __last, __binary_pred);
964 if (__first == __last)
967 // Do the real copy work.
968 _ForwardIterator __dest = __first;
970 while (++__first != __last)
971 if (!__binary_pred(__dest, __first))
972 *++__dest = _GLIBCXX_MOVE(*__first);
977 * @brief Remove consecutive duplicate values from a sequence.
978 * @ingroup mutating_algorithms
979 * @param __first A forward iterator.
980 * @param __last A forward iterator.
981 * @return An iterator designating the end of the resulting sequence.
983 * Removes all but the first element from each group of consecutive
984 * values that compare equal.
985 * unique() is stable, so the relative order of elements that are
986 * not removed is unchanged.
987 * Elements between the end of the resulting sequence and @p __last
988 * are still present, but their value is unspecified.
990 template<typename _ForwardIterator>
991 inline _ForwardIterator
992 unique(_ForwardIterator __first, _ForwardIterator __last)
994 // concept requirements
995 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
997 __glibcxx_function_requires(_EqualityComparableConcept<
998 typename iterator_traits<_ForwardIterator>::value_type>)
999 __glibcxx_requires_valid_range(__first, __last);
1001 return std::__unique(__first, __last,
1002 __gnu_cxx::__ops::__iter_equal_to_iter());
1006 * @brief Remove consecutive values from a sequence using a predicate.
1007 * @ingroup mutating_algorithms
1008 * @param __first A forward iterator.
1009 * @param __last A forward iterator.
1010 * @param __binary_pred A binary predicate.
1011 * @return An iterator designating the end of the resulting sequence.
1013 * Removes all but the first element from each group of consecutive
1014 * values for which @p __binary_pred returns true.
1015 * unique() is stable, so the relative order of elements that are
1016 * not removed is unchanged.
1017 * Elements between the end of the resulting sequence and @p __last
1018 * are still present, but their value is unspecified.
1020 template<typename _ForwardIterator, typename _BinaryPredicate>
1021 inline _ForwardIterator
1022 unique(_ForwardIterator __first, _ForwardIterator __last,
1023 _BinaryPredicate __binary_pred)
1025 // concept requirements
1026 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1028 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1029 typename iterator_traits<_ForwardIterator>::value_type,
1030 typename iterator_traits<_ForwardIterator>::value_type>)
1031 __glibcxx_requires_valid_range(__first, __last);
1033 return std::__unique(__first, __last,
1034 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1038 * This is an uglified
1039 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1041 * overloaded for forward iterators and output iterator as result.
1043 template<typename _ForwardIterator, typename _OutputIterator,
1044 typename _BinaryPredicate>
1046 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1047 _OutputIterator __result, _BinaryPredicate __binary_pred,
1048 forward_iterator_tag, output_iterator_tag)
1050 // concept requirements -- iterators already checked
1051 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1052 typename iterator_traits<_ForwardIterator>::value_type,
1053 typename iterator_traits<_ForwardIterator>::value_type>)
1055 _ForwardIterator __next = __first;
1056 *__result = *__first;
1057 while (++__next != __last)
1058 if (!__binary_pred(__first, __next))
1061 *++__result = *__first;
1067 * This is an uglified
1068 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1070 * overloaded for input iterators and output iterator as result.
1072 template<typename _InputIterator, typename _OutputIterator,
1073 typename _BinaryPredicate>
1075 __unique_copy(_InputIterator __first, _InputIterator __last,
1076 _OutputIterator __result, _BinaryPredicate __binary_pred,
1077 input_iterator_tag, output_iterator_tag)
1079 // concept requirements -- iterators already checked
1080 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1081 typename iterator_traits<_InputIterator>::value_type,
1082 typename iterator_traits<_InputIterator>::value_type>)
1084 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1085 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred))
1087 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred);
1088 *__result = __value;
1089 while (++__first != __last)
1090 if (!__rebound_pred(__first, __value))
1093 *++__result = __value;
1099 * This is an uglified
1100 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1102 * overloaded for input iterators and forward iterator as result.
1104 template<typename _InputIterator, typename _ForwardIterator,
1105 typename _BinaryPredicate>
1107 __unique_copy(_InputIterator __first, _InputIterator __last,
1108 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1109 input_iterator_tag, forward_iterator_tag)
1111 // concept requirements -- iterators already checked
1112 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1113 typename iterator_traits<_ForwardIterator>::value_type,
1114 typename iterator_traits<_InputIterator>::value_type>)
1115 *__result = *__first;
1116 while (++__first != __last)
1117 if (!__binary_pred(__result, __first))
1118 *++__result = *__first;
1123 * This is an uglified reverse(_BidirectionalIterator,
1124 * _BidirectionalIterator)
1125 * overloaded for bidirectional iterators.
1127 template<typename _BidirectionalIterator>
1129 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1130 bidirectional_iterator_tag)
1133 if (__first == __last || __first == --__last)
1137 std::iter_swap(__first, __last);
1143 * This is an uglified reverse(_BidirectionalIterator,
1144 * _BidirectionalIterator)
1145 * overloaded for random access iterators.
1147 template<typename _RandomAccessIterator>
1149 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1150 random_access_iterator_tag)
1152 if (__first == __last)
1155 while (__first < __last)
1157 std::iter_swap(__first, __last);
1164 * @brief Reverse a sequence.
1165 * @ingroup mutating_algorithms
1166 * @param __first A bidirectional iterator.
1167 * @param __last A bidirectional iterator.
1168 * @return reverse() returns no value.
1170 * Reverses the order of the elements in the range @p [__first,__last),
1171 * so that the first element becomes the last etc.
1172 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1173 * swaps @p *(__first+i) and @p *(__last-(i+1))
1175 template<typename _BidirectionalIterator>
1177 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1179 // concept requirements
1180 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1181 _BidirectionalIterator>)
1182 __glibcxx_requires_valid_range(__first, __last);
1183 std::__reverse(__first, __last, std::__iterator_category(__first));
1187 * @brief Copy a sequence, reversing its elements.
1188 * @ingroup mutating_algorithms
1189 * @param __first A bidirectional iterator.
1190 * @param __last A bidirectional iterator.
1191 * @param __result An output iterator.
1192 * @return An iterator designating the end of the resulting sequence.
1194 * Copies the elements in the range @p [__first,__last) to the
1195 * range @p [__result,__result+(__last-__first)) such that the
1196 * order of the elements is reversed. For every @c i such that @p
1197 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1198 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1199 * The ranges @p [__first,__last) and @p
1200 * [__result,__result+(__last-__first)) must not overlap.
1202 template<typename _BidirectionalIterator, typename _OutputIterator>
1204 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1205 _OutputIterator __result)
1207 // concept requirements
1208 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1209 _BidirectionalIterator>)
1210 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1211 typename iterator_traits<_BidirectionalIterator>::value_type>)
1212 __glibcxx_requires_valid_range(__first, __last);
1214 while (__first != __last)
1217 *__result = *__last;
1224 * This is a helper function for the rotate algorithm specialized on RAIs.
1225 * It returns the greatest common divisor of two integer values.
1227 template<typename _EuclideanRingElement>
1228 _EuclideanRingElement
1229 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1233 _EuclideanRingElement __t = __m % __n;
1240 /// This is a helper function for the rotate algorithm.
1241 template<typename _ForwardIterator>
1243 __rotate(_ForwardIterator __first,
1244 _ForwardIterator __middle,
1245 _ForwardIterator __last,
1246 forward_iterator_tag)
1248 if (__first == __middle)
1250 else if (__last == __middle)
1253 _ForwardIterator __first2 = __middle;
1256 std::iter_swap(__first, __first2);
1259 if (__first == __middle)
1260 __middle = __first2;
1262 while (__first2 != __last);
1264 _ForwardIterator __ret = __first;
1266 __first2 = __middle;
1268 while (__first2 != __last)
1270 std::iter_swap(__first, __first2);
1273 if (__first == __middle)
1274 __middle = __first2;
1275 else if (__first2 == __last)
1276 __first2 = __middle;
1281 /// This is a helper function for the rotate algorithm.
1282 template<typename _BidirectionalIterator>
1283 _BidirectionalIterator
1284 __rotate(_BidirectionalIterator __first,
1285 _BidirectionalIterator __middle,
1286 _BidirectionalIterator __last,
1287 bidirectional_iterator_tag)
1289 // concept requirements
1290 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1291 _BidirectionalIterator>)
1293 if (__first == __middle)
1295 else if (__last == __middle)
1298 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1299 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1301 while (__first != __middle && __middle != __last)
1303 std::iter_swap(__first, --__last);
1307 if (__first == __middle)
1309 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1314 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1319 /// This is a helper function for the rotate algorithm.
1320 template<typename _RandomAccessIterator>
1321 _RandomAccessIterator
1322 __rotate(_RandomAccessIterator __first,
1323 _RandomAccessIterator __middle,
1324 _RandomAccessIterator __last,
1325 random_access_iterator_tag)
1327 // concept requirements
1328 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1329 _RandomAccessIterator>)
1331 if (__first == __middle)
1333 else if (__last == __middle)
1336 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1338 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1341 _Distance __n = __last - __first;
1342 _Distance __k = __middle - __first;
1344 if (__k == __n - __k)
1346 std::swap_ranges(__first, __middle, __middle);
1350 _RandomAccessIterator __p = __first;
1351 _RandomAccessIterator __ret = __first + (__last - __middle);
1355 if (__k < __n - __k)
1357 if (__is_pod(_ValueType) && __k == 1)
1359 _ValueType __t = _GLIBCXX_MOVE(*__p);
1360 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
1361 *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
1364 _RandomAccessIterator __q = __p + __k;
1365 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1367 std::iter_swap(__p, __q);
1374 std::swap(__n, __k);
1380 if (__is_pod(_ValueType) && __k == 1)
1382 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
1383 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
1384 *__p = _GLIBCXX_MOVE(__t);
1387 _RandomAccessIterator __q = __p + __n;
1389 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1393 std::iter_swap(__p, __q);
1398 std::swap(__n, __k);
1403 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1404 // DR 488. rotate throws away useful information
1406 * @brief Rotate the elements of a sequence.
1407 * @ingroup mutating_algorithms
1408 * @param __first A forward iterator.
1409 * @param __middle A forward iterator.
1410 * @param __last A forward iterator.
1411 * @return first + (last - middle).
1413 * Rotates the elements of the range @p [__first,__last) by
1414 * @p (__middle - __first) positions so that the element at @p __middle
1415 * is moved to @p __first, the element at @p __middle+1 is moved to
1416 * @p __first+1 and so on for each element in the range
1417 * @p [__first,__last).
1419 * This effectively swaps the ranges @p [__first,__middle) and
1420 * @p [__middle,__last).
1423 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1424 * for each @p n in the range @p [0,__last-__first).
1426 template<typename _ForwardIterator>
1427 inline _ForwardIterator
1428 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1429 _ForwardIterator __last)
1431 // concept requirements
1432 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1434 __glibcxx_requires_valid_range(__first, __middle);
1435 __glibcxx_requires_valid_range(__middle, __last);
1437 return std::__rotate(__first, __middle, __last,
1438 std::__iterator_category(__first));
1442 * @brief Copy a sequence, rotating its elements.
1443 * @ingroup mutating_algorithms
1444 * @param __first A forward iterator.
1445 * @param __middle A forward iterator.
1446 * @param __last A forward iterator.
1447 * @param __result An output iterator.
1448 * @return An iterator designating the end of the resulting sequence.
1450 * Copies the elements of the range @p [__first,__last) to the
1451 * range beginning at @result, rotating the copied elements by
1452 * @p (__middle-__first) positions so that the element at @p __middle
1453 * is moved to @p __result, the element at @p __middle+1 is moved
1454 * to @p __result+1 and so on for each element in the range @p
1458 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1459 * for each @p n in the range @p [0,__last-__first).
1461 template<typename _ForwardIterator, typename _OutputIterator>
1462 inline _OutputIterator
1463 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1464 _ForwardIterator __last, _OutputIterator __result)
1466 // concept requirements
1467 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1468 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1469 typename iterator_traits<_ForwardIterator>::value_type>)
1470 __glibcxx_requires_valid_range(__first, __middle);
1471 __glibcxx_requires_valid_range(__middle, __last);
1473 return std::copy(__first, __middle,
1474 std::copy(__middle, __last, __result));
1477 /// This is a helper function...
1478 template<typename _ForwardIterator, typename _Predicate>
1480 __partition(_ForwardIterator __first, _ForwardIterator __last,
1481 _Predicate __pred, forward_iterator_tag)
1483 if (__first == __last)
1486 while (__pred(*__first))
1487 if (++__first == __last)
1490 _ForwardIterator __next = __first;
1492 while (++__next != __last)
1493 if (__pred(*__next))
1495 std::iter_swap(__first, __next);
1502 /// This is a helper function...
1503 template<typename _BidirectionalIterator, typename _Predicate>
1504 _BidirectionalIterator
1505 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1506 _Predicate __pred, bidirectional_iterator_tag)
1511 if (__first == __last)
1513 else if (__pred(*__first))
1519 if (__first == __last)
1521 else if (!bool(__pred(*__last)))
1525 std::iter_swap(__first, __last);
1532 /// This is a helper function...
1533 /// Requires __first != __last and !__pred(__first)
1534 /// and __len == distance(__first, __last).
1536 /// !__pred(__first) allows us to guarantee that we don't
1537 /// move-assign an element onto itself.
1538 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1541 __stable_partition_adaptive(_ForwardIterator __first,
1542 _ForwardIterator __last,
1543 _Predicate __pred, _Distance __len,
1545 _Distance __buffer_size)
1550 if (__len <= __buffer_size)
1552 _ForwardIterator __result1 = __first;
1553 _Pointer __result2 = __buffer;
1555 // The precondition guarantees that !__pred(__first), so
1556 // move that element to the buffer before starting the loop.
1557 // This ensures that we only call __pred once per element.
1558 *__result2 = _GLIBCXX_MOVE(*__first);
1561 for (; __first != __last; ++__first)
1562 if (__pred(__first))
1564 *__result1 = _GLIBCXX_MOVE(*__first);
1569 *__result2 = _GLIBCXX_MOVE(*__first);
1573 _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1577 _ForwardIterator __middle = __first;
1578 std::advance(__middle, __len / 2);
1579 _ForwardIterator __left_split =
1580 std::__stable_partition_adaptive(__first, __middle, __pred,
1581 __len / 2, __buffer,
1584 // Advance past true-predicate values to satisfy this
1585 // function's preconditions.
1586 _Distance __right_len = __len - __len / 2;
1587 _ForwardIterator __right_split =
1588 std::__find_if_not_n(__middle, __right_len, __pred);
1592 std::__stable_partition_adaptive(__right_split, __last, __pred,
1594 __buffer, __buffer_size);
1596 std::rotate(__left_split, __middle, __right_split);
1597 std::advance(__left_split, std::distance(__middle, __right_split));
1598 return __left_split;
1601 template<typename _ForwardIterator, typename _Predicate>
1603 __stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1606 __first = std::__find_if_not(__first, __last, __pred);
1608 if (__first == __last)
1611 typedef typename iterator_traits<_ForwardIterator>::value_type
1613 typedef typename iterator_traits<_ForwardIterator>::difference_type
1616 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last);
1618 std::__stable_partition_adaptive(__first, __last, __pred,
1619 _DistanceType(__buf.requested_size()),
1621 _DistanceType(__buf.size()));
1625 * @brief Move elements for which a predicate is true to the beginning
1626 * of a sequence, preserving relative ordering.
1627 * @ingroup mutating_algorithms
1628 * @param __first A forward iterator.
1629 * @param __last A forward iterator.
1630 * @param __pred A predicate functor.
1631 * @return An iterator @p middle such that @p __pred(i) is true for each
1632 * iterator @p i in the range @p [first,middle) and false for each @p i
1633 * in the range @p [middle,last).
1635 * Performs the same function as @p partition() with the additional
1636 * guarantee that the relative ordering of elements in each group is
1637 * preserved, so any two elements @p x and @p y in the range
1638 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1639 * relative ordering after calling @p stable_partition().
1641 template<typename _ForwardIterator, typename _Predicate>
1642 inline _ForwardIterator
1643 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1646 // concept requirements
1647 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1649 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1650 typename iterator_traits<_ForwardIterator>::value_type>)
1651 __glibcxx_requires_valid_range(__first, __last);
1653 return std::__stable_partition(__first, __last,
1654 __gnu_cxx::__ops::__pred_iter(__pred));
1657 /// This is a helper function for the sort routines.
1658 template<typename _RandomAccessIterator, typename _Compare>
1660 __heap_select(_RandomAccessIterator __first,
1661 _RandomAccessIterator __middle,
1662 _RandomAccessIterator __last, _Compare __comp)
1664 std::__make_heap(__first, __middle, __comp);
1665 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1666 if (__comp(__i, __first))
1667 std::__pop_heap(__first, __middle, __i, __comp);
1672 template<typename _InputIterator, typename _RandomAccessIterator,
1674 _RandomAccessIterator
1675 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
1676 _RandomAccessIterator __result_first,
1677 _RandomAccessIterator __result_last,
1680 typedef typename iterator_traits<_InputIterator>::value_type
1682 typedef iterator_traits<_RandomAccessIterator> _RItTraits;
1683 typedef typename _RItTraits::difference_type _DistanceType;
1685 if (__result_first == __result_last)
1686 return __result_last;
1687 _RandomAccessIterator __result_real_last = __result_first;
1688 while (__first != __last && __result_real_last != __result_last)
1690 *__result_real_last = *__first;
1691 ++__result_real_last;
1695 std::__make_heap(__result_first, __result_real_last, __comp);
1696 while (__first != __last)
1698 if (__comp(__first, __result_first))
1699 std::__adjust_heap(__result_first, _DistanceType(0),
1700 _DistanceType(__result_real_last
1702 _InputValueType(*__first), __comp);
1705 std::__sort_heap(__result_first, __result_real_last, __comp);
1706 return __result_real_last;
1710 * @brief Copy the smallest elements of a sequence.
1711 * @ingroup sorting_algorithms
1712 * @param __first An iterator.
1713 * @param __last Another iterator.
1714 * @param __result_first A random-access iterator.
1715 * @param __result_last Another random-access iterator.
1716 * @return An iterator indicating the end of the resulting sequence.
1718 * Copies and sorts the smallest N values from the range @p [__first,__last)
1719 * to the range beginning at @p __result_first, where the number of
1720 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1721 * @p (__result_last-__result_first).
1722 * After the sort if @e i and @e j are iterators in the range
1723 * @p [__result_first,__result_first+N) such that i precedes j then
1725 * The value returned is @p __result_first+N.
1727 template<typename _InputIterator, typename _RandomAccessIterator>
1728 inline _RandomAccessIterator
1729 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1730 _RandomAccessIterator __result_first,
1731 _RandomAccessIterator __result_last)
1733 typedef typename iterator_traits<_InputIterator>::value_type
1735 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1737 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1740 // concept requirements
1741 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1742 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1744 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1746 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1747 __glibcxx_requires_valid_range(__first, __last);
1748 __glibcxx_requires_valid_range(__result_first, __result_last);
1750 return std::__partial_sort_copy(__first, __last,
1751 __result_first, __result_last,
1752 __gnu_cxx::__ops::__iter_less_iter());
1756 * @brief Copy the smallest elements of a sequence using a predicate for
1758 * @ingroup sorting_algorithms
1759 * @param __first An input iterator.
1760 * @param __last Another input iterator.
1761 * @param __result_first A random-access iterator.
1762 * @param __result_last Another random-access iterator.
1763 * @param __comp A comparison functor.
1764 * @return An iterator indicating the end of the resulting sequence.
1766 * Copies and sorts the smallest N values from the range @p [__first,__last)
1767 * to the range beginning at @p result_first, where the number of
1768 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1769 * @p (__result_last-__result_first).
1770 * After the sort if @e i and @e j are iterators in the range
1771 * @p [__result_first,__result_first+N) such that i precedes j then
1772 * @p __comp(*j,*i) is false.
1773 * The value returned is @p __result_first+N.
1775 template<typename _InputIterator, typename _RandomAccessIterator,
1777 inline _RandomAccessIterator
1778 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1779 _RandomAccessIterator __result_first,
1780 _RandomAccessIterator __result_last,
1783 typedef typename iterator_traits<_InputIterator>::value_type
1785 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1787 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1790 // concept requirements
1791 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1792 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1793 _RandomAccessIterator>)
1794 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1796 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1797 _InputValueType, _OutputValueType>)
1798 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1799 _OutputValueType, _OutputValueType>)
1800 __glibcxx_requires_valid_range(__first, __last);
1801 __glibcxx_requires_valid_range(__result_first, __result_last);
1803 return std::__partial_sort_copy(__first, __last,
1804 __result_first, __result_last,
1805 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1808 /// This is a helper function for the sort routine.
1809 template<typename _RandomAccessIterator, typename _Compare>
1811 __unguarded_linear_insert(_RandomAccessIterator __last,
1814 typename iterator_traits<_RandomAccessIterator>::value_type
1815 __val = _GLIBCXX_MOVE(*__last);
1816 _RandomAccessIterator __next = __last;
1818 while (__comp(__val, __next))
1820 *__last = _GLIBCXX_MOVE(*__next);
1824 *__last = _GLIBCXX_MOVE(__val);
1827 /// This is a helper function for the sort routine.
1828 template<typename _RandomAccessIterator, typename _Compare>
1830 __insertion_sort(_RandomAccessIterator __first,
1831 _RandomAccessIterator __last, _Compare __comp)
1833 if (__first == __last) return;
1835 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1837 if (__comp(__i, __first))
1839 typename iterator_traits<_RandomAccessIterator>::value_type
1840 __val = _GLIBCXX_MOVE(*__i);
1841 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
1842 *__first = _GLIBCXX_MOVE(__val);
1845 std::__unguarded_linear_insert(__i,
1846 __gnu_cxx::__ops::__val_comp_iter(__comp));
1850 /// This is a helper function for the sort routine.
1851 template<typename _RandomAccessIterator, typename _Compare>
1853 __unguarded_insertion_sort(_RandomAccessIterator __first,
1854 _RandomAccessIterator __last, _Compare __comp)
1856 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
1857 std::__unguarded_linear_insert(__i,
1858 __gnu_cxx::__ops::__val_comp_iter(__comp));
1863 * This controls some aspect of the sort routines.
1865 enum { _S_threshold = 16 };
1867 /// This is a helper function for the sort routine.
1868 template<typename _RandomAccessIterator, typename _Compare>
1870 __final_insertion_sort(_RandomAccessIterator __first,
1871 _RandomAccessIterator __last, _Compare __comp)
1873 if (__last - __first > int(_S_threshold))
1875 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
1876 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
1880 std::__insertion_sort(__first, __last, __comp);
1883 /// This is a helper function...
1884 template<typename _RandomAccessIterator, typename _Compare>
1885 _RandomAccessIterator
1886 __unguarded_partition(_RandomAccessIterator __first,
1887 _RandomAccessIterator __last,
1888 _RandomAccessIterator __pivot, _Compare __comp)
1892 while (__comp(__first, __pivot))
1895 while (__comp(__pivot, __last))
1897 if (!(__first < __last))
1899 std::iter_swap(__first, __last);
1904 /// This is a helper function...
1905 template<typename _RandomAccessIterator, typename _Compare>
1906 inline _RandomAccessIterator
1907 __unguarded_partition_pivot(_RandomAccessIterator __first,
1908 _RandomAccessIterator __last, _Compare __comp)
1910 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
1911 std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
1913 return std::__unguarded_partition(__first + 1, __last, __first, __comp);
1916 template<typename _RandomAccessIterator, typename _Compare>
1918 __partial_sort(_RandomAccessIterator __first,
1919 _RandomAccessIterator __middle,
1920 _RandomAccessIterator __last,
1923 std::__heap_select(__first, __middle, __last, __comp);
1924 std::__sort_heap(__first, __middle, __comp);
1927 /// This is a helper function for the sort routine.
1928 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1930 __introsort_loop(_RandomAccessIterator __first,
1931 _RandomAccessIterator __last,
1932 _Size __depth_limit, _Compare __comp)
1934 while (__last - __first > int(_S_threshold))
1936 if (__depth_limit == 0)
1938 std::__partial_sort(__first, __last, __last, __comp);
1942 _RandomAccessIterator __cut =
1943 std::__unguarded_partition_pivot(__first, __last, __comp);
1944 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
1951 template<typename _RandomAccessIterator, typename _Compare>
1953 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
1956 if (__first != __last)
1958 std::__introsort_loop(__first, __last,
1959 std::__lg(__last - __first) * 2,
1961 std::__final_insertion_sort(__first, __last, __comp);
1965 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1967 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
1968 _RandomAccessIterator __last, _Size __depth_limit,
1971 while (__last - __first > 3)
1973 if (__depth_limit == 0)
1975 std::__heap_select(__first, __nth + 1, __last, __comp);
1976 // Place the nth largest element in its final position.
1977 std::iter_swap(__first, __nth);
1981 _RandomAccessIterator __cut =
1982 std::__unguarded_partition_pivot(__first, __last, __comp);
1988 std::__insertion_sort(__first, __last, __comp);
1993 // lower_bound moved to stl_algobase.h
1996 * @brief Finds the first position in which @p __val could be inserted
1997 * without changing the ordering.
1998 * @ingroup binary_search_algorithms
1999 * @param __first An iterator.
2000 * @param __last Another iterator.
2001 * @param __val The search term.
2002 * @param __comp A functor to use for comparisons.
2003 * @return An iterator pointing to the first element <em>not less
2004 * than</em> @p __val, or end() if every element is less
2006 * @ingroup binary_search_algorithms
2008 * The comparison function should have the same effects on ordering as
2009 * the function used for the initial sort.
2011 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2012 inline _ForwardIterator
2013 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2014 const _Tp& __val, _Compare __comp)
2016 typedef typename iterator_traits<_ForwardIterator>::value_type
2019 // concept requirements
2020 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2021 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2023 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2026 return std::__lower_bound(__first, __last, __val,
2027 __gnu_cxx::__ops::__iter_comp_val(__comp));
2030 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2032 __upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2033 const _Tp& __val, _Compare __comp)
2035 typedef typename iterator_traits<_ForwardIterator>::difference_type
2038 _DistanceType __len = std::distance(__first, __last);
2042 _DistanceType __half = __len >> 1;
2043 _ForwardIterator __middle = __first;
2044 std::advance(__middle, __half);
2045 if (__comp(__val, __middle))
2051 __len = __len - __half - 1;
2058 * @brief Finds the last position in which @p __val could be inserted
2059 * without changing the ordering.
2060 * @ingroup binary_search_algorithms
2061 * @param __first An iterator.
2062 * @param __last Another iterator.
2063 * @param __val The search term.
2064 * @return An iterator pointing to the first element greater than @p __val,
2065 * or end() if no elements are greater than @p __val.
2066 * @ingroup binary_search_algorithms
2068 template<typename _ForwardIterator, typename _Tp>
2069 inline _ForwardIterator
2070 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2073 typedef typename iterator_traits<_ForwardIterator>::value_type
2076 // concept requirements
2077 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2078 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2079 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2081 return std::__upper_bound(__first, __last, __val,
2082 __gnu_cxx::__ops::__val_less_iter());
2086 * @brief Finds the last position in which @p __val could be inserted
2087 * without changing the ordering.
2088 * @ingroup binary_search_algorithms
2089 * @param __first An iterator.
2090 * @param __last Another iterator.
2091 * @param __val The search term.
2092 * @param __comp A functor to use for comparisons.
2093 * @return An iterator pointing to the first element greater than @p __val,
2094 * or end() if no elements are greater than @p __val.
2095 * @ingroup binary_search_algorithms
2097 * The comparison function should have the same effects on ordering as
2098 * the function used for the initial sort.
2100 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2101 inline _ForwardIterator
2102 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2103 const _Tp& __val, _Compare __comp)
2105 typedef typename iterator_traits<_ForwardIterator>::value_type
2108 // concept requirements
2109 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2110 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2112 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2115 return std::__upper_bound(__first, __last, __val,
2116 __gnu_cxx::__ops::__val_comp_iter(__comp));
2119 template<typename _ForwardIterator, typename _Tp,
2120 typename _CompareItTp, typename _CompareTpIt>
2121 pair<_ForwardIterator, _ForwardIterator>
2122 __equal_range(_ForwardIterator __first, _ForwardIterator __last,
2124 _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it)
2126 typedef typename iterator_traits<_ForwardIterator>::difference_type
2129 _DistanceType __len = std::distance(__first, __last);
2133 _DistanceType __half = __len >> 1;
2134 _ForwardIterator __middle = __first;
2135 std::advance(__middle, __half);
2136 if (__comp_it_val(__middle, __val))
2140 __len = __len - __half - 1;
2142 else if (__comp_val_it(__val, __middle))
2146 _ForwardIterator __left
2147 = std::__lower_bound(__first, __middle, __val, __comp_it_val);
2148 std::advance(__first, __len);
2149 _ForwardIterator __right
2150 = std::__upper_bound(++__middle, __first, __val, __comp_val_it);
2151 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2154 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2158 * @brief Finds the largest subrange in which @p __val could be inserted
2159 * at any place in it without changing the ordering.
2160 * @ingroup binary_search_algorithms
2161 * @param __first An iterator.
2162 * @param __last Another iterator.
2163 * @param __val The search term.
2164 * @return An pair of iterators defining the subrange.
2165 * @ingroup binary_search_algorithms
2167 * This is equivalent to
2169 * std::make_pair(lower_bound(__first, __last, __val),
2170 * upper_bound(__first, __last, __val))
2172 * but does not actually call those functions.
2174 template<typename _ForwardIterator, typename _Tp>
2175 inline pair<_ForwardIterator, _ForwardIterator>
2176 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2179 typedef typename iterator_traits<_ForwardIterator>::value_type
2182 // concept requirements
2183 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2184 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2185 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2186 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2187 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2189 return std::__equal_range(__first, __last, __val,
2190 __gnu_cxx::__ops::__iter_less_val(),
2191 __gnu_cxx::__ops::__val_less_iter());
2195 * @brief Finds the largest subrange in which @p __val could be inserted
2196 * at any place in it without changing the ordering.
2197 * @param __first An iterator.
2198 * @param __last Another iterator.
2199 * @param __val The search term.
2200 * @param __comp A functor to use for comparisons.
2201 * @return An pair of iterators defining the subrange.
2202 * @ingroup binary_search_algorithms
2204 * This is equivalent to
2206 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2207 * upper_bound(__first, __last, __val, __comp))
2209 * but does not actually call those functions.
2211 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2212 inline pair<_ForwardIterator, _ForwardIterator>
2213 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2214 const _Tp& __val, _Compare __comp)
2216 typedef typename iterator_traits<_ForwardIterator>::value_type
2219 // concept requirements
2220 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2221 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2223 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2225 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2227 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2230 return std::__equal_range(__first, __last, __val,
2231 __gnu_cxx::__ops::__iter_comp_val(__comp),
2232 __gnu_cxx::__ops::__val_comp_iter(__comp));
2236 * @brief Determines whether an element exists in a range.
2237 * @ingroup binary_search_algorithms
2238 * @param __first An iterator.
2239 * @param __last Another iterator.
2240 * @param __val The search term.
2241 * @return True if @p __val (or its equivalent) is in [@p
2242 * __first,@p __last ].
2244 * Note that this does not actually return an iterator to @p __val. For
2245 * that, use std::find or a container's specialized find member functions.
2247 template<typename _ForwardIterator, typename _Tp>
2249 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2252 typedef typename iterator_traits<_ForwardIterator>::value_type
2255 // concept requirements
2256 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2257 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2258 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2259 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2261 _ForwardIterator __i
2262 = std::__lower_bound(__first, __last, __val,
2263 __gnu_cxx::__ops::__iter_less_val());
2264 return __i != __last && !(__val < *__i);
2268 * @brief Determines whether an element exists in a range.
2269 * @ingroup binary_search_algorithms
2270 * @param __first An iterator.
2271 * @param __last Another iterator.
2272 * @param __val The search term.
2273 * @param __comp A functor to use for comparisons.
2274 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2276 * Note that this does not actually return an iterator to @p __val. For
2277 * that, use std::find or a container's specialized find member functions.
2279 * The comparison function should have the same effects on ordering as
2280 * the function used for the initial sort.
2282 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2284 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2285 const _Tp& __val, _Compare __comp)
2287 typedef typename iterator_traits<_ForwardIterator>::value_type
2290 // concept requirements
2291 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2292 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2294 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2296 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2299 _ForwardIterator __i
2300 = std::__lower_bound(__first, __last, __val,
2301 __gnu_cxx::__ops::__iter_comp_val(__comp));
2302 return __i != __last && !bool(__comp(__val, *__i));
2307 /// This is a helper function for the __merge_adaptive routines.
2308 template<typename _InputIterator1, typename _InputIterator2,
2309 typename _OutputIterator, typename _Compare>
2311 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2312 _InputIterator2 __first2, _InputIterator2 __last2,
2313 _OutputIterator __result, _Compare __comp)
2315 while (__first1 != __last1 && __first2 != __last2)
2317 if (__comp(__first2, __first1))
2319 *__result = _GLIBCXX_MOVE(*__first2);
2324 *__result = _GLIBCXX_MOVE(*__first1);
2329 if (__first1 != __last1)
2330 _GLIBCXX_MOVE3(__first1, __last1, __result);
2333 /// This is a helper function for the __merge_adaptive routines.
2334 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2335 typename _BidirectionalIterator3, typename _Compare>
2337 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2338 _BidirectionalIterator1 __last1,
2339 _BidirectionalIterator2 __first2,
2340 _BidirectionalIterator2 __last2,
2341 _BidirectionalIterator3 __result,
2344 if (__first1 == __last1)
2346 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
2349 else if (__first2 == __last2)
2356 if (__comp(__last2, __last1))
2358 *--__result = _GLIBCXX_MOVE(*__last1);
2359 if (__first1 == __last1)
2361 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
2368 *--__result = _GLIBCXX_MOVE(*__last2);
2369 if (__first2 == __last2)
2376 /// This is a helper function for the merge routines.
2377 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2379 _BidirectionalIterator1
2380 __rotate_adaptive(_BidirectionalIterator1 __first,
2381 _BidirectionalIterator1 __middle,
2382 _BidirectionalIterator1 __last,
2383 _Distance __len1, _Distance __len2,
2384 _BidirectionalIterator2 __buffer,
2385 _Distance __buffer_size)
2387 _BidirectionalIterator2 __buffer_end;
2388 if (__len1 > __len2 && __len2 <= __buffer_size)
2392 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2393 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2394 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2399 else if (__len1 <= __buffer_size)
2403 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2404 _GLIBCXX_MOVE3(__middle, __last, __first);
2405 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2412 std::rotate(__first, __middle, __last);
2413 std::advance(__first, std::distance(__middle, __last));
2418 /// This is a helper function for the merge routines.
2419 template<typename _BidirectionalIterator, typename _Distance,
2420 typename _Pointer, typename _Compare>
2422 __merge_adaptive(_BidirectionalIterator __first,
2423 _BidirectionalIterator __middle,
2424 _BidirectionalIterator __last,
2425 _Distance __len1, _Distance __len2,
2426 _Pointer __buffer, _Distance __buffer_size,
2429 if (__len1 <= __len2 && __len1 <= __buffer_size)
2431 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2432 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2435 else if (__len2 <= __buffer_size)
2437 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2438 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2439 __buffer_end, __last, __comp);
2443 _BidirectionalIterator __first_cut = __first;
2444 _BidirectionalIterator __second_cut = __middle;
2445 _Distance __len11 = 0;
2446 _Distance __len22 = 0;
2447 if (__len1 > __len2)
2449 __len11 = __len1 / 2;
2450 std::advance(__first_cut, __len11);
2452 = std::__lower_bound(__middle, __last, *__first_cut,
2453 __gnu_cxx::__ops::__iter_comp_val(__comp));
2454 __len22 = std::distance(__middle, __second_cut);
2458 __len22 = __len2 / 2;
2459 std::advance(__second_cut, __len22);
2461 = std::__upper_bound(__first, __middle, *__second_cut,
2462 __gnu_cxx::__ops::__val_comp_iter(__comp));
2463 __len11 = std::distance(__first, __first_cut);
2466 _BidirectionalIterator __new_middle
2467 = std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2468 __len1 - __len11, __len22, __buffer,
2470 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2471 __len22, __buffer, __buffer_size, __comp);
2472 std::__merge_adaptive(__new_middle, __second_cut, __last,
2474 __len2 - __len22, __buffer,
2475 __buffer_size, __comp);
2479 /// This is a helper function for the merge routines.
2480 template<typename _BidirectionalIterator, typename _Distance,
2483 __merge_without_buffer(_BidirectionalIterator __first,
2484 _BidirectionalIterator __middle,
2485 _BidirectionalIterator __last,
2486 _Distance __len1, _Distance __len2,
2489 if (__len1 == 0 || __len2 == 0)
2492 if (__len1 + __len2 == 2)
2494 if (__comp(__middle, __first))
2495 std::iter_swap(__first, __middle);
2499 _BidirectionalIterator __first_cut = __first;
2500 _BidirectionalIterator __second_cut = __middle;
2501 _Distance __len11 = 0;
2502 _Distance __len22 = 0;
2503 if (__len1 > __len2)
2505 __len11 = __len1 / 2;
2506 std::advance(__first_cut, __len11);
2508 = std::__lower_bound(__middle, __last, *__first_cut,
2509 __gnu_cxx::__ops::__iter_comp_val(__comp));
2510 __len22 = std::distance(__middle, __second_cut);
2514 __len22 = __len2 / 2;
2515 std::advance(__second_cut, __len22);
2517 = std::__upper_bound(__first, __middle, *__second_cut,
2518 __gnu_cxx::__ops::__val_comp_iter(__comp));
2519 __len11 = std::distance(__first, __first_cut);
2522 std::rotate(__first_cut, __middle, __second_cut);
2523 _BidirectionalIterator __new_middle = __first_cut;
2524 std::advance(__new_middle, std::distance(__middle, __second_cut));
2525 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2526 __len11, __len22, __comp);
2527 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2528 __len1 - __len11, __len2 - __len22, __comp);
2531 template<typename _BidirectionalIterator, typename _Compare>
2533 __inplace_merge(_BidirectionalIterator __first,
2534 _BidirectionalIterator __middle,
2535 _BidirectionalIterator __last,
2538 typedef typename iterator_traits<_BidirectionalIterator>::value_type
2540 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
2543 if (__first == __middle || __middle == __last)
2546 const _DistanceType __len1 = std::distance(__first, __middle);
2547 const _DistanceType __len2 = std::distance(__middle, __last);
2549 typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
2550 _TmpBuf __buf(__first, __last);
2552 if (__buf.begin() == 0)
2553 std::__merge_without_buffer
2554 (__first, __middle, __last, __len1, __len2, __comp);
2556 std::__merge_adaptive
2557 (__first, __middle, __last, __len1, __len2, __buf.begin(),
2558 _DistanceType(__buf.size()), __comp);
2562 * @brief Merges two sorted ranges in place.
2563 * @ingroup sorting_algorithms
2564 * @param __first An iterator.
2565 * @param __middle Another iterator.
2566 * @param __last Another iterator.
2569 * Merges two sorted and consecutive ranges, [__first,__middle) and
2570 * [__middle,__last), and puts the result in [__first,__last). The
2571 * output will be sorted. The sort is @e stable, that is, for
2572 * equivalent elements in the two ranges, elements from the first
2573 * range will always come before elements from the second.
2575 * If enough additional memory is available, this takes (__last-__first)-1
2576 * comparisons. Otherwise an NlogN algorithm is used, where N is
2577 * distance(__first,__last).
2579 template<typename _BidirectionalIterator>
2581 inplace_merge(_BidirectionalIterator __first,
2582 _BidirectionalIterator __middle,
2583 _BidirectionalIterator __last)
2585 // concept requirements
2586 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2587 _BidirectionalIterator>)
2588 __glibcxx_function_requires(_LessThanComparableConcept<
2589 typename iterator_traits<_BidirectionalIterator>::value_type>)
2590 __glibcxx_requires_sorted(__first, __middle);
2591 __glibcxx_requires_sorted(__middle, __last);
2593 std::__inplace_merge(__first, __middle, __last,
2594 __gnu_cxx::__ops::__iter_less_iter());
2598 * @brief Merges two sorted ranges in place.
2599 * @ingroup sorting_algorithms
2600 * @param __first An iterator.
2601 * @param __middle Another iterator.
2602 * @param __last Another iterator.
2603 * @param __comp A functor to use for comparisons.
2606 * Merges two sorted and consecutive ranges, [__first,__middle) and
2607 * [middle,last), and puts the result in [__first,__last). The output will
2608 * be sorted. The sort is @e stable, that is, for equivalent
2609 * elements in the two ranges, elements from the first range will always
2610 * come before elements from the second.
2612 * If enough additional memory is available, this takes (__last-__first)-1
2613 * comparisons. Otherwise an NlogN algorithm is used, where N is
2614 * distance(__first,__last).
2616 * The comparison function should have the same effects on ordering as
2617 * the function used for the initial sort.
2619 template<typename _BidirectionalIterator, typename _Compare>
2621 inplace_merge(_BidirectionalIterator __first,
2622 _BidirectionalIterator __middle,
2623 _BidirectionalIterator __last,
2626 // concept requirements
2627 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2628 _BidirectionalIterator>)
2629 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2630 typename iterator_traits<_BidirectionalIterator>::value_type,
2631 typename iterator_traits<_BidirectionalIterator>::value_type>)
2632 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
2633 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
2635 std::__inplace_merge(__first, __middle, __last,
2636 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2640 /// This is a helper function for the __merge_sort_loop routines.
2641 template<typename _InputIterator, typename _OutputIterator,
2644 __move_merge(_InputIterator __first1, _InputIterator __last1,
2645 _InputIterator __first2, _InputIterator __last2,
2646 _OutputIterator __result, _Compare __comp)
2648 while (__first1 != __last1 && __first2 != __last2)
2650 if (__comp(__first2, __first1))
2652 *__result = _GLIBCXX_MOVE(*__first2);
2657 *__result = _GLIBCXX_MOVE(*__first1);
2662 return _GLIBCXX_MOVE3(__first2, __last2,
2663 _GLIBCXX_MOVE3(__first1, __last1,
2667 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2668 typename _Distance, typename _Compare>
2670 __merge_sort_loop(_RandomAccessIterator1 __first,
2671 _RandomAccessIterator1 __last,
2672 _RandomAccessIterator2 __result, _Distance __step_size,
2675 const _Distance __two_step = 2 * __step_size;
2677 while (__last - __first >= __two_step)
2679 __result = std::__move_merge(__first, __first + __step_size,
2680 __first + __step_size,
2681 __first + __two_step,
2683 __first += __two_step;
2685 __step_size = std::min(_Distance(__last - __first), __step_size);
2687 std::__move_merge(__first, __first + __step_size,
2688 __first + __step_size, __last, __result, __comp);
2691 template<typename _RandomAccessIterator, typename _Distance,
2694 __chunk_insertion_sort(_RandomAccessIterator __first,
2695 _RandomAccessIterator __last,
2696 _Distance __chunk_size, _Compare __comp)
2698 while (__last - __first >= __chunk_size)
2700 std::__insertion_sort(__first, __first + __chunk_size, __comp);
2701 __first += __chunk_size;
2703 std::__insertion_sort(__first, __last, __comp);
2706 enum { _S_chunk_size = 7 };
2708 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
2710 __merge_sort_with_buffer(_RandomAccessIterator __first,
2711 _RandomAccessIterator __last,
2712 _Pointer __buffer, _Compare __comp)
2714 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2717 const _Distance __len = __last - __first;
2718 const _Pointer __buffer_last = __buffer + __len;
2720 _Distance __step_size = _S_chunk_size;
2721 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
2723 while (__step_size < __len)
2725 std::__merge_sort_loop(__first, __last, __buffer,
2726 __step_size, __comp);
2728 std::__merge_sort_loop(__buffer, __buffer_last, __first,
2729 __step_size, __comp);
2734 template<typename _RandomAccessIterator, typename _Pointer,
2735 typename _Distance, typename _Compare>
2737 __stable_sort_adaptive(_RandomAccessIterator __first,
2738 _RandomAccessIterator __last,
2739 _Pointer __buffer, _Distance __buffer_size,
2742 const _Distance __len = (__last - __first + 1) / 2;
2743 const _RandomAccessIterator __middle = __first + __len;
2744 if (__len > __buffer_size)
2746 std::__stable_sort_adaptive(__first, __middle, __buffer,
2747 __buffer_size, __comp);
2748 std::__stable_sort_adaptive(__middle, __last, __buffer,
2749 __buffer_size, __comp);
2753 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2754 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2756 std::__merge_adaptive(__first, __middle, __last,
2757 _Distance(__middle - __first),
2758 _Distance(__last - __middle),
2759 __buffer, __buffer_size,
2763 /// This is a helper function for the stable sorting routines.
2764 template<typename _RandomAccessIterator, typename _Compare>
2766 __inplace_stable_sort(_RandomAccessIterator __first,
2767 _RandomAccessIterator __last, _Compare __comp)
2769 if (__last - __first < 15)
2771 std::__insertion_sort(__first, __last, __comp);
2774 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2775 std::__inplace_stable_sort(__first, __middle, __comp);
2776 std::__inplace_stable_sort(__middle, __last, __comp);
2777 std::__merge_without_buffer(__first, __middle, __last,
2785 // Set algorithms: includes, set_union, set_intersection, set_difference,
2786 // set_symmetric_difference. All of these algorithms have the precondition
2787 // that their input ranges are sorted and the postcondition that their output
2788 // ranges are sorted.
2790 template<typename _InputIterator1, typename _InputIterator2,
2793 __includes(_InputIterator1 __first1, _InputIterator1 __last1,
2794 _InputIterator2 __first2, _InputIterator2 __last2,
2797 while (__first1 != __last1 && __first2 != __last2)
2798 if (__comp(__first2, __first1))
2800 else if (__comp(__first1, __first2))
2803 ++__first1, ++__first2;
2805 return __first2 == __last2;
2809 * @brief Determines whether all elements of a sequence exists in a range.
2810 * @param __first1 Start of search range.
2811 * @param __last1 End of search range.
2812 * @param __first2 Start of sequence
2813 * @param __last2 End of sequence.
2814 * @return True if each element in [__first2,__last2) is contained in order
2815 * within [__first1,__last1). False otherwise.
2816 * @ingroup set_algorithms
2818 * This operation expects both [__first1,__last1) and
2819 * [__first2,__last2) to be sorted. Searches for the presence of
2820 * each element in [__first2,__last2) within [__first1,__last1).
2821 * The iterators over each range only move forward, so this is a
2822 * linear algorithm. If an element in [__first2,__last2) is not
2823 * found before the search iterator reaches @p __last2, false is
2826 template<typename _InputIterator1, typename _InputIterator2>
2828 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2829 _InputIterator2 __first2, _InputIterator2 __last2)
2831 // concept requirements
2832 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2833 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2834 __glibcxx_function_requires(_LessThanOpConcept<
2835 typename iterator_traits<_InputIterator1>::value_type,
2836 typename iterator_traits<_InputIterator2>::value_type>)
2837 __glibcxx_function_requires(_LessThanOpConcept<
2838 typename iterator_traits<_InputIterator2>::value_type,
2839 typename iterator_traits<_InputIterator1>::value_type>)
2840 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
2841 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
2843 return std::__includes(__first1, __last1, __first2, __last2,
2844 __gnu_cxx::__ops::__iter_less_iter());
2848 * @brief Determines whether all elements of a sequence exists in a range
2850 * @ingroup set_algorithms
2851 * @param __first1 Start of search range.
2852 * @param __last1 End of search range.
2853 * @param __first2 Start of sequence
2854 * @param __last2 End of sequence.
2855 * @param __comp Comparison function to use.
2856 * @return True if each element in [__first2,__last2) is contained
2857 * in order within [__first1,__last1) according to comp. False
2858 * otherwise. @ingroup set_algorithms
2860 * This operation expects both [__first1,__last1) and
2861 * [__first2,__last2) to be sorted. Searches for the presence of
2862 * each element in [__first2,__last2) within [__first1,__last1),
2863 * using comp to decide. The iterators over each range only move
2864 * forward, so this is a linear algorithm. If an element in
2865 * [__first2,__last2) is not found before the search iterator
2866 * reaches @p __last2, false is returned.
2868 template<typename _InputIterator1, typename _InputIterator2,
2871 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2872 _InputIterator2 __first2, _InputIterator2 __last2,
2875 // concept requirements
2876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2877 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2878 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2879 typename iterator_traits<_InputIterator1>::value_type,
2880 typename iterator_traits<_InputIterator2>::value_type>)
2881 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2882 typename iterator_traits<_InputIterator2>::value_type,
2883 typename iterator_traits<_InputIterator1>::value_type>)
2884 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
2885 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
2887 return std::__includes(__first1, __last1, __first2, __last2,
2888 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2897 // set_symmetric_difference
2901 template<typename _BidirectionalIterator, typename _Compare>
2903 __next_permutation(_BidirectionalIterator __first,
2904 _BidirectionalIterator __last, _Compare __comp)
2906 if (__first == __last)
2908 _BidirectionalIterator __i = __first;
2917 _BidirectionalIterator __ii = __i;
2919 if (__comp(__i, __ii))
2921 _BidirectionalIterator __j = __last;
2922 while (!__comp(__i, --__j))
2924 std::iter_swap(__i, __j);
2925 std::__reverse(__ii, __last,
2926 std::__iterator_category(__first));
2931 std::__reverse(__first, __last,
2932 std::__iterator_category(__first));
2939 * @brief Permute range into the next @e dictionary ordering.
2940 * @ingroup sorting_algorithms
2941 * @param __first Start of range.
2942 * @param __last End of range.
2943 * @return False if wrapped to first permutation, true otherwise.
2945 * Treats all permutations of the range as a set of @e dictionary sorted
2946 * sequences. Permutes the current sequence into the next one of this set.
2947 * Returns true if there are more sequences to generate. If the sequence
2948 * is the largest of the set, the smallest is generated and false returned.
2950 template<typename _BidirectionalIterator>
2952 next_permutation(_BidirectionalIterator __first,
2953 _BidirectionalIterator __last)
2955 // concept requirements
2956 __glibcxx_function_requires(_BidirectionalIteratorConcept<
2957 _BidirectionalIterator>)
2958 __glibcxx_function_requires(_LessThanComparableConcept<
2959 typename iterator_traits<_BidirectionalIterator>::value_type>)
2960 __glibcxx_requires_valid_range(__first, __last);
2962 return std::__next_permutation
2963 (__first, __last, __gnu_cxx::__ops::__iter_less_iter());
2967 * @brief Permute range into the next @e dictionary ordering using
2968 * comparison functor.
2969 * @ingroup sorting_algorithms
2970 * @param __first Start of range.
2971 * @param __last End of range.
2972 * @param __comp A comparison functor.
2973 * @return False if wrapped to first permutation, true otherwise.
2975 * Treats all permutations of the range [__first,__last) as a set of
2976 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2977 * sequence into the next one of this set. Returns true if there are more
2978 * sequences to generate. If the sequence is the largest of the set, the
2979 * smallest is generated and false returned.
2981 template<typename _BidirectionalIterator, typename _Compare>
2983 next_permutation(_BidirectionalIterator __first,
2984 _BidirectionalIterator __last, _Compare __comp)
2986 // concept requirements
2987 __glibcxx_function_requires(_BidirectionalIteratorConcept<
2988 _BidirectionalIterator>)
2989 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2990 typename iterator_traits<_BidirectionalIterator>::value_type,
2991 typename iterator_traits<_BidirectionalIterator>::value_type>)
2992 __glibcxx_requires_valid_range(__first, __last);
2994 return std::__next_permutation
2995 (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
2998 template<typename _BidirectionalIterator, typename _Compare>
3000 __prev_permutation(_BidirectionalIterator __first,
3001 _BidirectionalIterator __last, _Compare __comp)
3003 if (__first == __last)
3005 _BidirectionalIterator __i = __first;
3014 _BidirectionalIterator __ii = __i;
3016 if (__comp(__ii, __i))
3018 _BidirectionalIterator __j = __last;
3019 while (!__comp(--__j, __i))
3021 std::iter_swap(__i, __j);
3022 std::__reverse(__ii, __last,
3023 std::__iterator_category(__first));
3028 std::__reverse(__first, __last,
3029 std::__iterator_category(__first));
3036 * @brief Permute range into the previous @e dictionary ordering.
3037 * @ingroup sorting_algorithms
3038 * @param __first Start of range.
3039 * @param __last End of range.
3040 * @return False if wrapped to last permutation, true otherwise.
3042 * Treats all permutations of the range as a set of @e dictionary sorted
3043 * sequences. Permutes the current sequence into the previous one of this
3044 * set. Returns true if there are more sequences to generate. If the
3045 * sequence is the smallest of the set, the largest is generated and false
3048 template<typename _BidirectionalIterator>
3050 prev_permutation(_BidirectionalIterator __first,
3051 _BidirectionalIterator __last)
3053 // concept requirements
3054 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3055 _BidirectionalIterator>)
3056 __glibcxx_function_requires(_LessThanComparableConcept<
3057 typename iterator_traits<_BidirectionalIterator>::value_type>)
3058 __glibcxx_requires_valid_range(__first, __last);
3060 return std::__prev_permutation(__first, __last,
3061 __gnu_cxx::__ops::__iter_less_iter());
3065 * @brief Permute range into the previous @e dictionary ordering using
3066 * comparison functor.
3067 * @ingroup sorting_algorithms
3068 * @param __first Start of range.
3069 * @param __last End of range.
3070 * @param __comp A comparison functor.
3071 * @return False if wrapped to last permutation, true otherwise.
3073 * Treats all permutations of the range [__first,__last) as a set of
3074 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3075 * sequence into the previous one of this set. Returns true if there are
3076 * more sequences to generate. If the sequence is the smallest of the set,
3077 * the largest is generated and false returned.
3079 template<typename _BidirectionalIterator, typename _Compare>
3081 prev_permutation(_BidirectionalIterator __first,
3082 _BidirectionalIterator __last, _Compare __comp)
3084 // concept requirements
3085 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3086 _BidirectionalIterator>)
3087 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3088 typename iterator_traits<_BidirectionalIterator>::value_type,
3089 typename iterator_traits<_BidirectionalIterator>::value_type>)
3090 __glibcxx_requires_valid_range(__first, __last);
3092 return std::__prev_permutation(__first, __last,
3093 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3099 template<typename _InputIterator, typename _OutputIterator,
3100 typename _Predicate, typename _Tp>
3102 __replace_copy_if(_InputIterator __first, _InputIterator __last,
3103 _OutputIterator __result,
3104 _Predicate __pred, const _Tp& __new_value)
3106 for (; __first != __last; ++__first, ++__result)
3107 if (__pred(__first))
3108 *__result = __new_value;
3110 *__result = *__first;
3115 * @brief Copy a sequence, replacing each element of one value with another
3117 * @param __first An input iterator.
3118 * @param __last An input iterator.
3119 * @param __result An output iterator.
3120 * @param __old_value The value to be replaced.
3121 * @param __new_value The replacement value.
3122 * @return The end of the output sequence, @p result+(last-first).
3124 * Copies each element in the input range @p [__first,__last) to the
3125 * output range @p [__result,__result+(__last-__first)) replacing elements
3126 * equal to @p __old_value with @p __new_value.
3128 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3129 inline _OutputIterator
3130 replace_copy(_InputIterator __first, _InputIterator __last,
3131 _OutputIterator __result,
3132 const _Tp& __old_value, const _Tp& __new_value)
3134 // concept requirements
3135 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3136 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3137 typename iterator_traits<_InputIterator>::value_type>)
3138 __glibcxx_function_requires(_EqualOpConcept<
3139 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3140 __glibcxx_requires_valid_range(__first, __last);
3142 return std::__replace_copy_if(__first, __last, __result,
3143 __gnu_cxx::__ops::__iter_equals_val(__old_value),
3148 * @brief Copy a sequence, replacing each value for which a predicate
3149 * returns true with another value.
3150 * @ingroup mutating_algorithms
3151 * @param __first An input iterator.
3152 * @param __last An input iterator.
3153 * @param __result An output iterator.
3154 * @param __pred A predicate.
3155 * @param __new_value The replacement value.
3156 * @return The end of the output sequence, @p __result+(__last-__first).
3158 * Copies each element in the range @p [__first,__last) to the range
3159 * @p [__result,__result+(__last-__first)) replacing elements for which
3160 * @p __pred returns true with @p __new_value.
3162 template<typename _InputIterator, typename _OutputIterator,
3163 typename _Predicate, typename _Tp>
3164 inline _OutputIterator
3165 replace_copy_if(_InputIterator __first, _InputIterator __last,
3166 _OutputIterator __result,
3167 _Predicate __pred, const _Tp& __new_value)
3169 // concept requirements
3170 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3171 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3172 typename iterator_traits<_InputIterator>::value_type>)
3173 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3174 typename iterator_traits<_InputIterator>::value_type>)
3175 __glibcxx_requires_valid_range(__first, __last);
3177 return std::__replace_copy_if(__first, __last, __result,
3178 __gnu_cxx::__ops::__pred_iter(__pred),
3182 template<typename _InputIterator, typename _Predicate>
3183 typename iterator_traits<_InputIterator>::difference_type
3184 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3186 typename iterator_traits<_InputIterator>::difference_type __n = 0;
3187 for (; __first != __last; ++__first)
3188 if (__pred(__first))
3193 #if __cplusplus >= 201103L
3195 * @brief Determines whether the elements of a sequence are sorted.
3196 * @ingroup sorting_algorithms
3197 * @param __first An iterator.
3198 * @param __last Another iterator.
3199 * @return True if the elements are sorted, false otherwise.
3201 template<typename _ForwardIterator>
3203 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3204 { return std::is_sorted_until(__first, __last) == __last; }
3207 * @brief Determines whether the elements of a sequence are sorted
3208 * according to a comparison functor.
3209 * @ingroup sorting_algorithms
3210 * @param __first An iterator.
3211 * @param __last Another iterator.
3212 * @param __comp A comparison functor.
3213 * @return True if the elements are sorted, false otherwise.
3215 template<typename _ForwardIterator, typename _Compare>
3217 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3219 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3221 template<typename _ForwardIterator, typename _Compare>
3223 __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3226 if (__first == __last)
3229 _ForwardIterator __next = __first;
3230 for (++__next; __next != __last; __first = __next, ++__next)
3231 if (__comp(__next, __first))
3237 * @brief Determines the end of a sorted sequence.
3238 * @ingroup sorting_algorithms
3239 * @param __first An iterator.
3240 * @param __last Another iterator.
3241 * @return An iterator pointing to the last iterator i in [__first, __last)
3242 * for which the range [__first, i) is sorted.
3244 template<typename _ForwardIterator>
3245 inline _ForwardIterator
3246 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3248 // concept requirements
3249 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3250 __glibcxx_function_requires(_LessThanComparableConcept<
3251 typename iterator_traits<_ForwardIterator>::value_type>)
3252 __glibcxx_requires_valid_range(__first, __last);
3254 return std::__is_sorted_until(__first, __last,
3255 __gnu_cxx::__ops::__iter_less_iter());
3259 * @brief Determines the end of a sorted sequence using comparison functor.
3260 * @ingroup sorting_algorithms
3261 * @param __first An iterator.
3262 * @param __last Another iterator.
3263 * @param __comp A comparison functor.
3264 * @return An iterator pointing to the last iterator i in [__first, __last)
3265 * for which the range [__first, i) is sorted.
3267 template<typename _ForwardIterator, typename _Compare>
3268 inline _ForwardIterator
3269 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3272 // concept requirements
3273 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3274 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3275 typename iterator_traits<_ForwardIterator>::value_type,
3276 typename iterator_traits<_ForwardIterator>::value_type>)
3277 __glibcxx_requires_valid_range(__first, __last);
3279 return std::__is_sorted_until(__first, __last,
3280 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3284 * @brief Determines min and max at once as an ordered pair.
3285 * @ingroup sorting_algorithms
3286 * @param __a A thing of arbitrary type.
3287 * @param __b Another thing of arbitrary type.
3288 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3291 template<typename _Tp>
3292 _GLIBCXX14_CONSTEXPR
3293 inline pair<const _Tp&, const _Tp&>
3294 minmax(const _Tp& __a, const _Tp& __b)
3296 // concept requirements
3297 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3299 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3300 : pair<const _Tp&, const _Tp&>(__a, __b);
3304 * @brief Determines min and max at once as an ordered pair.
3305 * @ingroup sorting_algorithms
3306 * @param __a A thing of arbitrary type.
3307 * @param __b Another thing of arbitrary type.
3308 * @param __comp A @link comparison_functors comparison functor @endlink.
3309 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3312 template<typename _Tp, typename _Compare>
3313 _GLIBCXX14_CONSTEXPR
3314 inline pair<const _Tp&, const _Tp&>
3315 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3317 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3318 : pair<const _Tp&, const _Tp&>(__a, __b);
3321 template<typename _ForwardIterator, typename _Compare>
3322 _GLIBCXX14_CONSTEXPR
3323 pair<_ForwardIterator, _ForwardIterator>
3324 __minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3327 _ForwardIterator __next = __first;
3328 if (__first == __last
3329 || ++__next == __last)
3330 return std::make_pair(__first, __first);
3332 _ForwardIterator __min{}, __max{};
3333 if (__comp(__next, __first))
3347 while (__first != __last)
3350 if (++__next == __last)
3352 if (__comp(__first, __min))
3354 else if (!__comp(__first, __max))
3359 if (__comp(__next, __first))
3361 if (__comp(__next, __min))
3363 if (!__comp(__first, __max))
3368 if (__comp(__first, __min))
3370 if (!__comp(__next, __max))
3378 return std::make_pair(__min, __max);
3382 * @brief Return a pair of iterators pointing to the minimum and maximum
3383 * elements in a range.
3384 * @ingroup sorting_algorithms
3385 * @param __first Start of range.
3386 * @param __last End of range.
3387 * @return make_pair(m, M), where m is the first iterator i in
3388 * [__first, __last) such that no other element in the range is
3389 * smaller, and where M is the last iterator i in [__first, __last)
3390 * such that no other element in the range is larger.
3392 template<typename _ForwardIterator>
3393 _GLIBCXX14_CONSTEXPR
3394 inline pair<_ForwardIterator, _ForwardIterator>
3395 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3397 // concept requirements
3398 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3399 __glibcxx_function_requires(_LessThanComparableConcept<
3400 typename iterator_traits<_ForwardIterator>::value_type>)
3401 __glibcxx_requires_valid_range(__first, __last);
3403 return std::__minmax_element(__first, __last,
3404 __gnu_cxx::__ops::__iter_less_iter());
3408 * @brief Return a pair of iterators pointing to the minimum and maximum
3409 * elements in a range.
3410 * @ingroup sorting_algorithms
3411 * @param __first Start of range.
3412 * @param __last End of range.
3413 * @param __comp Comparison functor.
3414 * @return make_pair(m, M), where m is the first iterator i in
3415 * [__first, __last) such that no other element in the range is
3416 * smaller, and where M is the last iterator i in [__first, __last)
3417 * such that no other element in the range is larger.
3419 template<typename _ForwardIterator, typename _Compare>
3420 _GLIBCXX14_CONSTEXPR
3421 inline pair<_ForwardIterator, _ForwardIterator>
3422 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3425 // concept requirements
3426 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3427 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3428 typename iterator_traits<_ForwardIterator>::value_type,
3429 typename iterator_traits<_ForwardIterator>::value_type>)
3430 __glibcxx_requires_valid_range(__first, __last);
3432 return std::__minmax_element(__first, __last,
3433 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3437 template<typename _Tp>
3438 _GLIBCXX14_CONSTEXPR
3440 min(initializer_list<_Tp> __l)
3441 { return *std::min_element(__l.begin(), __l.end()); }
3443 template<typename _Tp, typename _Compare>
3444 _GLIBCXX14_CONSTEXPR
3446 min(initializer_list<_Tp> __l, _Compare __comp)
3447 { return *std::min_element(__l.begin(), __l.end(), __comp); }
3449 template<typename _Tp>
3450 _GLIBCXX14_CONSTEXPR
3452 max(initializer_list<_Tp> __l)
3453 { return *std::max_element(__l.begin(), __l.end()); }
3455 template<typename _Tp, typename _Compare>
3456 _GLIBCXX14_CONSTEXPR
3458 max(initializer_list<_Tp> __l, _Compare __comp)
3459 { return *std::max_element(__l.begin(), __l.end(), __comp); }
3461 template<typename _Tp>
3462 _GLIBCXX14_CONSTEXPR
3463 inline pair<_Tp, _Tp>
3464 minmax(initializer_list<_Tp> __l)
3466 pair<const _Tp*, const _Tp*> __p =
3467 std::minmax_element(__l.begin(), __l.end());
3468 return std::make_pair(*__p.first, *__p.second);
3471 template<typename _Tp, typename _Compare>
3472 _GLIBCXX14_CONSTEXPR
3473 inline pair<_Tp, _Tp>
3474 minmax(initializer_list<_Tp> __l, _Compare __comp)
3476 pair<const _Tp*, const _Tp*> __p =
3477 std::minmax_element(__l.begin(), __l.end(), __comp);
3478 return std::make_pair(*__p.first, *__p.second);
3481 template<typename _ForwardIterator1, typename _ForwardIterator2,
3482 typename _BinaryPredicate>
3484 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3485 _ForwardIterator2 __first2, _BinaryPredicate __pred)
3487 // Efficiently compare identical prefixes: O(N) if sequences
3488 // have the same elements in the same order.
3489 for (; __first1 != __last1; ++__first1, ++__first2)
3490 if (!__pred(__first1, __first2))
3493 if (__first1 == __last1)
3496 // Establish __last2 assuming equal ranges by iterating over the
3497 // rest of the list.
3498 _ForwardIterator2 __last2 = __first2;
3499 std::advance(__last2, std::distance(__first1, __last1));
3500 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3502 if (__scan != std::__find_if(__first1, __scan,
3503 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
3504 continue; // We've seen this one before.
3507 = std::__count_if(__first2, __last2,
3508 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
3509 if (0 == __matches ||
3510 std::__count_if(__scan, __last1,
3511 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
3519 * @brief Checks whether a permutation of the second sequence is equal
3520 * to the first sequence.
3521 * @ingroup non_mutating_algorithms
3522 * @param __first1 Start of first range.
3523 * @param __last1 End of first range.
3524 * @param __first2 Start of second range.
3525 * @return true if there exists a permutation of the elements in the range
3526 * [__first2, __first2 + (__last1 - __first1)), beginning with
3527 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3528 * returns true; otherwise, returns false.
3530 template<typename _ForwardIterator1, typename _ForwardIterator2>
3532 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3533 _ForwardIterator2 __first2)
3535 // concept requirements
3536 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3537 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3538 __glibcxx_function_requires(_EqualOpConcept<
3539 typename iterator_traits<_ForwardIterator1>::value_type,
3540 typename iterator_traits<_ForwardIterator2>::value_type>)
3541 __glibcxx_requires_valid_range(__first1, __last1);
3543 return std::__is_permutation(__first1, __last1, __first2,
3544 __gnu_cxx::__ops::__iter_equal_to_iter());
3548 * @brief Checks whether a permutation of the second sequence is equal
3549 * to the first sequence.
3550 * @ingroup non_mutating_algorithms
3551 * @param __first1 Start of first range.
3552 * @param __last1 End of first range.
3553 * @param __first2 Start of second range.
3554 * @param __pred A binary predicate.
3555 * @return true if there exists a permutation of the elements in
3556 * the range [__first2, __first2 + (__last1 - __first1)),
3557 * beginning with ForwardIterator2 begin, such that
3558 * equal(__first1, __last1, __begin, __pred) returns true;
3559 * otherwise, returns false.
3561 template<typename _ForwardIterator1, typename _ForwardIterator2,
3562 typename _BinaryPredicate>
3564 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3565 _ForwardIterator2 __first2, _BinaryPredicate __pred)
3567 // concept requirements
3568 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3569 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3570 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3571 typename iterator_traits<_ForwardIterator1>::value_type,
3572 typename iterator_traits<_ForwardIterator2>::value_type>)
3573 __glibcxx_requires_valid_range(__first1, __last1);
3575 return std::__is_permutation(__first1, __last1, __first2,
3576 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3579 #if __cplusplus > 201103L
3580 template<typename _ForwardIterator1, typename _ForwardIterator2,
3581 typename _BinaryPredicate>
3583 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3584 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3585 _BinaryPredicate __pred)
3588 = typename iterator_traits<_ForwardIterator1>::iterator_category;
3590 = typename iterator_traits<_ForwardIterator2>::iterator_category;
3591 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
3592 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
3593 constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA();
3596 auto __d1 = std::distance(__first1, __last1);
3597 auto __d2 = std::distance(__first2, __last2);
3602 // Efficiently compare identical prefixes: O(N) if sequences
3603 // have the same elements in the same order.
3604 for (; __first1 != __last1 && __first2 != __last2;
3605 ++__first1, ++__first2)
3606 if (!__pred(__first1, __first2))
3611 if (__first1 == __last1)
3616 auto __d1 = std::distance(__first1, __last1);
3617 auto __d2 = std::distance(__first2, __last2);
3618 if (__d1 == 0 && __d2 == 0)
3624 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3626 if (__scan != std::__find_if(__first1, __scan,
3627 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
3628 continue; // We've seen this one before.
3630 auto __matches = std::__count_if(__first2, __last2,
3631 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
3633 || std::__count_if(__scan, __last1,
3634 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
3642 * @brief Checks whether a permutaion of the second sequence is equal
3643 * to the first sequence.
3644 * @ingroup non_mutating_algorithms
3645 * @param __first1 Start of first range.
3646 * @param __last1 End of first range.
3647 * @param __first2 Start of second range.
3648 * @param __last2 End of first range.
3649 * @return true if there exists a permutation of the elements in the range
3650 * [__first2, __last2), beginning with ForwardIterator2 begin,
3651 * such that equal(__first1, __last1, begin) returns true;
3652 * otherwise, returns false.
3654 template<typename _ForwardIterator1, typename _ForwardIterator2>
3656 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3657 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3659 __glibcxx_requires_valid_range(__first1, __last1);
3660 __glibcxx_requires_valid_range(__first2, __last2);
3663 std::__is_permutation(__first1, __last1, __first2, __last2,
3664 __gnu_cxx::__ops::__iter_equal_to_iter());
3668 * @brief Checks whether a permutation of the second sequence is equal
3669 * to the first sequence.
3670 * @ingroup non_mutating_algorithms
3671 * @param __first1 Start of first range.
3672 * @param __last1 End of first range.
3673 * @param __first2 Start of second range.
3674 * @param __last2 End of first range.
3675 * @param __pred A binary predicate.
3676 * @return true if there exists a permutation of the elements in the range
3677 * [__first2, __last2), beginning with ForwardIterator2 begin,
3678 * such that equal(__first1, __last1, __begin, __pred) returns true;
3679 * otherwise, returns false.
3681 template<typename _ForwardIterator1, typename _ForwardIterator2,
3682 typename _BinaryPredicate>
3684 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3685 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3686 _BinaryPredicate __pred)
3688 __glibcxx_requires_valid_range(__first1, __last1);
3689 __glibcxx_requires_valid_range(__first2, __last2);
3691 return std::__is_permutation(__first1, __last1, __first2, __last2,
3692 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3696 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3698 * @brief Shuffle the elements of a sequence using a uniform random
3700 * @ingroup mutating_algorithms
3701 * @param __first A forward iterator.
3702 * @param __last A forward iterator.
3703 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3706 * Reorders the elements in the range @p [__first,__last) using @p __g to
3707 * provide random numbers.
3709 template<typename _RandomAccessIterator,
3710 typename _UniformRandomNumberGenerator>
3712 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3713 _UniformRandomNumberGenerator&& __g)
3715 // concept requirements
3716 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3717 _RandomAccessIterator>)
3718 __glibcxx_requires_valid_range(__first, __last);
3720 if (__first == __last)
3723 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3726 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
3727 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
3728 typedef typename __distr_type::param_type __p_type;
3731 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
3732 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
3738 _GLIBCXX_END_NAMESPACE_VERSION
3740 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3743 * @brief Apply a function to every element of a sequence.
3744 * @ingroup non_mutating_algorithms
3745 * @param __first An input iterator.
3746 * @param __last An input iterator.
3747 * @param __f A unary function object.
3748 * @return @p __f (std::move(@p __f) in C++0x).
3750 * Applies the function object @p __f to each element in the range
3751 * @p [first,last). @p __f must not modify the order of the sequence.
3752 * If @p __f has a return value it is ignored.
3754 template<typename _InputIterator, typename _Function>
3756 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3758 // concept requirements
3759 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3760 __glibcxx_requires_valid_range(__first, __last);
3761 for (; __first != __last; ++__first)
3763 return _GLIBCXX_MOVE(__f);
3767 * @brief Find the first occurrence of a value in a sequence.
3768 * @ingroup non_mutating_algorithms
3769 * @param __first An input iterator.
3770 * @param __last An input iterator.
3771 * @param __val The value to find.
3772 * @return The first iterator @c i in the range @p [__first,__last)
3773 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3775 template<typename _InputIterator, typename _Tp>
3776 inline _InputIterator
3777 find(_InputIterator __first, _InputIterator __last,
3780 // concept requirements
3781 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3782 __glibcxx_function_requires(_EqualOpConcept<
3783 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3784 __glibcxx_requires_valid_range(__first, __last);
3785 return std::__find_if(__first, __last,
3786 __gnu_cxx::__ops::__iter_equals_val(__val));
3790 * @brief Find the first element in a sequence for which a
3791 * predicate is true.
3792 * @ingroup non_mutating_algorithms
3793 * @param __first An input iterator.
3794 * @param __last An input iterator.
3795 * @param __pred A predicate.
3796 * @return The first iterator @c i in the range @p [__first,__last)
3797 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3799 template<typename _InputIterator, typename _Predicate>
3800 inline _InputIterator
3801 find_if(_InputIterator __first, _InputIterator __last,
3804 // concept requirements
3805 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3806 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3807 typename iterator_traits<_InputIterator>::value_type>)
3808 __glibcxx_requires_valid_range(__first, __last);
3810 return std::__find_if(__first, __last,
3811 __gnu_cxx::__ops::__pred_iter(__pred));
3815 * @brief Find element from a set in a sequence.
3816 * @ingroup non_mutating_algorithms
3817 * @param __first1 Start of range to search.
3818 * @param __last1 End of range to search.
3819 * @param __first2 Start of match candidates.
3820 * @param __last2 End of match candidates.
3821 * @return The first iterator @c i in the range
3822 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3823 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3825 * Searches the range @p [__first1,__last1) for an element that is
3826 * equal to some element in the range [__first2,__last2). If
3827 * found, returns an iterator in the range [__first1,__last1),
3828 * otherwise returns @p __last1.
3830 template<typename _InputIterator, typename _ForwardIterator>
3832 find_first_of(_InputIterator __first1, _InputIterator __last1,
3833 _ForwardIterator __first2, _ForwardIterator __last2)
3835 // concept requirements
3836 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3837 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3838 __glibcxx_function_requires(_EqualOpConcept<
3839 typename iterator_traits<_InputIterator>::value_type,
3840 typename iterator_traits<_ForwardIterator>::value_type>)
3841 __glibcxx_requires_valid_range(__first1, __last1);
3842 __glibcxx_requires_valid_range(__first2, __last2);
3844 for (; __first1 != __last1; ++__first1)
3845 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3846 if (*__first1 == *__iter)
3852 * @brief Find element from a set in a sequence using a predicate.
3853 * @ingroup non_mutating_algorithms
3854 * @param __first1 Start of range to search.
3855 * @param __last1 End of range to search.
3856 * @param __first2 Start of match candidates.
3857 * @param __last2 End of match candidates.
3858 * @param __comp Predicate to use.
3859 * @return The first iterator @c i in the range
3860 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3861 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3862 * such iterator exists.
3865 * Searches the range @p [__first1,__last1) for an element that is
3866 * equal to some element in the range [__first2,__last2). If
3867 * found, returns an iterator in the range [__first1,__last1),
3868 * otherwise returns @p __last1.
3870 template<typename _InputIterator, typename _ForwardIterator,
3871 typename _BinaryPredicate>
3873 find_first_of(_InputIterator __first1, _InputIterator __last1,
3874 _ForwardIterator __first2, _ForwardIterator __last2,
3875 _BinaryPredicate __comp)
3877 // concept requirements
3878 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3879 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3880 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3881 typename iterator_traits<_InputIterator>::value_type,
3882 typename iterator_traits<_ForwardIterator>::value_type>)
3883 __glibcxx_requires_valid_range(__first1, __last1);
3884 __glibcxx_requires_valid_range(__first2, __last2);
3886 for (; __first1 != __last1; ++__first1)
3887 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3888 if (__comp(*__first1, *__iter))
3894 * @brief Find two adjacent values in a sequence that are equal.
3895 * @ingroup non_mutating_algorithms
3896 * @param __first A forward iterator.
3897 * @param __last A forward iterator.
3898 * @return The first iterator @c i such that @c i and @c i+1 are both
3899 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3900 * or @p __last if no such iterator exists.
3902 template<typename _ForwardIterator>
3903 inline _ForwardIterator
3904 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
3906 // concept requirements
3907 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3908 __glibcxx_function_requires(_EqualityComparableConcept<
3909 typename iterator_traits<_ForwardIterator>::value_type>)
3910 __glibcxx_requires_valid_range(__first, __last);
3912 return std::__adjacent_find(__first, __last,
3913 __gnu_cxx::__ops::__iter_equal_to_iter());
3917 * @brief Find two adjacent values in a sequence using a predicate.
3918 * @ingroup non_mutating_algorithms
3919 * @param __first A forward iterator.
3920 * @param __last A forward iterator.
3921 * @param __binary_pred A binary predicate.
3922 * @return The first iterator @c i such that @c i and @c i+1 are both
3923 * valid iterators in @p [__first,__last) and such that
3924 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3927 template<typename _ForwardIterator, typename _BinaryPredicate>
3928 inline _ForwardIterator
3929 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
3930 _BinaryPredicate __binary_pred)
3932 // concept requirements
3933 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3934 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3935 typename iterator_traits<_ForwardIterator>::value_type,
3936 typename iterator_traits<_ForwardIterator>::value_type>)
3937 __glibcxx_requires_valid_range(__first, __last);
3939 return std::__adjacent_find(__first, __last,
3940 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
3944 * @brief Count the number of copies of a value in a sequence.
3945 * @ingroup non_mutating_algorithms
3946 * @param __first An input iterator.
3947 * @param __last An input iterator.
3948 * @param __value The value to be counted.
3949 * @return The number of iterators @c i in the range @p [__first,__last)
3950 * for which @c *i == @p __value
3952 template<typename _InputIterator, typename _Tp>
3953 inline typename iterator_traits<_InputIterator>::difference_type
3954 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
3956 // concept requirements
3957 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3958 __glibcxx_function_requires(_EqualOpConcept<
3959 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3960 __glibcxx_requires_valid_range(__first, __last);
3962 return std::__count_if(__first, __last,
3963 __gnu_cxx::__ops::__iter_equals_val(__value));
3967 * @brief Count the elements of a sequence for which a predicate is true.
3968 * @ingroup non_mutating_algorithms
3969 * @param __first An input iterator.
3970 * @param __last An input iterator.
3971 * @param __pred A predicate.
3972 * @return The number of iterators @c i in the range @p [__first,__last)
3973 * for which @p __pred(*i) is true.
3975 template<typename _InputIterator, typename _Predicate>
3976 inline typename iterator_traits<_InputIterator>::difference_type
3977 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3979 // concept requirements
3980 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3981 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3982 typename iterator_traits<_InputIterator>::value_type>)
3983 __glibcxx_requires_valid_range(__first, __last);
3985 return std::__count_if(__first, __last,
3986 __gnu_cxx::__ops::__pred_iter(__pred));
3990 * @brief Search a sequence for a matching sub-sequence.
3991 * @ingroup non_mutating_algorithms
3992 * @param __first1 A forward iterator.
3993 * @param __last1 A forward iterator.
3994 * @param __first2 A forward iterator.
3995 * @param __last2 A forward iterator.
3996 * @return The first iterator @c i in the range @p
3997 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
3998 * *(__first2+N) for each @c N in the range @p
3999 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4001 * Searches the range @p [__first1,__last1) for a sub-sequence that
4002 * compares equal value-by-value with the sequence given by @p
4003 * [__first2,__last2) and returns an iterator to the first element
4004 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4007 * Because the sub-sequence must lie completely within the range @p
4008 * [__first1,__last1) it must start at a position less than @p
4009 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4010 * length of the sub-sequence.
4012 * This means that the returned iterator @c i will be in the range
4013 * @p [__first1,__last1-(__last2-__first2))
4015 template<typename _ForwardIterator1, typename _ForwardIterator2>
4016 inline _ForwardIterator1
4017 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4018 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4020 // concept requirements
4021 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4022 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4023 __glibcxx_function_requires(_EqualOpConcept<
4024 typename iterator_traits<_ForwardIterator1>::value_type,
4025 typename iterator_traits<_ForwardIterator2>::value_type>)
4026 __glibcxx_requires_valid_range(__first1, __last1);
4027 __glibcxx_requires_valid_range(__first2, __last2);
4029 return std::__search(__first1, __last1, __first2, __last2,
4030 __gnu_cxx::__ops::__iter_equal_to_iter());
4034 * @brief Search a sequence for a matching sub-sequence using a predicate.
4035 * @ingroup non_mutating_algorithms
4036 * @param __first1 A forward iterator.
4037 * @param __last1 A forward iterator.
4038 * @param __first2 A forward iterator.
4039 * @param __last2 A forward iterator.
4040 * @param __predicate A binary predicate.
4041 * @return The first iterator @c i in the range
4042 * @p [__first1,__last1-(__last2-__first2)) such that
4043 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4044 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4046 * Searches the range @p [__first1,__last1) for a sub-sequence that
4047 * compares equal value-by-value with the sequence given by @p
4048 * [__first2,__last2), using @p __predicate to determine equality,
4049 * and returns an iterator to the first element of the
4050 * sub-sequence, or @p __last1 if no such iterator exists.
4052 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4054 template<typename _ForwardIterator1, typename _ForwardIterator2,
4055 typename _BinaryPredicate>
4056 inline _ForwardIterator1
4057 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4058 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4059 _BinaryPredicate __predicate)
4061 // concept requirements
4062 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4063 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4064 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4065 typename iterator_traits<_ForwardIterator1>::value_type,
4066 typename iterator_traits<_ForwardIterator2>::value_type>)
4067 __glibcxx_requires_valid_range(__first1, __last1);
4068 __glibcxx_requires_valid_range(__first2, __last2);
4070 return std::__search(__first1, __last1, __first2, __last2,
4071 __gnu_cxx::__ops::__iter_comp_iter(__predicate));
4075 * @brief Search a sequence for a number of consecutive values.
4076 * @ingroup non_mutating_algorithms
4077 * @param __first A forward iterator.
4078 * @param __last A forward iterator.
4079 * @param __count The number of consecutive values.
4080 * @param __val The value to find.
4081 * @return The first iterator @c i in the range @p
4082 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4083 * each @c N in the range @p [0,__count), or @p __last if no such
4086 * Searches the range @p [__first,__last) for @p count consecutive elements
4087 * equal to @p __val.
4089 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4090 inline _ForwardIterator
4091 search_n(_ForwardIterator __first, _ForwardIterator __last,
4092 _Integer __count, const _Tp& __val)
4094 // concept requirements
4095 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4096 __glibcxx_function_requires(_EqualOpConcept<
4097 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4098 __glibcxx_requires_valid_range(__first, __last);
4100 return std::__search_n(__first, __last, __count,
4101 __gnu_cxx::__ops::__iter_equals_val(__val));
4106 * @brief Search a sequence for a number of consecutive values using a
4108 * @ingroup non_mutating_algorithms
4109 * @param __first A forward iterator.
4110 * @param __last A forward iterator.
4111 * @param __count The number of consecutive values.
4112 * @param __val The value to find.
4113 * @param __binary_pred A binary predicate.
4114 * @return The first iterator @c i in the range @p
4115 * [__first,__last-__count) such that @p
4116 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4117 * @p [0,__count), or @p __last if no such iterator exists.
4119 * Searches the range @p [__first,__last) for @p __count
4120 * consecutive elements for which the predicate returns true.
4122 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4123 typename _BinaryPredicate>
4124 inline _ForwardIterator
4125 search_n(_ForwardIterator __first, _ForwardIterator __last,
4126 _Integer __count, const _Tp& __val,
4127 _BinaryPredicate __binary_pred)
4129 // concept requirements
4130 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4131 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4132 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4133 __glibcxx_requires_valid_range(__first, __last);
4135 return std::__search_n(__first, __last, __count,
4136 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
4141 * @brief Perform an operation on a sequence.
4142 * @ingroup mutating_algorithms
4143 * @param __first An input iterator.
4144 * @param __last An input iterator.
4145 * @param __result An output iterator.
4146 * @param __unary_op A unary operator.
4147 * @return An output iterator equal to @p __result+(__last-__first).
4149 * Applies the operator to each element in the input range and assigns
4150 * the results to successive elements of the output sequence.
4151 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4152 * range @p [0,__last-__first).
4154 * @p unary_op must not alter its argument.
4156 template<typename _InputIterator, typename _OutputIterator,
4157 typename _UnaryOperation>
4159 transform(_InputIterator __first, _InputIterator __last,
4160 _OutputIterator __result, _UnaryOperation __unary_op)
4162 // concept requirements
4163 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4164 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4165 // "the type returned by a _UnaryOperation"
4166 __typeof__(__unary_op(*__first))>)
4167 __glibcxx_requires_valid_range(__first, __last);
4169 for (; __first != __last; ++__first, ++__result)
4170 *__result = __unary_op(*__first);
4175 * @brief Perform an operation on corresponding elements of two sequences.
4176 * @ingroup mutating_algorithms
4177 * @param __first1 An input iterator.
4178 * @param __last1 An input iterator.
4179 * @param __first2 An input iterator.
4180 * @param __result An output iterator.
4181 * @param __binary_op A binary operator.
4182 * @return An output iterator equal to @p result+(last-first).
4184 * Applies the operator to the corresponding elements in the two
4185 * input ranges and assigns the results to successive elements of the
4188 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4189 * @c N in the range @p [0,__last1-__first1).
4191 * @p binary_op must not alter either of its arguments.
4193 template<typename _InputIterator1, typename _InputIterator2,
4194 typename _OutputIterator, typename _BinaryOperation>
4196 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4197 _InputIterator2 __first2, _OutputIterator __result,
4198 _BinaryOperation __binary_op)
4200 // concept requirements
4201 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4202 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4203 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4204 // "the type returned by a _BinaryOperation"
4205 __typeof__(__binary_op(*__first1,*__first2))>)
4206 __glibcxx_requires_valid_range(__first1, __last1);
4208 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4209 *__result = __binary_op(*__first1, *__first2);
4214 * @brief Replace each occurrence of one value in a sequence with another
4216 * @ingroup mutating_algorithms
4217 * @param __first A forward iterator.
4218 * @param __last A forward iterator.
4219 * @param __old_value The value to be replaced.
4220 * @param __new_value The replacement value.
4221 * @return replace() returns no value.
4223 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4224 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4226 template<typename _ForwardIterator, typename _Tp>
4228 replace(_ForwardIterator __first, _ForwardIterator __last,
4229 const _Tp& __old_value, const _Tp& __new_value)
4231 // concept requirements
4232 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4234 __glibcxx_function_requires(_EqualOpConcept<
4235 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4236 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4237 typename iterator_traits<_ForwardIterator>::value_type>)
4238 __glibcxx_requires_valid_range(__first, __last);
4240 for (; __first != __last; ++__first)
4241 if (*__first == __old_value)
4242 *__first = __new_value;
4246 * @brief Replace each value in a sequence for which a predicate returns
4247 * true with another value.
4248 * @ingroup mutating_algorithms
4249 * @param __first A forward iterator.
4250 * @param __last A forward iterator.
4251 * @param __pred A predicate.
4252 * @param __new_value The replacement value.
4253 * @return replace_if() returns no value.
4255 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4256 * is true then the assignment @c *i = @p __new_value is performed.
4258 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4260 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4261 _Predicate __pred, const _Tp& __new_value)
4263 // concept requirements
4264 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4266 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4267 typename iterator_traits<_ForwardIterator>::value_type>)
4268 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4269 typename iterator_traits<_ForwardIterator>::value_type>)
4270 __glibcxx_requires_valid_range(__first, __last);
4272 for (; __first != __last; ++__first)
4273 if (__pred(*__first))
4274 *__first = __new_value;
4278 * @brief Assign the result of a function object to each value in a
4280 * @ingroup mutating_algorithms
4281 * @param __first A forward iterator.
4282 * @param __last A forward iterator.
4283 * @param __gen A function object taking no arguments and returning
4284 * std::iterator_traits<_ForwardIterator>::value_type
4285 * @return generate() returns no value.
4287 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4288 * @p [__first,__last).
4290 template<typename _ForwardIterator, typename _Generator>
4292 generate(_ForwardIterator __first, _ForwardIterator __last,
4295 // concept requirements
4296 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4297 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4298 typename iterator_traits<_ForwardIterator>::value_type>)
4299 __glibcxx_requires_valid_range(__first, __last);
4301 for (; __first != __last; ++__first)
4306 * @brief Assign the result of a function object to each value in a
4308 * @ingroup mutating_algorithms
4309 * @param __first A forward iterator.
4310 * @param __n The length of the sequence.
4311 * @param __gen A function object taking no arguments and returning
4312 * std::iterator_traits<_ForwardIterator>::value_type
4313 * @return The end of the sequence, @p __first+__n
4315 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4316 * @p [__first,__first+__n).
4318 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4319 * DR 865. More algorithms that throw away information
4321 template<typename _OutputIterator, typename _Size, typename _Generator>
4323 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4325 // concept requirements
4326 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4327 // "the type returned by a _Generator"
4328 __typeof__(__gen())>)
4330 for (__decltype(__n + 0) __niter = __n;
4331 __niter > 0; --__niter, ++__first)
4337 * @brief Copy a sequence, removing consecutive duplicate values.
4338 * @ingroup mutating_algorithms
4339 * @param __first An input iterator.
4340 * @param __last An input iterator.
4341 * @param __result An output iterator.
4342 * @return An iterator designating the end of the resulting sequence.
4344 * Copies each element in the range @p [__first,__last) to the range
4345 * beginning at @p __result, except that only the first element is copied
4346 * from groups of consecutive elements that compare equal.
4347 * unique_copy() is stable, so the relative order of elements that are
4348 * copied is unchanged.
4350 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4351 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4353 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4354 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4357 template<typename _InputIterator, typename _OutputIterator>
4358 inline _OutputIterator
4359 unique_copy(_InputIterator __first, _InputIterator __last,
4360 _OutputIterator __result)
4362 // concept requirements
4363 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4364 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4365 typename iterator_traits<_InputIterator>::value_type>)
4366 __glibcxx_function_requires(_EqualityComparableConcept<
4367 typename iterator_traits<_InputIterator>::value_type>)
4368 __glibcxx_requires_valid_range(__first, __last);
4370 if (__first == __last)
4372 return std::__unique_copy(__first, __last, __result,
4373 __gnu_cxx::__ops::__iter_equal_to_iter(),
4374 std::__iterator_category(__first),
4375 std::__iterator_category(__result));
4379 * @brief Copy a sequence, removing consecutive values using a predicate.
4380 * @ingroup mutating_algorithms
4381 * @param __first An input iterator.
4382 * @param __last An input iterator.
4383 * @param __result An output iterator.
4384 * @param __binary_pred A binary predicate.
4385 * @return An iterator designating the end of the resulting sequence.
4387 * Copies each element in the range @p [__first,__last) to the range
4388 * beginning at @p __result, except that only the first element is copied
4389 * from groups of consecutive elements for which @p __binary_pred returns
4391 * unique_copy() is stable, so the relative order of elements that are
4392 * copied is unchanged.
4394 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4395 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4397 template<typename _InputIterator, typename _OutputIterator,
4398 typename _BinaryPredicate>
4399 inline _OutputIterator
4400 unique_copy(_InputIterator __first, _InputIterator __last,
4401 _OutputIterator __result,
4402 _BinaryPredicate __binary_pred)
4404 // concept requirements -- predicates checked later
4405 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4406 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4407 typename iterator_traits<_InputIterator>::value_type>)
4408 __glibcxx_requires_valid_range(__first, __last);
4410 if (__first == __last)
4412 return std::__unique_copy(__first, __last, __result,
4413 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
4414 std::__iterator_category(__first),
4415 std::__iterator_category(__result));
4419 * @brief Randomly shuffle the elements of a sequence.
4420 * @ingroup mutating_algorithms
4421 * @param __first A forward iterator.
4422 * @param __last A forward iterator.
4425 * Reorder the elements in the range @p [__first,__last) using a random
4426 * distribution, so that every possible ordering of the sequence is
4429 template<typename _RandomAccessIterator>
4431 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4433 // concept requirements
4434 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4435 _RandomAccessIterator>)
4436 __glibcxx_requires_valid_range(__first, __last);
4438 if (__first != __last)
4439 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4441 // XXX rand() % N is not uniformly distributed
4442 _RandomAccessIterator __j = __first
4443 + std::rand() % ((__i - __first) + 1);
4445 std::iter_swap(__i, __j);
4450 * @brief Shuffle the elements of a sequence using a random number
4452 * @ingroup mutating_algorithms
4453 * @param __first A forward iterator.
4454 * @param __last A forward iterator.
4455 * @param __rand The RNG functor or function.
4458 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4459 * provide a random distribution. Calling @p __rand(N) for a positive
4460 * integer @p N should return a randomly chosen integer from the
4463 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4465 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4466 #if __cplusplus >= 201103L
4467 _RandomNumberGenerator&& __rand)
4469 _RandomNumberGenerator& __rand)
4472 // concept requirements
4473 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4474 _RandomAccessIterator>)
4475 __glibcxx_requires_valid_range(__first, __last);
4477 if (__first == __last)
4479 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4481 _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
4483 std::iter_swap(__i, __j);
4489 * @brief Move elements for which a predicate is true to the beginning
4491 * @ingroup mutating_algorithms
4492 * @param __first A forward iterator.
4493 * @param __last A forward iterator.
4494 * @param __pred A predicate functor.
4495 * @return An iterator @p middle such that @p __pred(i) is true for each
4496 * iterator @p i in the range @p [__first,middle) and false for each @p i
4497 * in the range @p [middle,__last).
4499 * @p __pred must not modify its operand. @p partition() does not preserve
4500 * the relative ordering of elements in each group, use
4501 * @p stable_partition() if this is needed.
4503 template<typename _ForwardIterator, typename _Predicate>
4504 inline _ForwardIterator
4505 partition(_ForwardIterator __first, _ForwardIterator __last,
4508 // concept requirements
4509 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4511 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4512 typename iterator_traits<_ForwardIterator>::value_type>)
4513 __glibcxx_requires_valid_range(__first, __last);
4515 return std::__partition(__first, __last, __pred,
4516 std::__iterator_category(__first));
4521 * @brief Sort the smallest elements of a sequence.
4522 * @ingroup sorting_algorithms
4523 * @param __first An iterator.
4524 * @param __middle Another iterator.
4525 * @param __last Another iterator.
4528 * Sorts the smallest @p (__middle-__first) elements in the range
4529 * @p [first,last) and moves them to the range @p [__first,__middle). The
4530 * order of the remaining elements in the range @p [__middle,__last) is
4532 * After the sort if @e i and @e j are iterators in the range
4533 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4534 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4536 template<typename _RandomAccessIterator>
4538 partial_sort(_RandomAccessIterator __first,
4539 _RandomAccessIterator __middle,
4540 _RandomAccessIterator __last)
4542 // concept requirements
4543 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4544 _RandomAccessIterator>)
4545 __glibcxx_function_requires(_LessThanComparableConcept<
4546 typename iterator_traits<_RandomAccessIterator>::value_type>)
4547 __glibcxx_requires_valid_range(__first, __middle);
4548 __glibcxx_requires_valid_range(__middle, __last);
4550 std::__partial_sort(__first, __middle, __last,
4551 __gnu_cxx::__ops::__iter_less_iter());
4555 * @brief Sort the smallest elements of a sequence using a predicate
4557 * @ingroup sorting_algorithms
4558 * @param __first An iterator.
4559 * @param __middle Another iterator.
4560 * @param __last Another iterator.
4561 * @param __comp A comparison functor.
4564 * Sorts the smallest @p (__middle-__first) elements in the range
4565 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4566 * order of the remaining elements in the range @p [__middle,__last) is
4568 * After the sort if @e i and @e j are iterators in the range
4569 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4570 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4573 template<typename _RandomAccessIterator, typename _Compare>
4575 partial_sort(_RandomAccessIterator __first,
4576 _RandomAccessIterator __middle,
4577 _RandomAccessIterator __last,
4580 // concept requirements
4581 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4582 _RandomAccessIterator>)
4583 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4584 typename iterator_traits<_RandomAccessIterator>::value_type,
4585 typename iterator_traits<_RandomAccessIterator>::value_type>)
4586 __glibcxx_requires_valid_range(__first, __middle);
4587 __glibcxx_requires_valid_range(__middle, __last);
4589 std::__partial_sort(__first, __middle, __last,
4590 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4594 * @brief Sort a sequence just enough to find a particular position.
4595 * @ingroup sorting_algorithms
4596 * @param __first An iterator.
4597 * @param __nth Another iterator.
4598 * @param __last Another iterator.
4601 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4602 * is the same element that would have been in that position had the
4603 * whole sequence been sorted. The elements either side of @p *__nth are
4604 * not completely sorted, but for any iterator @e i in the range
4605 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4606 * holds that *j < *i is false.
4608 template<typename _RandomAccessIterator>
4610 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4611 _RandomAccessIterator __last)
4613 // concept requirements
4614 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4615 _RandomAccessIterator>)
4616 __glibcxx_function_requires(_LessThanComparableConcept<
4617 typename iterator_traits<_RandomAccessIterator>::value_type>)
4618 __glibcxx_requires_valid_range(__first, __nth);
4619 __glibcxx_requires_valid_range(__nth, __last);
4621 if (__first == __last || __nth == __last)
4624 std::__introselect(__first, __nth, __last,
4625 std::__lg(__last - __first) * 2,
4626 __gnu_cxx::__ops::__iter_less_iter());
4630 * @brief Sort a sequence just enough to find a particular position
4631 * using a predicate for comparison.
4632 * @ingroup sorting_algorithms
4633 * @param __first An iterator.
4634 * @param __nth Another iterator.
4635 * @param __last Another iterator.
4636 * @param __comp A comparison functor.
4639 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4640 * is the same element that would have been in that position had the
4641 * whole sequence been sorted. The elements either side of @p *__nth are
4642 * not completely sorted, but for any iterator @e i in the range
4643 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4644 * holds that @p __comp(*j,*i) is false.
4646 template<typename _RandomAccessIterator, typename _Compare>
4648 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4649 _RandomAccessIterator __last, _Compare __comp)
4651 // concept requirements
4652 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4653 _RandomAccessIterator>)
4654 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4655 typename iterator_traits<_RandomAccessIterator>::value_type,
4656 typename iterator_traits<_RandomAccessIterator>::value_type>)
4657 __glibcxx_requires_valid_range(__first, __nth);
4658 __glibcxx_requires_valid_range(__nth, __last);
4660 if (__first == __last || __nth == __last)
4663 std::__introselect(__first, __nth, __last,
4664 std::__lg(__last - __first) * 2,
4665 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4669 * @brief Sort the elements of a sequence.
4670 * @ingroup sorting_algorithms
4671 * @param __first An iterator.
4672 * @param __last Another iterator.
4675 * Sorts the elements in the range @p [__first,__last) in ascending order,
4676 * such that for each iterator @e i in the range @p [__first,__last-1),
4677 * *(i+1)<*i is false.
4679 * The relative ordering of equivalent elements is not preserved, use
4680 * @p stable_sort() if this is needed.
4682 template<typename _RandomAccessIterator>
4684 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4686 // concept requirements
4687 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4688 _RandomAccessIterator>)
4689 __glibcxx_function_requires(_LessThanComparableConcept<
4690 typename iterator_traits<_RandomAccessIterator>::value_type>)
4691 __glibcxx_requires_valid_range(__first, __last);
4693 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
4697 * @brief Sort the elements of a sequence using a predicate for comparison.
4698 * @ingroup sorting_algorithms
4699 * @param __first An iterator.
4700 * @param __last Another iterator.
4701 * @param __comp A comparison functor.
4704 * Sorts the elements in the range @p [__first,__last) in ascending order,
4705 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4706 * range @p [__first,__last-1).
4708 * The relative ordering of equivalent elements is not preserved, use
4709 * @p stable_sort() if this is needed.
4711 template<typename _RandomAccessIterator, typename _Compare>
4713 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4716 // concept requirements
4717 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4718 _RandomAccessIterator>)
4719 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4720 typename iterator_traits<_RandomAccessIterator>::value_type,
4721 typename iterator_traits<_RandomAccessIterator>::value_type>)
4722 __glibcxx_requires_valid_range(__first, __last);
4724 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
4727 template<typename _InputIterator1, typename _InputIterator2,
4728 typename _OutputIterator, typename _Compare>
4730 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4731 _InputIterator2 __first2, _InputIterator2 __last2,
4732 _OutputIterator __result, _Compare __comp)
4734 while (__first1 != __last1 && __first2 != __last2)
4736 if (__comp(__first2, __first1))
4738 *__result = *__first2;
4743 *__result = *__first1;
4748 return std::copy(__first2, __last2,
4749 std::copy(__first1, __last1, __result));
4753 * @brief Merges two sorted ranges.
4754 * @ingroup sorting_algorithms
4755 * @param __first1 An iterator.
4756 * @param __first2 Another iterator.
4757 * @param __last1 Another iterator.
4758 * @param __last2 Another iterator.
4759 * @param __result An iterator pointing to the end of the merged range.
4760 * @return An iterator pointing to the first element <em>not less
4763 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4764 * the sorted range @p [__result, __result + (__last1-__first1) +
4765 * (__last2-__first2)). Both input ranges must be sorted, and the
4766 * output range must not overlap with either of the input ranges.
4767 * The sort is @e stable, that is, for equivalent elements in the
4768 * two ranges, elements from the first range will always come
4769 * before elements from the second.
4771 template<typename _InputIterator1, typename _InputIterator2,
4772 typename _OutputIterator>
4773 inline _OutputIterator
4774 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4775 _InputIterator2 __first2, _InputIterator2 __last2,
4776 _OutputIterator __result)
4778 // concept requirements
4779 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4780 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4781 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4782 typename iterator_traits<_InputIterator1>::value_type>)
4783 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4784 typename iterator_traits<_InputIterator2>::value_type>)
4785 __glibcxx_function_requires(_LessThanOpConcept<
4786 typename iterator_traits<_InputIterator2>::value_type,
4787 typename iterator_traits<_InputIterator1>::value_type>)
4788 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4789 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4791 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4792 __first2, __last2, __result,
4793 __gnu_cxx::__ops::__iter_less_iter());
4797 * @brief Merges two sorted ranges.
4798 * @ingroup sorting_algorithms
4799 * @param __first1 An iterator.
4800 * @param __first2 Another iterator.
4801 * @param __last1 Another iterator.
4802 * @param __last2 Another iterator.
4803 * @param __result An iterator pointing to the end of the merged range.
4804 * @param __comp A functor to use for comparisons.
4805 * @return An iterator pointing to the first element "not less
4808 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4809 * the sorted range @p [__result, __result + (__last1-__first1) +
4810 * (__last2-__first2)). Both input ranges must be sorted, and the
4811 * output range must not overlap with either of the input ranges.
4812 * The sort is @e stable, that is, for equivalent elements in the
4813 * two ranges, elements from the first range will always come
4814 * before elements from the second.
4816 * The comparison function should have the same effects on ordering as
4817 * the function used for the initial sort.
4819 template<typename _InputIterator1, typename _InputIterator2,
4820 typename _OutputIterator, typename _Compare>
4821 inline _OutputIterator
4822 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4823 _InputIterator2 __first2, _InputIterator2 __last2,
4824 _OutputIterator __result, _Compare __comp)
4826 // concept requirements
4827 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4828 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4829 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4830 typename iterator_traits<_InputIterator1>::value_type>)
4831 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4832 typename iterator_traits<_InputIterator2>::value_type>)
4833 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4834 typename iterator_traits<_InputIterator2>::value_type,
4835 typename iterator_traits<_InputIterator1>::value_type>)
4836 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
4837 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
4839 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4840 __first2, __last2, __result,
4841 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4844 template<typename _RandomAccessIterator, typename _Compare>
4846 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4849 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4851 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4854 typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
4855 _TmpBuf __buf(__first, __last);
4857 if (__buf.begin() == 0)
4858 std::__inplace_stable_sort(__first, __last, __comp);
4860 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
4861 _DistanceType(__buf.size()), __comp);
4865 * @brief Sort the elements of a sequence, preserving the relative order
4866 * of equivalent elements.
4867 * @ingroup sorting_algorithms
4868 * @param __first An iterator.
4869 * @param __last Another iterator.
4872 * Sorts the elements in the range @p [__first,__last) in ascending order,
4873 * such that for each iterator @p i in the range @p [__first,__last-1),
4874 * @p *(i+1)<*i is false.
4876 * The relative ordering of equivalent elements is preserved, so any two
4877 * elements @p x and @p y in the range @p [__first,__last) such that
4878 * @p x<y is false and @p y<x is false will have the same relative
4879 * ordering after calling @p stable_sort().
4881 template<typename _RandomAccessIterator>
4883 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4885 // concept requirements
4886 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4887 _RandomAccessIterator>)
4888 __glibcxx_function_requires(_LessThanComparableConcept<
4889 typename iterator_traits<_RandomAccessIterator>::value_type>)
4890 __glibcxx_requires_valid_range(__first, __last);
4892 _GLIBCXX_STD_A::__stable_sort(__first, __last,
4893 __gnu_cxx::__ops::__iter_less_iter());
4897 * @brief Sort the elements of a sequence using a predicate for comparison,
4898 * preserving the relative order of equivalent elements.
4899 * @ingroup sorting_algorithms
4900 * @param __first An iterator.
4901 * @param __last Another iterator.
4902 * @param __comp A comparison functor.
4905 * Sorts the elements in the range @p [__first,__last) in ascending order,
4906 * such that for each iterator @p i in the range @p [__first,__last-1),
4907 * @p __comp(*(i+1),*i) is false.
4909 * The relative ordering of equivalent elements is preserved, so any two
4910 * elements @p x and @p y in the range @p [__first,__last) such that
4911 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
4912 * relative ordering after calling @p stable_sort().
4914 template<typename _RandomAccessIterator, typename _Compare>
4916 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4919 // concept requirements
4920 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4921 _RandomAccessIterator>)
4922 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4923 typename iterator_traits<_RandomAccessIterator>::value_type,
4924 typename iterator_traits<_RandomAccessIterator>::value_type>)
4925 __glibcxx_requires_valid_range(__first, __last);
4927 _GLIBCXX_STD_A::__stable_sort(__first, __last,
4928 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4931 template<typename _InputIterator1, typename _InputIterator2,
4932 typename _OutputIterator,
4935 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4936 _InputIterator2 __first2, _InputIterator2 __last2,
4937 _OutputIterator __result, _Compare __comp)
4939 while (__first1 != __last1 && __first2 != __last2)
4941 if (__comp(__first1, __first2))
4943 *__result = *__first1;
4946 else if (__comp(__first2, __first1))
4948 *__result = *__first2;
4953 *__result = *__first1;
4959 return std::copy(__first2, __last2,
4960 std::copy(__first1, __last1, __result));
4964 * @brief Return the union of two sorted ranges.
4965 * @ingroup set_algorithms
4966 * @param __first1 Start of first range.
4967 * @param __last1 End of first range.
4968 * @param __first2 Start of second range.
4969 * @param __last2 End of second range.
4970 * @return End of the output range.
4971 * @ingroup set_algorithms
4973 * This operation iterates over both ranges, copying elements present in
4974 * each range in order to the output range. Iterators increment for each
4975 * range. When the current element of one range is less than the other,
4976 * that element is copied and the iterator advanced. If an element is
4977 * contained in both ranges, the element from the first range is copied and
4978 * both ranges advance. The output range may not overlap either input
4981 template<typename _InputIterator1, typename _InputIterator2,
4982 typename _OutputIterator>
4983 inline _OutputIterator
4984 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4985 _InputIterator2 __first2, _InputIterator2 __last2,
4986 _OutputIterator __result)
4988 // concept requirements
4989 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4990 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4991 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4992 typename iterator_traits<_InputIterator1>::value_type>)
4993 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4994 typename iterator_traits<_InputIterator2>::value_type>)
4995 __glibcxx_function_requires(_LessThanOpConcept<
4996 typename iterator_traits<_InputIterator1>::value_type,
4997 typename iterator_traits<_InputIterator2>::value_type>)
4998 __glibcxx_function_requires(_LessThanOpConcept<
4999 typename iterator_traits<_InputIterator2>::value_type,
5000 typename iterator_traits<_InputIterator1>::value_type>)
5001 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5002 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5004 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5005 __first2, __last2, __result,
5006 __gnu_cxx::__ops::__iter_less_iter());
5010 * @brief Return the union of two sorted ranges using a comparison functor.
5011 * @ingroup set_algorithms
5012 * @param __first1 Start of first range.
5013 * @param __last1 End of first range.
5014 * @param __first2 Start of second range.
5015 * @param __last2 End of second range.
5016 * @param __comp The comparison functor.
5017 * @return End of the output range.
5018 * @ingroup set_algorithms
5020 * This operation iterates over both ranges, copying elements present in
5021 * each range in order to the output range. Iterators increment for each
5022 * range. When the current element of one range is less than the other
5023 * according to @p __comp, that element is copied and the iterator advanced.
5024 * If an equivalent element according to @p __comp is contained in both
5025 * ranges, the element from the first range is copied and both ranges
5026 * advance. The output range may not overlap either input range.
5028 template<typename _InputIterator1, typename _InputIterator2,
5029 typename _OutputIterator, typename _Compare>
5030 inline _OutputIterator
5031 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5032 _InputIterator2 __first2, _InputIterator2 __last2,
5033 _OutputIterator __result, _Compare __comp)
5035 // concept requirements
5036 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5037 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5038 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5039 typename iterator_traits<_InputIterator1>::value_type>)
5040 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5041 typename iterator_traits<_InputIterator2>::value_type>)
5042 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5043 typename iterator_traits<_InputIterator1>::value_type,
5044 typename iterator_traits<_InputIterator2>::value_type>)
5045 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5046 typename iterator_traits<_InputIterator2>::value_type,
5047 typename iterator_traits<_InputIterator1>::value_type>)
5048 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5049 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5051 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5052 __first2, __last2, __result,
5053 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5056 template<typename _InputIterator1, typename _InputIterator2,
5057 typename _OutputIterator,
5060 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5061 _InputIterator2 __first2, _InputIterator2 __last2,
5062 _OutputIterator __result, _Compare __comp)
5064 while (__first1 != __last1 && __first2 != __last2)
5065 if (__comp(__first1, __first2))
5067 else if (__comp(__first2, __first1))
5071 *__result = *__first1;
5080 * @brief Return the intersection of two sorted ranges.
5081 * @ingroup set_algorithms
5082 * @param __first1 Start of first range.
5083 * @param __last1 End of first range.
5084 * @param __first2 Start of second range.
5085 * @param __last2 End of second range.
5086 * @return End of the output range.
5087 * @ingroup set_algorithms
5089 * This operation iterates over both ranges, copying elements present in
5090 * both ranges in order to the output range. Iterators increment for each
5091 * range. When the current element of one range is less than the other,
5092 * that iterator advances. If an element is contained in both ranges, the
5093 * element from the first range is copied and both ranges advance. The
5094 * output range may not overlap either input range.
5096 template<typename _InputIterator1, typename _InputIterator2,
5097 typename _OutputIterator>
5098 inline _OutputIterator
5099 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5100 _InputIterator2 __first2, _InputIterator2 __last2,
5101 _OutputIterator __result)
5103 // concept requirements
5104 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5105 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5106 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5107 typename iterator_traits<_InputIterator1>::value_type>)
5108 __glibcxx_function_requires(_LessThanOpConcept<
5109 typename iterator_traits<_InputIterator1>::value_type,
5110 typename iterator_traits<_InputIterator2>::value_type>)
5111 __glibcxx_function_requires(_LessThanOpConcept<
5112 typename iterator_traits<_InputIterator2>::value_type,
5113 typename iterator_traits<_InputIterator1>::value_type>)
5114 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5115 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5117 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5118 __first2, __last2, __result,
5119 __gnu_cxx::__ops::__iter_less_iter());
5123 * @brief Return the intersection of two sorted ranges using comparison
5125 * @ingroup set_algorithms
5126 * @param __first1 Start of first range.
5127 * @param __last1 End of first range.
5128 * @param __first2 Start of second range.
5129 * @param __last2 End of second range.
5130 * @param __comp The comparison functor.
5131 * @return End of the output range.
5132 * @ingroup set_algorithms
5134 * This operation iterates over both ranges, copying elements present in
5135 * both ranges in order to the output range. Iterators increment for each
5136 * range. When the current element of one range is less than the other
5137 * according to @p __comp, that iterator advances. If an element is
5138 * contained in both ranges according to @p __comp, the element from the
5139 * first range is copied and both ranges advance. The output range may not
5140 * overlap either input range.
5142 template<typename _InputIterator1, typename _InputIterator2,
5143 typename _OutputIterator, typename _Compare>
5144 inline _OutputIterator
5145 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5146 _InputIterator2 __first2, _InputIterator2 __last2,
5147 _OutputIterator __result, _Compare __comp)
5149 // concept requirements
5150 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5151 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5152 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5153 typename iterator_traits<_InputIterator1>::value_type>)
5154 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5155 typename iterator_traits<_InputIterator1>::value_type,
5156 typename iterator_traits<_InputIterator2>::value_type>)
5157 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5158 typename iterator_traits<_InputIterator2>::value_type,
5159 typename iterator_traits<_InputIterator1>::value_type>)
5160 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5161 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5163 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5164 __first2, __last2, __result,
5165 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5168 template<typename _InputIterator1, typename _InputIterator2,
5169 typename _OutputIterator,
5172 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5173 _InputIterator2 __first2, _InputIterator2 __last2,
5174 _OutputIterator __result, _Compare __comp)
5176 while (__first1 != __last1 && __first2 != __last2)
5177 if (__comp(__first1, __first2))
5179 *__result = *__first1;
5183 else if (__comp(__first2, __first1))
5190 return std::copy(__first1, __last1, __result);
5194 * @brief Return the difference of two sorted ranges.
5195 * @ingroup set_algorithms
5196 * @param __first1 Start of first range.
5197 * @param __last1 End of first range.
5198 * @param __first2 Start of second range.
5199 * @param __last2 End of second range.
5200 * @return End of the output range.
5201 * @ingroup set_algorithms
5203 * This operation iterates over both ranges, copying elements present in
5204 * the first range but not the second in order to the output range.
5205 * Iterators increment for each range. When the current element of the
5206 * first range is less than the second, that element is copied and the
5207 * iterator advances. If the current element of the second range is less,
5208 * the iterator advances, but no element is copied. If an element is
5209 * contained in both ranges, no elements are copied and both ranges
5210 * advance. The output range may not overlap either input range.
5212 template<typename _InputIterator1, typename _InputIterator2,
5213 typename _OutputIterator>
5214 inline _OutputIterator
5215 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5216 _InputIterator2 __first2, _InputIterator2 __last2,
5217 _OutputIterator __result)
5219 // concept requirements
5220 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5221 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5222 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5223 typename iterator_traits<_InputIterator1>::value_type>)
5224 __glibcxx_function_requires(_LessThanOpConcept<
5225 typename iterator_traits<_InputIterator1>::value_type,
5226 typename iterator_traits<_InputIterator2>::value_type>)
5227 __glibcxx_function_requires(_LessThanOpConcept<
5228 typename iterator_traits<_InputIterator2>::value_type,
5229 typename iterator_traits<_InputIterator1>::value_type>)
5230 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5231 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5233 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5234 __first2, __last2, __result,
5235 __gnu_cxx::__ops::__iter_less_iter());
5239 * @brief Return the difference of two sorted ranges using comparison
5241 * @ingroup set_algorithms
5242 * @param __first1 Start of first range.
5243 * @param __last1 End of first range.
5244 * @param __first2 Start of second range.
5245 * @param __last2 End of second range.
5246 * @param __comp The comparison functor.
5247 * @return End of the output range.
5248 * @ingroup set_algorithms
5250 * This operation iterates over both ranges, copying elements present in
5251 * the first range but not the second in order to the output range.
5252 * Iterators increment for each range. When the current element of the
5253 * first range is less than the second according to @p __comp, that element
5254 * is copied and the iterator advances. If the current element of the
5255 * second range is less, no element is copied and the iterator advances.
5256 * If an element is contained in both ranges according to @p __comp, no
5257 * elements are copied and both ranges advance. The output range may not
5258 * overlap either input range.
5260 template<typename _InputIterator1, typename _InputIterator2,
5261 typename _OutputIterator, typename _Compare>
5262 inline _OutputIterator
5263 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5264 _InputIterator2 __first2, _InputIterator2 __last2,
5265 _OutputIterator __result, _Compare __comp)
5267 // concept requirements
5268 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5269 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5270 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5271 typename iterator_traits<_InputIterator1>::value_type>)
5272 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5273 typename iterator_traits<_InputIterator1>::value_type,
5274 typename iterator_traits<_InputIterator2>::value_type>)
5275 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5276 typename iterator_traits<_InputIterator2>::value_type,
5277 typename iterator_traits<_InputIterator1>::value_type>)
5278 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5279 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5281 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5282 __first2, __last2, __result,
5283 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5286 template<typename _InputIterator1, typename _InputIterator2,
5287 typename _OutputIterator,
5290 __set_symmetric_difference(_InputIterator1 __first1,
5291 _InputIterator1 __last1,
5292 _InputIterator2 __first2,
5293 _InputIterator2 __last2,
5294 _OutputIterator __result,
5297 while (__first1 != __last1 && __first2 != __last2)
5298 if (__comp(__first1, __first2))
5300 *__result = *__first1;
5304 else if (__comp(__first2, __first1))
5306 *__result = *__first2;
5315 return std::copy(__first2, __last2,
5316 std::copy(__first1, __last1, __result));
5320 * @brief Return the symmetric difference of two sorted ranges.
5321 * @ingroup set_algorithms
5322 * @param __first1 Start of first range.
5323 * @param __last1 End of first range.
5324 * @param __first2 Start of second range.
5325 * @param __last2 End of second range.
5326 * @return End of the output range.
5327 * @ingroup set_algorithms
5329 * This operation iterates over both ranges, copying elements present in
5330 * one range but not the other in order to the output range. Iterators
5331 * increment for each range. When the current element of one range is less
5332 * than the other, that element is copied and the iterator advances. If an
5333 * element is contained in both ranges, no elements are copied and both
5334 * ranges advance. The output range may not overlap either input range.
5336 template<typename _InputIterator1, typename _InputIterator2,
5337 typename _OutputIterator>
5338 inline _OutputIterator
5339 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5340 _InputIterator2 __first2, _InputIterator2 __last2,
5341 _OutputIterator __result)
5343 // concept requirements
5344 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5345 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5346 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5347 typename iterator_traits<_InputIterator1>::value_type>)
5348 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5349 typename iterator_traits<_InputIterator2>::value_type>)
5350 __glibcxx_function_requires(_LessThanOpConcept<
5351 typename iterator_traits<_InputIterator1>::value_type,
5352 typename iterator_traits<_InputIterator2>::value_type>)
5353 __glibcxx_function_requires(_LessThanOpConcept<
5354 typename iterator_traits<_InputIterator2>::value_type,
5355 typename iterator_traits<_InputIterator1>::value_type>)
5356 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5357 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5359 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5360 __first2, __last2, __result,
5361 __gnu_cxx::__ops::__iter_less_iter());
5365 * @brief Return the symmetric difference of two sorted ranges using
5366 * comparison functor.
5367 * @ingroup set_algorithms
5368 * @param __first1 Start of first range.
5369 * @param __last1 End of first range.
5370 * @param __first2 Start of second range.
5371 * @param __last2 End of second range.
5372 * @param __comp The comparison functor.
5373 * @return End of the output range.
5374 * @ingroup set_algorithms
5376 * This operation iterates over both ranges, copying elements present in
5377 * one range but not the other in order to the output range. Iterators
5378 * increment for each range. When the current element of one range is less
5379 * than the other according to @p comp, that element is copied and the
5380 * iterator advances. If an element is contained in both ranges according
5381 * to @p __comp, no elements are copied and both ranges advance. The output
5382 * range may not overlap either input range.
5384 template<typename _InputIterator1, typename _InputIterator2,
5385 typename _OutputIterator, typename _Compare>
5386 inline _OutputIterator
5387 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5388 _InputIterator2 __first2, _InputIterator2 __last2,
5389 _OutputIterator __result,
5392 // concept requirements
5393 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5394 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5395 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5396 typename iterator_traits<_InputIterator1>::value_type>)
5397 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5398 typename iterator_traits<_InputIterator2>::value_type>)
5399 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5400 typename iterator_traits<_InputIterator1>::value_type,
5401 typename iterator_traits<_InputIterator2>::value_type>)
5402 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5403 typename iterator_traits<_InputIterator2>::value_type,
5404 typename iterator_traits<_InputIterator1>::value_type>)
5405 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5406 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5408 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5409 __first2, __last2, __result,
5410 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5413 template<typename _ForwardIterator, typename _Compare>
5414 _GLIBCXX14_CONSTEXPR
5416 __min_element(_ForwardIterator __first, _ForwardIterator __last,
5419 if (__first == __last)
5421 _ForwardIterator __result = __first;
5422 while (++__first != __last)
5423 if (__comp(__first, __result))
5429 * @brief Return the minimum element in a range.
5430 * @ingroup sorting_algorithms
5431 * @param __first Start of range.
5432 * @param __last End of range.
5433 * @return Iterator referencing the first instance of the smallest value.
5435 template<typename _ForwardIterator>
5436 _GLIBCXX14_CONSTEXPR
5438 inline min_element(_ForwardIterator __first, _ForwardIterator __last)
5440 // concept requirements
5441 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5442 __glibcxx_function_requires(_LessThanComparableConcept<
5443 typename iterator_traits<_ForwardIterator>::value_type>)
5444 __glibcxx_requires_valid_range(__first, __last);
5446 return _GLIBCXX_STD_A::__min_element(__first, __last,
5447 __gnu_cxx::__ops::__iter_less_iter());
5451 * @brief Return the minimum element in a range using comparison functor.
5452 * @ingroup sorting_algorithms
5453 * @param __first Start of range.
5454 * @param __last End of range.
5455 * @param __comp Comparison functor.
5456 * @return Iterator referencing the first instance of the smallest value
5457 * according to __comp.
5459 template<typename _ForwardIterator, typename _Compare>
5460 _GLIBCXX14_CONSTEXPR
5461 inline _ForwardIterator
5462 min_element(_ForwardIterator __first, _ForwardIterator __last,
5465 // concept requirements
5466 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5467 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5468 typename iterator_traits<_ForwardIterator>::value_type,
5469 typename iterator_traits<_ForwardIterator>::value_type>)
5470 __glibcxx_requires_valid_range(__first, __last);
5472 return _GLIBCXX_STD_A::__min_element(__first, __last,
5473 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5476 template<typename _ForwardIterator, typename _Compare>
5477 _GLIBCXX14_CONSTEXPR
5479 __max_element(_ForwardIterator __first, _ForwardIterator __last,
5482 if (__first == __last) return __first;
5483 _ForwardIterator __result = __first;
5484 while (++__first != __last)
5485 if (__comp(__result, __first))
5491 * @brief Return the maximum element in a range.
5492 * @ingroup sorting_algorithms
5493 * @param __first Start of range.
5494 * @param __last End of range.
5495 * @return Iterator referencing the first instance of the largest value.
5497 template<typename _ForwardIterator>
5498 _GLIBCXX14_CONSTEXPR
5499 inline _ForwardIterator
5500 max_element(_ForwardIterator __first, _ForwardIterator __last)
5502 // concept requirements
5503 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5504 __glibcxx_function_requires(_LessThanComparableConcept<
5505 typename iterator_traits<_ForwardIterator>::value_type>)
5506 __glibcxx_requires_valid_range(__first, __last);
5508 return _GLIBCXX_STD_A::__max_element(__first, __last,
5509 __gnu_cxx::__ops::__iter_less_iter());
5513 * @brief Return the maximum element in a range using comparison functor.
5514 * @ingroup sorting_algorithms
5515 * @param __first Start of range.
5516 * @param __last End of range.
5517 * @param __comp Comparison functor.
5518 * @return Iterator referencing the first instance of the largest value
5519 * according to __comp.
5521 template<typename _ForwardIterator, typename _Compare>
5522 _GLIBCXX14_CONSTEXPR
5523 inline _ForwardIterator
5524 max_element(_ForwardIterator __first, _ForwardIterator __last,
5527 // concept requirements
5528 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5529 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5530 typename iterator_traits<_ForwardIterator>::value_type,
5531 typename iterator_traits<_ForwardIterator>::value_type>)
5532 __glibcxx_requires_valid_range(__first, __last);
5534 return _GLIBCXX_STD_A::__max_element(__first, __last,
5535 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5538 _GLIBCXX_END_NAMESPACE_ALGO
5541 #endif /* _STL_ALGO_H */