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