1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 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 *__a
76 template<typename _Iterator
, typename _Compare
>
78 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
84 std::iter_swap(__a
, __b
);
85 else if (__comp(__a
, __c
))
86 std::iter_swap(__a
, __c
);
88 else if (__comp(__a
, __c
))
90 else if (__comp(__b
, __c
))
91 std::iter_swap(__a
, __c
);
93 std::iter_swap(__a
, __b
);
96 /// This is an overload used by find algos for the Input Iterator case.
97 template<typename _InputIterator
, typename _Predicate
>
99 __find_if(_InputIterator __first
, _InputIterator __last
,
100 _Predicate __pred
, input_iterator_tag
)
102 while (__first
!= __last
&& !__pred(__first
))
107 /// This is an overload used by find algos for the RAI case.
108 template<typename _RandomAccessIterator
, typename _Predicate
>
109 _RandomAccessIterator
110 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
111 _Predicate __pred
, random_access_iterator_tag
)
113 typename iterator_traits
<_RandomAccessIterator
>::difference_type
114 __trip_count
= (__last
- __first
) >> 2;
116 for (; __trip_count
> 0; --__trip_count
)
135 switch (__last
- __first
)
155 template<typename _Iterator
, typename _Predicate
>
157 __find_if(_Iterator __first
, _Iterator __last
, _Predicate __pred
)
159 return __find_if(__first
, __last
, __pred
,
160 std::__iterator_category(__first
));
163 /// Provided for stable_partition to use.
164 template<typename _InputIterator
, typename _Predicate
>
165 inline _InputIterator
166 __find_if_not(_InputIterator __first
, _InputIterator __last
,
169 return std::__find_if(__first
, __last
,
170 __gnu_cxx::__ops::__negate(__pred
),
171 std::__iterator_category(__first
));
174 /// Like find_if_not(), but uses and updates a count of the
175 /// remaining range length instead of comparing against an end
177 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
179 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
181 for (; __len
; --__len
, ++__first
)
182 if (!__pred(__first
))
189 // set_symmetric_difference
200 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
201 typename _BinaryPredicate
>
203 __search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
204 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
205 _BinaryPredicate __predicate
)
207 // Test for empty ranges
208 if (__first1
== __last1
|| __first2
== __last2
)
211 // Test for a pattern of length 1.
212 _ForwardIterator2
__p1(__first2
);
213 if (++__p1
== __last2
)
214 return std::__find_if(__first1
, __last1
,
215 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
218 _ForwardIterator2 __p
;
219 _ForwardIterator1 __current
= __first1
;
224 std::__find_if(__first1
, __last1
,
225 __gnu_cxx::__ops::__iter_comp_iter(__predicate
, __first2
));
227 if (__first1
== __last1
)
231 __current
= __first1
;
232 if (++__current
== __last1
)
235 while (__predicate(__current
, __p
))
237 if (++__p
== __last2
)
239 if (++__current
== __last1
)
250 * This is an helper function for search_n overloaded for forward iterators.
252 template<typename _ForwardIterator
, typename _Integer
,
253 typename _UnaryPredicate
>
255 __search_n_aux(_ForwardIterator __first
, _ForwardIterator __last
,
256 _Integer __count
, _UnaryPredicate __unary_pred
,
257 std::forward_iterator_tag
)
259 __first
= std::__find_if(__first
, __last
, __unary_pred
);
260 while (__first
!= __last
)
262 typename iterator_traits
<_ForwardIterator
>::difference_type
264 _ForwardIterator __i
= __first
;
266 while (__i
!= __last
&& __n
!= 1 && __unary_pred(__i
))
275 __first
= std::__find_if(++__i
, __last
, __unary_pred
);
281 * This is an helper function for search_n overloaded for random access
284 template<typename _RandomAccessIter
, typename _Integer
,
285 typename _UnaryPredicate
>
287 __search_n_aux(_RandomAccessIter __first
, _RandomAccessIter __last
,
288 _Integer __count
, _UnaryPredicate __unary_pred
,
289 std::random_access_iterator_tag
)
291 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
294 _DistanceType __tailSize
= __last
- __first
;
295 _DistanceType __remainder
= __count
;
297 while (__remainder
<= __tailSize
) // the main loop...
299 __first
+= __remainder
;
300 __tailSize
-= __remainder
;
301 // __first here is always pointing to one past the last element of
302 // next possible match.
303 _RandomAccessIter __backTrack
= __first
;
304 while (__unary_pred(--__backTrack
))
306 if (--__remainder
== 0)
307 return (__first
- __count
); // Success
309 __remainder
= __count
+ 1 - (__first
- __backTrack
);
311 return __last
; // Failure
314 template<typename _ForwardIterator
, typename _Integer
,
315 typename _UnaryPredicate
>
317 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
319 _UnaryPredicate __unary_pred
)
325 return std::__find_if(__first
, __last
, __unary_pred
);
327 return std::__search_n_aux(__first
, __last
, __count
, __unary_pred
,
328 std::__iterator_category(__first
));
331 // find_end for forward iterators.
332 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
333 typename _BinaryPredicate
>
335 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
336 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
337 forward_iterator_tag
, forward_iterator_tag
,
338 _BinaryPredicate __comp
)
340 if (__first2
== __last2
)
343 _ForwardIterator1 __result
= __last1
;
346 _ForwardIterator1 __new_result
347 = std::__search(__first1
, __last1
, __first2
, __last2
, __comp
);
348 if (__new_result
== __last1
)
352 __result
= __new_result
;
353 __first1
= __new_result
;
359 // find_end for bidirectional iterators (much faster).
360 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
361 typename _BinaryPredicate
>
362 _BidirectionalIterator1
363 __find_end(_BidirectionalIterator1 __first1
,
364 _BidirectionalIterator1 __last1
,
365 _BidirectionalIterator2 __first2
,
366 _BidirectionalIterator2 __last2
,
367 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
368 _BinaryPredicate __comp
)
370 // concept requirements
371 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
372 _BidirectionalIterator1
>)
373 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
374 _BidirectionalIterator2
>)
376 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
377 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
379 _RevIterator1
__rlast1(__first1
);
380 _RevIterator2
__rlast2(__first2
);
381 _RevIterator1 __rresult
= std::__search(_RevIterator1(__last1
), __rlast1
,
382 _RevIterator2(__last2
), __rlast2
,
385 if (__rresult
== __rlast1
)
389 _BidirectionalIterator1 __result
= __rresult
.base();
390 std::advance(__result
, -std::distance(__first2
, __last2
));
396 * @brief Find last matching subsequence in a sequence.
397 * @ingroup non_mutating_algorithms
398 * @param __first1 Start of range to search.
399 * @param __last1 End of range to search.
400 * @param __first2 Start of sequence to match.
401 * @param __last2 End of sequence to match.
402 * @return The last iterator @c i in the range
403 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
404 * @p *(__first2+N) for each @c N in the range @p
405 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
407 * Searches the range @p [__first1,__last1) for a sub-sequence that
408 * compares equal value-by-value with the sequence given by @p
409 * [__first2,__last2) and returns an iterator to the __first
410 * element of the sub-sequence, or @p __last1 if the sub-sequence
411 * is not found. The sub-sequence will be the last such
412 * subsequence contained in [__first1,__last1).
414 * Because the sub-sequence must lie completely within the range @p
415 * [__first1,__last1) it must start at a position less than @p
416 * __last1-(__last2-__first2) where @p __last2-__first2 is the
417 * length of the sub-sequence. This means that the returned
418 * iterator @c i will be in the range @p
419 * [__first1,__last1-(__last2-__first2))
421 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
422 inline _ForwardIterator1
423 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
424 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
426 // concept requirements
427 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
428 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
429 __glibcxx_function_requires(_EqualOpConcept
<
430 typename iterator_traits
<_ForwardIterator1
>::value_type
,
431 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
432 __glibcxx_requires_valid_range(__first1
, __last1
);
433 __glibcxx_requires_valid_range(__first2
, __last2
);
435 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
436 std::__iterator_category(__first1
),
437 std::__iterator_category(__first2
),
438 __gnu_cxx::__ops::__iter_equal_to_iter());
442 * @brief Find last matching subsequence in a sequence using a predicate.
443 * @ingroup non_mutating_algorithms
444 * @param __first1 Start of range to search.
445 * @param __last1 End of range to search.
446 * @param __first2 Start of sequence to match.
447 * @param __last2 End of sequence to match.
448 * @param __comp The predicate to use.
449 * @return The last iterator @c i in the range @p
450 * [__first1,__last1-(__last2-__first2)) such that @c
451 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
452 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
455 * Searches the range @p [__first1,__last1) for a sub-sequence that
456 * compares equal value-by-value with the sequence given by @p
457 * [__first2,__last2) using comp as a predicate and returns an
458 * iterator to the first element of the sub-sequence, or @p __last1
459 * if the sub-sequence is not found. The sub-sequence will be the
460 * last such subsequence contained in [__first,__last1).
462 * Because the sub-sequence must lie completely within the range @p
463 * [__first1,__last1) it must start at a position less than @p
464 * __last1-(__last2-__first2) where @p __last2-__first2 is the
465 * length of the sub-sequence. This means that the returned
466 * iterator @c i will be in the range @p
467 * [__first1,__last1-(__last2-__first2))
469 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
470 typename _BinaryPredicate
>
471 inline _ForwardIterator1
472 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
473 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
474 _BinaryPredicate __comp
)
476 // concept requirements
477 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
478 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
479 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
480 typename iterator_traits
<_ForwardIterator1
>::value_type
,
481 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
482 __glibcxx_requires_valid_range(__first1
, __last1
);
483 __glibcxx_requires_valid_range(__first2
, __last2
);
485 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
486 std::__iterator_category(__first1
),
487 std::__iterator_category(__first2
),
488 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
491 #if __cplusplus >= 201103L
493 * @brief Checks that a predicate is true for all the elements
495 * @ingroup non_mutating_algorithms
496 * @param __first An input iterator.
497 * @param __last An input iterator.
498 * @param __pred A predicate.
499 * @return True if the check is true, false otherwise.
501 * Returns true if @p __pred is true for each element in the range
502 * @p [__first,__last), and false otherwise.
504 template<typename _InputIterator
, typename _Predicate
>
506 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
507 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
510 * @brief Checks that a predicate is false for all the elements
512 * @ingroup non_mutating_algorithms
513 * @param __first An input iterator.
514 * @param __last An input iterator.
515 * @param __pred A predicate.
516 * @return True if the check is true, false otherwise.
518 * Returns true if @p __pred is false for each element in the range
519 * @p [__first,__last), and false otherwise.
521 template<typename _InputIterator
, typename _Predicate
>
523 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
524 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
527 * @brief Checks that a predicate is false for at least an element
529 * @ingroup non_mutating_algorithms
530 * @param __first An input iterator.
531 * @param __last An input iterator.
532 * @param __pred A predicate.
533 * @return True if the check is true, false otherwise.
535 * Returns true if an element exists in the range @p
536 * [__first,__last) such that @p __pred is true, and false
539 template<typename _InputIterator
, typename _Predicate
>
541 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
542 { return !std::none_of(__first
, __last
, __pred
); }
545 * @brief Find the first element in a sequence for which a
546 * predicate is false.
547 * @ingroup non_mutating_algorithms
548 * @param __first An input iterator.
549 * @param __last An input iterator.
550 * @param __pred A predicate.
551 * @return The first iterator @c i in the range @p [__first,__last)
552 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
554 template<typename _InputIterator
, typename _Predicate
>
555 inline _InputIterator
556 find_if_not(_InputIterator __first
, _InputIterator __last
,
559 // concept requirements
560 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
561 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
562 typename iterator_traits
<_InputIterator
>::value_type
>)
563 __glibcxx_requires_valid_range(__first
, __last
);
564 return std::__find_if_not(__first
, __last
,
565 __gnu_cxx::__ops::__pred_iter(__pred
));
569 * @brief Checks whether the sequence is partitioned.
570 * @ingroup mutating_algorithms
571 * @param __first An input iterator.
572 * @param __last An input iterator.
573 * @param __pred A predicate.
574 * @return True if the range @p [__first,__last) is partioned by @p __pred,
575 * i.e. if all elements that satisfy @p __pred appear before those that
578 template<typename _InputIterator
, typename _Predicate
>
580 is_partitioned(_InputIterator __first
, _InputIterator __last
,
583 __first
= std::find_if_not(__first
, __last
, __pred
);
584 return std::none_of(__first
, __last
, __pred
);
588 * @brief Find the partition point of a partitioned range.
589 * @ingroup mutating_algorithms
590 * @param __first An iterator.
591 * @param __last Another iterator.
592 * @param __pred A predicate.
593 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
594 * and @p none_of(mid, __last, __pred) are both true.
596 template<typename _ForwardIterator
, typename _Predicate
>
598 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
601 // concept requirements
602 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
603 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
604 typename iterator_traits
<_ForwardIterator
>::value_type
>)
606 // A specific debug-mode test will be necessary...
607 __glibcxx_requires_valid_range(__first
, __last
);
609 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
612 _DistanceType __len
= std::distance(__first
, __last
);
613 _DistanceType __half
;
614 _ForwardIterator __middle
;
620 std::advance(__middle
, __half
);
621 if (__pred(*__middle
))
625 __len
= __len
- __half
- 1;
634 template<typename _InputIterator
, typename _OutputIterator
,
637 __remove_copy_if(_InputIterator __first
, _InputIterator __last
,
638 _OutputIterator __result
, _Predicate __pred
)
640 for (; __first
!= __last
; ++__first
)
641 if (!__pred(__first
))
643 *__result
= *__first
;
650 * @brief Copy a sequence, removing elements of a given value.
651 * @ingroup mutating_algorithms
652 * @param __first An input iterator.
653 * @param __last An input iterator.
654 * @param __result An output iterator.
655 * @param __value The value to be removed.
656 * @return An iterator designating the end of the resulting sequence.
658 * Copies each element in the range @p [__first,__last) not equal
659 * to @p __value to the range beginning at @p __result.
660 * remove_copy() is stable, so the relative order of elements that
661 * are copied is unchanged.
663 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
664 inline _OutputIterator
665 remove_copy(_InputIterator __first
, _InputIterator __last
,
666 _OutputIterator __result
, const _Tp
& __value
)
668 // concept requirements
669 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
670 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
671 typename iterator_traits
<_InputIterator
>::value_type
>)
672 __glibcxx_function_requires(_EqualOpConcept
<
673 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
674 __glibcxx_requires_valid_range(__first
, __last
);
676 return std::__remove_copy_if(__first
, __last
, __result
,
677 __gnu_cxx::__ops::__iter_equals_val(__value
));
681 * @brief Copy a sequence, removing elements for which a predicate is true.
682 * @ingroup mutating_algorithms
683 * @param __first An input iterator.
684 * @param __last An input iterator.
685 * @param __result An output iterator.
686 * @param __pred A predicate.
687 * @return An iterator designating the end of the resulting sequence.
689 * Copies each element in the range @p [__first,__last) for which
690 * @p __pred returns false to the range beginning at @p __result.
692 * remove_copy_if() is stable, so the relative order of elements that are
693 * copied is unchanged.
695 template<typename _InputIterator
, typename _OutputIterator
,
697 inline _OutputIterator
698 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
699 _OutputIterator __result
, _Predicate __pred
)
701 // concept requirements
702 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
703 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
704 typename iterator_traits
<_InputIterator
>::value_type
>)
705 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
706 typename iterator_traits
<_InputIterator
>::value_type
>)
707 __glibcxx_requires_valid_range(__first
, __last
);
709 return std::__remove_copy_if(__first
, __last
, __result
,
710 __gnu_cxx::__ops::__pred_iter(__pred
));
713 #if __cplusplus >= 201103L
715 * @brief Copy the elements of a sequence for which a predicate is true.
716 * @ingroup mutating_algorithms
717 * @param __first An input iterator.
718 * @param __last An input iterator.
719 * @param __result An output iterator.
720 * @param __pred A predicate.
721 * @return An iterator designating the end of the resulting sequence.
723 * Copies each element in the range @p [__first,__last) for which
724 * @p __pred returns true to the range beginning at @p __result.
726 * copy_if() is stable, so the relative order of elements that are
727 * copied is unchanged.
729 template<typename _InputIterator
, typename _OutputIterator
,
732 copy_if(_InputIterator __first
, _InputIterator __last
,
733 _OutputIterator __result
, _Predicate __pred
)
735 // concept requirements
736 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
737 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
738 typename iterator_traits
<_InputIterator
>::value_type
>)
739 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
740 typename iterator_traits
<_InputIterator
>::value_type
>)
741 __glibcxx_requires_valid_range(__first
, __last
);
743 for (; __first
!= __last
; ++__first
)
744 if (__pred(*__first
))
746 *__result
= *__first
;
752 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
754 __copy_n(_InputIterator __first
, _Size __n
,
755 _OutputIterator __result
, input_iterator_tag
)
761 *__result
= *__first
;
772 template<typename _RandomAccessIterator
, typename _Size
,
773 typename _OutputIterator
>
774 inline _OutputIterator
775 __copy_n(_RandomAccessIterator __first
, _Size __n
,
776 _OutputIterator __result
, random_access_iterator_tag
)
777 { return std::copy(__first
, __first
+ __n
, __result
); }
780 * @brief Copies the range [first,first+n) into [result,result+n).
781 * @ingroup mutating_algorithms
782 * @param __first An input iterator.
783 * @param __n The number of elements to copy.
784 * @param __result An output iterator.
787 * This inline function will boil down to a call to @c memmove whenever
788 * possible. Failing that, if random access iterators are passed, then the
789 * loop count will be known (and therefore a candidate for compiler
790 * optimizations such as unrolling).
792 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
793 inline _OutputIterator
794 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
796 // concept requirements
797 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
798 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
799 typename iterator_traits
<_InputIterator
>::value_type
>)
801 return std::__copy_n(__first
, __n
, __result
,
802 std::__iterator_category(__first
));
806 * @brief Copy the elements of a sequence to separate output sequences
807 * depending on the truth value of a predicate.
808 * @ingroup mutating_algorithms
809 * @param __first An input iterator.
810 * @param __last An input iterator.
811 * @param __out_true An output iterator.
812 * @param __out_false An output iterator.
813 * @param __pred A predicate.
814 * @return A pair designating the ends of the resulting sequences.
816 * Copies each element in the range @p [__first,__last) for which
817 * @p __pred returns true to the range beginning at @p out_true
818 * and each element for which @p __pred returns false to @p __out_false.
820 template<typename _InputIterator
, typename _OutputIterator1
,
821 typename _OutputIterator2
, typename _Predicate
>
822 pair
<_OutputIterator1
, _OutputIterator2
>
823 partition_copy(_InputIterator __first
, _InputIterator __last
,
824 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
827 // concept requirements
828 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
829 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
830 typename iterator_traits
<_InputIterator
>::value_type
>)
831 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
832 typename iterator_traits
<_InputIterator
>::value_type
>)
833 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
834 typename iterator_traits
<_InputIterator
>::value_type
>)
835 __glibcxx_requires_valid_range(__first
, __last
);
837 for (; __first
!= __last
; ++__first
)
838 if (__pred(*__first
))
840 *__out_true
= *__first
;
845 *__out_false
= *__first
;
849 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
853 template<typename _ForwardIterator
, typename _Predicate
>
855 __remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
858 __first
= std::__find_if(__first
, __last
, __pred
);
859 if (__first
== __last
)
861 _ForwardIterator __result
= __first
;
863 for (; __first
!= __last
; ++__first
)
864 if (!__pred(__first
))
866 *__result
= _GLIBCXX_MOVE(*__first
);
873 * @brief Remove elements from a sequence.
874 * @ingroup mutating_algorithms
875 * @param __first An input iterator.
876 * @param __last An input iterator.
877 * @param __value The value to be removed.
878 * @return An iterator designating the end of the resulting sequence.
880 * All elements equal to @p __value are removed from the range
881 * @p [__first,__last).
883 * remove() is stable, so the relative order of elements that are
884 * not removed is unchanged.
886 * Elements between the end of the resulting sequence and @p __last
887 * are still present, but their value is unspecified.
889 template<typename _ForwardIterator
, typename _Tp
>
890 inline _ForwardIterator
891 remove(_ForwardIterator __first
, _ForwardIterator __last
,
894 // concept requirements
895 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
897 __glibcxx_function_requires(_EqualOpConcept
<
898 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
899 __glibcxx_requires_valid_range(__first
, __last
);
901 return std::__remove_if(__first
, __last
,
902 __gnu_cxx::__ops::__iter_equals_val(__value
));
906 * @brief Remove elements from a sequence using a predicate.
907 * @ingroup mutating_algorithms
908 * @param __first A forward iterator.
909 * @param __last A forward iterator.
910 * @param __pred A predicate.
911 * @return An iterator designating the end of the resulting sequence.
913 * All elements for which @p __pred returns true are removed from the range
914 * @p [__first,__last).
916 * remove_if() is stable, so the relative order of elements that are
917 * not removed is unchanged.
919 * Elements between the end of the resulting sequence and @p __last
920 * are still present, but their value is unspecified.
922 template<typename _ForwardIterator
, typename _Predicate
>
923 inline _ForwardIterator
924 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
927 // concept requirements
928 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
930 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
931 typename iterator_traits
<_ForwardIterator
>::value_type
>)
932 __glibcxx_requires_valid_range(__first
, __last
);
934 return std::__remove_if(__first
, __last
,
935 __gnu_cxx::__ops::__pred_iter(__pred
));
938 template<typename _ForwardIterator
, typename _BinaryPredicate
>
940 __adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
941 _BinaryPredicate __binary_pred
)
943 if (__first
== __last
)
945 _ForwardIterator __next
= __first
;
946 while (++__next
!= __last
)
948 if (__binary_pred(__first
, __next
))
955 template<typename _ForwardIterator
, typename _BinaryPredicate
>
957 __unique(_ForwardIterator __first
, _ForwardIterator __last
,
958 _BinaryPredicate __binary_pred
)
960 // Skip the beginning, if already unique.
961 __first
= std::__adjacent_find(__first
, __last
, __binary_pred
);
962 if (__first
== __last
)
965 // Do the real copy work.
966 _ForwardIterator __dest
= __first
;
968 while (++__first
!= __last
)
969 if (!__binary_pred(__dest
, __first
))
970 *++__dest
= _GLIBCXX_MOVE(*__first
);
975 * @brief Remove consecutive duplicate values from a sequence.
976 * @ingroup mutating_algorithms
977 * @param __first A forward iterator.
978 * @param __last A forward iterator.
979 * @return An iterator designating the end of the resulting sequence.
981 * Removes all but the first element from each group of consecutive
982 * values that compare equal.
983 * unique() is stable, so the relative order of elements that are
984 * not removed is unchanged.
985 * Elements between the end of the resulting sequence and @p __last
986 * are still present, but their value is unspecified.
988 template<typename _ForwardIterator
>
989 inline _ForwardIterator
990 unique(_ForwardIterator __first
, _ForwardIterator __last
)
992 // concept requirements
993 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
995 __glibcxx_function_requires(_EqualityComparableConcept
<
996 typename iterator_traits
<_ForwardIterator
>::value_type
>)
997 __glibcxx_requires_valid_range(__first
, __last
);
999 return std::__unique(__first
, __last
,
1000 __gnu_cxx::__ops::__iter_equal_to_iter());
1004 * @brief Remove consecutive values from a sequence using a predicate.
1005 * @ingroup mutating_algorithms
1006 * @param __first A forward iterator.
1007 * @param __last A forward iterator.
1008 * @param __binary_pred A binary predicate.
1009 * @return An iterator designating the end of the resulting sequence.
1011 * Removes all but the first element from each group of consecutive
1012 * values for which @p __binary_pred returns true.
1013 * unique() is stable, so the relative order of elements that are
1014 * not removed is unchanged.
1015 * Elements between the end of the resulting sequence and @p __last
1016 * are still present, but their value is unspecified.
1018 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1019 inline _ForwardIterator
1020 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1021 _BinaryPredicate __binary_pred
)
1023 // concept requirements
1024 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1026 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1027 typename iterator_traits
<_ForwardIterator
>::value_type
,
1028 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1029 __glibcxx_requires_valid_range(__first
, __last
);
1031 return std::__unique(__first
, __last
,
1032 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
1036 * This is an uglified
1037 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1039 * overloaded for forward iterators and output iterator as result.
1041 template<typename _ForwardIterator
, typename _OutputIterator
,
1042 typename _BinaryPredicate
>
1044 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1045 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1046 forward_iterator_tag
, output_iterator_tag
)
1048 // concept requirements -- iterators already checked
1049 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1050 typename iterator_traits
<_ForwardIterator
>::value_type
,
1051 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1053 _ForwardIterator __next
= __first
;
1054 *__result
= *__first
;
1055 while (++__next
!= __last
)
1056 if (!__binary_pred(__first
, __next
))
1059 *++__result
= *__first
;
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1068 * overloaded for input iterators and output iterator as result.
1070 template<typename _InputIterator
, typename _OutputIterator
,
1071 typename _BinaryPredicate
>
1073 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1074 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1075 input_iterator_tag
, output_iterator_tag
)
1077 // concept requirements -- iterators already checked
1078 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1079 typename iterator_traits
<_InputIterator
>::value_type
,
1080 typename iterator_traits
<_InputIterator
>::value_type
>)
1082 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1083 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred
))
1085 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred
);
1086 *__result
= __value
;
1087 while (++__first
!= __last
)
1088 if (!__rebound_pred(__first
, __value
))
1091 *++__result
= __value
;
1097 * This is an uglified
1098 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1100 * overloaded for input iterators and forward iterator as result.
1102 template<typename _InputIterator
, typename _ForwardIterator
,
1103 typename _BinaryPredicate
>
1105 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1106 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1107 input_iterator_tag
, forward_iterator_tag
)
1109 // concept requirements -- iterators already checked
1110 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1111 typename iterator_traits
<_ForwardIterator
>::value_type
,
1112 typename iterator_traits
<_InputIterator
>::value_type
>)
1113 *__result
= *__first
;
1114 while (++__first
!= __last
)
1115 if (!__binary_pred(__result
, __first
))
1116 *++__result
= *__first
;
1121 * This is an uglified reverse(_BidirectionalIterator,
1122 * _BidirectionalIterator)
1123 * overloaded for bidirectional iterators.
1125 template<typename _BidirectionalIterator
>
1127 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1128 bidirectional_iterator_tag
)
1131 if (__first
== __last
|| __first
== --__last
)
1135 std::iter_swap(__first
, __last
);
1141 * This is an uglified reverse(_BidirectionalIterator,
1142 * _BidirectionalIterator)
1143 * overloaded for random access iterators.
1145 template<typename _RandomAccessIterator
>
1147 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1148 random_access_iterator_tag
)
1150 if (__first
== __last
)
1153 while (__first
< __last
)
1155 std::iter_swap(__first
, __last
);
1162 * @brief Reverse a sequence.
1163 * @ingroup mutating_algorithms
1164 * @param __first A bidirectional iterator.
1165 * @param __last A bidirectional iterator.
1166 * @return reverse() returns no value.
1168 * Reverses the order of the elements in the range @p [__first,__last),
1169 * so that the first element becomes the last etc.
1170 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1171 * swaps @p *(__first+i) and @p *(__last-(i+1))
1173 template<typename _BidirectionalIterator
>
1175 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1177 // concept requirements
1178 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1179 _BidirectionalIterator
>)
1180 __glibcxx_requires_valid_range(__first
, __last
);
1181 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1185 * @brief Copy a sequence, reversing its elements.
1186 * @ingroup mutating_algorithms
1187 * @param __first A bidirectional iterator.
1188 * @param __last A bidirectional iterator.
1189 * @param __result An output iterator.
1190 * @return An iterator designating the end of the resulting sequence.
1192 * Copies the elements in the range @p [__first,__last) to the
1193 * range @p [__result,__result+(__last-__first)) such that the
1194 * order of the elements is reversed. For every @c i such that @p
1195 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1196 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1197 * The ranges @p [__first,__last) and @p
1198 * [__result,__result+(__last-__first)) must not overlap.
1200 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1202 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1203 _OutputIterator __result
)
1205 // concept requirements
1206 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1207 _BidirectionalIterator
>)
1208 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1209 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1210 __glibcxx_requires_valid_range(__first
, __last
);
1212 while (__first
!= __last
)
1215 *__result
= *__last
;
1222 * This is a helper function for the rotate algorithm specialized on RAIs.
1223 * It returns the greatest common divisor of two integer values.
1225 template<typename _EuclideanRingElement
>
1226 _EuclideanRingElement
1227 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1231 _EuclideanRingElement __t
= __m
% __n
;
1238 /// This is a helper function for the rotate algorithm.
1239 template<typename _ForwardIterator
>
1241 __rotate(_ForwardIterator __first
,
1242 _ForwardIterator __middle
,
1243 _ForwardIterator __last
,
1244 forward_iterator_tag
)
1246 if (__first
== __middle
|| __last
== __middle
)
1249 _ForwardIterator __first2
= __middle
;
1252 std::iter_swap(__first
, __first2
);
1255 if (__first
== __middle
)
1256 __middle
= __first2
;
1258 while (__first2
!= __last
);
1260 __first2
= __middle
;
1262 while (__first2
!= __last
)
1264 std::iter_swap(__first
, __first2
);
1267 if (__first
== __middle
)
1268 __middle
= __first2
;
1269 else if (__first2
== __last
)
1270 __first2
= __middle
;
1274 /// This is a helper function for the rotate algorithm.
1275 template<typename _BidirectionalIterator
>
1277 __rotate(_BidirectionalIterator __first
,
1278 _BidirectionalIterator __middle
,
1279 _BidirectionalIterator __last
,
1280 bidirectional_iterator_tag
)
1282 // concept requirements
1283 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1284 _BidirectionalIterator
>)
1286 if (__first
== __middle
|| __last
== __middle
)
1289 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1290 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1292 while (__first
!= __middle
&& __middle
!= __last
)
1294 std::iter_swap(__first
, --__last
);
1298 if (__first
== __middle
)
1299 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1301 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1304 /// This is a helper function for the rotate algorithm.
1305 template<typename _RandomAccessIterator
>
1307 __rotate(_RandomAccessIterator __first
,
1308 _RandomAccessIterator __middle
,
1309 _RandomAccessIterator __last
,
1310 random_access_iterator_tag
)
1312 // concept requirements
1313 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1314 _RandomAccessIterator
>)
1316 if (__first
== __middle
|| __last
== __middle
)
1319 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1321 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1324 _Distance __n
= __last
- __first
;
1325 _Distance __k
= __middle
- __first
;
1327 if (__k
== __n
- __k
)
1329 std::swap_ranges(__first
, __middle
, __middle
);
1333 _RandomAccessIterator __p
= __first
;
1337 if (__k
< __n
- __k
)
1339 if (__is_pod(_ValueType
) && __k
== 1)
1341 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1342 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1343 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1346 _RandomAccessIterator __q
= __p
+ __k
;
1347 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1349 std::iter_swap(__p
, __q
);
1356 std::swap(__n
, __k
);
1362 if (__is_pod(_ValueType
) && __k
== 1)
1364 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1365 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1366 *__p
= _GLIBCXX_MOVE(__t
);
1369 _RandomAccessIterator __q
= __p
+ __n
;
1371 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1375 std::iter_swap(__p
, __q
);
1380 std::swap(__n
, __k
);
1386 * @brief Rotate the elements of a sequence.
1387 * @ingroup mutating_algorithms
1388 * @param __first A forward iterator.
1389 * @param __middle A forward iterator.
1390 * @param __last A forward iterator.
1393 * Rotates the elements of the range @p [__first,__last) by
1394 * @p (__middle - __first) positions so that the element at @p __middle
1395 * is moved to @p __first, the element at @p __middle+1 is moved to
1396 * @p __first+1 and so on for each element in the range
1397 * @p [__first,__last).
1399 * This effectively swaps the ranges @p [__first,__middle) and
1400 * @p [__middle,__last).
1403 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1404 * for each @p n in the range @p [0,__last-__first).
1406 template<typename _ForwardIterator
>
1408 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1409 _ForwardIterator __last
)
1411 // concept requirements
1412 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1414 __glibcxx_requires_valid_range(__first
, __middle
);
1415 __glibcxx_requires_valid_range(__middle
, __last
);
1417 std::__rotate(__first
, __middle
, __last
,
1418 std::__iterator_category(__first
));
1422 * @brief Copy a sequence, rotating its elements.
1423 * @ingroup mutating_algorithms
1424 * @param __first A forward iterator.
1425 * @param __middle A forward iterator.
1426 * @param __last A forward iterator.
1427 * @param __result An output iterator.
1428 * @return An iterator designating the end of the resulting sequence.
1430 * Copies the elements of the range @p [__first,__last) to the
1431 * range beginning at @result, rotating the copied elements by
1432 * @p (__middle-__first) positions so that the element at @p __middle
1433 * is moved to @p __result, the element at @p __middle+1 is moved
1434 * to @p __result+1 and so on for each element in the range @p
1438 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1439 * for each @p n in the range @p [0,__last-__first).
1441 template<typename _ForwardIterator
, typename _OutputIterator
>
1442 inline _OutputIterator
1443 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1444 _ForwardIterator __last
, _OutputIterator __result
)
1446 // concept requirements
1447 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1448 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1449 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1450 __glibcxx_requires_valid_range(__first
, __middle
);
1451 __glibcxx_requires_valid_range(__middle
, __last
);
1453 return std::copy(__first
, __middle
,
1454 std::copy(__middle
, __last
, __result
));
1457 /// This is a helper function...
1458 template<typename _ForwardIterator
, typename _Predicate
>
1460 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1461 _Predicate __pred
, forward_iterator_tag
)
1463 if (__first
== __last
)
1466 while (__pred(*__first
))
1467 if (++__first
== __last
)
1470 _ForwardIterator __next
= __first
;
1472 while (++__next
!= __last
)
1473 if (__pred(*__next
))
1475 std::iter_swap(__first
, __next
);
1482 /// This is a helper function...
1483 template<typename _BidirectionalIterator
, typename _Predicate
>
1484 _BidirectionalIterator
1485 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1486 _Predicate __pred
, bidirectional_iterator_tag
)
1491 if (__first
== __last
)
1493 else if (__pred(*__first
))
1499 if (__first
== __last
)
1501 else if (!bool(__pred(*__last
)))
1505 std::iter_swap(__first
, __last
);
1512 /// This is a helper function...
1513 /// Requires __len != 0 and !__pred(*__first),
1514 /// same as __stable_partition_adaptive.
1515 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1517 __inplace_stable_partition(_ForwardIterator __first
,
1518 _Predicate __pred
, _Distance __len
)
1522 _ForwardIterator __middle
= __first
;
1523 std::advance(__middle
, __len
/ 2);
1524 _ForwardIterator __left_split
=
1525 std::__inplace_stable_partition(__first
, __pred
, __len
/ 2);
1526 // Advance past true-predicate values to satisfy this
1527 // function's preconditions.
1528 _Distance __right_len
= __len
- __len
/ 2;
1529 _ForwardIterator __right_split
=
1530 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1532 __right_split
= std::__inplace_stable_partition(__middle
,
1535 std::rotate(__left_split
, __middle
, __right_split
);
1536 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1537 return __left_split
;
1540 /// This is a helper function...
1541 /// Requires __first != __last and !__pred(__first)
1542 /// and __len == distance(__first, __last).
1544 /// !__pred(__first) allows us to guarantee that we don't
1545 /// move-assign an element onto itself.
1546 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1549 __stable_partition_adaptive(_ForwardIterator __first
,
1550 _ForwardIterator __last
,
1551 _Predicate __pred
, _Distance __len
,
1553 _Distance __buffer_size
)
1555 if (__len
<= __buffer_size
)
1557 _ForwardIterator __result1
= __first
;
1558 _Pointer __result2
= __buffer
;
1559 // The precondition guarantees that !__pred(__first), so
1560 // move that element to the buffer before starting the loop.
1561 // This ensures that we only call __pred once per element.
1562 *__result2
= _GLIBCXX_MOVE(*__first
);
1565 for (; __first
!= __last
; ++__first
)
1566 if (__pred(__first
))
1568 *__result1
= _GLIBCXX_MOVE(*__first
);
1573 *__result2
= _GLIBCXX_MOVE(*__first
);
1576 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1581 _ForwardIterator __middle
= __first
;
1582 std::advance(__middle
, __len
/ 2);
1583 _ForwardIterator __left_split
=
1584 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1585 __len
/ 2, __buffer
,
1587 // Advance past true-predicate values to satisfy this
1588 // function's preconditions.
1589 _Distance __right_len
= __len
- __len
/ 2;
1590 _ForwardIterator __right_split
=
1591 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1594 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1596 __buffer
, __buffer_size
);
1597 std::rotate(__left_split
, __middle
, __right_split
);
1598 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1599 return __left_split
;
1603 template<typename _ForwardIterator
, typename _Predicate
>
1605 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1608 __first
= std::__find_if_not(__first
, __last
, __pred
);
1610 if (__first
== __last
)
1613 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1615 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1618 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
, __last
);
1619 if (__buf
.size() > 0)
1621 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1622 _DistanceType(__buf
.requested_size()),
1624 _DistanceType(__buf
.size()));
1627 std::__inplace_stable_partition(__first
, __pred
,
1628 _DistanceType(__buf
.requested_size()));
1632 * @brief Move elements for which a predicate is true to the beginning
1633 * of a sequence, preserving relative ordering.
1634 * @ingroup mutating_algorithms
1635 * @param __first A forward iterator.
1636 * @param __last A forward iterator.
1637 * @param __pred A predicate functor.
1638 * @return An iterator @p middle such that @p __pred(i) is true for each
1639 * iterator @p i in the range @p [first,middle) and false for each @p i
1640 * in the range @p [middle,last).
1642 * Performs the same function as @p partition() with the additional
1643 * guarantee that the relative ordering of elements in each group is
1644 * preserved, so any two elements @p x and @p y in the range
1645 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1646 * relative ordering after calling @p stable_partition().
1648 template<typename _ForwardIterator
, typename _Predicate
>
1649 inline _ForwardIterator
1650 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1653 // concept requirements
1654 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1656 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1657 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1658 __glibcxx_requires_valid_range(__first
, __last
);
1660 return std::__stable_partition(__first
, __last
,
1661 __gnu_cxx::__ops::__pred_iter(__pred
));
1664 /// This is a helper function for the sort routines.
1665 template<typename _RandomAccessIterator
, typename _Compare
>
1667 __heap_select(_RandomAccessIterator __first
,
1668 _RandomAccessIterator __middle
,
1669 _RandomAccessIterator __last
, _Compare __comp
)
1671 std::__make_heap(__first
, __middle
, __comp
);
1672 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1673 if (__comp(__i
, __first
))
1674 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1679 template<typename _InputIterator
, typename _RandomAccessIterator
,
1681 _RandomAccessIterator
1682 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1683 _RandomAccessIterator __result_first
,
1684 _RandomAccessIterator __result_last
,
1687 typedef typename iterator_traits
<_InputIterator
>::value_type
1689 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1690 typedef typename
_RItTraits::difference_type _DistanceType
;
1692 if (__result_first
== __result_last
)
1693 return __result_last
;
1694 _RandomAccessIterator __result_real_last
= __result_first
;
1695 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1697 *__result_real_last
= *__first
;
1698 ++__result_real_last
;
1702 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1703 while (__first
!= __last
)
1705 if (__comp(__first
, __result_first
))
1706 std::__adjust_heap(__result_first
, _DistanceType(0),
1707 _DistanceType(__result_real_last
1709 _InputValueType(*__first
), __comp
);
1712 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1713 return __result_real_last
;
1717 * @brief Copy the smallest elements of a sequence.
1718 * @ingroup sorting_algorithms
1719 * @param __first An iterator.
1720 * @param __last Another iterator.
1721 * @param __result_first A random-access iterator.
1722 * @param __result_last Another random-access iterator.
1723 * @return An iterator indicating the end of the resulting sequence.
1725 * Copies and sorts the smallest N values from the range @p [__first,__last)
1726 * to the range beginning at @p __result_first, where the number of
1727 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1728 * @p (__result_last-__result_first).
1729 * After the sort if @e i and @e j are iterators in the range
1730 * @p [__result_first,__result_first+N) such that i precedes j then
1732 * The value returned is @p __result_first+N.
1734 template<typename _InputIterator
, typename _RandomAccessIterator
>
1735 inline _RandomAccessIterator
1736 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1737 _RandomAccessIterator __result_first
,
1738 _RandomAccessIterator __result_last
)
1740 typedef typename iterator_traits
<_InputIterator
>::value_type
1742 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1744 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1747 // concept requirements
1748 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1749 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1751 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1753 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1754 __glibcxx_requires_valid_range(__first
, __last
);
1755 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1757 return std::__partial_sort_copy(__first
, __last
,
1758 __result_first
, __result_last
,
1759 __gnu_cxx::__ops::__iter_less_iter());
1763 * @brief Copy the smallest elements of a sequence using a predicate for
1765 * @ingroup sorting_algorithms
1766 * @param __first An input iterator.
1767 * @param __last Another input iterator.
1768 * @param __result_first A random-access iterator.
1769 * @param __result_last Another random-access iterator.
1770 * @param __comp A comparison functor.
1771 * @return An iterator indicating the end of the resulting sequence.
1773 * Copies and sorts the smallest N values from the range @p [__first,__last)
1774 * to the range beginning at @p result_first, where the number of
1775 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1776 * @p (__result_last-__result_first).
1777 * After the sort if @e i and @e j are iterators in the range
1778 * @p [__result_first,__result_first+N) such that i precedes j then
1779 * @p __comp(*j,*i) is false.
1780 * The value returned is @p __result_first+N.
1782 template<typename _InputIterator
, typename _RandomAccessIterator
,
1784 inline _RandomAccessIterator
1785 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1786 _RandomAccessIterator __result_first
,
1787 _RandomAccessIterator __result_last
,
1790 typedef typename iterator_traits
<_InputIterator
>::value_type
1792 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1794 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1797 // concept requirements
1798 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1799 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1800 _RandomAccessIterator
>)
1801 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1803 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1804 _InputValueType
, _OutputValueType
>)
1805 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1806 _OutputValueType
, _OutputValueType
>)
1807 __glibcxx_requires_valid_range(__first
, __last
);
1808 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1810 return std::__partial_sort_copy(__first
, __last
,
1811 __result_first
, __result_last
,
1812 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1815 /// This is a helper function for the sort routine.
1816 template<typename _RandomAccessIterator
, typename _Compare
>
1818 __unguarded_linear_insert(_RandomAccessIterator __last
,
1821 typename iterator_traits
<_RandomAccessIterator
>::value_type
1822 __val
= _GLIBCXX_MOVE(*__last
);
1823 _RandomAccessIterator __next
= __last
;
1825 while (__comp(__val
, __next
))
1827 *__last
= _GLIBCXX_MOVE(*__next
);
1831 *__last
= _GLIBCXX_MOVE(__val
);
1834 /// This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator
, typename _Compare
>
1837 __insertion_sort(_RandomAccessIterator __first
,
1838 _RandomAccessIterator __last
, _Compare __comp
)
1840 if (__first
== __last
) return;
1842 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1844 if (__comp(__i
, __first
))
1846 typename iterator_traits
<_RandomAccessIterator
>::value_type
1847 __val
= _GLIBCXX_MOVE(*__i
);
1848 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1849 *__first
= _GLIBCXX_MOVE(__val
);
1852 std::__unguarded_linear_insert(__i
,
1853 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1857 /// This is a helper function for the sort routine.
1858 template<typename _RandomAccessIterator
, typename _Compare
>
1860 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1861 _RandomAccessIterator __last
, _Compare __comp
)
1863 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1864 std::__unguarded_linear_insert(__i
,
1865 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1870 * This controls some aspect of the sort routines.
1872 enum { _S_threshold
= 16 };
1874 /// This is a helper function for the sort routine.
1875 template<typename _RandomAccessIterator
, typename _Compare
>
1877 __final_insertion_sort(_RandomAccessIterator __first
,
1878 _RandomAccessIterator __last
, _Compare __comp
)
1880 if (__last
- __first
> int(_S_threshold
))
1882 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1883 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1887 std::__insertion_sort(__first
, __last
, __comp
);
1890 /// This is a helper function...
1891 template<typename _RandomAccessIterator
, typename _Compare
>
1892 _RandomAccessIterator
1893 __unguarded_partition(_RandomAccessIterator __first
,
1894 _RandomAccessIterator __last
,
1895 _RandomAccessIterator __pivot
, _Compare __comp
)
1899 while (__comp(__first
, __pivot
))
1902 while (__comp(__pivot
, __last
))
1904 if (!(__first
< __last
))
1906 std::iter_swap(__first
, __last
);
1911 /// This is a helper function...
1912 template<typename _RandomAccessIterator
, typename _Compare
>
1913 inline _RandomAccessIterator
1914 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1915 _RandomAccessIterator __last
, _Compare __comp
)
1917 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1918 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
1919 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1922 template<typename _RandomAccessIterator
, typename _Compare
>
1924 __partial_sort(_RandomAccessIterator __first
,
1925 _RandomAccessIterator __middle
,
1926 _RandomAccessIterator __last
,
1929 std::__heap_select(__first
, __middle
, __last
, __comp
);
1930 std::__sort_heap(__first
, __middle
, __comp
);
1933 /// This is a helper function for the sort routine.
1934 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1936 __introsort_loop(_RandomAccessIterator __first
,
1937 _RandomAccessIterator __last
,
1938 _Size __depth_limit
, _Compare __comp
)
1940 while (__last
- __first
> int(_S_threshold
))
1942 if (__depth_limit
== 0)
1944 std::__partial_sort(__first
, __last
, __last
, __comp
);
1948 _RandomAccessIterator __cut
=
1949 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1950 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1957 template<typename _RandomAccessIterator
, typename _Compare
>
1959 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1962 if (__first
!= __last
)
1964 std::__introsort_loop(__first
, __last
,
1965 std::__lg(__last
- __first
) * 2,
1967 std::__final_insertion_sort(__first
, __last
, __comp
);
1971 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1973 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1974 _RandomAccessIterator __last
, _Size __depth_limit
,
1977 while (__last
- __first
> 3)
1979 if (__depth_limit
== 0)
1981 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1982 // Place the nth largest element in its final position.
1983 std::iter_swap(__first
, __nth
);
1987 _RandomAccessIterator __cut
=
1988 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1994 std::__insertion_sort(__first
, __last
, __comp
);
1999 // lower_bound moved to stl_algobase.h
2002 * @brief Finds the first position in which @p __val could be inserted
2003 * without changing the ordering.
2004 * @ingroup binary_search_algorithms
2005 * @param __first An iterator.
2006 * @param __last Another iterator.
2007 * @param __val The search term.
2008 * @param __comp A functor to use for comparisons.
2009 * @return An iterator pointing to the first element <em>not less
2010 * than</em> @p __val, or end() if every element is less
2012 * @ingroup binary_search_algorithms
2014 * The comparison function should have the same effects on ordering as
2015 * the function used for the initial sort.
2017 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2018 inline _ForwardIterator
2019 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2020 const _Tp
& __val
, _Compare __comp
)
2022 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2025 // concept requirements
2026 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2027 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2029 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2032 return std::__lower_bound(__first
, __last
, __val
,
2033 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2036 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2038 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2039 const _Tp
& __val
, _Compare __comp
)
2041 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2044 _DistanceType __len
= std::distance(__first
, __last
);
2048 _DistanceType __half
= __len
>> 1;
2049 _ForwardIterator __middle
= __first
;
2050 std::advance(__middle
, __half
);
2051 if (__comp(__val
, __middle
))
2057 __len
= __len
- __half
- 1;
2064 * @brief Finds the last position in which @p __val could be inserted
2065 * without changing the ordering.
2066 * @ingroup binary_search_algorithms
2067 * @param __first An iterator.
2068 * @param __last Another iterator.
2069 * @param __val The search term.
2070 * @return An iterator pointing to the first element greater than @p __val,
2071 * or end() if no elements are greater than @p __val.
2072 * @ingroup binary_search_algorithms
2074 template<typename _ForwardIterator
, typename _Tp
>
2075 inline _ForwardIterator
2076 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2079 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2082 // concept requirements
2083 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2084 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2085 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2087 return std::__upper_bound(__first
, __last
, __val
,
2088 __gnu_cxx::__ops::__val_less_iter());
2092 * @brief Finds the last position in which @p __val could be inserted
2093 * without changing the ordering.
2094 * @ingroup binary_search_algorithms
2095 * @param __first An iterator.
2096 * @param __last Another iterator.
2097 * @param __val The search term.
2098 * @param __comp A functor to use for comparisons.
2099 * @return An iterator pointing to the first element greater than @p __val,
2100 * or end() if no elements are greater than @p __val.
2101 * @ingroup binary_search_algorithms
2103 * The comparison function should have the same effects on ordering as
2104 * the function used for the initial sort.
2106 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2107 inline _ForwardIterator
2108 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2109 const _Tp
& __val
, _Compare __comp
)
2111 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2114 // concept requirements
2115 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2116 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2118 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2121 return std::__upper_bound(__first
, __last
, __val
,
2122 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2125 template<typename _ForwardIterator
, typename _Tp
,
2126 typename _CompareItTp
, typename _CompareTpIt
>
2127 pair
<_ForwardIterator
, _ForwardIterator
>
2128 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2130 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2132 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2135 _DistanceType __len
= std::distance(__first
, __last
);
2139 _DistanceType __half
= __len
>> 1;
2140 _ForwardIterator __middle
= __first
;
2141 std::advance(__middle
, __half
);
2142 if (__comp_it_val(__middle
, __val
))
2146 __len
= __len
- __half
- 1;
2148 else if (__comp_val_it(__val
, __middle
))
2152 _ForwardIterator __left
2153 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2154 std::advance(__first
, __len
);
2155 _ForwardIterator __right
2156 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2157 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2160 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2164 * @brief Finds the largest subrange in which @p __val could be inserted
2165 * at any place in it without changing the ordering.
2166 * @ingroup binary_search_algorithms
2167 * @param __first An iterator.
2168 * @param __last Another iterator.
2169 * @param __val The search term.
2170 * @return An pair of iterators defining the subrange.
2171 * @ingroup binary_search_algorithms
2173 * This is equivalent to
2175 * std::make_pair(lower_bound(__first, __last, __val),
2176 * upper_bound(__first, __last, __val))
2178 * but does not actually call those functions.
2180 template<typename _ForwardIterator
, typename _Tp
>
2181 inline pair
<_ForwardIterator
, _ForwardIterator
>
2182 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2185 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2188 // concept requirements
2189 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2190 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2191 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2192 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2193 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2195 return std::__equal_range(__first
, __last
, __val
,
2196 __gnu_cxx::__ops::__iter_less_val(),
2197 __gnu_cxx::__ops::__val_less_iter());
2201 * @brief Finds the largest subrange in which @p __val could be inserted
2202 * at any place in it without changing the ordering.
2203 * @param __first An iterator.
2204 * @param __last Another iterator.
2205 * @param __val The search term.
2206 * @param __comp A functor to use for comparisons.
2207 * @return An pair of iterators defining the subrange.
2208 * @ingroup binary_search_algorithms
2210 * This is equivalent to
2212 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2213 * upper_bound(__first, __last, __val, __comp))
2215 * but does not actually call those functions.
2217 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2218 inline pair
<_ForwardIterator
, _ForwardIterator
>
2219 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2220 const _Tp
& __val
, _Compare __comp
)
2222 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2225 // concept requirements
2226 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2227 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2229 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2231 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2233 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2236 return std::__equal_range(__first
, __last
, __val
,
2237 __gnu_cxx::__ops::__iter_comp_val(__comp
),
2238 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2242 * @brief Determines whether an element exists in a range.
2243 * @ingroup binary_search_algorithms
2244 * @param __first An iterator.
2245 * @param __last Another iterator.
2246 * @param __val The search term.
2247 * @return True if @p __val (or its equivalent) is in [@p
2248 * __first,@p __last ].
2250 * Note that this does not actually return an iterator to @p __val. For
2251 * that, use std::find or a container's specialized find member functions.
2253 template<typename _ForwardIterator
, typename _Tp
>
2255 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2258 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2261 // concept requirements
2262 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2263 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2264 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2265 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2267 _ForwardIterator __i
2268 = std::__lower_bound(__first
, __last
, __val
,
2269 __gnu_cxx::__ops::__iter_less_val());
2270 return __i
!= __last
&& !(__val
< *__i
);
2274 * @brief Determines whether an element exists in a range.
2275 * @ingroup binary_search_algorithms
2276 * @param __first An iterator.
2277 * @param __last Another iterator.
2278 * @param __val The search term.
2279 * @param __comp A functor to use for comparisons.
2280 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2282 * Note that this does not actually return an iterator to @p __val. For
2283 * that, use std::find or a container's specialized find member functions.
2285 * The comparison function should have the same effects on ordering as
2286 * the function used for the initial sort.
2288 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2290 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2291 const _Tp
& __val
, _Compare __comp
)
2293 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2296 // concept requirements
2297 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2298 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2300 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2302 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2305 _ForwardIterator __i
2306 = std::__lower_bound(__first
, __last
, __val
,
2307 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2308 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2313 /// This is a helper function for the __merge_adaptive routines.
2314 template<typename _InputIterator1
, typename _InputIterator2
,
2315 typename _OutputIterator
, typename _Compare
>
2317 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2318 _InputIterator2 __first2
, _InputIterator2 __last2
,
2319 _OutputIterator __result
, _Compare __comp
)
2321 while (__first1
!= __last1
&& __first2
!= __last2
)
2323 if (__comp(__first2
, __first1
))
2325 *__result
= _GLIBCXX_MOVE(*__first2
);
2330 *__result
= _GLIBCXX_MOVE(*__first1
);
2335 if (__first1
!= __last1
)
2336 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2339 /// This is a helper function for the __merge_adaptive routines.
2340 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2341 typename _BidirectionalIterator3
, typename _Compare
>
2343 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2344 _BidirectionalIterator1 __last1
,
2345 _BidirectionalIterator2 __first2
,
2346 _BidirectionalIterator2 __last2
,
2347 _BidirectionalIterator3 __result
,
2350 if (__first1
== __last1
)
2352 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2355 else if (__first2
== __last2
)
2362 if (__comp(__last2
, __last1
))
2364 *--__result
= _GLIBCXX_MOVE(*__last1
);
2365 if (__first1
== __last1
)
2367 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2374 *--__result
= _GLIBCXX_MOVE(*__last2
);
2375 if (__first2
== __last2
)
2382 /// This is a helper function for the merge routines.
2383 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2385 _BidirectionalIterator1
2386 __rotate_adaptive(_BidirectionalIterator1 __first
,
2387 _BidirectionalIterator1 __middle
,
2388 _BidirectionalIterator1 __last
,
2389 _Distance __len1
, _Distance __len2
,
2390 _BidirectionalIterator2 __buffer
,
2391 _Distance __buffer_size
)
2393 _BidirectionalIterator2 __buffer_end
;
2394 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2398 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2399 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2400 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2405 else if (__len1
<= __buffer_size
)
2409 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2410 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2411 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2418 std::rotate(__first
, __middle
, __last
);
2419 std::advance(__first
, std::distance(__middle
, __last
));
2424 /// This is a helper function for the merge routines.
2425 template<typename _BidirectionalIterator
, typename _Distance
,
2426 typename _Pointer
, typename _Compare
>
2428 __merge_adaptive(_BidirectionalIterator __first
,
2429 _BidirectionalIterator __middle
,
2430 _BidirectionalIterator __last
,
2431 _Distance __len1
, _Distance __len2
,
2432 _Pointer __buffer
, _Distance __buffer_size
,
2435 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2437 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2438 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2441 else if (__len2
<= __buffer_size
)
2443 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2444 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2445 __buffer_end
, __last
, __comp
);
2449 _BidirectionalIterator __first_cut
= __first
;
2450 _BidirectionalIterator __second_cut
= __middle
;
2451 _Distance __len11
= 0;
2452 _Distance __len22
= 0;
2453 if (__len1
> __len2
)
2455 __len11
= __len1
/ 2;
2456 std::advance(__first_cut
, __len11
);
2458 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2459 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2460 __len22
= std::distance(__middle
, __second_cut
);
2464 __len22
= __len2
/ 2;
2465 std::advance(__second_cut
, __len22
);
2467 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2468 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2469 __len11
= std::distance(__first
, __first_cut
);
2471 _BidirectionalIterator __new_middle
2472 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2473 __len1
- __len11
, __len22
, __buffer
,
2475 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2476 __len22
, __buffer
, __buffer_size
, __comp
);
2477 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2479 __len2
- __len22
, __buffer
,
2480 __buffer_size
, __comp
);
2484 /// This is a helper function for the merge routines.
2485 template<typename _BidirectionalIterator
, typename _Distance
,
2488 __merge_without_buffer(_BidirectionalIterator __first
,
2489 _BidirectionalIterator __middle
,
2490 _BidirectionalIterator __last
,
2491 _Distance __len1
, _Distance __len2
,
2494 if (__len1
== 0 || __len2
== 0)
2496 if (__len1
+ __len2
== 2)
2498 if (__comp(__middle
, __first
))
2499 std::iter_swap(__first
, __middle
);
2502 _BidirectionalIterator __first_cut
= __first
;
2503 _BidirectionalIterator __second_cut
= __middle
;
2504 _Distance __len11
= 0;
2505 _Distance __len22
= 0;
2506 if (__len1
> __len2
)
2508 __len11
= __len1
/ 2;
2509 std::advance(__first_cut
, __len11
);
2511 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2512 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2513 __len22
= std::distance(__middle
, __second_cut
);
2517 __len22
= __len2
/ 2;
2518 std::advance(__second_cut
, __len22
);
2520 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2521 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2522 __len11
= std::distance(__first
, __first_cut
);
2524 std::rotate(__first_cut
, __middle
, __second_cut
);
2525 _BidirectionalIterator __new_middle
= __first_cut
;
2526 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2527 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2528 __len11
, __len22
, __comp
);
2529 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2530 __len1
- __len11
, __len2
- __len22
, __comp
);
2533 template<typename _BidirectionalIterator
, typename _Compare
>
2535 __inplace_merge(_BidirectionalIterator __first
,
2536 _BidirectionalIterator __middle
,
2537 _BidirectionalIterator __last
,
2540 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2542 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2545 if (__first
== __middle
|| __middle
== __last
)
2548 const _DistanceType __len1
= std::distance(__first
, __middle
);
2549 const _DistanceType __len2
= std::distance(__middle
, __last
);
2551 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2552 _TmpBuf
__buf(__first
, __last
);
2554 if (__buf
.begin() == 0)
2555 std::__merge_without_buffer
2556 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2558 std::__merge_adaptive
2559 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2560 _DistanceType(__buf
.size()), __comp
);
2564 * @brief Merges two sorted ranges in place.
2565 * @ingroup sorting_algorithms
2566 * @param __first An iterator.
2567 * @param __middle Another iterator.
2568 * @param __last Another iterator.
2571 * Merges two sorted and consecutive ranges, [__first,__middle) and
2572 * [__middle,__last), and puts the result in [__first,__last). The
2573 * output will be sorted. The sort is @e stable, that is, for
2574 * equivalent elements in the two ranges, elements from the first
2575 * range will always come before elements from the second.
2577 * If enough additional memory is available, this takes (__last-__first)-1
2578 * comparisons. Otherwise an NlogN algorithm is used, where N is
2579 * distance(__first,__last).
2581 template<typename _BidirectionalIterator
>
2583 inplace_merge(_BidirectionalIterator __first
,
2584 _BidirectionalIterator __middle
,
2585 _BidirectionalIterator __last
)
2587 // concept requirements
2588 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2589 _BidirectionalIterator
>)
2590 __glibcxx_function_requires(_LessThanComparableConcept
<
2591 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2592 __glibcxx_requires_sorted(__first
, __middle
);
2593 __glibcxx_requires_sorted(__middle
, __last
);
2595 std::__inplace_merge(__first
, __middle
, __last
,
2596 __gnu_cxx::__ops::__iter_less_iter());
2600 * @brief Merges two sorted ranges in place.
2601 * @ingroup sorting_algorithms
2602 * @param __first An iterator.
2603 * @param __middle Another iterator.
2604 * @param __last Another iterator.
2605 * @param __comp A functor to use for comparisons.
2608 * Merges two sorted and consecutive ranges, [__first,__middle) and
2609 * [middle,last), and puts the result in [__first,__last). The output will
2610 * be sorted. The sort is @e stable, that is, for equivalent
2611 * elements in the two ranges, elements from the first range will always
2612 * come before elements from the second.
2614 * If enough additional memory is available, this takes (__last-__first)-1
2615 * comparisons. Otherwise an NlogN algorithm is used, where N is
2616 * distance(__first,__last).
2618 * The comparison function should have the same effects on ordering as
2619 * the function used for the initial sort.
2621 template<typename _BidirectionalIterator
, typename _Compare
>
2623 inplace_merge(_BidirectionalIterator __first
,
2624 _BidirectionalIterator __middle
,
2625 _BidirectionalIterator __last
,
2628 // concept requirements
2629 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2630 _BidirectionalIterator
>)
2631 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2632 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2633 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2634 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2635 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2637 std::__inplace_merge(__first
, __middle
, __last
,
2638 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2642 /// This is a helper function for the __merge_sort_loop routines.
2643 template<typename _InputIterator
, typename _OutputIterator
,
2646 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2647 _InputIterator __first2
, _InputIterator __last2
,
2648 _OutputIterator __result
, _Compare __comp
)
2650 while (__first1
!= __last1
&& __first2
!= __last2
)
2652 if (__comp(__first2
, __first1
))
2654 *__result
= _GLIBCXX_MOVE(*__first2
);
2659 *__result
= _GLIBCXX_MOVE(*__first1
);
2664 return _GLIBCXX_MOVE3(__first2
, __last2
,
2665 _GLIBCXX_MOVE3(__first1
, __last1
,
2669 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2670 typename _Distance
, typename _Compare
>
2672 __merge_sort_loop(_RandomAccessIterator1 __first
,
2673 _RandomAccessIterator1 __last
,
2674 _RandomAccessIterator2 __result
, _Distance __step_size
,
2677 const _Distance __two_step
= 2 * __step_size
;
2679 while (__last
- __first
>= __two_step
)
2681 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2682 __first
+ __step_size
,
2683 __first
+ __two_step
,
2685 __first
+= __two_step
;
2687 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2689 std::__move_merge(__first
, __first
+ __step_size
,
2690 __first
+ __step_size
, __last
, __result
, __comp
);
2693 template<typename _RandomAccessIterator
, typename _Distance
,
2696 __chunk_insertion_sort(_RandomAccessIterator __first
,
2697 _RandomAccessIterator __last
,
2698 _Distance __chunk_size
, _Compare __comp
)
2700 while (__last
- __first
>= __chunk_size
)
2702 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2703 __first
+= __chunk_size
;
2705 std::__insertion_sort(__first
, __last
, __comp
);
2708 enum { _S_chunk_size
= 7 };
2710 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2712 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2713 _RandomAccessIterator __last
,
2714 _Pointer __buffer
, _Compare __comp
)
2716 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2719 const _Distance __len
= __last
- __first
;
2720 const _Pointer __buffer_last
= __buffer
+ __len
;
2722 _Distance __step_size
= _S_chunk_size
;
2723 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2725 while (__step_size
< __len
)
2727 std::__merge_sort_loop(__first
, __last
, __buffer
,
2728 __step_size
, __comp
);
2730 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2731 __step_size
, __comp
);
2736 template<typename _RandomAccessIterator
, typename _Pointer
,
2737 typename _Distance
, typename _Compare
>
2739 __stable_sort_adaptive(_RandomAccessIterator __first
,
2740 _RandomAccessIterator __last
,
2741 _Pointer __buffer
, _Distance __buffer_size
,
2744 const _Distance __len
= (__last
- __first
+ 1) / 2;
2745 const _RandomAccessIterator __middle
= __first
+ __len
;
2746 if (__len
> __buffer_size
)
2748 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2749 __buffer_size
, __comp
);
2750 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2751 __buffer_size
, __comp
);
2755 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2756 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2758 std::__merge_adaptive(__first
, __middle
, __last
,
2759 _Distance(__middle
- __first
),
2760 _Distance(__last
- __middle
),
2761 __buffer
, __buffer_size
,
2765 /// This is a helper function for the stable sorting routines.
2766 template<typename _RandomAccessIterator
, typename _Compare
>
2768 __inplace_stable_sort(_RandomAccessIterator __first
,
2769 _RandomAccessIterator __last
, _Compare __comp
)
2771 if (__last
- __first
< 15)
2773 std::__insertion_sort(__first
, __last
, __comp
);
2776 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2777 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2778 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2779 std::__merge_without_buffer(__first
, __middle
, __last
,
2787 // Set algorithms: includes, set_union, set_intersection, set_difference,
2788 // set_symmetric_difference. All of these algorithms have the precondition
2789 // that their input ranges are sorted and the postcondition that their output
2790 // ranges are sorted.
2792 template<typename _InputIterator1
, typename _InputIterator2
,
2795 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2796 _InputIterator2 __first2
, _InputIterator2 __last2
,
2799 while (__first1
!= __last1
&& __first2
!= __last2
)
2800 if (__comp(__first2
, __first1
))
2802 else if (__comp(__first1
, __first2
))
2805 ++__first1
, ++__first2
;
2807 return __first2
== __last2
;
2811 * @brief Determines whether all elements of a sequence exists in a range.
2812 * @param __first1 Start of search range.
2813 * @param __last1 End of search range.
2814 * @param __first2 Start of sequence
2815 * @param __last2 End of sequence.
2816 * @return True if each element in [__first2,__last2) is contained in order
2817 * within [__first1,__last1). False otherwise.
2818 * @ingroup set_algorithms
2820 * This operation expects both [__first1,__last1) and
2821 * [__first2,__last2) to be sorted. Searches for the presence of
2822 * each element in [__first2,__last2) within [__first1,__last1).
2823 * The iterators over each range only move forward, so this is a
2824 * linear algorithm. If an element in [__first2,__last2) is not
2825 * found before the search iterator reaches @p __last2, false is
2828 template<typename _InputIterator1
, typename _InputIterator2
>
2830 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2831 _InputIterator2 __first2
, _InputIterator2 __last2
)
2833 // concept requirements
2834 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2835 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2836 __glibcxx_function_requires(_LessThanOpConcept
<
2837 typename iterator_traits
<_InputIterator1
>::value_type
,
2838 typename iterator_traits
<_InputIterator2
>::value_type
>)
2839 __glibcxx_function_requires(_LessThanOpConcept
<
2840 typename iterator_traits
<_InputIterator2
>::value_type
,
2841 typename iterator_traits
<_InputIterator1
>::value_type
>)
2842 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2843 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2845 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2846 __gnu_cxx::__ops::__iter_less_iter());
2850 * @brief Determines whether all elements of a sequence exists in a range
2852 * @ingroup set_algorithms
2853 * @param __first1 Start of search range.
2854 * @param __last1 End of search range.
2855 * @param __first2 Start of sequence
2856 * @param __last2 End of sequence.
2857 * @param __comp Comparison function to use.
2858 * @return True if each element in [__first2,__last2) is contained
2859 * in order within [__first1,__last1) according to comp. False
2860 * otherwise. @ingroup set_algorithms
2862 * This operation expects both [__first1,__last1) and
2863 * [__first2,__last2) to be sorted. Searches for the presence of
2864 * each element in [__first2,__last2) within [__first1,__last1),
2865 * using comp to decide. The iterators over each range only move
2866 * forward, so this is a linear algorithm. If an element in
2867 * [__first2,__last2) is not found before the search iterator
2868 * reaches @p __last2, false is returned.
2870 template<typename _InputIterator1
, typename _InputIterator2
,
2873 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2874 _InputIterator2 __first2
, _InputIterator2 __last2
,
2877 // concept requirements
2878 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2879 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2880 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2881 typename iterator_traits
<_InputIterator1
>::value_type
,
2882 typename iterator_traits
<_InputIterator2
>::value_type
>)
2883 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2884 typename iterator_traits
<_InputIterator2
>::value_type
,
2885 typename iterator_traits
<_InputIterator1
>::value_type
>)
2886 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2887 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2889 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2890 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2899 // set_symmetric_difference
2903 template<typename _BidirectionalIterator
, typename _Compare
>
2905 __next_permutation(_BidirectionalIterator __first
,
2906 _BidirectionalIterator __last
, _Compare __comp
)
2908 if (__first
== __last
)
2910 _BidirectionalIterator __i
= __first
;
2919 _BidirectionalIterator __ii
= __i
;
2921 if (__comp(__i
, __ii
))
2923 _BidirectionalIterator __j
= __last
;
2924 while (!__comp(__i
, --__j
))
2926 std::iter_swap(__i
, __j
);
2927 std::__reverse(__ii
, __last
,
2928 std::__iterator_category(__first
));
2933 std::__reverse(__first
, __last
,
2934 std::__iterator_category(__first
));
2941 * @brief Permute range into the next @e dictionary ordering.
2942 * @ingroup sorting_algorithms
2943 * @param __first Start of range.
2944 * @param __last End of range.
2945 * @return False if wrapped to first permutation, true otherwise.
2947 * Treats all permutations of the range as a set of @e dictionary sorted
2948 * sequences. Permutes the current sequence into the next one of this set.
2949 * Returns true if there are more sequences to generate. If the sequence
2950 * is the largest of the set, the smallest is generated and false returned.
2952 template<typename _BidirectionalIterator
>
2954 next_permutation(_BidirectionalIterator __first
,
2955 _BidirectionalIterator __last
)
2957 // concept requirements
2958 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2959 _BidirectionalIterator
>)
2960 __glibcxx_function_requires(_LessThanComparableConcept
<
2961 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2962 __glibcxx_requires_valid_range(__first
, __last
);
2964 return std::__next_permutation
2965 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2969 * @brief Permute range into the next @e dictionary ordering using
2970 * comparison functor.
2971 * @ingroup sorting_algorithms
2972 * @param __first Start of range.
2973 * @param __last End of range.
2974 * @param __comp A comparison functor.
2975 * @return False if wrapped to first permutation, true otherwise.
2977 * Treats all permutations of the range [__first,__last) as a set of
2978 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2979 * sequence into the next one of this set. Returns true if there are more
2980 * sequences to generate. If the sequence is the largest of the set, the
2981 * smallest is generated and false returned.
2983 template<typename _BidirectionalIterator
, typename _Compare
>
2985 next_permutation(_BidirectionalIterator __first
,
2986 _BidirectionalIterator __last
, _Compare __comp
)
2988 // concept requirements
2989 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2990 _BidirectionalIterator
>)
2991 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2992 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2993 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2994 __glibcxx_requires_valid_range(__first
, __last
);
2996 return std::__next_permutation
2997 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3000 template<typename _BidirectionalIterator
, typename _Compare
>
3002 __prev_permutation(_BidirectionalIterator __first
,
3003 _BidirectionalIterator __last
, _Compare __comp
)
3005 if (__first
== __last
)
3007 _BidirectionalIterator __i
= __first
;
3016 _BidirectionalIterator __ii
= __i
;
3018 if (__comp(__ii
, __i
))
3020 _BidirectionalIterator __j
= __last
;
3021 while (!__comp(--__j
, __i
))
3023 std::iter_swap(__i
, __j
);
3024 std::__reverse(__ii
, __last
,
3025 std::__iterator_category(__first
));
3030 std::__reverse(__first
, __last
,
3031 std::__iterator_category(__first
));
3038 * @brief Permute range into the previous @e dictionary ordering.
3039 * @ingroup sorting_algorithms
3040 * @param __first Start of range.
3041 * @param __last End of range.
3042 * @return False if wrapped to last permutation, true otherwise.
3044 * Treats all permutations of the range as a set of @e dictionary sorted
3045 * sequences. Permutes the current sequence into the previous one of this
3046 * set. Returns true if there are more sequences to generate. If the
3047 * sequence is the smallest of the set, the largest is generated and false
3050 template<typename _BidirectionalIterator
>
3052 prev_permutation(_BidirectionalIterator __first
,
3053 _BidirectionalIterator __last
)
3055 // concept requirements
3056 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3057 _BidirectionalIterator
>)
3058 __glibcxx_function_requires(_LessThanComparableConcept
<
3059 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3060 __glibcxx_requires_valid_range(__first
, __last
);
3062 return std::__prev_permutation(__first
, __last
,
3063 __gnu_cxx::__ops::__iter_less_iter());
3067 * @brief Permute range into the previous @e dictionary ordering using
3068 * comparison functor.
3069 * @ingroup sorting_algorithms
3070 * @param __first Start of range.
3071 * @param __last End of range.
3072 * @param __comp A comparison functor.
3073 * @return False if wrapped to last permutation, true otherwise.
3075 * Treats all permutations of the range [__first,__last) as a set of
3076 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3077 * sequence into the previous one of this set. Returns true if there are
3078 * more sequences to generate. If the sequence is the smallest of the set,
3079 * the largest is generated and false returned.
3081 template<typename _BidirectionalIterator
, typename _Compare
>
3083 prev_permutation(_BidirectionalIterator __first
,
3084 _BidirectionalIterator __last
, _Compare __comp
)
3086 // concept requirements
3087 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3088 _BidirectionalIterator
>)
3089 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3090 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3091 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3092 __glibcxx_requires_valid_range(__first
, __last
);
3094 return std::__prev_permutation(__first
, __last
,
3095 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3101 template<typename _InputIterator
, typename _OutputIterator
,
3102 typename _Predicate
, typename _Tp
>
3104 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3105 _OutputIterator __result
,
3106 _Predicate __pred
, const _Tp
& __new_value
)
3108 for (; __first
!= __last
; ++__first
, ++__result
)
3109 if (__pred(__first
))
3110 *__result
= __new_value
;
3112 *__result
= *__first
;
3117 * @brief Copy a sequence, replacing each element of one value with another
3119 * @param __first An input iterator.
3120 * @param __last An input iterator.
3121 * @param __result An output iterator.
3122 * @param __old_value The value to be replaced.
3123 * @param __new_value The replacement value.
3124 * @return The end of the output sequence, @p result+(last-first).
3126 * Copies each element in the input range @p [__first,__last) to the
3127 * output range @p [__result,__result+(__last-__first)) replacing elements
3128 * equal to @p __old_value with @p __new_value.
3130 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3131 inline _OutputIterator
3132 replace_copy(_InputIterator __first
, _InputIterator __last
,
3133 _OutputIterator __result
,
3134 const _Tp
& __old_value
, const _Tp
& __new_value
)
3136 // concept requirements
3137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3138 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3139 typename iterator_traits
<_InputIterator
>::value_type
>)
3140 __glibcxx_function_requires(_EqualOpConcept
<
3141 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3142 __glibcxx_requires_valid_range(__first
, __last
);
3144 return std::__replace_copy_if(__first
, __last
, __result
,
3145 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3150 * @brief Copy a sequence, replacing each value for which a predicate
3151 * returns true with another value.
3152 * @ingroup mutating_algorithms
3153 * @param __first An input iterator.
3154 * @param __last An input iterator.
3155 * @param __result An output iterator.
3156 * @param __pred A predicate.
3157 * @param __new_value The replacement value.
3158 * @return The end of the output sequence, @p __result+(__last-__first).
3160 * Copies each element in the range @p [__first,__last) to the range
3161 * @p [__result,__result+(__last-__first)) replacing elements for which
3162 * @p __pred returns true with @p __new_value.
3164 template<typename _InputIterator
, typename _OutputIterator
,
3165 typename _Predicate
, typename _Tp
>
3166 inline _OutputIterator
3167 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3168 _OutputIterator __result
,
3169 _Predicate __pred
, const _Tp
& __new_value
)
3171 // concept requirements
3172 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3173 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3174 typename iterator_traits
<_InputIterator
>::value_type
>)
3175 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3176 typename iterator_traits
<_InputIterator
>::value_type
>)
3177 __glibcxx_requires_valid_range(__first
, __last
);
3179 return std::__replace_copy_if(__first
, __last
, __result
,
3180 __gnu_cxx::__ops::__pred_iter(__pred
),
3184 template<typename _InputIterator
, typename _Predicate
>
3185 typename iterator_traits
<_InputIterator
>::difference_type
3186 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3188 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3189 for (; __first
!= __last
; ++__first
)
3190 if (__pred(__first
))
3195 #if __cplusplus >= 201103L
3197 * @brief Determines whether the elements of a sequence are sorted.
3198 * @ingroup sorting_algorithms
3199 * @param __first An iterator.
3200 * @param __last Another iterator.
3201 * @return True if the elements are sorted, false otherwise.
3203 template<typename _ForwardIterator
>
3205 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3206 { return std::is_sorted_until(__first
, __last
) == __last
; }
3209 * @brief Determines whether the elements of a sequence are sorted
3210 * according to a comparison functor.
3211 * @ingroup sorting_algorithms
3212 * @param __first An iterator.
3213 * @param __last Another iterator.
3214 * @param __comp A comparison functor.
3215 * @return True if the elements are sorted, false otherwise.
3217 template<typename _ForwardIterator
, typename _Compare
>
3219 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3221 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3223 template<typename _ForwardIterator
, typename _Compare
>
3225 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3228 if (__first
== __last
)
3231 _ForwardIterator __next
= __first
;
3232 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3233 if (__comp(__next
, __first
))
3239 * @brief Determines the end of a sorted sequence.
3240 * @ingroup sorting_algorithms
3241 * @param __first An iterator.
3242 * @param __last Another iterator.
3243 * @return An iterator pointing to the last iterator i in [__first, __last)
3244 * for which the range [__first, i) is sorted.
3246 template<typename _ForwardIterator
>
3247 inline _ForwardIterator
3248 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3250 // concept requirements
3251 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3252 __glibcxx_function_requires(_LessThanComparableConcept
<
3253 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3254 __glibcxx_requires_valid_range(__first
, __last
);
3256 return std::__is_sorted_until(__first
, __last
,
3257 __gnu_cxx::__ops::__iter_less_iter());
3261 * @brief Determines the end of a sorted sequence using comparison functor.
3262 * @ingroup sorting_algorithms
3263 * @param __first An iterator.
3264 * @param __last Another iterator.
3265 * @param __comp A comparison functor.
3266 * @return An iterator pointing to the last iterator i in [__first, __last)
3267 * for which the range [__first, i) is sorted.
3269 template<typename _ForwardIterator
, typename _Compare
>
3270 inline _ForwardIterator
3271 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3274 // concept requirements
3275 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3276 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3277 typename iterator_traits
<_ForwardIterator
>::value_type
,
3278 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3279 __glibcxx_requires_valid_range(__first
, __last
);
3281 return std::__is_sorted_until(__first
, __last
,
3282 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3286 * @brief Determines min and max at once as an ordered pair.
3287 * @ingroup sorting_algorithms
3288 * @param __a A thing of arbitrary type.
3289 * @param __b Another thing of arbitrary type.
3290 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3293 template<typename _Tp
>
3294 inline pair
<const _Tp
&, const _Tp
&>
3295 minmax(const _Tp
& __a
, const _Tp
& __b
)
3297 // concept requirements
3298 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3300 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3301 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3305 * @brief Determines min and max at once as an ordered pair.
3306 * @ingroup sorting_algorithms
3307 * @param __a A thing of arbitrary type.
3308 * @param __b Another thing of arbitrary type.
3309 * @param __comp A @link comparison_functors comparison functor @endlink.
3310 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3313 template<typename _Tp
, typename _Compare
>
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 pair
<_ForwardIterator
, _ForwardIterator
>
3323 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3326 _ForwardIterator __next
= __first
;
3327 if (__first
== __last
3328 || ++__next
== __last
)
3329 return std::make_pair(__first
, __first
);
3331 _ForwardIterator __min
, __max
;
3332 if (__comp(__next
, __first
))
3346 while (__first
!= __last
)
3349 if (++__next
== __last
)
3351 if (__comp(__first
, __min
))
3353 else if (!__comp(__first
, __max
))
3358 if (__comp(__next
, __first
))
3360 if (__comp(__next
, __min
))
3362 if (!__comp(__first
, __max
))
3367 if (__comp(__first
, __min
))
3369 if (!__comp(__next
, __max
))
3377 return std::make_pair(__min
, __max
);
3381 * @brief Return a pair of iterators pointing to the minimum and maximum
3382 * elements in a range.
3383 * @ingroup sorting_algorithms
3384 * @param __first Start of range.
3385 * @param __last End of range.
3386 * @return make_pair(m, M), where m is the first iterator i in
3387 * [__first, __last) such that no other element in the range is
3388 * smaller, and where M is the last iterator i in [__first, __last)
3389 * such that no other element in the range is larger.
3391 template<typename _ForwardIterator
>
3392 inline pair
<_ForwardIterator
, _ForwardIterator
>
3393 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3395 // concept requirements
3396 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3397 __glibcxx_function_requires(_LessThanComparableConcept
<
3398 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3399 __glibcxx_requires_valid_range(__first
, __last
);
3401 return std::__minmax_element(__first
, __last
,
3402 __gnu_cxx::__ops::__iter_less_iter());
3406 * @brief Return a pair of iterators pointing to the minimum and maximum
3407 * elements in a range.
3408 * @ingroup sorting_algorithms
3409 * @param __first Start of range.
3410 * @param __last End of range.
3411 * @param __comp Comparison functor.
3412 * @return make_pair(m, M), where m is the first iterator i in
3413 * [__first, __last) such that no other element in the range is
3414 * smaller, and where M is the last iterator i in [__first, __last)
3415 * such that no other element in the range is larger.
3417 template<typename _ForwardIterator
, typename _Compare
>
3418 inline pair
<_ForwardIterator
, _ForwardIterator
>
3419 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3422 // concept requirements
3423 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3424 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3425 typename iterator_traits
<_ForwardIterator
>::value_type
,
3426 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3427 __glibcxx_requires_valid_range(__first
, __last
);
3429 return std::__minmax_element(__first
, __last
,
3430 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3434 template<typename _Tp
>
3436 min(initializer_list
<_Tp
> __l
)
3437 { return *std::min_element(__l
.begin(), __l
.end()); }
3439 template<typename _Tp
, typename _Compare
>
3441 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3442 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3444 template<typename _Tp
>
3446 max(initializer_list
<_Tp
> __l
)
3447 { return *std::max_element(__l
.begin(), __l
.end()); }
3449 template<typename _Tp
, typename _Compare
>
3451 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3452 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3454 template<typename _Tp
>
3455 inline pair
<_Tp
, _Tp
>
3456 minmax(initializer_list
<_Tp
> __l
)
3458 pair
<const _Tp
*, const _Tp
*> __p
=
3459 std::minmax_element(__l
.begin(), __l
.end());
3460 return std::make_pair(*__p
.first
, *__p
.second
);
3463 template<typename _Tp
, typename _Compare
>
3464 inline pair
<_Tp
, _Tp
>
3465 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3467 pair
<const _Tp
*, const _Tp
*> __p
=
3468 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3469 return std::make_pair(*__p
.first
, *__p
.second
);
3472 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3473 typename _BinaryPredicate
>
3475 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3476 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3478 // Efficiently compare identical prefixes: O(N) if sequences
3479 // have the same elements in the same order.
3480 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
3481 if (!__pred(__first1
, __first2
))
3484 if (__first1
== __last1
)
3487 // Establish __last2 assuming equal ranges by iterating over the
3488 // rest of the list.
3489 _ForwardIterator2 __last2
= __first2
;
3490 std::advance(__last2
, std::distance(__first1
, __last1
));
3491 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3493 if (__scan
!= std::__find_if(__first1
, __scan
,
3494 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3495 continue; // We've seen this one before.
3498 = std::__count_if(__first2
, __last2
,
3499 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3500 if (0 == __matches
||
3501 std::__count_if(__scan
, __last1
,
3502 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3510 * @brief Checks whether a permutation of the second sequence is equal
3511 * to the first sequence.
3512 * @ingroup non_mutating_algorithms
3513 * @param __first1 Start of first range.
3514 * @param __last1 End of first range.
3515 * @param __first2 Start of second range.
3516 * @return true if there exists a permutation of the elements in the range
3517 * [__first2, __first2 + (__last1 - __first1)), beginning with
3518 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3519 * returns true; otherwise, returns false.
3521 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3523 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3524 _ForwardIterator2 __first2
)
3526 // concept requirements
3527 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3528 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3529 __glibcxx_function_requires(_EqualOpConcept
<
3530 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3531 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3532 __glibcxx_requires_valid_range(__first1
, __last1
);
3534 return std::__is_permutation(__first1
, __last1
, __first2
,
3535 __gnu_cxx::__ops::__iter_equal_to_iter());
3539 * @brief Checks whether a permutation of the second sequence is equal
3540 * to the first sequence.
3541 * @ingroup non_mutating_algorithms
3542 * @param __first1 Start of first range.
3543 * @param __last1 End of first range.
3544 * @param __first2 Start of second range.
3545 * @param __pred A binary predicate.
3546 * @return true if there exists a permutation of the elements in
3547 * the range [__first2, __first2 + (__last1 - __first1)),
3548 * beginning with ForwardIterator2 begin, such that
3549 * equal(__first1, __last1, __begin, __pred) returns true;
3550 * otherwise, returns false.
3552 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3553 typename _BinaryPredicate
>
3555 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3556 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3558 // concept requirements
3559 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3560 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3561 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3562 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3563 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3564 __glibcxx_requires_valid_range(__first1
, __last1
);
3566 return std::__is_permutation(__first1
, __last1
, __first2
,
3567 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3570 #if __cplusplus > 201103L
3571 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3572 typename _BinaryPredicate
>
3574 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3575 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3576 _BinaryPredicate __pred
)
3579 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3581 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3582 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3583 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3584 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3587 auto __d1
= std::distance(__first1
, __last1
);
3588 auto __d2
= std::distance(__first2
, __last2
);
3593 // Efficiently compare identical prefixes: O(N) if sequences
3594 // have the same elements in the same order.
3595 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
3596 if (!__pred(__first1
, __first2
))
3601 if (__first1
== __last1
)
3606 auto __d1
= std::distance(__first1
, __last1
);
3607 auto __d2
= std::distance(__first2
, __last2
);
3608 if (__d1
== 0 && __d2
== 0)
3614 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3616 if (__scan
!= std::__find_if(__first1
, __scan
,
3617 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3618 continue; // We've seen this one before.
3620 auto __matches
= std::__count_if(__first2
, __last2
,
3621 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3623 || std::__count_if(__scan
, __last1
,
3624 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3632 * @brief Checks whether a permutaion of the second sequence is equal
3633 * to the first sequence.
3634 * @ingroup non_mutating_algorithms
3635 * @param __first1 Start of first range.
3636 * @param __last1 End of first range.
3637 * @param __first2 Start of second range.
3638 * @param __last2 End of first range.
3639 * @return true if there exists a permutation of the elements in the range
3640 * [__first2, __last2), beginning with ForwardIterator2 begin,
3641 * such that equal(__first1, __last1, begin) returns true;
3642 * otherwise, returns false.
3644 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3646 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3647 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3649 __glibcxx_requires_valid_range(__first1
, __last1
);
3650 __glibcxx_requires_valid_range(__first2
, __last2
);
3653 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3654 __gnu_cxx::__ops::__iter_equal_to_iter());
3658 * @brief Checks whether a permutation of the second sequence is equal
3659 * to the first sequence.
3660 * @ingroup non_mutating_algorithms
3661 * @param __first1 Start of first range.
3662 * @param __last1 End of first range.
3663 * @param __first2 Start of second range.
3664 * @param __last2 End of first range.
3665 * @param __pred A binary predicate.
3666 * @return true if there exists a permutation of the elements in the range
3667 * [__first2, __last2), beginning with ForwardIterator2 begin,
3668 * such that equal(__first1, __last1, __begin, __pred) returns true;
3669 * otherwise, returns false.
3671 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3672 typename _BinaryPredicate
>
3674 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3675 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3676 _BinaryPredicate __pred
)
3678 __glibcxx_requires_valid_range(__first1
, __last1
);
3679 __glibcxx_requires_valid_range(__first2
, __last2
);
3681 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3682 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3686 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3688 * @brief Shuffle the elements of a sequence using a uniform random
3690 * @ingroup mutating_algorithms
3691 * @param __first A forward iterator.
3692 * @param __last A forward iterator.
3693 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3696 * Reorders the elements in the range @p [__first,__last) using @p __g to
3697 * provide random numbers.
3699 template<typename _RandomAccessIterator
,
3700 typename _UniformRandomNumberGenerator
>
3702 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3703 _UniformRandomNumberGenerator
&& __g
)
3705 // concept requirements
3706 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3707 _RandomAccessIterator
>)
3708 __glibcxx_requires_valid_range(__first
, __last
);
3710 if (__first
== __last
)
3713 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3716 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3717 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3718 typedef typename
__distr_type::param_type __p_type
;
3721 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3722 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3728 _GLIBCXX_END_NAMESPACE_VERSION
3730 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3733 * @brief Apply a function to every element of a sequence.
3734 * @ingroup non_mutating_algorithms
3735 * @param __first An input iterator.
3736 * @param __last An input iterator.
3737 * @param __f A unary function object.
3738 * @return @p __f (std::move(@p __f) in C++0x).
3740 * Applies the function object @p __f to each element in the range
3741 * @p [first,last). @p __f must not modify the order of the sequence.
3742 * If @p __f has a return value it is ignored.
3744 template<typename _InputIterator
, typename _Function
>
3746 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3748 // concept requirements
3749 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3750 __glibcxx_requires_valid_range(__first
, __last
);
3751 for (; __first
!= __last
; ++__first
)
3753 return _GLIBCXX_MOVE(__f
);
3757 * @brief Find the first occurrence of a value in a sequence.
3758 * @ingroup non_mutating_algorithms
3759 * @param __first An input iterator.
3760 * @param __last An input iterator.
3761 * @param __val The value to find.
3762 * @return The first iterator @c i in the range @p [__first,__last)
3763 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3765 template<typename _InputIterator
, typename _Tp
>
3766 inline _InputIterator
3767 find(_InputIterator __first
, _InputIterator __last
,
3770 // concept requirements
3771 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3772 __glibcxx_function_requires(_EqualOpConcept
<
3773 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3774 __glibcxx_requires_valid_range(__first
, __last
);
3775 return std::__find_if(__first
, __last
,
3776 __gnu_cxx::__ops::__iter_equals_val(__val
));
3780 * @brief Find the first element in a sequence for which a
3781 * predicate is true.
3782 * @ingroup non_mutating_algorithms
3783 * @param __first An input iterator.
3784 * @param __last An input iterator.
3785 * @param __pred A predicate.
3786 * @return The first iterator @c i in the range @p [__first,__last)
3787 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3789 template<typename _InputIterator
, typename _Predicate
>
3790 inline _InputIterator
3791 find_if(_InputIterator __first
, _InputIterator __last
,
3794 // concept requirements
3795 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3796 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3797 typename iterator_traits
<_InputIterator
>::value_type
>)
3798 __glibcxx_requires_valid_range(__first
, __last
);
3800 return std::__find_if(__first
, __last
,
3801 __gnu_cxx::__ops::__pred_iter(__pred
));
3805 * @brief Find element from a set in a sequence.
3806 * @ingroup non_mutating_algorithms
3807 * @param __first1 Start of range to search.
3808 * @param __last1 End of range to search.
3809 * @param __first2 Start of match candidates.
3810 * @param __last2 End of match candidates.
3811 * @return The first iterator @c i in the range
3812 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3813 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3815 * Searches the range @p [__first1,__last1) for an element that is
3816 * equal to some element in the range [__first2,__last2). If
3817 * found, returns an iterator in the range [__first1,__last1),
3818 * otherwise returns @p __last1.
3820 template<typename _InputIterator
, typename _ForwardIterator
>
3822 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3823 _ForwardIterator __first2
, _ForwardIterator __last2
)
3825 // concept requirements
3826 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3827 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3828 __glibcxx_function_requires(_EqualOpConcept
<
3829 typename iterator_traits
<_InputIterator
>::value_type
,
3830 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3831 __glibcxx_requires_valid_range(__first1
, __last1
);
3832 __glibcxx_requires_valid_range(__first2
, __last2
);
3834 for (; __first1
!= __last1
; ++__first1
)
3835 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3836 if (*__first1
== *__iter
)
3842 * @brief Find element from a set in a sequence using a predicate.
3843 * @ingroup non_mutating_algorithms
3844 * @param __first1 Start of range to search.
3845 * @param __last1 End of range to search.
3846 * @param __first2 Start of match candidates.
3847 * @param __last2 End of match candidates.
3848 * @param __comp Predicate to use.
3849 * @return The first iterator @c i in the range
3850 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3851 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3852 * such iterator exists.
3855 * Searches the range @p [__first1,__last1) for an element that is
3856 * equal to some element in the range [__first2,__last2). If
3857 * found, returns an iterator in the range [__first1,__last1),
3858 * otherwise returns @p __last1.
3860 template<typename _InputIterator
, typename _ForwardIterator
,
3861 typename _BinaryPredicate
>
3863 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3864 _ForwardIterator __first2
, _ForwardIterator __last2
,
3865 _BinaryPredicate __comp
)
3867 // concept requirements
3868 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3869 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3870 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3871 typename iterator_traits
<_InputIterator
>::value_type
,
3872 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3873 __glibcxx_requires_valid_range(__first1
, __last1
);
3874 __glibcxx_requires_valid_range(__first2
, __last2
);
3876 for (; __first1
!= __last1
; ++__first1
)
3877 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3878 if (__comp(*__first1
, *__iter
))
3884 * @brief Find two adjacent values in a sequence that are equal.
3885 * @ingroup non_mutating_algorithms
3886 * @param __first A forward iterator.
3887 * @param __last A forward iterator.
3888 * @return The first iterator @c i such that @c i and @c i+1 are both
3889 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3890 * or @p __last if no such iterator exists.
3892 template<typename _ForwardIterator
>
3893 inline _ForwardIterator
3894 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3896 // concept requirements
3897 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3898 __glibcxx_function_requires(_EqualityComparableConcept
<
3899 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3900 __glibcxx_requires_valid_range(__first
, __last
);
3902 return std::__adjacent_find(__first
, __last
,
3903 __gnu_cxx::__ops::__iter_equal_to_iter());
3907 * @brief Find two adjacent values in a sequence using a predicate.
3908 * @ingroup non_mutating_algorithms
3909 * @param __first A forward iterator.
3910 * @param __last A forward iterator.
3911 * @param __binary_pred A binary predicate.
3912 * @return The first iterator @c i such that @c i and @c i+1 are both
3913 * valid iterators in @p [__first,__last) and such that
3914 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3917 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3918 inline _ForwardIterator
3919 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3920 _BinaryPredicate __binary_pred
)
3922 // concept requirements
3923 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3924 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3925 typename iterator_traits
<_ForwardIterator
>::value_type
,
3926 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3927 __glibcxx_requires_valid_range(__first
, __last
);
3929 return std::__adjacent_find(__first
, __last
,
3930 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
3934 * @brief Count the number of copies of a value in a sequence.
3935 * @ingroup non_mutating_algorithms
3936 * @param __first An input iterator.
3937 * @param __last An input iterator.
3938 * @param __value The value to be counted.
3939 * @return The number of iterators @c i in the range @p [__first,__last)
3940 * for which @c *i == @p __value
3942 template<typename _InputIterator
, typename _Tp
>
3943 inline typename iterator_traits
<_InputIterator
>::difference_type
3944 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3946 // concept requirements
3947 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3948 __glibcxx_function_requires(_EqualOpConcept
<
3949 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3950 __glibcxx_requires_valid_range(__first
, __last
);
3952 return std::__count_if(__first
, __last
,
3953 __gnu_cxx::__ops::__iter_equals_val(__value
));
3957 * @brief Count the elements of a sequence for which a predicate is true.
3958 * @ingroup non_mutating_algorithms
3959 * @param __first An input iterator.
3960 * @param __last An input iterator.
3961 * @param __pred A predicate.
3962 * @return The number of iterators @c i in the range @p [__first,__last)
3963 * for which @p __pred(*i) is true.
3965 template<typename _InputIterator
, typename _Predicate
>
3966 inline typename iterator_traits
<_InputIterator
>::difference_type
3967 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3969 // concept requirements
3970 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3971 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3972 typename iterator_traits
<_InputIterator
>::value_type
>)
3973 __glibcxx_requires_valid_range(__first
, __last
);
3975 return std::__count_if(__first
, __last
,
3976 __gnu_cxx::__ops::__pred_iter(__pred
));
3980 * @brief Search a sequence for a matching sub-sequence.
3981 * @ingroup non_mutating_algorithms
3982 * @param __first1 A forward iterator.
3983 * @param __last1 A forward iterator.
3984 * @param __first2 A forward iterator.
3985 * @param __last2 A forward iterator.
3986 * @return The first iterator @c i in the range @p
3987 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
3988 * *(__first2+N) for each @c N in the range @p
3989 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
3991 * Searches the range @p [__first1,__last1) for a sub-sequence that
3992 * compares equal value-by-value with the sequence given by @p
3993 * [__first2,__last2) and returns an iterator to the first element
3994 * of the sub-sequence, or @p __last1 if the sub-sequence is not
3997 * Because the sub-sequence must lie completely within the range @p
3998 * [__first1,__last1) it must start at a position less than @p
3999 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4000 * length of the sub-sequence.
4002 * This means that the returned iterator @c i will be in the range
4003 * @p [__first1,__last1-(__last2-__first2))
4005 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4006 inline _ForwardIterator1
4007 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4008 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4010 // concept requirements
4011 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4012 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4013 __glibcxx_function_requires(_EqualOpConcept
<
4014 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4015 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4016 __glibcxx_requires_valid_range(__first1
, __last1
);
4017 __glibcxx_requires_valid_range(__first2
, __last2
);
4019 return std::__search(__first1
, __last1
, __first2
, __last2
,
4020 __gnu_cxx::__ops::__iter_equal_to_iter());
4024 * @brief Search a sequence for a matching sub-sequence using a predicate.
4025 * @ingroup non_mutating_algorithms
4026 * @param __first1 A forward iterator.
4027 * @param __last1 A forward iterator.
4028 * @param __first2 A forward iterator.
4029 * @param __last2 A forward iterator.
4030 * @param __predicate A binary predicate.
4031 * @return The first iterator @c i in the range
4032 * @p [__first1,__last1-(__last2-__first2)) such that
4033 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4034 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4036 * Searches the range @p [__first1,__last1) for a sub-sequence that
4037 * compares equal value-by-value with the sequence given by @p
4038 * [__first2,__last2), using @p __predicate to determine equality,
4039 * and returns an iterator to the first element of the
4040 * sub-sequence, or @p __last1 if no such iterator exists.
4042 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4044 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4045 typename _BinaryPredicate
>
4046 inline _ForwardIterator1
4047 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4048 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4049 _BinaryPredicate __predicate
)
4051 // concept requirements
4052 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4053 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4054 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4055 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4056 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4057 __glibcxx_requires_valid_range(__first1
, __last1
);
4058 __glibcxx_requires_valid_range(__first2
, __last2
);
4060 return std::__search(__first1
, __last1
, __first2
, __last2
,
4061 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4065 * @brief Search a sequence for a number of consecutive values.
4066 * @ingroup non_mutating_algorithms
4067 * @param __first A forward iterator.
4068 * @param __last A forward iterator.
4069 * @param __count The number of consecutive values.
4070 * @param __val The value to find.
4071 * @return The first iterator @c i in the range @p
4072 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4073 * each @c N in the range @p [0,__count), or @p __last if no such
4076 * Searches the range @p [__first,__last) for @p count consecutive elements
4077 * equal to @p __val.
4079 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4080 inline _ForwardIterator
4081 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4082 _Integer __count
, const _Tp
& __val
)
4084 // concept requirements
4085 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4086 __glibcxx_function_requires(_EqualOpConcept
<
4087 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4088 __glibcxx_requires_valid_range(__first
, __last
);
4090 return std::__search_n(__first
, __last
, __count
,
4091 __gnu_cxx::__ops::__iter_equals_val(__val
));
4096 * @brief Search a sequence for a number of consecutive values using a
4098 * @ingroup non_mutating_algorithms
4099 * @param __first A forward iterator.
4100 * @param __last A forward iterator.
4101 * @param __count The number of consecutive values.
4102 * @param __val The value to find.
4103 * @param __binary_pred A binary predicate.
4104 * @return The first iterator @c i in the range @p
4105 * [__first,__last-__count) such that @p
4106 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4107 * @p [0,__count), or @p __last if no such iterator exists.
4109 * Searches the range @p [__first,__last) for @p __count
4110 * consecutive elements for which the predicate returns true.
4112 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4113 typename _BinaryPredicate
>
4114 inline _ForwardIterator
4115 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4116 _Integer __count
, const _Tp
& __val
,
4117 _BinaryPredicate __binary_pred
)
4119 // concept requirements
4120 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4121 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4122 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4123 __glibcxx_requires_valid_range(__first
, __last
);
4125 return std::__search_n(__first
, __last
, __count
,
4126 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4131 * @brief Perform an operation on a sequence.
4132 * @ingroup mutating_algorithms
4133 * @param __first An input iterator.
4134 * @param __last An input iterator.
4135 * @param __result An output iterator.
4136 * @param __unary_op A unary operator.
4137 * @return An output iterator equal to @p __result+(__last-__first).
4139 * Applies the operator to each element in the input range and assigns
4140 * the results to successive elements of the output sequence.
4141 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4142 * range @p [0,__last-__first).
4144 * @p unary_op must not alter its argument.
4146 template<typename _InputIterator
, typename _OutputIterator
,
4147 typename _UnaryOperation
>
4149 transform(_InputIterator __first
, _InputIterator __last
,
4150 _OutputIterator __result
, _UnaryOperation __unary_op
)
4152 // concept requirements
4153 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4154 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4155 // "the type returned by a _UnaryOperation"
4156 __typeof__(__unary_op(*__first
))>)
4157 __glibcxx_requires_valid_range(__first
, __last
);
4159 for (; __first
!= __last
; ++__first
, ++__result
)
4160 *__result
= __unary_op(*__first
);
4165 * @brief Perform an operation on corresponding elements of two sequences.
4166 * @ingroup mutating_algorithms
4167 * @param __first1 An input iterator.
4168 * @param __last1 An input iterator.
4169 * @param __first2 An input iterator.
4170 * @param __result An output iterator.
4171 * @param __binary_op A binary operator.
4172 * @return An output iterator equal to @p result+(last-first).
4174 * Applies the operator to the corresponding elements in the two
4175 * input ranges and assigns the results to successive elements of the
4178 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4179 * @c N in the range @p [0,__last1-__first1).
4181 * @p binary_op must not alter either of its arguments.
4183 template<typename _InputIterator1
, typename _InputIterator2
,
4184 typename _OutputIterator
, typename _BinaryOperation
>
4186 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4187 _InputIterator2 __first2
, _OutputIterator __result
,
4188 _BinaryOperation __binary_op
)
4190 // concept requirements
4191 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4192 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4193 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4194 // "the type returned by a _BinaryOperation"
4195 __typeof__(__binary_op(*__first1
,*__first2
))>)
4196 __glibcxx_requires_valid_range(__first1
, __last1
);
4198 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
4199 *__result
= __binary_op(*__first1
, *__first2
);
4204 * @brief Replace each occurrence of one value in a sequence with another
4206 * @ingroup mutating_algorithms
4207 * @param __first A forward iterator.
4208 * @param __last A forward iterator.
4209 * @param __old_value The value to be replaced.
4210 * @param __new_value The replacement value.
4211 * @return replace() returns no value.
4213 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4214 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4216 template<typename _ForwardIterator
, typename _Tp
>
4218 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4219 const _Tp
& __old_value
, const _Tp
& __new_value
)
4221 // concept requirements
4222 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4224 __glibcxx_function_requires(_EqualOpConcept
<
4225 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4226 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4227 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4228 __glibcxx_requires_valid_range(__first
, __last
);
4230 for (; __first
!= __last
; ++__first
)
4231 if (*__first
== __old_value
)
4232 *__first
= __new_value
;
4236 * @brief Replace each value in a sequence for which a predicate returns
4237 * true with another value.
4238 * @ingroup mutating_algorithms
4239 * @param __first A forward iterator.
4240 * @param __last A forward iterator.
4241 * @param __pred A predicate.
4242 * @param __new_value The replacement value.
4243 * @return replace_if() returns no value.
4245 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4246 * is true then the assignment @c *i = @p __new_value is performed.
4248 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4250 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4251 _Predicate __pred
, const _Tp
& __new_value
)
4253 // concept requirements
4254 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4256 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4257 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4258 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4259 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4260 __glibcxx_requires_valid_range(__first
, __last
);
4262 for (; __first
!= __last
; ++__first
)
4263 if (__pred(*__first
))
4264 *__first
= __new_value
;
4268 * @brief Assign the result of a function object to each value in a
4270 * @ingroup mutating_algorithms
4271 * @param __first A forward iterator.
4272 * @param __last A forward iterator.
4273 * @param __gen A function object taking no arguments and returning
4274 * std::iterator_traits<_ForwardIterator>::value_type
4275 * @return generate() returns no value.
4277 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4278 * @p [__first,__last).
4280 template<typename _ForwardIterator
, typename _Generator
>
4282 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4285 // concept requirements
4286 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4287 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4288 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4289 __glibcxx_requires_valid_range(__first
, __last
);
4291 for (; __first
!= __last
; ++__first
)
4296 * @brief Assign the result of a function object to each value in a
4298 * @ingroup mutating_algorithms
4299 * @param __first A forward iterator.
4300 * @param __n The length of the sequence.
4301 * @param __gen A function object taking no arguments and returning
4302 * std::iterator_traits<_ForwardIterator>::value_type
4303 * @return The end of the sequence, @p __first+__n
4305 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4306 * @p [__first,__first+__n).
4308 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4309 * DR 865. More algorithms that throw away information
4311 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4313 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4315 // concept requirements
4316 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4317 // "the type returned by a _Generator"
4318 __typeof__(__gen())>)
4320 for (__decltype(__n
+ 0) __niter
= __n
;
4321 __niter
> 0; --__niter
, ++__first
)
4327 * @brief Copy a sequence, removing consecutive duplicate values.
4328 * @ingroup mutating_algorithms
4329 * @param __first An input iterator.
4330 * @param __last An input iterator.
4331 * @param __result An output iterator.
4332 * @return An iterator designating the end of the resulting sequence.
4334 * Copies each element in the range @p [__first,__last) to the range
4335 * beginning at @p __result, except that only the first element is copied
4336 * from groups of consecutive elements that compare equal.
4337 * unique_copy() is stable, so the relative order of elements that are
4338 * copied is unchanged.
4340 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4341 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4343 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4344 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4347 template<typename _InputIterator
, typename _OutputIterator
>
4348 inline _OutputIterator
4349 unique_copy(_InputIterator __first
, _InputIterator __last
,
4350 _OutputIterator __result
)
4352 // concept requirements
4353 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4354 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4355 typename iterator_traits
<_InputIterator
>::value_type
>)
4356 __glibcxx_function_requires(_EqualityComparableConcept
<
4357 typename iterator_traits
<_InputIterator
>::value_type
>)
4358 __glibcxx_requires_valid_range(__first
, __last
);
4360 if (__first
== __last
)
4362 return std::__unique_copy(__first
, __last
, __result
,
4363 __gnu_cxx::__ops::__iter_equal_to_iter(),
4364 std::__iterator_category(__first
),
4365 std::__iterator_category(__result
));
4369 * @brief Copy a sequence, removing consecutive values using a predicate.
4370 * @ingroup mutating_algorithms
4371 * @param __first An input iterator.
4372 * @param __last An input iterator.
4373 * @param __result An output iterator.
4374 * @param __binary_pred A binary predicate.
4375 * @return An iterator designating the end of the resulting sequence.
4377 * Copies each element in the range @p [__first,__last) to the range
4378 * beginning at @p __result, except that only the first element is copied
4379 * from groups of consecutive elements for which @p __binary_pred returns
4381 * unique_copy() is stable, so the relative order of elements that are
4382 * copied is unchanged.
4384 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4385 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4387 template<typename _InputIterator
, typename _OutputIterator
,
4388 typename _BinaryPredicate
>
4389 inline _OutputIterator
4390 unique_copy(_InputIterator __first
, _InputIterator __last
,
4391 _OutputIterator __result
,
4392 _BinaryPredicate __binary_pred
)
4394 // concept requirements -- predicates checked later
4395 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4396 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4397 typename iterator_traits
<_InputIterator
>::value_type
>)
4398 __glibcxx_requires_valid_range(__first
, __last
);
4400 if (__first
== __last
)
4402 return std::__unique_copy(__first
, __last
, __result
,
4403 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4404 std::__iterator_category(__first
),
4405 std::__iterator_category(__result
));
4409 * @brief Randomly shuffle the elements of a sequence.
4410 * @ingroup mutating_algorithms
4411 * @param __first A forward iterator.
4412 * @param __last A forward iterator.
4415 * Reorder the elements in the range @p [__first,__last) using a random
4416 * distribution, so that every possible ordering of the sequence is
4419 template<typename _RandomAccessIterator
>
4421 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4423 // concept requirements
4424 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4425 _RandomAccessIterator
>)
4426 __glibcxx_requires_valid_range(__first
, __last
);
4428 if (__first
!= __last
)
4429 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4430 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
4434 * @brief Shuffle the elements of a sequence using a random number
4436 * @ingroup mutating_algorithms
4437 * @param __first A forward iterator.
4438 * @param __last A forward iterator.
4439 * @param __rand The RNG functor or function.
4442 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4443 * provide a random distribution. Calling @p __rand(N) for a positive
4444 * integer @p N should return a randomly chosen integer from the
4447 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4449 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4450 #if __cplusplus >= 201103L
4451 _RandomNumberGenerator
&& __rand
)
4453 _RandomNumberGenerator
& __rand
)
4456 // concept requirements
4457 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4458 _RandomAccessIterator
>)
4459 __glibcxx_requires_valid_range(__first
, __last
);
4461 if (__first
== __last
)
4463 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4464 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
4469 * @brief Move elements for which a predicate is true to the beginning
4471 * @ingroup mutating_algorithms
4472 * @param __first A forward iterator.
4473 * @param __last A forward iterator.
4474 * @param __pred A predicate functor.
4475 * @return An iterator @p middle such that @p __pred(i) is true for each
4476 * iterator @p i in the range @p [__first,middle) and false for each @p i
4477 * in the range @p [middle,__last).
4479 * @p __pred must not modify its operand. @p partition() does not preserve
4480 * the relative ordering of elements in each group, use
4481 * @p stable_partition() if this is needed.
4483 template<typename _ForwardIterator
, typename _Predicate
>
4484 inline _ForwardIterator
4485 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4488 // concept requirements
4489 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4491 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4492 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4493 __glibcxx_requires_valid_range(__first
, __last
);
4495 return std::__partition(__first
, __last
, __pred
,
4496 std::__iterator_category(__first
));
4501 * @brief Sort the smallest elements of a sequence.
4502 * @ingroup sorting_algorithms
4503 * @param __first An iterator.
4504 * @param __middle Another iterator.
4505 * @param __last Another iterator.
4508 * Sorts the smallest @p (__middle-__first) elements in the range
4509 * @p [first,last) and moves them to the range @p [__first,__middle). The
4510 * order of the remaining elements in the range @p [__middle,__last) is
4512 * After the sort if @e i and @e j are iterators in the range
4513 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4514 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4516 template<typename _RandomAccessIterator
>
4518 partial_sort(_RandomAccessIterator __first
,
4519 _RandomAccessIterator __middle
,
4520 _RandomAccessIterator __last
)
4522 // concept requirements
4523 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4524 _RandomAccessIterator
>)
4525 __glibcxx_function_requires(_LessThanComparableConcept
<
4526 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4527 __glibcxx_requires_valid_range(__first
, __middle
);
4528 __glibcxx_requires_valid_range(__middle
, __last
);
4530 std::__partial_sort(__first
, __middle
, __last
,
4531 __gnu_cxx::__ops::__iter_less_iter());
4535 * @brief Sort the smallest elements of a sequence using a predicate
4537 * @ingroup sorting_algorithms
4538 * @param __first An iterator.
4539 * @param __middle Another iterator.
4540 * @param __last Another iterator.
4541 * @param __comp A comparison functor.
4544 * Sorts the smallest @p (__middle-__first) elements in the range
4545 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4546 * order of the remaining elements in the range @p [__middle,__last) is
4548 * After the sort if @e i and @e j are iterators in the range
4549 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4550 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4553 template<typename _RandomAccessIterator
, typename _Compare
>
4555 partial_sort(_RandomAccessIterator __first
,
4556 _RandomAccessIterator __middle
,
4557 _RandomAccessIterator __last
,
4560 // concept requirements
4561 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4562 _RandomAccessIterator
>)
4563 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4564 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4565 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4566 __glibcxx_requires_valid_range(__first
, __middle
);
4567 __glibcxx_requires_valid_range(__middle
, __last
);
4569 std::__partial_sort(__first
, __middle
, __last
,
4570 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4574 * @brief Sort a sequence just enough to find a particular position.
4575 * @ingroup sorting_algorithms
4576 * @param __first An iterator.
4577 * @param __nth Another iterator.
4578 * @param __last Another iterator.
4581 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4582 * is the same element that would have been in that position had the
4583 * whole sequence been sorted. The elements either side of @p *__nth are
4584 * not completely sorted, but for any iterator @e i in the range
4585 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4586 * holds that *j < *i is false.
4588 template<typename _RandomAccessIterator
>
4590 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4591 _RandomAccessIterator __last
)
4593 // concept requirements
4594 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4595 _RandomAccessIterator
>)
4596 __glibcxx_function_requires(_LessThanComparableConcept
<
4597 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4598 __glibcxx_requires_valid_range(__first
, __nth
);
4599 __glibcxx_requires_valid_range(__nth
, __last
);
4601 if (__first
== __last
|| __nth
== __last
)
4604 std::__introselect(__first
, __nth
, __last
,
4605 std::__lg(__last
- __first
) * 2,
4606 __gnu_cxx::__ops::__iter_less_iter());
4610 * @brief Sort a sequence just enough to find a particular position
4611 * using a predicate for comparison.
4612 * @ingroup sorting_algorithms
4613 * @param __first An iterator.
4614 * @param __nth Another iterator.
4615 * @param __last Another iterator.
4616 * @param __comp A comparison functor.
4619 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4620 * is the same element that would have been in that position had the
4621 * whole sequence been sorted. The elements either side of @p *__nth are
4622 * not completely sorted, but for any iterator @e i in the range
4623 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4624 * holds that @p __comp(*j,*i) is false.
4626 template<typename _RandomAccessIterator
, typename _Compare
>
4628 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4629 _RandomAccessIterator __last
, _Compare __comp
)
4631 // concept requirements
4632 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4633 _RandomAccessIterator
>)
4634 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4635 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4636 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4637 __glibcxx_requires_valid_range(__first
, __nth
);
4638 __glibcxx_requires_valid_range(__nth
, __last
);
4640 if (__first
== __last
|| __nth
== __last
)
4643 std::__introselect(__first
, __nth
, __last
,
4644 std::__lg(__last
- __first
) * 2,
4645 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4649 * @brief Sort the elements of a sequence.
4650 * @ingroup sorting_algorithms
4651 * @param __first An iterator.
4652 * @param __last Another iterator.
4655 * Sorts the elements in the range @p [__first,__last) in ascending order,
4656 * such that for each iterator @e i in the range @p [__first,__last-1),
4657 * *(i+1)<*i is false.
4659 * The relative ordering of equivalent elements is not preserved, use
4660 * @p stable_sort() if this is needed.
4662 template<typename _RandomAccessIterator
>
4664 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4666 // concept requirements
4667 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4668 _RandomAccessIterator
>)
4669 __glibcxx_function_requires(_LessThanComparableConcept
<
4670 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4671 __glibcxx_requires_valid_range(__first
, __last
);
4673 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4677 * @brief Sort the elements of a sequence using a predicate for comparison.
4678 * @ingroup sorting_algorithms
4679 * @param __first An iterator.
4680 * @param __last Another iterator.
4681 * @param __comp A comparison functor.
4684 * Sorts the elements in the range @p [__first,__last) in ascending order,
4685 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4686 * range @p [__first,__last-1).
4688 * The relative ordering of equivalent elements is not preserved, use
4689 * @p stable_sort() if this is needed.
4691 template<typename _RandomAccessIterator
, typename _Compare
>
4693 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4696 // concept requirements
4697 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4698 _RandomAccessIterator
>)
4699 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4700 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4701 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4702 __glibcxx_requires_valid_range(__first
, __last
);
4704 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4707 template<typename _InputIterator1
, typename _InputIterator2
,
4708 typename _OutputIterator
, typename _Compare
>
4710 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4711 _InputIterator2 __first2
, _InputIterator2 __last2
,
4712 _OutputIterator __result
, _Compare __comp
)
4714 while (__first1
!= __last1
&& __first2
!= __last2
)
4716 if (__comp(__first2
, __first1
))
4718 *__result
= *__first2
;
4723 *__result
= *__first1
;
4728 return std::copy(__first2
, __last2
,
4729 std::copy(__first1
, __last1
, __result
));
4733 * @brief Merges two sorted ranges.
4734 * @ingroup sorting_algorithms
4735 * @param __first1 An iterator.
4736 * @param __first2 Another iterator.
4737 * @param __last1 Another iterator.
4738 * @param __last2 Another iterator.
4739 * @param __result An iterator pointing to the end of the merged range.
4740 * @return An iterator pointing to the first element <em>not less
4743 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4744 * the sorted range @p [__result, __result + (__last1-__first1) +
4745 * (__last2-__first2)). Both input ranges must be sorted, and the
4746 * output range must not overlap with either of the input ranges.
4747 * The sort is @e stable, that is, for equivalent elements in the
4748 * two ranges, elements from the first range will always come
4749 * before elements from the second.
4751 template<typename _InputIterator1
, typename _InputIterator2
,
4752 typename _OutputIterator
>
4753 inline _OutputIterator
4754 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4755 _InputIterator2 __first2
, _InputIterator2 __last2
,
4756 _OutputIterator __result
)
4758 // concept requirements
4759 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4760 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4761 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4762 typename iterator_traits
<_InputIterator1
>::value_type
>)
4763 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4764 typename iterator_traits
<_InputIterator2
>::value_type
>)
4765 __glibcxx_function_requires(_LessThanOpConcept
<
4766 typename iterator_traits
<_InputIterator2
>::value_type
,
4767 typename iterator_traits
<_InputIterator1
>::value_type
>)
4768 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4769 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4771 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4772 __first2
, __last2
, __result
,
4773 __gnu_cxx::__ops::__iter_less_iter());
4777 * @brief Merges two sorted ranges.
4778 * @ingroup sorting_algorithms
4779 * @param __first1 An iterator.
4780 * @param __first2 Another iterator.
4781 * @param __last1 Another iterator.
4782 * @param __last2 Another iterator.
4783 * @param __result An iterator pointing to the end of the merged range.
4784 * @param __comp A functor to use for comparisons.
4785 * @return An iterator pointing to the first element "not less
4788 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4789 * the sorted range @p [__result, __result + (__last1-__first1) +
4790 * (__last2-__first2)). Both input ranges must be sorted, and the
4791 * output range must not overlap with either of the input ranges.
4792 * The sort is @e stable, that is, for equivalent elements in the
4793 * two ranges, elements from the first range will always come
4794 * before elements from the second.
4796 * The comparison function should have the same effects on ordering as
4797 * the function used for the initial sort.
4799 template<typename _InputIterator1
, typename _InputIterator2
,
4800 typename _OutputIterator
, typename _Compare
>
4801 inline _OutputIterator
4802 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4803 _InputIterator2 __first2
, _InputIterator2 __last2
,
4804 _OutputIterator __result
, _Compare __comp
)
4806 // concept requirements
4807 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4808 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4809 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4810 typename iterator_traits
<_InputIterator1
>::value_type
>)
4811 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4812 typename iterator_traits
<_InputIterator2
>::value_type
>)
4813 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4814 typename iterator_traits
<_InputIterator2
>::value_type
,
4815 typename iterator_traits
<_InputIterator1
>::value_type
>)
4816 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4817 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4819 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4820 __first2
, __last2
, __result
,
4821 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4824 template<typename _RandomAccessIterator
, typename _Compare
>
4826 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4829 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4831 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4834 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
4835 _TmpBuf
__buf(__first
, __last
);
4837 if (__buf
.begin() == 0)
4838 std::__inplace_stable_sort(__first
, __last
, __comp
);
4840 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4841 _DistanceType(__buf
.size()), __comp
);
4845 * @brief Sort the elements of a sequence, preserving the relative order
4846 * of equivalent elements.
4847 * @ingroup sorting_algorithms
4848 * @param __first An iterator.
4849 * @param __last Another iterator.
4852 * Sorts the elements in the range @p [__first,__last) in ascending order,
4853 * such that for each iterator @p i in the range @p [__first,__last-1),
4854 * @p *(i+1)<*i is false.
4856 * The relative ordering of equivalent elements is preserved, so any two
4857 * elements @p x and @p y in the range @p [__first,__last) such that
4858 * @p x<y is false and @p y<x is false will have the same relative
4859 * ordering after calling @p stable_sort().
4861 template<typename _RandomAccessIterator
>
4863 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4865 // concept requirements
4866 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4867 _RandomAccessIterator
>)
4868 __glibcxx_function_requires(_LessThanComparableConcept
<
4869 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4870 __glibcxx_requires_valid_range(__first
, __last
);
4872 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4873 __gnu_cxx::__ops::__iter_less_iter());
4877 * @brief Sort the elements of a sequence using a predicate for comparison,
4878 * preserving the relative order of equivalent elements.
4879 * @ingroup sorting_algorithms
4880 * @param __first An iterator.
4881 * @param __last Another iterator.
4882 * @param __comp A comparison functor.
4885 * Sorts the elements in the range @p [__first,__last) in ascending order,
4886 * such that for each iterator @p i in the range @p [__first,__last-1),
4887 * @p __comp(*(i+1),*i) is false.
4889 * The relative ordering of equivalent elements is preserved, so any two
4890 * elements @p x and @p y in the range @p [__first,__last) such that
4891 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
4892 * relative ordering after calling @p stable_sort().
4894 template<typename _RandomAccessIterator
, typename _Compare
>
4896 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4899 // concept requirements
4900 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4901 _RandomAccessIterator
>)
4902 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4903 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4904 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4905 __glibcxx_requires_valid_range(__first
, __last
);
4907 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4908 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4911 template<typename _InputIterator1
, typename _InputIterator2
,
4912 typename _OutputIterator
,
4915 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4916 _InputIterator2 __first2
, _InputIterator2 __last2
,
4917 _OutputIterator __result
, _Compare __comp
)
4919 while (__first1
!= __last1
&& __first2
!= __last2
)
4921 if (__comp(__first1
, __first2
))
4923 *__result
= *__first1
;
4926 else if (__comp(__first2
, __first1
))
4928 *__result
= *__first2
;
4933 *__result
= *__first1
;
4939 return std::copy(__first2
, __last2
,
4940 std::copy(__first1
, __last1
, __result
));
4944 * @brief Return the union of two sorted ranges.
4945 * @ingroup set_algorithms
4946 * @param __first1 Start of first range.
4947 * @param __last1 End of first range.
4948 * @param __first2 Start of second range.
4949 * @param __last2 End of second range.
4950 * @return End of the output range.
4951 * @ingroup set_algorithms
4953 * This operation iterates over both ranges, copying elements present in
4954 * each range in order to the output range. Iterators increment for each
4955 * range. When the current element of one range is less than the other,
4956 * that element is copied and the iterator advanced. If an element is
4957 * contained in both ranges, the element from the first range is copied and
4958 * both ranges advance. The output range may not overlap either input
4961 template<typename _InputIterator1
, typename _InputIterator2
,
4962 typename _OutputIterator
>
4963 inline _OutputIterator
4964 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4965 _InputIterator2 __first2
, _InputIterator2 __last2
,
4966 _OutputIterator __result
)
4968 // concept requirements
4969 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4970 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4971 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4972 typename iterator_traits
<_InputIterator1
>::value_type
>)
4973 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4974 typename iterator_traits
<_InputIterator2
>::value_type
>)
4975 __glibcxx_function_requires(_LessThanOpConcept
<
4976 typename iterator_traits
<_InputIterator1
>::value_type
,
4977 typename iterator_traits
<_InputIterator2
>::value_type
>)
4978 __glibcxx_function_requires(_LessThanOpConcept
<
4979 typename iterator_traits
<_InputIterator2
>::value_type
,
4980 typename iterator_traits
<_InputIterator1
>::value_type
>)
4981 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4982 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4984 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
4985 __first2
, __last2
, __result
,
4986 __gnu_cxx::__ops::__iter_less_iter());
4990 * @brief Return the union of two sorted ranges using a comparison functor.
4991 * @ingroup set_algorithms
4992 * @param __first1 Start of first range.
4993 * @param __last1 End of first range.
4994 * @param __first2 Start of second range.
4995 * @param __last2 End of second range.
4996 * @param __comp The comparison functor.
4997 * @return End of the output range.
4998 * @ingroup set_algorithms
5000 * This operation iterates over both ranges, copying elements present in
5001 * each range in order to the output range. Iterators increment for each
5002 * range. When the current element of one range is less than the other
5003 * according to @p __comp, that element is copied and the iterator advanced.
5004 * If an equivalent element according to @p __comp is contained in both
5005 * ranges, the element from the first range is copied and both ranges
5006 * advance. The output range may not overlap either input range.
5008 template<typename _InputIterator1
, typename _InputIterator2
,
5009 typename _OutputIterator
, typename _Compare
>
5010 inline _OutputIterator
5011 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5012 _InputIterator2 __first2
, _InputIterator2 __last2
,
5013 _OutputIterator __result
, _Compare __comp
)
5015 // concept requirements
5016 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5017 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5018 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5019 typename iterator_traits
<_InputIterator1
>::value_type
>)
5020 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5021 typename iterator_traits
<_InputIterator2
>::value_type
>)
5022 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5023 typename iterator_traits
<_InputIterator1
>::value_type
,
5024 typename iterator_traits
<_InputIterator2
>::value_type
>)
5025 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5026 typename iterator_traits
<_InputIterator2
>::value_type
,
5027 typename iterator_traits
<_InputIterator1
>::value_type
>)
5028 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5029 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5031 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5032 __first2
, __last2
, __result
,
5033 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5036 template<typename _InputIterator1
, typename _InputIterator2
,
5037 typename _OutputIterator
,
5040 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5041 _InputIterator2 __first2
, _InputIterator2 __last2
,
5042 _OutputIterator __result
, _Compare __comp
)
5044 while (__first1
!= __last1
&& __first2
!= __last2
)
5045 if (__comp(__first1
, __first2
))
5047 else if (__comp(__first2
, __first1
))
5051 *__result
= *__first1
;
5060 * @brief Return the intersection of two sorted ranges.
5061 * @ingroup set_algorithms
5062 * @param __first1 Start of first range.
5063 * @param __last1 End of first range.
5064 * @param __first2 Start of second range.
5065 * @param __last2 End of second range.
5066 * @return End of the output range.
5067 * @ingroup set_algorithms
5069 * This operation iterates over both ranges, copying elements present in
5070 * both ranges in order to the output range. Iterators increment for each
5071 * range. When the current element of one range is less than the other,
5072 * that iterator advances. If an element is contained in both ranges, the
5073 * element from the first range is copied and both ranges advance. The
5074 * output range may not overlap either input range.
5076 template<typename _InputIterator1
, typename _InputIterator2
,
5077 typename _OutputIterator
>
5078 inline _OutputIterator
5079 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5080 _InputIterator2 __first2
, _InputIterator2 __last2
,
5081 _OutputIterator __result
)
5083 // concept requirements
5084 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5085 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5086 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5087 typename iterator_traits
<_InputIterator1
>::value_type
>)
5088 __glibcxx_function_requires(_LessThanOpConcept
<
5089 typename iterator_traits
<_InputIterator1
>::value_type
,
5090 typename iterator_traits
<_InputIterator2
>::value_type
>)
5091 __glibcxx_function_requires(_LessThanOpConcept
<
5092 typename iterator_traits
<_InputIterator2
>::value_type
,
5093 typename iterator_traits
<_InputIterator1
>::value_type
>)
5094 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5095 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5097 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5098 __first2
, __last2
, __result
,
5099 __gnu_cxx::__ops::__iter_less_iter());
5103 * @brief Return the intersection of two sorted ranges using comparison
5105 * @ingroup set_algorithms
5106 * @param __first1 Start of first range.
5107 * @param __last1 End of first range.
5108 * @param __first2 Start of second range.
5109 * @param __last2 End of second range.
5110 * @param __comp The comparison functor.
5111 * @return End of the output range.
5112 * @ingroup set_algorithms
5114 * This operation iterates over both ranges, copying elements present in
5115 * both ranges in order to the output range. Iterators increment for each
5116 * range. When the current element of one range is less than the other
5117 * according to @p __comp, that iterator advances. If an element is
5118 * contained in both ranges according to @p __comp, the element from the
5119 * first range is copied and both ranges advance. The output range may not
5120 * overlap either input range.
5122 template<typename _InputIterator1
, typename _InputIterator2
,
5123 typename _OutputIterator
, typename _Compare
>
5124 inline _OutputIterator
5125 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5126 _InputIterator2 __first2
, _InputIterator2 __last2
,
5127 _OutputIterator __result
, _Compare __comp
)
5129 // concept requirements
5130 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5131 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5132 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5133 typename iterator_traits
<_InputIterator1
>::value_type
>)
5134 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5135 typename iterator_traits
<_InputIterator1
>::value_type
,
5136 typename iterator_traits
<_InputIterator2
>::value_type
>)
5137 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5138 typename iterator_traits
<_InputIterator2
>::value_type
,
5139 typename iterator_traits
<_InputIterator1
>::value_type
>)
5140 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5141 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5143 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5144 __first2
, __last2
, __result
,
5145 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5148 template<typename _InputIterator1
, typename _InputIterator2
,
5149 typename _OutputIterator
,
5152 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5153 _InputIterator2 __first2
, _InputIterator2 __last2
,
5154 _OutputIterator __result
, _Compare __comp
)
5156 while (__first1
!= __last1
&& __first2
!= __last2
)
5157 if (__comp(__first1
, __first2
))
5159 *__result
= *__first1
;
5163 else if (__comp(__first2
, __first1
))
5170 return std::copy(__first1
, __last1
, __result
);
5174 * @brief Return the difference of two sorted ranges.
5175 * @ingroup set_algorithms
5176 * @param __first1 Start of first range.
5177 * @param __last1 End of first range.
5178 * @param __first2 Start of second range.
5179 * @param __last2 End of second range.
5180 * @return End of the output range.
5181 * @ingroup set_algorithms
5183 * This operation iterates over both ranges, copying elements present in
5184 * the first range but not the second in order to the output range.
5185 * Iterators increment for each range. When the current element of the
5186 * first range is less than the second, that element is copied and the
5187 * iterator advances. If the current element of the second range is less,
5188 * the iterator advances, but no element is copied. If an element is
5189 * contained in both ranges, no elements are copied and both ranges
5190 * advance. The output range may not overlap either input range.
5192 template<typename _InputIterator1
, typename _InputIterator2
,
5193 typename _OutputIterator
>
5194 inline _OutputIterator
5195 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5196 _InputIterator2 __first2
, _InputIterator2 __last2
,
5197 _OutputIterator __result
)
5199 // concept requirements
5200 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5201 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5202 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5203 typename iterator_traits
<_InputIterator1
>::value_type
>)
5204 __glibcxx_function_requires(_LessThanOpConcept
<
5205 typename iterator_traits
<_InputIterator1
>::value_type
,
5206 typename iterator_traits
<_InputIterator2
>::value_type
>)
5207 __glibcxx_function_requires(_LessThanOpConcept
<
5208 typename iterator_traits
<_InputIterator2
>::value_type
,
5209 typename iterator_traits
<_InputIterator1
>::value_type
>)
5210 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5211 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5213 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5214 __first2
, __last2
, __result
,
5215 __gnu_cxx::__ops::__iter_less_iter());
5219 * @brief Return the difference of two sorted ranges using comparison
5221 * @ingroup set_algorithms
5222 * @param __first1 Start of first range.
5223 * @param __last1 End of first range.
5224 * @param __first2 Start of second range.
5225 * @param __last2 End of second range.
5226 * @param __comp The comparison functor.
5227 * @return End of the output range.
5228 * @ingroup set_algorithms
5230 * This operation iterates over both ranges, copying elements present in
5231 * the first range but not the second in order to the output range.
5232 * Iterators increment for each range. When the current element of the
5233 * first range is less than the second according to @p __comp, that element
5234 * is copied and the iterator advances. If the current element of the
5235 * second range is less, no element is copied and the iterator advances.
5236 * If an element is contained in both ranges according to @p __comp, no
5237 * elements are copied and both ranges advance. The output range may not
5238 * overlap either input range.
5240 template<typename _InputIterator1
, typename _InputIterator2
,
5241 typename _OutputIterator
, typename _Compare
>
5242 inline _OutputIterator
5243 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5244 _InputIterator2 __first2
, _InputIterator2 __last2
,
5245 _OutputIterator __result
, _Compare __comp
)
5247 // concept requirements
5248 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5249 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5250 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5251 typename iterator_traits
<_InputIterator1
>::value_type
>)
5252 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5253 typename iterator_traits
<_InputIterator1
>::value_type
,
5254 typename iterator_traits
<_InputIterator2
>::value_type
>)
5255 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5256 typename iterator_traits
<_InputIterator2
>::value_type
,
5257 typename iterator_traits
<_InputIterator1
>::value_type
>)
5258 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5259 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5261 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5262 __first2
, __last2
, __result
,
5263 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5266 template<typename _InputIterator1
, typename _InputIterator2
,
5267 typename _OutputIterator
,
5270 __set_symmetric_difference(_InputIterator1 __first1
,
5271 _InputIterator1 __last1
,
5272 _InputIterator2 __first2
,
5273 _InputIterator2 __last2
,
5274 _OutputIterator __result
,
5277 while (__first1
!= __last1
&& __first2
!= __last2
)
5278 if (__comp(__first1
, __first2
))
5280 *__result
= *__first1
;
5284 else if (__comp(__first2
, __first1
))
5286 *__result
= *__first2
;
5295 return std::copy(__first2
, __last2
,
5296 std::copy(__first1
, __last1
, __result
));
5300 * @brief Return the symmetric difference of two sorted ranges.
5301 * @ingroup set_algorithms
5302 * @param __first1 Start of first range.
5303 * @param __last1 End of first range.
5304 * @param __first2 Start of second range.
5305 * @param __last2 End of second range.
5306 * @return End of the output range.
5307 * @ingroup set_algorithms
5309 * This operation iterates over both ranges, copying elements present in
5310 * one range but not the other in order to the output range. Iterators
5311 * increment for each range. When the current element of one range is less
5312 * than the other, that element is copied and the iterator advances. If an
5313 * element is contained in both ranges, no elements are copied and both
5314 * ranges advance. The output range may not overlap either input range.
5316 template<typename _InputIterator1
, typename _InputIterator2
,
5317 typename _OutputIterator
>
5318 inline _OutputIterator
5319 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5320 _InputIterator2 __first2
, _InputIterator2 __last2
,
5321 _OutputIterator __result
)
5323 // concept requirements
5324 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5325 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5326 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5327 typename iterator_traits
<_InputIterator1
>::value_type
>)
5328 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5329 typename iterator_traits
<_InputIterator2
>::value_type
>)
5330 __glibcxx_function_requires(_LessThanOpConcept
<
5331 typename iterator_traits
<_InputIterator1
>::value_type
,
5332 typename iterator_traits
<_InputIterator2
>::value_type
>)
5333 __glibcxx_function_requires(_LessThanOpConcept
<
5334 typename iterator_traits
<_InputIterator2
>::value_type
,
5335 typename iterator_traits
<_InputIterator1
>::value_type
>)
5336 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5337 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5339 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5340 __first2
, __last2
, __result
,
5341 __gnu_cxx::__ops::__iter_less_iter());
5345 * @brief Return the symmetric difference of two sorted ranges using
5346 * comparison functor.
5347 * @ingroup set_algorithms
5348 * @param __first1 Start of first range.
5349 * @param __last1 End of first range.
5350 * @param __first2 Start of second range.
5351 * @param __last2 End of second range.
5352 * @param __comp The comparison functor.
5353 * @return End of the output range.
5354 * @ingroup set_algorithms
5356 * This operation iterates over both ranges, copying elements present in
5357 * one range but not the other in order to the output range. Iterators
5358 * increment for each range. When the current element of one range is less
5359 * than the other according to @p comp, that element is copied and the
5360 * iterator advances. If an element is contained in both ranges according
5361 * to @p __comp, no elements are copied and both ranges advance. The output
5362 * range may not overlap either input range.
5364 template<typename _InputIterator1
, typename _InputIterator2
,
5365 typename _OutputIterator
, typename _Compare
>
5366 inline _OutputIterator
5367 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5368 _InputIterator2 __first2
, _InputIterator2 __last2
,
5369 _OutputIterator __result
,
5372 // concept requirements
5373 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5374 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5375 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5376 typename iterator_traits
<_InputIterator1
>::value_type
>)
5377 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5378 typename iterator_traits
<_InputIterator2
>::value_type
>)
5379 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5380 typename iterator_traits
<_InputIterator1
>::value_type
,
5381 typename iterator_traits
<_InputIterator2
>::value_type
>)
5382 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5383 typename iterator_traits
<_InputIterator2
>::value_type
,
5384 typename iterator_traits
<_InputIterator1
>::value_type
>)
5385 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5386 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5388 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5389 __first2
, __last2
, __result
,
5390 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5393 template<typename _ForwardIterator
, typename _Compare
>
5395 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5398 if (__first
== __last
)
5400 _ForwardIterator __result
= __first
;
5401 while (++__first
!= __last
)
5402 if (__comp(__first
, __result
))
5408 * @brief Return the minimum element in a range.
5409 * @ingroup sorting_algorithms
5410 * @param __first Start of range.
5411 * @param __last End of range.
5412 * @return Iterator referencing the first instance of the smallest value.
5414 template<typename _ForwardIterator
>
5416 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5418 // concept requirements
5419 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5420 __glibcxx_function_requires(_LessThanComparableConcept
<
5421 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5422 __glibcxx_requires_valid_range(__first
, __last
);
5424 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5425 __gnu_cxx::__ops::__iter_less_iter());
5429 * @brief Return the minimum element in a range using comparison functor.
5430 * @ingroup sorting_algorithms
5431 * @param __first Start of range.
5432 * @param __last End of range.
5433 * @param __comp Comparison functor.
5434 * @return Iterator referencing the first instance of the smallest value
5435 * according to __comp.
5437 template<typename _ForwardIterator
, typename _Compare
>
5438 inline _ForwardIterator
5439 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5442 // concept requirements
5443 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5444 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5445 typename iterator_traits
<_ForwardIterator
>::value_type
,
5446 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5447 __glibcxx_requires_valid_range(__first
, __last
);
5449 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5450 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5453 template<typename _ForwardIterator
, typename _Compare
>
5455 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5458 if (__first
== __last
) return __first
;
5459 _ForwardIterator __result
= __first
;
5460 while (++__first
!= __last
)
5461 if (__comp(__result
, __first
))
5467 * @brief Return the maximum element in a range.
5468 * @ingroup sorting_algorithms
5469 * @param __first Start of range.
5470 * @param __last End of range.
5471 * @return Iterator referencing the first instance of the largest value.
5473 template<typename _ForwardIterator
>
5474 inline _ForwardIterator
5475 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5477 // concept requirements
5478 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5479 __glibcxx_function_requires(_LessThanComparableConcept
<
5480 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5481 __glibcxx_requires_valid_range(__first
, __last
);
5483 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5484 __gnu_cxx::__ops::__iter_less_iter());
5488 * @brief Return the maximum element in a range using comparison functor.
5489 * @ingroup sorting_algorithms
5490 * @param __first Start of range.
5491 * @param __last End of range.
5492 * @param __comp Comparison functor.
5493 * @return Iterator referencing the first instance of the largest value
5494 * according to __comp.
5496 template<typename _ForwardIterator
, typename _Compare
>
5497 inline _ForwardIterator
5498 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5501 // concept requirements
5502 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5503 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5504 typename iterator_traits
<_ForwardIterator
>::value_type
,
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_comp_iter(__comp
));
5512 _GLIBCXX_END_NAMESPACE_ALGO
5515 #endif /* _STL_ALGO_H */