1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 <bits/uniform_int_dist.h>
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 inline namespace _V2
1243 /// This is a helper function for the rotate algorithm.
1244 template<typename _ForwardIterator
>
1246 __rotate(_ForwardIterator __first
,
1247 _ForwardIterator __middle
,
1248 _ForwardIterator __last
,
1249 forward_iterator_tag
)
1251 if (__first
== __middle
)
1253 else if (__last
== __middle
)
1256 _ForwardIterator __first2
= __middle
;
1259 std::iter_swap(__first
, __first2
);
1262 if (__first
== __middle
)
1263 __middle
= __first2
;
1265 while (__first2
!= __last
);
1267 _ForwardIterator __ret
= __first
;
1269 __first2
= __middle
;
1271 while (__first2
!= __last
)
1273 std::iter_swap(__first
, __first2
);
1276 if (__first
== __middle
)
1277 __middle
= __first2
;
1278 else if (__first2
== __last
)
1279 __first2
= __middle
;
1284 /// This is a helper function for the rotate algorithm.
1285 template<typename _BidirectionalIterator
>
1286 _BidirectionalIterator
1287 __rotate(_BidirectionalIterator __first
,
1288 _BidirectionalIterator __middle
,
1289 _BidirectionalIterator __last
,
1290 bidirectional_iterator_tag
)
1292 // concept requirements
1293 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1294 _BidirectionalIterator
>)
1296 if (__first
== __middle
)
1298 else if (__last
== __middle
)
1301 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1302 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1304 while (__first
!= __middle
&& __middle
!= __last
)
1306 std::iter_swap(__first
, --__last
);
1310 if (__first
== __middle
)
1312 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1317 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1322 /// This is a helper function for the rotate algorithm.
1323 template<typename _RandomAccessIterator
>
1324 _RandomAccessIterator
1325 __rotate(_RandomAccessIterator __first
,
1326 _RandomAccessIterator __middle
,
1327 _RandomAccessIterator __last
,
1328 random_access_iterator_tag
)
1330 // concept requirements
1331 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1332 _RandomAccessIterator
>)
1334 if (__first
== __middle
)
1336 else if (__last
== __middle
)
1339 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1341 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1344 _Distance __n
= __last
- __first
;
1345 _Distance __k
= __middle
- __first
;
1347 if (__k
== __n
- __k
)
1349 std::swap_ranges(__first
, __middle
, __middle
);
1353 _RandomAccessIterator __p
= __first
;
1354 _RandomAccessIterator __ret
= __first
+ (__last
- __middle
);
1358 if (__k
< __n
- __k
)
1360 if (__is_pod(_ValueType
) && __k
== 1)
1362 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1363 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1364 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1367 _RandomAccessIterator __q
= __p
+ __k
;
1368 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1370 std::iter_swap(__p
, __q
);
1377 std::swap(__n
, __k
);
1383 if (__is_pod(_ValueType
) && __k
== 1)
1385 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1386 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1387 *__p
= _GLIBCXX_MOVE(__t
);
1390 _RandomAccessIterator __q
= __p
+ __n
;
1392 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1396 std::iter_swap(__p
, __q
);
1401 std::swap(__n
, __k
);
1406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1407 // DR 488. rotate throws away useful information
1409 * @brief Rotate the elements of a sequence.
1410 * @ingroup mutating_algorithms
1411 * @param __first A forward iterator.
1412 * @param __middle A forward iterator.
1413 * @param __last A forward iterator.
1414 * @return first + (last - middle).
1416 * Rotates the elements of the range @p [__first,__last) by
1417 * @p (__middle - __first) positions so that the element at @p __middle
1418 * is moved to @p __first, the element at @p __middle+1 is moved to
1419 * @p __first+1 and so on for each element in the range
1420 * @p [__first,__last).
1422 * This effectively swaps the ranges @p [__first,__middle) and
1423 * @p [__middle,__last).
1426 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1427 * for each @p n in the range @p [0,__last-__first).
1429 template<typename _ForwardIterator
>
1430 inline _ForwardIterator
1431 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1432 _ForwardIterator __last
)
1434 // concept requirements
1435 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1437 __glibcxx_requires_valid_range(__first
, __middle
);
1438 __glibcxx_requires_valid_range(__middle
, __last
);
1440 return std::__rotate(__first
, __middle
, __last
,
1441 std::__iterator_category(__first
));
1447 * @brief Copy a sequence, rotating its elements.
1448 * @ingroup mutating_algorithms
1449 * @param __first A forward iterator.
1450 * @param __middle A forward iterator.
1451 * @param __last A forward iterator.
1452 * @param __result An output iterator.
1453 * @return An iterator designating the end of the resulting sequence.
1455 * Copies the elements of the range @p [__first,__last) to the
1456 * range beginning at @result, rotating the copied elements by
1457 * @p (__middle-__first) positions so that the element at @p __middle
1458 * is moved to @p __result, the element at @p __middle+1 is moved
1459 * to @p __result+1 and so on for each element in the range @p
1463 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1464 * for each @p n in the range @p [0,__last-__first).
1466 template<typename _ForwardIterator
, typename _OutputIterator
>
1467 inline _OutputIterator
1468 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1469 _ForwardIterator __last
, _OutputIterator __result
)
1471 // concept requirements
1472 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1473 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1474 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1475 __glibcxx_requires_valid_range(__first
, __middle
);
1476 __glibcxx_requires_valid_range(__middle
, __last
);
1478 return std::copy(__first
, __middle
,
1479 std::copy(__middle
, __last
, __result
));
1482 /// This is a helper function...
1483 template<typename _ForwardIterator
, typename _Predicate
>
1485 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1486 _Predicate __pred
, forward_iterator_tag
)
1488 if (__first
== __last
)
1491 while (__pred(*__first
))
1492 if (++__first
== __last
)
1495 _ForwardIterator __next
= __first
;
1497 while (++__next
!= __last
)
1498 if (__pred(*__next
))
1500 std::iter_swap(__first
, __next
);
1507 /// This is a helper function...
1508 template<typename _BidirectionalIterator
, typename _Predicate
>
1509 _BidirectionalIterator
1510 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1511 _Predicate __pred
, bidirectional_iterator_tag
)
1516 if (__first
== __last
)
1518 else if (__pred(*__first
))
1524 if (__first
== __last
)
1526 else if (!bool(__pred(*__last
)))
1530 std::iter_swap(__first
, __last
);
1537 /// This is a helper function...
1538 /// Requires __first != __last and !__pred(__first)
1539 /// and __len == distance(__first, __last).
1541 /// !__pred(__first) allows us to guarantee that we don't
1542 /// move-assign an element onto itself.
1543 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1546 __stable_partition_adaptive(_ForwardIterator __first
,
1547 _ForwardIterator __last
,
1548 _Predicate __pred
, _Distance __len
,
1550 _Distance __buffer_size
)
1555 if (__len
<= __buffer_size
)
1557 _ForwardIterator __result1
= __first
;
1558 _Pointer __result2
= __buffer
;
1560 // The precondition guarantees that !__pred(__first), so
1561 // move that element to the buffer before starting the loop.
1562 // This ensures that we only call __pred once per element.
1563 *__result2
= _GLIBCXX_MOVE(*__first
);
1566 for (; __first
!= __last
; ++__first
)
1567 if (__pred(__first
))
1569 *__result1
= _GLIBCXX_MOVE(*__first
);
1574 *__result2
= _GLIBCXX_MOVE(*__first
);
1578 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1582 _ForwardIterator __middle
= __first
;
1583 std::advance(__middle
, __len
/ 2);
1584 _ForwardIterator __left_split
=
1585 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1586 __len
/ 2, __buffer
,
1589 // Advance past true-predicate values to satisfy this
1590 // function's preconditions.
1591 _Distance __right_len
= __len
- __len
/ 2;
1592 _ForwardIterator __right_split
=
1593 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1597 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1599 __buffer
, __buffer_size
);
1601 std::rotate(__left_split
, __middle
, __right_split
);
1602 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1603 return __left_split
;
1606 template<typename _ForwardIterator
, typename _Predicate
>
1608 __stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1611 __first
= std::__find_if_not(__first
, __last
, __pred
);
1613 if (__first
== __last
)
1616 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1618 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1621 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
, __last
);
1623 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1624 _DistanceType(__buf
.requested_size()),
1626 _DistanceType(__buf
.size()));
1630 * @brief Move elements for which a predicate is true to the beginning
1631 * of a sequence, preserving relative ordering.
1632 * @ingroup mutating_algorithms
1633 * @param __first A forward iterator.
1634 * @param __last A forward iterator.
1635 * @param __pred A predicate functor.
1636 * @return An iterator @p middle such that @p __pred(i) is true for each
1637 * iterator @p i in the range @p [first,middle) and false for each @p i
1638 * in the range @p [middle,last).
1640 * Performs the same function as @p partition() with the additional
1641 * guarantee that the relative ordering of elements in each group is
1642 * preserved, so any two elements @p x and @p y in the range
1643 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1644 * relative ordering after calling @p stable_partition().
1646 template<typename _ForwardIterator
, typename _Predicate
>
1647 inline _ForwardIterator
1648 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1651 // concept requirements
1652 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1654 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1655 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1656 __glibcxx_requires_valid_range(__first
, __last
);
1658 return std::__stable_partition(__first
, __last
,
1659 __gnu_cxx::__ops::__pred_iter(__pred
));
1662 /// This is a helper function for the sort routines.
1663 template<typename _RandomAccessIterator
, typename _Compare
>
1665 __heap_select(_RandomAccessIterator __first
,
1666 _RandomAccessIterator __middle
,
1667 _RandomAccessIterator __last
, _Compare __comp
)
1669 std::__make_heap(__first
, __middle
, __comp
);
1670 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1671 if (__comp(__i
, __first
))
1672 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1677 template<typename _InputIterator
, typename _RandomAccessIterator
,
1679 _RandomAccessIterator
1680 __partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1681 _RandomAccessIterator __result_first
,
1682 _RandomAccessIterator __result_last
,
1685 typedef typename iterator_traits
<_InputIterator
>::value_type
1687 typedef iterator_traits
<_RandomAccessIterator
> _RItTraits
;
1688 typedef typename
_RItTraits::difference_type _DistanceType
;
1690 if (__result_first
== __result_last
)
1691 return __result_last
;
1692 _RandomAccessIterator __result_real_last
= __result_first
;
1693 while (__first
!= __last
&& __result_real_last
!= __result_last
)
1695 *__result_real_last
= *__first
;
1696 ++__result_real_last
;
1700 std::__make_heap(__result_first
, __result_real_last
, __comp
);
1701 while (__first
!= __last
)
1703 if (__comp(__first
, __result_first
))
1704 std::__adjust_heap(__result_first
, _DistanceType(0),
1705 _DistanceType(__result_real_last
1707 _InputValueType(*__first
), __comp
);
1710 std::__sort_heap(__result_first
, __result_real_last
, __comp
);
1711 return __result_real_last
;
1715 * @brief Copy the smallest elements of a sequence.
1716 * @ingroup sorting_algorithms
1717 * @param __first An iterator.
1718 * @param __last Another iterator.
1719 * @param __result_first A random-access iterator.
1720 * @param __result_last Another random-access iterator.
1721 * @return An iterator indicating the end of the resulting sequence.
1723 * Copies and sorts the smallest N values from the range @p [__first,__last)
1724 * to the range beginning at @p __result_first, where the number of
1725 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1726 * @p (__result_last-__result_first).
1727 * After the sort if @e i and @e j are iterators in the range
1728 * @p [__result_first,__result_first+N) such that i precedes j then
1730 * The value returned is @p __result_first+N.
1732 template<typename _InputIterator
, typename _RandomAccessIterator
>
1733 inline _RandomAccessIterator
1734 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1735 _RandomAccessIterator __result_first
,
1736 _RandomAccessIterator __result_last
)
1738 #ifdef _GLIBCXX_CONCEPT_CHECKS
1739 typedef typename iterator_traits
<_InputIterator
>::value_type
1741 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1745 // concept requirements
1746 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1747 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1749 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1751 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1752 __glibcxx_requires_valid_range(__first
, __last
);
1753 __glibcxx_requires_irreflexive(__first
, __last
);
1754 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1756 return std::__partial_sort_copy(__first
, __last
,
1757 __result_first
, __result_last
,
1758 __gnu_cxx::__ops::__iter_less_iter());
1762 * @brief Copy the smallest elements of a sequence using a predicate for
1764 * @ingroup sorting_algorithms
1765 * @param __first An input iterator.
1766 * @param __last Another input iterator.
1767 * @param __result_first A random-access iterator.
1768 * @param __result_last Another random-access iterator.
1769 * @param __comp A comparison functor.
1770 * @return An iterator indicating the end of the resulting sequence.
1772 * Copies and sorts the smallest N values from the range @p [__first,__last)
1773 * to the range beginning at @p result_first, where the number of
1774 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1775 * @p (__result_last-__result_first).
1776 * After the sort if @e i and @e j are iterators in the range
1777 * @p [__result_first,__result_first+N) such that i precedes j then
1778 * @p __comp(*j,*i) is false.
1779 * The value returned is @p __result_first+N.
1781 template<typename _InputIterator
, typename _RandomAccessIterator
,
1783 inline _RandomAccessIterator
1784 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1785 _RandomAccessIterator __result_first
,
1786 _RandomAccessIterator __result_last
,
1789 #ifdef _GLIBCXX_CONCEPT_CHECKS
1790 typedef typename iterator_traits
<_InputIterator
>::value_type
1792 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1796 // concept requirements
1797 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1798 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1799 _RandomAccessIterator
>)
1800 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1802 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1803 _InputValueType
, _OutputValueType
>)
1804 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
1805 _OutputValueType
, _OutputValueType
>)
1806 __glibcxx_requires_valid_range(__first
, __last
);
1807 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
1808 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1810 return std::__partial_sort_copy(__first
, __last
,
1811 __result_first
, __result_last
,
1812 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
1815 /// This is a helper function for the sort routine.
1816 template<typename _RandomAccessIterator
, typename _Compare
>
1818 __unguarded_linear_insert(_RandomAccessIterator __last
,
1821 typename iterator_traits
<_RandomAccessIterator
>::value_type
1822 __val
= _GLIBCXX_MOVE(*__last
);
1823 _RandomAccessIterator __next
= __last
;
1825 while (__comp(__val
, __next
))
1827 *__last
= _GLIBCXX_MOVE(*__next
);
1831 *__last
= _GLIBCXX_MOVE(__val
);
1834 /// This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator
, typename _Compare
>
1837 __insertion_sort(_RandomAccessIterator __first
,
1838 _RandomAccessIterator __last
, _Compare __comp
)
1840 if (__first
== __last
) return;
1842 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1844 if (__comp(__i
, __first
))
1846 typename iterator_traits
<_RandomAccessIterator
>::value_type
1847 __val
= _GLIBCXX_MOVE(*__i
);
1848 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
1849 *__first
= _GLIBCXX_MOVE(__val
);
1852 std::__unguarded_linear_insert(__i
,
1853 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1857 /// This is a helper function for the sort routine.
1858 template<typename _RandomAccessIterator
, typename _Compare
>
1860 __unguarded_insertion_sort(_RandomAccessIterator __first
,
1861 _RandomAccessIterator __last
, _Compare __comp
)
1863 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
1864 std::__unguarded_linear_insert(__i
,
1865 __gnu_cxx::__ops::__val_comp_iter(__comp
));
1870 * This controls some aspect of the sort routines.
1872 enum { _S_threshold
= 16 };
1874 /// This is a helper function for the sort routine.
1875 template<typename _RandomAccessIterator
, typename _Compare
>
1877 __final_insertion_sort(_RandomAccessIterator __first
,
1878 _RandomAccessIterator __last
, _Compare __comp
)
1880 if (__last
- __first
> int(_S_threshold
))
1882 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
1883 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
1887 std::__insertion_sort(__first
, __last
, __comp
);
1890 /// This is a helper function...
1891 template<typename _RandomAccessIterator
, typename _Compare
>
1892 _RandomAccessIterator
1893 __unguarded_partition(_RandomAccessIterator __first
,
1894 _RandomAccessIterator __last
,
1895 _RandomAccessIterator __pivot
, _Compare __comp
)
1899 while (__comp(__first
, __pivot
))
1902 while (__comp(__pivot
, __last
))
1904 if (!(__first
< __last
))
1906 std::iter_swap(__first
, __last
);
1911 /// This is a helper function...
1912 template<typename _RandomAccessIterator
, typename _Compare
>
1913 inline _RandomAccessIterator
1914 __unguarded_partition_pivot(_RandomAccessIterator __first
,
1915 _RandomAccessIterator __last
, _Compare __comp
)
1917 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
1918 std::__move_median_to_first(__first
, __first
+ 1, __mid
, __last
- 1,
1920 return std::__unguarded_partition(__first
+ 1, __last
, __first
, __comp
);
1923 template<typename _RandomAccessIterator
, typename _Compare
>
1925 __partial_sort(_RandomAccessIterator __first
,
1926 _RandomAccessIterator __middle
,
1927 _RandomAccessIterator __last
,
1930 std::__heap_select(__first
, __middle
, __last
, __comp
);
1931 std::__sort_heap(__first
, __middle
, __comp
);
1934 /// This is a helper function for the sort routine.
1935 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1937 __introsort_loop(_RandomAccessIterator __first
,
1938 _RandomAccessIterator __last
,
1939 _Size __depth_limit
, _Compare __comp
)
1941 while (__last
- __first
> int(_S_threshold
))
1943 if (__depth_limit
== 0)
1945 std::__partial_sort(__first
, __last
, __last
, __comp
);
1949 _RandomAccessIterator __cut
=
1950 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1951 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
1958 template<typename _RandomAccessIterator
, typename _Compare
>
1960 __sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1963 if (__first
!= __last
)
1965 std::__introsort_loop(__first
, __last
,
1966 std::__lg(__last
- __first
) * 2,
1968 std::__final_insertion_sort(__first
, __last
, __comp
);
1972 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
1974 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
1975 _RandomAccessIterator __last
, _Size __depth_limit
,
1978 while (__last
- __first
> 3)
1980 if (__depth_limit
== 0)
1982 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
1983 // Place the nth largest element in its final position.
1984 std::iter_swap(__first
, __nth
);
1988 _RandomAccessIterator __cut
=
1989 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
1995 std::__insertion_sort(__first
, __last
, __comp
);
2000 // lower_bound moved to stl_algobase.h
2003 * @brief Finds the first position in which @p __val could be inserted
2004 * without changing the ordering.
2005 * @ingroup binary_search_algorithms
2006 * @param __first An iterator.
2007 * @param __last Another iterator.
2008 * @param __val The search term.
2009 * @param __comp A functor to use for comparisons.
2010 * @return An iterator pointing to the first element <em>not less
2011 * than</em> @p __val, or end() if every element is less
2013 * @ingroup binary_search_algorithms
2015 * The comparison function should have the same effects on ordering as
2016 * the function used for the initial sort.
2018 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2019 inline _ForwardIterator
2020 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2021 const _Tp
& __val
, _Compare __comp
)
2023 // concept requirements
2024 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2025 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2026 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2027 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2029 __glibcxx_requires_irreflexive_pred2(__first
, __last
, __comp
);
2031 return std::__lower_bound(__first
, __last
, __val
,
2032 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2035 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2037 __upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2038 const _Tp
& __val
, _Compare __comp
)
2040 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2043 _DistanceType __len
= std::distance(__first
, __last
);
2047 _DistanceType __half
= __len
>> 1;
2048 _ForwardIterator __middle
= __first
;
2049 std::advance(__middle
, __half
);
2050 if (__comp(__val
, __middle
))
2056 __len
= __len
- __half
- 1;
2063 * @brief Finds the last position in which @p __val could be inserted
2064 * without changing the ordering.
2065 * @ingroup binary_search_algorithms
2066 * @param __first An iterator.
2067 * @param __last Another iterator.
2068 * @param __val The search term.
2069 * @return An iterator pointing to the first element greater than @p __val,
2070 * or end() if no elements are greater than @p __val.
2071 * @ingroup binary_search_algorithms
2073 template<typename _ForwardIterator
, typename _Tp
>
2074 inline _ForwardIterator
2075 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2078 // concept requirements
2079 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2080 __glibcxx_function_requires(_LessThanOpConcept
<
2081 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2082 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2083 __glibcxx_requires_irreflexive2(__first
, __last
);
2085 return std::__upper_bound(__first
, __last
, __val
,
2086 __gnu_cxx::__ops::__val_less_iter());
2090 * @brief Finds the last position in which @p __val could be inserted
2091 * without changing the ordering.
2092 * @ingroup binary_search_algorithms
2093 * @param __first An iterator.
2094 * @param __last Another iterator.
2095 * @param __val The search term.
2096 * @param __comp A functor to use for comparisons.
2097 * @return An iterator pointing to the first element greater than @p __val,
2098 * or end() if no elements are greater than @p __val.
2099 * @ingroup binary_search_algorithms
2101 * The comparison function should have the same effects on ordering as
2102 * the function used for the initial sort.
2104 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2105 inline _ForwardIterator
2106 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2107 const _Tp
& __val
, _Compare __comp
)
2109 // concept requirements
2110 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2111 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2112 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2113 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2115 __glibcxx_requires_irreflexive_pred2(__first
, __last
, __comp
);
2117 return std::__upper_bound(__first
, __last
, __val
,
2118 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2121 template<typename _ForwardIterator
, typename _Tp
,
2122 typename _CompareItTp
, typename _CompareTpIt
>
2123 pair
<_ForwardIterator
, _ForwardIterator
>
2124 __equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2126 _CompareItTp __comp_it_val
, _CompareTpIt __comp_val_it
)
2128 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2131 _DistanceType __len
= std::distance(__first
, __last
);
2135 _DistanceType __half
= __len
>> 1;
2136 _ForwardIterator __middle
= __first
;
2137 std::advance(__middle
, __half
);
2138 if (__comp_it_val(__middle
, __val
))
2142 __len
= __len
- __half
- 1;
2144 else if (__comp_val_it(__val
, __middle
))
2148 _ForwardIterator __left
2149 = std::__lower_bound(__first
, __middle
, __val
, __comp_it_val
);
2150 std::advance(__first
, __len
);
2151 _ForwardIterator __right
2152 = std::__upper_bound(++__middle
, __first
, __val
, __comp_val_it
);
2153 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2156 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2160 * @brief Finds the largest subrange in which @p __val could be inserted
2161 * at any place in it without changing the ordering.
2162 * @ingroup binary_search_algorithms
2163 * @param __first An iterator.
2164 * @param __last Another iterator.
2165 * @param __val The search term.
2166 * @return An pair of iterators defining the subrange.
2167 * @ingroup binary_search_algorithms
2169 * This is equivalent to
2171 * std::make_pair(lower_bound(__first, __last, __val),
2172 * upper_bound(__first, __last, __val))
2174 * but does not actually call those functions.
2176 template<typename _ForwardIterator
, typename _Tp
>
2177 inline pair
<_ForwardIterator
, _ForwardIterator
>
2178 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2181 // concept requirements
2182 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2183 __glibcxx_function_requires(_LessThanOpConcept
<
2184 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2185 __glibcxx_function_requires(_LessThanOpConcept
<
2186 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2187 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2188 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2189 __glibcxx_requires_irreflexive2(__first
, __last
);
2191 return std::__equal_range(__first
, __last
, __val
,
2192 __gnu_cxx::__ops::__iter_less_val(),
2193 __gnu_cxx::__ops::__val_less_iter());
2197 * @brief Finds the largest subrange in which @p __val could be inserted
2198 * at any place in it without changing the ordering.
2199 * @param __first An iterator.
2200 * @param __last Another iterator.
2201 * @param __val The search term.
2202 * @param __comp A functor to use for comparisons.
2203 * @return An pair of iterators defining the subrange.
2204 * @ingroup binary_search_algorithms
2206 * This is equivalent to
2208 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2209 * upper_bound(__first, __last, __val, __comp))
2211 * but does not actually call those functions.
2213 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2214 inline pair
<_ForwardIterator
, _ForwardIterator
>
2215 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2216 const _Tp
& __val
, _Compare __comp
)
2218 // concept requirements
2219 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2220 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2221 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
2222 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2223 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2224 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2226 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2228 __glibcxx_requires_irreflexive_pred2(__first
, __last
, __comp
);
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 // concept requirements
2253 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2254 __glibcxx_function_requires(_LessThanOpConcept
<
2255 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2256 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2257 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2258 __glibcxx_requires_irreflexive2(__first
, __last
);
2260 _ForwardIterator __i
2261 = std::__lower_bound(__first
, __last
, __val
,
2262 __gnu_cxx::__ops::__iter_less_val());
2263 return __i
!= __last
&& !(__val
< *__i
);
2267 * @brief Determines whether an element exists in a range.
2268 * @ingroup binary_search_algorithms
2269 * @param __first An iterator.
2270 * @param __last Another iterator.
2271 * @param __val The search term.
2272 * @param __comp A functor to use for comparisons.
2273 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2275 * Note that this does not actually return an iterator to @p __val. For
2276 * that, use std::find or a container's specialized find member functions.
2278 * The comparison function should have the same effects on ordering as
2279 * the function used for the initial sort.
2281 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2283 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2284 const _Tp
& __val
, _Compare __comp
)
2286 // concept requirements
2287 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2288 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2289 _Tp
, typename iterator_traits
<_ForwardIterator
>::value_type
>)
2290 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2292 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2294 __glibcxx_requires_irreflexive_pred2(__first
, __last
, __comp
);
2296 _ForwardIterator __i
2297 = std::__lower_bound(__first
, __last
, __val
,
2298 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2299 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2304 /// This is a helper function for the __merge_adaptive routines.
2305 template<typename _InputIterator1
, typename _InputIterator2
,
2306 typename _OutputIterator
, typename _Compare
>
2308 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2309 _InputIterator2 __first2
, _InputIterator2 __last2
,
2310 _OutputIterator __result
, _Compare __comp
)
2312 while (__first1
!= __last1
&& __first2
!= __last2
)
2314 if (__comp(__first2
, __first1
))
2316 *__result
= _GLIBCXX_MOVE(*__first2
);
2321 *__result
= _GLIBCXX_MOVE(*__first1
);
2326 if (__first1
!= __last1
)
2327 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2330 /// This is a helper function for the __merge_adaptive routines.
2331 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2332 typename _BidirectionalIterator3
, typename _Compare
>
2334 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2335 _BidirectionalIterator1 __last1
,
2336 _BidirectionalIterator2 __first2
,
2337 _BidirectionalIterator2 __last2
,
2338 _BidirectionalIterator3 __result
,
2341 if (__first1
== __last1
)
2343 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2346 else if (__first2
== __last2
)
2353 if (__comp(__last2
, __last1
))
2355 *--__result
= _GLIBCXX_MOVE(*__last1
);
2356 if (__first1
== __last1
)
2358 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2365 *--__result
= _GLIBCXX_MOVE(*__last2
);
2366 if (__first2
== __last2
)
2373 /// This is a helper function for the merge routines.
2374 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2376 _BidirectionalIterator1
2377 __rotate_adaptive(_BidirectionalIterator1 __first
,
2378 _BidirectionalIterator1 __middle
,
2379 _BidirectionalIterator1 __last
,
2380 _Distance __len1
, _Distance __len2
,
2381 _BidirectionalIterator2 __buffer
,
2382 _Distance __buffer_size
)
2384 _BidirectionalIterator2 __buffer_end
;
2385 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2389 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2390 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2391 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2396 else if (__len1
<= __buffer_size
)
2400 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2401 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2402 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2409 std::rotate(__first
, __middle
, __last
);
2410 std::advance(__first
, std::distance(__middle
, __last
));
2415 /// This is a helper function for the merge routines.
2416 template<typename _BidirectionalIterator
, typename _Distance
,
2417 typename _Pointer
, typename _Compare
>
2419 __merge_adaptive(_BidirectionalIterator __first
,
2420 _BidirectionalIterator __middle
,
2421 _BidirectionalIterator __last
,
2422 _Distance __len1
, _Distance __len2
,
2423 _Pointer __buffer
, _Distance __buffer_size
,
2426 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2428 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2429 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2432 else if (__len2
<= __buffer_size
)
2434 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2435 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2436 __buffer_end
, __last
, __comp
);
2440 _BidirectionalIterator __first_cut
= __first
;
2441 _BidirectionalIterator __second_cut
= __middle
;
2442 _Distance __len11
= 0;
2443 _Distance __len22
= 0;
2444 if (__len1
> __len2
)
2446 __len11
= __len1
/ 2;
2447 std::advance(__first_cut
, __len11
);
2449 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2450 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2451 __len22
= std::distance(__middle
, __second_cut
);
2455 __len22
= __len2
/ 2;
2456 std::advance(__second_cut
, __len22
);
2458 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2459 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2460 __len11
= std::distance(__first
, __first_cut
);
2463 _BidirectionalIterator __new_middle
2464 = std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2465 __len1
- __len11
, __len22
, __buffer
,
2467 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2468 __len22
, __buffer
, __buffer_size
, __comp
);
2469 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2471 __len2
- __len22
, __buffer
,
2472 __buffer_size
, __comp
);
2476 /// This is a helper function for the merge routines.
2477 template<typename _BidirectionalIterator
, typename _Distance
,
2480 __merge_without_buffer(_BidirectionalIterator __first
,
2481 _BidirectionalIterator __middle
,
2482 _BidirectionalIterator __last
,
2483 _Distance __len1
, _Distance __len2
,
2486 if (__len1
== 0 || __len2
== 0)
2489 if (__len1
+ __len2
== 2)
2491 if (__comp(__middle
, __first
))
2492 std::iter_swap(__first
, __middle
);
2496 _BidirectionalIterator __first_cut
= __first
;
2497 _BidirectionalIterator __second_cut
= __middle
;
2498 _Distance __len11
= 0;
2499 _Distance __len22
= 0;
2500 if (__len1
> __len2
)
2502 __len11
= __len1
/ 2;
2503 std::advance(__first_cut
, __len11
);
2505 = std::__lower_bound(__middle
, __last
, *__first_cut
,
2506 __gnu_cxx::__ops::__iter_comp_val(__comp
));
2507 __len22
= std::distance(__middle
, __second_cut
);
2511 __len22
= __len2
/ 2;
2512 std::advance(__second_cut
, __len22
);
2514 = std::__upper_bound(__first
, __middle
, *__second_cut
,
2515 __gnu_cxx::__ops::__val_comp_iter(__comp
));
2516 __len11
= std::distance(__first
, __first_cut
);
2519 std::rotate(__first_cut
, __middle
, __second_cut
);
2520 _BidirectionalIterator __new_middle
= __first_cut
;
2521 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2522 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2523 __len11
, __len22
, __comp
);
2524 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2525 __len1
- __len11
, __len2
- __len22
, __comp
);
2528 template<typename _BidirectionalIterator
, typename _Compare
>
2530 __inplace_merge(_BidirectionalIterator __first
,
2531 _BidirectionalIterator __middle
,
2532 _BidirectionalIterator __last
,
2535 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
2537 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
2540 if (__first
== __middle
|| __middle
== __last
)
2543 const _DistanceType __len1
= std::distance(__first
, __middle
);
2544 const _DistanceType __len2
= std::distance(__middle
, __last
);
2546 typedef _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> _TmpBuf
;
2547 _TmpBuf
__buf(__first
, __last
);
2549 if (__buf
.begin() == 0)
2550 std::__merge_without_buffer
2551 (__first
, __middle
, __last
, __len1
, __len2
, __comp
);
2553 std::__merge_adaptive
2554 (__first
, __middle
, __last
, __len1
, __len2
, __buf
.begin(),
2555 _DistanceType(__buf
.size()), __comp
);
2559 * @brief Merges two sorted ranges in place.
2560 * @ingroup sorting_algorithms
2561 * @param __first An iterator.
2562 * @param __middle Another iterator.
2563 * @param __last Another iterator.
2566 * Merges two sorted and consecutive ranges, [__first,__middle) and
2567 * [__middle,__last), and puts the result in [__first,__last). The
2568 * output will be sorted. The sort is @e stable, that is, for
2569 * equivalent elements in the two ranges, elements from the first
2570 * range will always come before elements from the second.
2572 * If enough additional memory is available, this takes (__last-__first)-1
2573 * comparisons. Otherwise an NlogN algorithm is used, where N is
2574 * distance(__first,__last).
2576 template<typename _BidirectionalIterator
>
2578 inplace_merge(_BidirectionalIterator __first
,
2579 _BidirectionalIterator __middle
,
2580 _BidirectionalIterator __last
)
2582 // concept requirements
2583 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2584 _BidirectionalIterator
>)
2585 __glibcxx_function_requires(_LessThanComparableConcept
<
2586 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2587 __glibcxx_requires_sorted(__first
, __middle
);
2588 __glibcxx_requires_sorted(__middle
, __last
);
2589 __glibcxx_requires_irreflexive(__first
, __last
);
2591 std::__inplace_merge(__first
, __middle
, __last
,
2592 __gnu_cxx::__ops::__iter_less_iter());
2596 * @brief Merges two sorted ranges in place.
2597 * @ingroup sorting_algorithms
2598 * @param __first An iterator.
2599 * @param __middle Another iterator.
2600 * @param __last Another iterator.
2601 * @param __comp A functor to use for comparisons.
2604 * Merges two sorted and consecutive ranges, [__first,__middle) and
2605 * [middle,last), and puts the result in [__first,__last). The output will
2606 * be sorted. The sort is @e stable, that is, for equivalent
2607 * elements in the two ranges, elements from the first range will always
2608 * come before elements from the second.
2610 * If enough additional memory is available, this takes (__last-__first)-1
2611 * comparisons. Otherwise an NlogN algorithm is used, where N is
2612 * distance(__first,__last).
2614 * The comparison function should have the same effects on ordering as
2615 * the function used for the initial sort.
2617 template<typename _BidirectionalIterator
, typename _Compare
>
2619 inplace_merge(_BidirectionalIterator __first
,
2620 _BidirectionalIterator __middle
,
2621 _BidirectionalIterator __last
,
2624 // concept requirements
2625 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2626 _BidirectionalIterator
>)
2627 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2628 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2629 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2630 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
2631 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
2632 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
2634 std::__inplace_merge(__first
, __middle
, __last
,
2635 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2639 /// This is a helper function for the __merge_sort_loop routines.
2640 template<typename _InputIterator
, typename _OutputIterator
,
2643 __move_merge(_InputIterator __first1
, _InputIterator __last1
,
2644 _InputIterator __first2
, _InputIterator __last2
,
2645 _OutputIterator __result
, _Compare __comp
)
2647 while (__first1
!= __last1
&& __first2
!= __last2
)
2649 if (__comp(__first2
, __first1
))
2651 *__result
= _GLIBCXX_MOVE(*__first2
);
2656 *__result
= _GLIBCXX_MOVE(*__first1
);
2661 return _GLIBCXX_MOVE3(__first2
, __last2
,
2662 _GLIBCXX_MOVE3(__first1
, __last1
,
2666 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2667 typename _Distance
, typename _Compare
>
2669 __merge_sort_loop(_RandomAccessIterator1 __first
,
2670 _RandomAccessIterator1 __last
,
2671 _RandomAccessIterator2 __result
, _Distance __step_size
,
2674 const _Distance __two_step
= 2 * __step_size
;
2676 while (__last
- __first
>= __two_step
)
2678 __result
= std::__move_merge(__first
, __first
+ __step_size
,
2679 __first
+ __step_size
,
2680 __first
+ __two_step
,
2682 __first
+= __two_step
;
2684 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2686 std::__move_merge(__first
, __first
+ __step_size
,
2687 __first
+ __step_size
, __last
, __result
, __comp
);
2690 template<typename _RandomAccessIterator
, typename _Distance
,
2693 __chunk_insertion_sort(_RandomAccessIterator __first
,
2694 _RandomAccessIterator __last
,
2695 _Distance __chunk_size
, _Compare __comp
)
2697 while (__last
- __first
>= __chunk_size
)
2699 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2700 __first
+= __chunk_size
;
2702 std::__insertion_sort(__first
, __last
, __comp
);
2705 enum { _S_chunk_size
= 7 };
2707 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2709 __merge_sort_with_buffer(_RandomAccessIterator __first
,
2710 _RandomAccessIterator __last
,
2711 _Pointer __buffer
, _Compare __comp
)
2713 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2716 const _Distance __len
= __last
- __first
;
2717 const _Pointer __buffer_last
= __buffer
+ __len
;
2719 _Distance __step_size
= _S_chunk_size
;
2720 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2722 while (__step_size
< __len
)
2724 std::__merge_sort_loop(__first
, __last
, __buffer
,
2725 __step_size
, __comp
);
2727 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
2728 __step_size
, __comp
);
2733 template<typename _RandomAccessIterator
, typename _Pointer
,
2734 typename _Distance
, typename _Compare
>
2736 __stable_sort_adaptive(_RandomAccessIterator __first
,
2737 _RandomAccessIterator __last
,
2738 _Pointer __buffer
, _Distance __buffer_size
,
2741 const _Distance __len
= (__last
- __first
+ 1) / 2;
2742 const _RandomAccessIterator __middle
= __first
+ __len
;
2743 if (__len
> __buffer_size
)
2745 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
2746 __buffer_size
, __comp
);
2747 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
2748 __buffer_size
, __comp
);
2752 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
2753 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
2755 std::__merge_adaptive(__first
, __middle
, __last
,
2756 _Distance(__middle
- __first
),
2757 _Distance(__last
- __middle
),
2758 __buffer
, __buffer_size
,
2762 /// This is a helper function for the stable sorting routines.
2763 template<typename _RandomAccessIterator
, typename _Compare
>
2765 __inplace_stable_sort(_RandomAccessIterator __first
,
2766 _RandomAccessIterator __last
, _Compare __comp
)
2768 if (__last
- __first
< 15)
2770 std::__insertion_sort(__first
, __last
, __comp
);
2773 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2774 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2775 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2776 std::__merge_without_buffer(__first
, __middle
, __last
,
2784 // Set algorithms: includes, set_union, set_intersection, set_difference,
2785 // set_symmetric_difference. All of these algorithms have the precondition
2786 // that their input ranges are sorted and the postcondition that their output
2787 // ranges are sorted.
2789 template<typename _InputIterator1
, typename _InputIterator2
,
2792 __includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2793 _InputIterator2 __first2
, _InputIterator2 __last2
,
2796 while (__first1
!= __last1
&& __first2
!= __last2
)
2797 if (__comp(__first2
, __first1
))
2799 else if (__comp(__first1
, __first2
))
2807 return __first2
== __last2
;
2811 * @brief Determines whether all elements of a sequence exists in a range.
2812 * @param __first1 Start of search range.
2813 * @param __last1 End of search range.
2814 * @param __first2 Start of sequence
2815 * @param __last2 End of sequence.
2816 * @return True if each element in [__first2,__last2) is contained in order
2817 * within [__first1,__last1). False otherwise.
2818 * @ingroup set_algorithms
2820 * This operation expects both [__first1,__last1) and
2821 * [__first2,__last2) to be sorted. Searches for the presence of
2822 * each element in [__first2,__last2) within [__first1,__last1).
2823 * The iterators over each range only move forward, so this is a
2824 * linear algorithm. If an element in [__first2,__last2) is not
2825 * found before the search iterator reaches @p __last2, false is
2828 template<typename _InputIterator1
, typename _InputIterator2
>
2830 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2831 _InputIterator2 __first2
, _InputIterator2 __last2
)
2833 // concept requirements
2834 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2835 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2836 __glibcxx_function_requires(_LessThanOpConcept
<
2837 typename iterator_traits
<_InputIterator1
>::value_type
,
2838 typename iterator_traits
<_InputIterator2
>::value_type
>)
2839 __glibcxx_function_requires(_LessThanOpConcept
<
2840 typename iterator_traits
<_InputIterator2
>::value_type
,
2841 typename iterator_traits
<_InputIterator1
>::value_type
>)
2842 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
2843 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
2844 __glibcxx_requires_irreflexive2(__first1
, __last1
);
2845 __glibcxx_requires_irreflexive2(__first2
, __last2
);
2847 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2848 __gnu_cxx::__ops::__iter_less_iter());
2852 * @brief Determines whether all elements of a sequence exists in a range
2854 * @ingroup set_algorithms
2855 * @param __first1 Start of search range.
2856 * @param __last1 End of search range.
2857 * @param __first2 Start of sequence
2858 * @param __last2 End of sequence.
2859 * @param __comp Comparison function to use.
2860 * @return True if each element in [__first2,__last2) is contained
2861 * in order within [__first1,__last1) according to comp. False
2862 * otherwise. @ingroup set_algorithms
2864 * This operation expects both [__first1,__last1) and
2865 * [__first2,__last2) to be sorted. Searches for the presence of
2866 * each element in [__first2,__last2) within [__first1,__last1),
2867 * using comp to decide. The iterators over each range only move
2868 * forward, so this is a linear algorithm. If an element in
2869 * [__first2,__last2) is not found before the search iterator
2870 * reaches @p __last2, false is returned.
2872 template<typename _InputIterator1
, typename _InputIterator2
,
2875 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
2876 _InputIterator2 __first2
, _InputIterator2 __last2
,
2879 // concept requirements
2880 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2881 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2882 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2883 typename iterator_traits
<_InputIterator1
>::value_type
,
2884 typename iterator_traits
<_InputIterator2
>::value_type
>)
2885 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2886 typename iterator_traits
<_InputIterator2
>::value_type
,
2887 typename iterator_traits
<_InputIterator1
>::value_type
>)
2888 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
2889 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
2890 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
2891 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
2893 return std::__includes(__first1
, __last1
, __first2
, __last2
,
2894 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
2903 // set_symmetric_difference
2907 template<typename _BidirectionalIterator
, typename _Compare
>
2909 __next_permutation(_BidirectionalIterator __first
,
2910 _BidirectionalIterator __last
, _Compare __comp
)
2912 if (__first
== __last
)
2914 _BidirectionalIterator __i
= __first
;
2923 _BidirectionalIterator __ii
= __i
;
2925 if (__comp(__i
, __ii
))
2927 _BidirectionalIterator __j
= __last
;
2928 while (!__comp(__i
, --__j
))
2930 std::iter_swap(__i
, __j
);
2931 std::__reverse(__ii
, __last
,
2932 std::__iterator_category(__first
));
2937 std::__reverse(__first
, __last
,
2938 std::__iterator_category(__first
));
2945 * @brief Permute range into the next @e dictionary ordering.
2946 * @ingroup sorting_algorithms
2947 * @param __first Start of range.
2948 * @param __last End of range.
2949 * @return False if wrapped to first permutation, true otherwise.
2951 * Treats all permutations of the range as a set of @e dictionary sorted
2952 * sequences. Permutes the current sequence into the next one of this set.
2953 * Returns true if there are more sequences to generate. If the sequence
2954 * is the largest of the set, the smallest is generated and false returned.
2956 template<typename _BidirectionalIterator
>
2958 next_permutation(_BidirectionalIterator __first
,
2959 _BidirectionalIterator __last
)
2961 // concept requirements
2962 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2963 _BidirectionalIterator
>)
2964 __glibcxx_function_requires(_LessThanComparableConcept
<
2965 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2966 __glibcxx_requires_valid_range(__first
, __last
);
2967 __glibcxx_requires_irreflexive(__first
, __last
);
2969 return std::__next_permutation
2970 (__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
2974 * @brief Permute range into the next @e dictionary ordering using
2975 * comparison functor.
2976 * @ingroup sorting_algorithms
2977 * @param __first Start of range.
2978 * @param __last End of range.
2979 * @param __comp A comparison functor.
2980 * @return False if wrapped to first permutation, true otherwise.
2982 * Treats all permutations of the range [__first,__last) as a set of
2983 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2984 * sequence into the next one of this set. Returns true if there are more
2985 * sequences to generate. If the sequence is the largest of the set, the
2986 * smallest is generated and false returned.
2988 template<typename _BidirectionalIterator
, typename _Compare
>
2990 next_permutation(_BidirectionalIterator __first
,
2991 _BidirectionalIterator __last
, _Compare __comp
)
2993 // concept requirements
2994 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2995 _BidirectionalIterator
>)
2996 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2997 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
2998 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2999 __glibcxx_requires_valid_range(__first
, __last
);
3000 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3002 return std::__next_permutation
3003 (__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3006 template<typename _BidirectionalIterator
, typename _Compare
>
3008 __prev_permutation(_BidirectionalIterator __first
,
3009 _BidirectionalIterator __last
, _Compare __comp
)
3011 if (__first
== __last
)
3013 _BidirectionalIterator __i
= __first
;
3022 _BidirectionalIterator __ii
= __i
;
3024 if (__comp(__ii
, __i
))
3026 _BidirectionalIterator __j
= __last
;
3027 while (!__comp(--__j
, __i
))
3029 std::iter_swap(__i
, __j
);
3030 std::__reverse(__ii
, __last
,
3031 std::__iterator_category(__first
));
3036 std::__reverse(__first
, __last
,
3037 std::__iterator_category(__first
));
3044 * @brief Permute range into the previous @e dictionary ordering.
3045 * @ingroup sorting_algorithms
3046 * @param __first Start of range.
3047 * @param __last End of range.
3048 * @return False if wrapped to last permutation, true otherwise.
3050 * Treats all permutations of the range as a set of @e dictionary sorted
3051 * sequences. Permutes the current sequence into the previous one of this
3052 * set. Returns true if there are more sequences to generate. If the
3053 * sequence is the smallest of the set, the largest is generated and false
3056 template<typename _BidirectionalIterator
>
3058 prev_permutation(_BidirectionalIterator __first
,
3059 _BidirectionalIterator __last
)
3061 // concept requirements
3062 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3063 _BidirectionalIterator
>)
3064 __glibcxx_function_requires(_LessThanComparableConcept
<
3065 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3066 __glibcxx_requires_valid_range(__first
, __last
);
3067 __glibcxx_requires_irreflexive(__first
, __last
);
3069 return std::__prev_permutation(__first
, __last
,
3070 __gnu_cxx::__ops::__iter_less_iter());
3074 * @brief Permute range into the previous @e dictionary ordering using
3075 * comparison functor.
3076 * @ingroup sorting_algorithms
3077 * @param __first Start of range.
3078 * @param __last End of range.
3079 * @param __comp A comparison functor.
3080 * @return False if wrapped to last permutation, true otherwise.
3082 * Treats all permutations of the range [__first,__last) as a set of
3083 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3084 * sequence into the previous one of this set. Returns true if there are
3085 * more sequences to generate. If the sequence is the smallest of the set,
3086 * the largest is generated and false returned.
3088 template<typename _BidirectionalIterator
, typename _Compare
>
3090 prev_permutation(_BidirectionalIterator __first
,
3091 _BidirectionalIterator __last
, _Compare __comp
)
3093 // concept requirements
3094 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3095 _BidirectionalIterator
>)
3096 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3097 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3098 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3099 __glibcxx_requires_valid_range(__first
, __last
);
3100 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3102 return std::__prev_permutation(__first
, __last
,
3103 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3109 template<typename _InputIterator
, typename _OutputIterator
,
3110 typename _Predicate
, typename _Tp
>
3112 __replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3113 _OutputIterator __result
,
3114 _Predicate __pred
, const _Tp
& __new_value
)
3116 for (; __first
!= __last
; ++__first
, (void)++__result
)
3117 if (__pred(__first
))
3118 *__result
= __new_value
;
3120 *__result
= *__first
;
3125 * @brief Copy a sequence, replacing each element of one value with another
3127 * @param __first An input iterator.
3128 * @param __last An input iterator.
3129 * @param __result An output iterator.
3130 * @param __old_value The value to be replaced.
3131 * @param __new_value The replacement value.
3132 * @return The end of the output sequence, @p result+(last-first).
3134 * Copies each element in the input range @p [__first,__last) to the
3135 * output range @p [__result,__result+(__last-__first)) replacing elements
3136 * equal to @p __old_value with @p __new_value.
3138 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3139 inline _OutputIterator
3140 replace_copy(_InputIterator __first
, _InputIterator __last
,
3141 _OutputIterator __result
,
3142 const _Tp
& __old_value
, const _Tp
& __new_value
)
3144 // concept requirements
3145 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3146 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3147 typename iterator_traits
<_InputIterator
>::value_type
>)
3148 __glibcxx_function_requires(_EqualOpConcept
<
3149 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3150 __glibcxx_requires_valid_range(__first
, __last
);
3152 return std::__replace_copy_if(__first
, __last
, __result
,
3153 __gnu_cxx::__ops::__iter_equals_val(__old_value
),
3158 * @brief Copy a sequence, replacing each value for which a predicate
3159 * returns true with another value.
3160 * @ingroup mutating_algorithms
3161 * @param __first An input iterator.
3162 * @param __last An input iterator.
3163 * @param __result An output iterator.
3164 * @param __pred A predicate.
3165 * @param __new_value The replacement value.
3166 * @return The end of the output sequence, @p __result+(__last-__first).
3168 * Copies each element in the range @p [__first,__last) to the range
3169 * @p [__result,__result+(__last-__first)) replacing elements for which
3170 * @p __pred returns true with @p __new_value.
3172 template<typename _InputIterator
, typename _OutputIterator
,
3173 typename _Predicate
, typename _Tp
>
3174 inline _OutputIterator
3175 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3176 _OutputIterator __result
,
3177 _Predicate __pred
, const _Tp
& __new_value
)
3179 // concept requirements
3180 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3181 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3182 typename iterator_traits
<_InputIterator
>::value_type
>)
3183 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3184 typename iterator_traits
<_InputIterator
>::value_type
>)
3185 __glibcxx_requires_valid_range(__first
, __last
);
3187 return std::__replace_copy_if(__first
, __last
, __result
,
3188 __gnu_cxx::__ops::__pred_iter(__pred
),
3192 template<typename _InputIterator
, typename _Predicate
>
3193 typename iterator_traits
<_InputIterator
>::difference_type
3194 __count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3196 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
3197 for (; __first
!= __last
; ++__first
)
3198 if (__pred(__first
))
3203 #if __cplusplus >= 201103L
3205 * @brief Determines whether the elements of a sequence are sorted.
3206 * @ingroup sorting_algorithms
3207 * @param __first An iterator.
3208 * @param __last Another iterator.
3209 * @return True if the elements are sorted, false otherwise.
3211 template<typename _ForwardIterator
>
3213 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3214 { return std::is_sorted_until(__first
, __last
) == __last
; }
3217 * @brief Determines whether the elements of a sequence are sorted
3218 * according to a comparison functor.
3219 * @ingroup sorting_algorithms
3220 * @param __first An iterator.
3221 * @param __last Another iterator.
3222 * @param __comp A comparison functor.
3223 * @return True if the elements are sorted, false otherwise.
3225 template<typename _ForwardIterator
, typename _Compare
>
3227 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3229 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3231 template<typename _ForwardIterator
, typename _Compare
>
3233 __is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3236 if (__first
== __last
)
3239 _ForwardIterator __next
= __first
;
3240 for (++__next
; __next
!= __last
; __first
= __next
, (void)++__next
)
3241 if (__comp(__next
, __first
))
3247 * @brief Determines the end of a sorted sequence.
3248 * @ingroup sorting_algorithms
3249 * @param __first An iterator.
3250 * @param __last Another iterator.
3251 * @return An iterator pointing to the last iterator i in [__first, __last)
3252 * for which the range [__first, i) is sorted.
3254 template<typename _ForwardIterator
>
3255 inline _ForwardIterator
3256 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3258 // concept requirements
3259 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3260 __glibcxx_function_requires(_LessThanComparableConcept
<
3261 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3262 __glibcxx_requires_valid_range(__first
, __last
);
3263 __glibcxx_requires_irreflexive(__first
, __last
);
3265 return std::__is_sorted_until(__first
, __last
,
3266 __gnu_cxx::__ops::__iter_less_iter());
3270 * @brief Determines the end of a sorted sequence using comparison functor.
3271 * @ingroup sorting_algorithms
3272 * @param __first An iterator.
3273 * @param __last Another iterator.
3274 * @param __comp A comparison functor.
3275 * @return An iterator pointing to the last iterator i in [__first, __last)
3276 * for which the range [__first, i) is sorted.
3278 template<typename _ForwardIterator
, typename _Compare
>
3279 inline _ForwardIterator
3280 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
3283 // concept requirements
3284 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3285 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3286 typename iterator_traits
<_ForwardIterator
>::value_type
,
3287 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3288 __glibcxx_requires_valid_range(__first
, __last
);
3289 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3291 return std::__is_sorted_until(__first
, __last
,
3292 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3296 * @brief Determines min and max at once as an ordered pair.
3297 * @ingroup sorting_algorithms
3298 * @param __a A thing of arbitrary type.
3299 * @param __b Another thing of arbitrary type.
3300 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3303 template<typename _Tp
>
3304 _GLIBCXX14_CONSTEXPR
3305 inline pair
<const _Tp
&, const _Tp
&>
3306 minmax(const _Tp
& __a
, const _Tp
& __b
)
3308 // concept requirements
3309 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3311 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3312 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3316 * @brief Determines min and max at once as an ordered pair.
3317 * @ingroup sorting_algorithms
3318 * @param __a A thing of arbitrary type.
3319 * @param __b Another thing of arbitrary type.
3320 * @param __comp A @link comparison_functors comparison functor @endlink.
3321 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3324 template<typename _Tp
, typename _Compare
>
3325 _GLIBCXX14_CONSTEXPR
3326 inline pair
<const _Tp
&, const _Tp
&>
3327 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
3329 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
3330 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
3333 template<typename _ForwardIterator
, typename _Compare
>
3334 _GLIBCXX14_CONSTEXPR
3335 pair
<_ForwardIterator
, _ForwardIterator
>
3336 __minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3339 _ForwardIterator __next
= __first
;
3340 if (__first
== __last
3341 || ++__next
== __last
)
3342 return std::make_pair(__first
, __first
);
3344 _ForwardIterator __min
{}, __max
{};
3345 if (__comp(__next
, __first
))
3359 while (__first
!= __last
)
3362 if (++__next
== __last
)
3364 if (__comp(__first
, __min
))
3366 else if (!__comp(__first
, __max
))
3371 if (__comp(__next
, __first
))
3373 if (__comp(__next
, __min
))
3375 if (!__comp(__first
, __max
))
3380 if (__comp(__first
, __min
))
3382 if (!__comp(__next
, __max
))
3390 return std::make_pair(__min
, __max
);
3394 * @brief Return a pair of iterators pointing to the minimum and maximum
3395 * elements in a range.
3396 * @ingroup sorting_algorithms
3397 * @param __first Start of range.
3398 * @param __last End of range.
3399 * @return make_pair(m, M), where m is the first iterator i in
3400 * [__first, __last) such that no other element in the range is
3401 * smaller, and where M is the last iterator i in [__first, __last)
3402 * such that no other element in the range is larger.
3404 template<typename _ForwardIterator
>
3405 _GLIBCXX14_CONSTEXPR
3406 inline pair
<_ForwardIterator
, _ForwardIterator
>
3407 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
3409 // concept requirements
3410 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3411 __glibcxx_function_requires(_LessThanComparableConcept
<
3412 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3413 __glibcxx_requires_valid_range(__first
, __last
);
3414 __glibcxx_requires_irreflexive(__first
, __last
);
3416 return std::__minmax_element(__first
, __last
,
3417 __gnu_cxx::__ops::__iter_less_iter());
3421 * @brief Return a pair of iterators pointing to the minimum and maximum
3422 * elements in a range.
3423 * @ingroup sorting_algorithms
3424 * @param __first Start of range.
3425 * @param __last End of range.
3426 * @param __comp Comparison functor.
3427 * @return make_pair(m, M), where m is the first iterator i in
3428 * [__first, __last) such that no other element in the range is
3429 * smaller, and where M is the last iterator i in [__first, __last)
3430 * such that no other element in the range is larger.
3432 template<typename _ForwardIterator
, typename _Compare
>
3433 _GLIBCXX14_CONSTEXPR
3434 inline pair
<_ForwardIterator
, _ForwardIterator
>
3435 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
3438 // concept requirements
3439 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3440 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3441 typename iterator_traits
<_ForwardIterator
>::value_type
,
3442 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3443 __glibcxx_requires_valid_range(__first
, __last
);
3444 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
3446 return std::__minmax_element(__first
, __last
,
3447 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
3451 template<typename _Tp
>
3452 _GLIBCXX14_CONSTEXPR
3454 min(initializer_list
<_Tp
> __l
)
3455 { return *std::min_element(__l
.begin(), __l
.end()); }
3457 template<typename _Tp
, typename _Compare
>
3458 _GLIBCXX14_CONSTEXPR
3460 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
3461 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
3463 template<typename _Tp
>
3464 _GLIBCXX14_CONSTEXPR
3466 max(initializer_list
<_Tp
> __l
)
3467 { return *std::max_element(__l
.begin(), __l
.end()); }
3469 template<typename _Tp
, typename _Compare
>
3470 _GLIBCXX14_CONSTEXPR
3472 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
3473 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
3475 template<typename _Tp
>
3476 _GLIBCXX14_CONSTEXPR
3477 inline pair
<_Tp
, _Tp
>
3478 minmax(initializer_list
<_Tp
> __l
)
3480 pair
<const _Tp
*, const _Tp
*> __p
=
3481 std::minmax_element(__l
.begin(), __l
.end());
3482 return std::make_pair(*__p
.first
, *__p
.second
);
3485 template<typename _Tp
, typename _Compare
>
3486 _GLIBCXX14_CONSTEXPR
3487 inline pair
<_Tp
, _Tp
>
3488 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
3490 pair
<const _Tp
*, const _Tp
*> __p
=
3491 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
3492 return std::make_pair(*__p
.first
, *__p
.second
);
3495 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3496 typename _BinaryPredicate
>
3498 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3499 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3501 // Efficiently compare identical prefixes: O(N) if sequences
3502 // have the same elements in the same order.
3503 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
3504 if (!__pred(__first1
, __first2
))
3507 if (__first1
== __last1
)
3510 // Establish __last2 assuming equal ranges by iterating over the
3511 // rest of the list.
3512 _ForwardIterator2 __last2
= __first2
;
3513 std::advance(__last2
, std::distance(__first1
, __last1
));
3514 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3516 if (__scan
!= std::__find_if(__first1
, __scan
,
3517 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3518 continue; // We've seen this one before.
3521 = std::__count_if(__first2
, __last2
,
3522 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3523 if (0 == __matches
||
3524 std::__count_if(__scan
, __last1
,
3525 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3533 * @brief Checks whether a permutation of the second sequence is equal
3534 * to the first sequence.
3535 * @ingroup non_mutating_algorithms
3536 * @param __first1 Start of first range.
3537 * @param __last1 End of first range.
3538 * @param __first2 Start of second range.
3539 * @return true if there exists a permutation of the elements in the range
3540 * [__first2, __first2 + (__last1 - __first1)), beginning with
3541 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3542 * returns true; otherwise, returns false.
3544 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3546 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3547 _ForwardIterator2 __first2
)
3549 // concept requirements
3550 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3551 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3552 __glibcxx_function_requires(_EqualOpConcept
<
3553 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3554 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3555 __glibcxx_requires_valid_range(__first1
, __last1
);
3557 return std::__is_permutation(__first1
, __last1
, __first2
,
3558 __gnu_cxx::__ops::__iter_equal_to_iter());
3562 * @brief Checks whether a permutation of the second sequence is equal
3563 * to the first sequence.
3564 * @ingroup non_mutating_algorithms
3565 * @param __first1 Start of first range.
3566 * @param __last1 End of first range.
3567 * @param __first2 Start of second range.
3568 * @param __pred A binary predicate.
3569 * @return true if there exists a permutation of the elements in
3570 * the range [__first2, __first2 + (__last1 - __first1)),
3571 * beginning with ForwardIterator2 begin, such that
3572 * equal(__first1, __last1, __begin, __pred) returns true;
3573 * otherwise, returns false.
3575 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3576 typename _BinaryPredicate
>
3578 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3579 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
3581 // concept requirements
3582 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
3583 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
3584 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3585 typename iterator_traits
<_ForwardIterator1
>::value_type
,
3586 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
3587 __glibcxx_requires_valid_range(__first1
, __last1
);
3589 return std::__is_permutation(__first1
, __last1
, __first2
,
3590 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3593 #if __cplusplus > 201103L
3594 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3595 typename _BinaryPredicate
>
3597 __is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3598 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3599 _BinaryPredicate __pred
)
3602 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
3604 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
3605 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
3606 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
3607 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
3610 auto __d1
= std::distance(__first1
, __last1
);
3611 auto __d2
= std::distance(__first2
, __last2
);
3616 // Efficiently compare identical prefixes: O(N) if sequences
3617 // have the same elements in the same order.
3618 for (; __first1
!= __last1
&& __first2
!= __last2
;
3619 ++__first1
, (void)++__first2
)
3620 if (!__pred(__first1
, __first2
))
3625 if (__first1
== __last1
)
3630 auto __d1
= std::distance(__first1
, __last1
);
3631 auto __d2
= std::distance(__first2
, __last2
);
3632 if (__d1
== 0 && __d2
== 0)
3638 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
3640 if (__scan
!= std::__find_if(__first1
, __scan
,
3641 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
)))
3642 continue; // We've seen this one before.
3644 auto __matches
= std::__count_if(__first2
, __last2
,
3645 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
));
3647 || std::__count_if(__scan
, __last1
,
3648 __gnu_cxx::__ops::__iter_comp_iter(__pred
, __scan
))
3656 * @brief Checks whether a permutaion of the second sequence is equal
3657 * to the first sequence.
3658 * @ingroup non_mutating_algorithms
3659 * @param __first1 Start of first range.
3660 * @param __last1 End of first range.
3661 * @param __first2 Start of second range.
3662 * @param __last2 End of first range.
3663 * @return true if there exists a permutation of the elements in the range
3664 * [__first2, __last2), beginning with ForwardIterator2 begin,
3665 * such that equal(__first1, __last1, begin) returns true;
3666 * otherwise, returns false.
3668 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
3670 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3671 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
3673 __glibcxx_requires_valid_range(__first1
, __last1
);
3674 __glibcxx_requires_valid_range(__first2
, __last2
);
3677 std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3678 __gnu_cxx::__ops::__iter_equal_to_iter());
3682 * @brief Checks whether a permutation of the second sequence is equal
3683 * to the first sequence.
3684 * @ingroup non_mutating_algorithms
3685 * @param __first1 Start of first range.
3686 * @param __last1 End of first range.
3687 * @param __first2 Start of second range.
3688 * @param __last2 End of first range.
3689 * @param __pred A binary predicate.
3690 * @return true if there exists a permutation of the elements in the range
3691 * [__first2, __last2), beginning with ForwardIterator2 begin,
3692 * such that equal(__first1, __last1, __begin, __pred) returns true;
3693 * otherwise, returns false.
3695 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
3696 typename _BinaryPredicate
>
3698 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
3699 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
3700 _BinaryPredicate __pred
)
3702 __glibcxx_requires_valid_range(__first1
, __last1
);
3703 __glibcxx_requires_valid_range(__first2
, __last2
);
3705 return std::__is_permutation(__first1
, __last1
, __first2
, __last2
,
3706 __gnu_cxx::__ops::__iter_comp_iter(__pred
));
3710 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3712 * @brief Shuffle the elements of a sequence using a uniform random
3714 * @ingroup mutating_algorithms
3715 * @param __first A forward iterator.
3716 * @param __last A forward iterator.
3717 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3720 * Reorders the elements in the range @p [__first,__last) using @p __g to
3721 * provide random numbers.
3723 template<typename _RandomAccessIterator
,
3724 typename _UniformRandomNumberGenerator
>
3726 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3727 _UniformRandomNumberGenerator
&& __g
)
3729 // concept requirements
3730 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3731 _RandomAccessIterator
>)
3732 __glibcxx_requires_valid_range(__first
, __last
);
3734 if (__first
== __last
)
3737 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3740 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
3741 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
3742 typedef typename
__distr_type::param_type __p_type
;
3745 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
3746 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
3752 _GLIBCXX_END_NAMESPACE_VERSION
3754 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3757 * @brief Apply a function to every element of a sequence.
3758 * @ingroup non_mutating_algorithms
3759 * @param __first An input iterator.
3760 * @param __last An input iterator.
3761 * @param __f A unary function object.
3762 * @return @p __f (std::move(@p __f) in C++0x).
3764 * Applies the function object @p __f to each element in the range
3765 * @p [first,last). @p __f must not modify the order of the sequence.
3766 * If @p __f has a return value it is ignored.
3768 template<typename _InputIterator
, typename _Function
>
3770 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
3772 // concept requirements
3773 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3774 __glibcxx_requires_valid_range(__first
, __last
);
3775 for (; __first
!= __last
; ++__first
)
3777 return _GLIBCXX_MOVE(__f
);
3781 * @brief Find the first occurrence of a value in a sequence.
3782 * @ingroup non_mutating_algorithms
3783 * @param __first An input iterator.
3784 * @param __last An input iterator.
3785 * @param __val The value to find.
3786 * @return The first iterator @c i in the range @p [__first,__last)
3787 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3789 template<typename _InputIterator
, typename _Tp
>
3790 inline _InputIterator
3791 find(_InputIterator __first
, _InputIterator __last
,
3794 // concept requirements
3795 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3796 __glibcxx_function_requires(_EqualOpConcept
<
3797 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3798 __glibcxx_requires_valid_range(__first
, __last
);
3799 return std::__find_if(__first
, __last
,
3800 __gnu_cxx::__ops::__iter_equals_val(__val
));
3804 * @brief Find the first element in a sequence for which a
3805 * predicate is true.
3806 * @ingroup non_mutating_algorithms
3807 * @param __first An input iterator.
3808 * @param __last An input iterator.
3809 * @param __pred A predicate.
3810 * @return The first iterator @c i in the range @p [__first,__last)
3811 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3813 template<typename _InputIterator
, typename _Predicate
>
3814 inline _InputIterator
3815 find_if(_InputIterator __first
, _InputIterator __last
,
3818 // concept requirements
3819 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3820 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3821 typename iterator_traits
<_InputIterator
>::value_type
>)
3822 __glibcxx_requires_valid_range(__first
, __last
);
3824 return std::__find_if(__first
, __last
,
3825 __gnu_cxx::__ops::__pred_iter(__pred
));
3829 * @brief Find element from a set in a sequence.
3830 * @ingroup non_mutating_algorithms
3831 * @param __first1 Start of range to search.
3832 * @param __last1 End of range to search.
3833 * @param __first2 Start of match candidates.
3834 * @param __last2 End of match candidates.
3835 * @return The first iterator @c i in the range
3836 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3837 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3839 * Searches the range @p [__first1,__last1) for an element that is
3840 * equal to some element in the range [__first2,__last2). If
3841 * found, returns an iterator in the range [__first1,__last1),
3842 * otherwise returns @p __last1.
3844 template<typename _InputIterator
, typename _ForwardIterator
>
3846 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3847 _ForwardIterator __first2
, _ForwardIterator __last2
)
3849 // concept requirements
3850 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3851 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3852 __glibcxx_function_requires(_EqualOpConcept
<
3853 typename iterator_traits
<_InputIterator
>::value_type
,
3854 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3855 __glibcxx_requires_valid_range(__first1
, __last1
);
3856 __glibcxx_requires_valid_range(__first2
, __last2
);
3858 for (; __first1
!= __last1
; ++__first1
)
3859 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3860 if (*__first1
== *__iter
)
3866 * @brief Find element from a set in a sequence using a predicate.
3867 * @ingroup non_mutating_algorithms
3868 * @param __first1 Start of range to search.
3869 * @param __last1 End of range to search.
3870 * @param __first2 Start of match candidates.
3871 * @param __last2 End of match candidates.
3872 * @param __comp Predicate to use.
3873 * @return The first iterator @c i in the range
3874 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3875 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3876 * such iterator exists.
3879 * Searches the range @p [__first1,__last1) for an element that is
3880 * equal to some element in the range [__first2,__last2). If
3881 * found, returns an iterator in the range [__first1,__last1),
3882 * otherwise returns @p __last1.
3884 template<typename _InputIterator
, typename _ForwardIterator
,
3885 typename _BinaryPredicate
>
3887 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
3888 _ForwardIterator __first2
, _ForwardIterator __last2
,
3889 _BinaryPredicate __comp
)
3891 // concept requirements
3892 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3893 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3894 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3895 typename iterator_traits
<_InputIterator
>::value_type
,
3896 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3897 __glibcxx_requires_valid_range(__first1
, __last1
);
3898 __glibcxx_requires_valid_range(__first2
, __last2
);
3900 for (; __first1
!= __last1
; ++__first1
)
3901 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
3902 if (__comp(*__first1
, *__iter
))
3908 * @brief Find two adjacent values in a sequence that are equal.
3909 * @ingroup non_mutating_algorithms
3910 * @param __first A forward iterator.
3911 * @param __last A forward iterator.
3912 * @return The first iterator @c i such that @c i and @c i+1 are both
3913 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3914 * or @p __last if no such iterator exists.
3916 template<typename _ForwardIterator
>
3917 inline _ForwardIterator
3918 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
3920 // concept requirements
3921 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3922 __glibcxx_function_requires(_EqualityComparableConcept
<
3923 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3924 __glibcxx_requires_valid_range(__first
, __last
);
3926 return std::__adjacent_find(__first
, __last
,
3927 __gnu_cxx::__ops::__iter_equal_to_iter());
3931 * @brief Find two adjacent values in a sequence using a predicate.
3932 * @ingroup non_mutating_algorithms
3933 * @param __first A forward iterator.
3934 * @param __last A forward iterator.
3935 * @param __binary_pred A binary predicate.
3936 * @return The first iterator @c i such that @c i and @c i+1 are both
3937 * valid iterators in @p [__first,__last) and such that
3938 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3941 template<typename _ForwardIterator
, typename _BinaryPredicate
>
3942 inline _ForwardIterator
3943 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
3944 _BinaryPredicate __binary_pred
)
3946 // concept requirements
3947 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3948 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
3949 typename iterator_traits
<_ForwardIterator
>::value_type
,
3950 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3951 __glibcxx_requires_valid_range(__first
, __last
);
3953 return std::__adjacent_find(__first
, __last
,
3954 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
));
3958 * @brief Count the number of copies of a value in a sequence.
3959 * @ingroup non_mutating_algorithms
3960 * @param __first An input iterator.
3961 * @param __last An input iterator.
3962 * @param __value The value to be counted.
3963 * @return The number of iterators @c i in the range @p [__first,__last)
3964 * for which @c *i == @p __value
3966 template<typename _InputIterator
, typename _Tp
>
3967 inline typename iterator_traits
<_InputIterator
>::difference_type
3968 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
3970 // concept requirements
3971 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3972 __glibcxx_function_requires(_EqualOpConcept
<
3973 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3974 __glibcxx_requires_valid_range(__first
, __last
);
3976 return std::__count_if(__first
, __last
,
3977 __gnu_cxx::__ops::__iter_equals_val(__value
));
3981 * @brief Count the elements of a sequence for which a predicate is true.
3982 * @ingroup non_mutating_algorithms
3983 * @param __first An input iterator.
3984 * @param __last An input iterator.
3985 * @param __pred A predicate.
3986 * @return The number of iterators @c i in the range @p [__first,__last)
3987 * for which @p __pred(*i) is true.
3989 template<typename _InputIterator
, typename _Predicate
>
3990 inline typename iterator_traits
<_InputIterator
>::difference_type
3991 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
3993 // concept requirements
3994 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3995 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3996 typename iterator_traits
<_InputIterator
>::value_type
>)
3997 __glibcxx_requires_valid_range(__first
, __last
);
3999 return std::__count_if(__first
, __last
,
4000 __gnu_cxx::__ops::__pred_iter(__pred
));
4004 * @brief Search a sequence for a matching sub-sequence.
4005 * @ingroup non_mutating_algorithms
4006 * @param __first1 A forward iterator.
4007 * @param __last1 A forward iterator.
4008 * @param __first2 A forward iterator.
4009 * @param __last2 A forward iterator.
4010 * @return The first iterator @c i in the range @p
4011 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4012 * *(__first2+N) for each @c N in the range @p
4013 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4015 * Searches the range @p [__first1,__last1) for a sub-sequence that
4016 * compares equal value-by-value with the sequence given by @p
4017 * [__first2,__last2) and returns an iterator to the first element
4018 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4021 * Because the sub-sequence must lie completely within the range @p
4022 * [__first1,__last1) it must start at a position less than @p
4023 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4024 * length of the sub-sequence.
4026 * This means that the returned iterator @c i will be in the range
4027 * @p [__first1,__last1-(__last2-__first2))
4029 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4030 inline _ForwardIterator1
4031 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4032 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4034 // concept requirements
4035 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4036 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4037 __glibcxx_function_requires(_EqualOpConcept
<
4038 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4039 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4040 __glibcxx_requires_valid_range(__first1
, __last1
);
4041 __glibcxx_requires_valid_range(__first2
, __last2
);
4043 return std::__search(__first1
, __last1
, __first2
, __last2
,
4044 __gnu_cxx::__ops::__iter_equal_to_iter());
4048 * @brief Search a sequence for a matching sub-sequence using a predicate.
4049 * @ingroup non_mutating_algorithms
4050 * @param __first1 A forward iterator.
4051 * @param __last1 A forward iterator.
4052 * @param __first2 A forward iterator.
4053 * @param __last2 A forward iterator.
4054 * @param __predicate A binary predicate.
4055 * @return The first iterator @c i in the range
4056 * @p [__first1,__last1-(__last2-__first2)) such that
4057 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4058 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4060 * Searches the range @p [__first1,__last1) for a sub-sequence that
4061 * compares equal value-by-value with the sequence given by @p
4062 * [__first2,__last2), using @p __predicate to determine equality,
4063 * and returns an iterator to the first element of the
4064 * sub-sequence, or @p __last1 if no such iterator exists.
4066 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4068 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4069 typename _BinaryPredicate
>
4070 inline _ForwardIterator1
4071 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4072 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4073 _BinaryPredicate __predicate
)
4075 // concept requirements
4076 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4077 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4078 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4079 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4080 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4081 __glibcxx_requires_valid_range(__first1
, __last1
);
4082 __glibcxx_requires_valid_range(__first2
, __last2
);
4084 return std::__search(__first1
, __last1
, __first2
, __last2
,
4085 __gnu_cxx::__ops::__iter_comp_iter(__predicate
));
4089 * @brief Search a sequence for a number of consecutive values.
4090 * @ingroup non_mutating_algorithms
4091 * @param __first A forward iterator.
4092 * @param __last A forward iterator.
4093 * @param __count The number of consecutive values.
4094 * @param __val The value to find.
4095 * @return The first iterator @c i in the range @p
4096 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4097 * each @c N in the range @p [0,__count), or @p __last if no such
4100 * Searches the range @p [__first,__last) for @p count consecutive elements
4101 * equal to @p __val.
4103 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4104 inline _ForwardIterator
4105 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4106 _Integer __count
, const _Tp
& __val
)
4108 // concept requirements
4109 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4110 __glibcxx_function_requires(_EqualOpConcept
<
4111 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4112 __glibcxx_requires_valid_range(__first
, __last
);
4114 return std::__search_n(__first
, __last
, __count
,
4115 __gnu_cxx::__ops::__iter_equals_val(__val
));
4120 * @brief Search a sequence for a number of consecutive values using a
4122 * @ingroup non_mutating_algorithms
4123 * @param __first A forward iterator.
4124 * @param __last A forward iterator.
4125 * @param __count The number of consecutive values.
4126 * @param __val The value to find.
4127 * @param __binary_pred A binary predicate.
4128 * @return The first iterator @c i in the range @p
4129 * [__first,__last-__count) such that @p
4130 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4131 * @p [0,__count), or @p __last if no such iterator exists.
4133 * Searches the range @p [__first,__last) for @p __count
4134 * consecutive elements for which the predicate returns true.
4136 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4137 typename _BinaryPredicate
>
4138 inline _ForwardIterator
4139 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4140 _Integer __count
, const _Tp
& __val
,
4141 _BinaryPredicate __binary_pred
)
4143 // concept requirements
4144 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4145 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4146 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4147 __glibcxx_requires_valid_range(__first
, __last
);
4149 return std::__search_n(__first
, __last
, __count
,
4150 __gnu_cxx::__ops::__iter_comp_val(__binary_pred
, __val
));
4155 * @brief Perform an operation on a sequence.
4156 * @ingroup mutating_algorithms
4157 * @param __first An input iterator.
4158 * @param __last An input iterator.
4159 * @param __result An output iterator.
4160 * @param __unary_op A unary operator.
4161 * @return An output iterator equal to @p __result+(__last-__first).
4163 * Applies the operator to each element in the input range and assigns
4164 * the results to successive elements of the output sequence.
4165 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4166 * range @p [0,__last-__first).
4168 * @p unary_op must not alter its argument.
4170 template<typename _InputIterator
, typename _OutputIterator
,
4171 typename _UnaryOperation
>
4173 transform(_InputIterator __first
, _InputIterator __last
,
4174 _OutputIterator __result
, _UnaryOperation __unary_op
)
4176 // concept requirements
4177 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4178 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4179 // "the type returned by a _UnaryOperation"
4180 __typeof__(__unary_op(*__first
))>)
4181 __glibcxx_requires_valid_range(__first
, __last
);
4183 for (; __first
!= __last
; ++__first
, (void)++__result
)
4184 *__result
= __unary_op(*__first
);
4189 * @brief Perform an operation on corresponding elements of two sequences.
4190 * @ingroup mutating_algorithms
4191 * @param __first1 An input iterator.
4192 * @param __last1 An input iterator.
4193 * @param __first2 An input iterator.
4194 * @param __result An output iterator.
4195 * @param __binary_op A binary operator.
4196 * @return An output iterator equal to @p result+(last-first).
4198 * Applies the operator to the corresponding elements in the two
4199 * input ranges and assigns the results to successive elements of the
4202 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4203 * @c N in the range @p [0,__last1-__first1).
4205 * @p binary_op must not alter either of its arguments.
4207 template<typename _InputIterator1
, typename _InputIterator2
,
4208 typename _OutputIterator
, typename _BinaryOperation
>
4210 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
4211 _InputIterator2 __first2
, _OutputIterator __result
,
4212 _BinaryOperation __binary_op
)
4214 // concept requirements
4215 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4216 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4217 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4218 // "the type returned by a _BinaryOperation"
4219 __typeof__(__binary_op(*__first1
,*__first2
))>)
4220 __glibcxx_requires_valid_range(__first1
, __last1
);
4222 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
, ++__result
)
4223 *__result
= __binary_op(*__first1
, *__first2
);
4228 * @brief Replace each occurrence of one value in a sequence with another
4230 * @ingroup mutating_algorithms
4231 * @param __first A forward iterator.
4232 * @param __last A forward iterator.
4233 * @param __old_value The value to be replaced.
4234 * @param __new_value The replacement value.
4235 * @return replace() returns no value.
4237 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4238 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4240 template<typename _ForwardIterator
, typename _Tp
>
4242 replace(_ForwardIterator __first
, _ForwardIterator __last
,
4243 const _Tp
& __old_value
, const _Tp
& __new_value
)
4245 // concept requirements
4246 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4248 __glibcxx_function_requires(_EqualOpConcept
<
4249 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4250 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4251 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4252 __glibcxx_requires_valid_range(__first
, __last
);
4254 for (; __first
!= __last
; ++__first
)
4255 if (*__first
== __old_value
)
4256 *__first
= __new_value
;
4260 * @brief Replace each value in a sequence for which a predicate returns
4261 * true with another value.
4262 * @ingroup mutating_algorithms
4263 * @param __first A forward iterator.
4264 * @param __last A forward iterator.
4265 * @param __pred A predicate.
4266 * @param __new_value The replacement value.
4267 * @return replace_if() returns no value.
4269 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4270 * is true then the assignment @c *i = @p __new_value is performed.
4272 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
4274 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
4275 _Predicate __pred
, const _Tp
& __new_value
)
4277 // concept requirements
4278 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4280 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
4281 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4282 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4283 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4284 __glibcxx_requires_valid_range(__first
, __last
);
4286 for (; __first
!= __last
; ++__first
)
4287 if (__pred(*__first
))
4288 *__first
= __new_value
;
4292 * @brief Assign the result of a function object to each value in a
4294 * @ingroup mutating_algorithms
4295 * @param __first A forward iterator.
4296 * @param __last A forward iterator.
4297 * @param __gen A function object taking no arguments and returning
4298 * std::iterator_traits<_ForwardIterator>::value_type
4299 * @return generate() returns no value.
4301 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4302 * @p [__first,__last).
4304 template<typename _ForwardIterator
, typename _Generator
>
4306 generate(_ForwardIterator __first
, _ForwardIterator __last
,
4309 // concept requirements
4310 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4311 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
4312 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4313 __glibcxx_requires_valid_range(__first
, __last
);
4315 for (; __first
!= __last
; ++__first
)
4320 * @brief Assign the result of a function object to each value in a
4322 * @ingroup mutating_algorithms
4323 * @param __first A forward iterator.
4324 * @param __n The length of the sequence.
4325 * @param __gen A function object taking no arguments and returning
4326 * std::iterator_traits<_ForwardIterator>::value_type
4327 * @return The end of the sequence, @p __first+__n
4329 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4330 * @p [__first,__first+__n).
4332 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4333 * DR 865. More algorithms that throw away information
4335 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
4337 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
4339 // concept requirements
4340 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4341 // "the type returned by a _Generator"
4342 __typeof__(__gen())>)
4344 for (__decltype(__n
+ 0) __niter
= __n
;
4345 __niter
> 0; --__niter
, ++__first
)
4351 * @brief Copy a sequence, removing consecutive duplicate values.
4352 * @ingroup mutating_algorithms
4353 * @param __first An input iterator.
4354 * @param __last An input iterator.
4355 * @param __result An output iterator.
4356 * @return An iterator designating the end of the resulting sequence.
4358 * Copies each element in the range @p [__first,__last) to the range
4359 * beginning at @p __result, except that only the first element is copied
4360 * from groups of consecutive elements that compare equal.
4361 * unique_copy() is stable, so the relative order of elements that are
4362 * copied is unchanged.
4364 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4365 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4367 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4368 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4371 template<typename _InputIterator
, typename _OutputIterator
>
4372 inline _OutputIterator
4373 unique_copy(_InputIterator __first
, _InputIterator __last
,
4374 _OutputIterator __result
)
4376 // concept requirements
4377 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4378 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4379 typename iterator_traits
<_InputIterator
>::value_type
>)
4380 __glibcxx_function_requires(_EqualityComparableConcept
<
4381 typename iterator_traits
<_InputIterator
>::value_type
>)
4382 __glibcxx_requires_valid_range(__first
, __last
);
4384 if (__first
== __last
)
4386 return std::__unique_copy(__first
, __last
, __result
,
4387 __gnu_cxx::__ops::__iter_equal_to_iter(),
4388 std::__iterator_category(__first
),
4389 std::__iterator_category(__result
));
4393 * @brief Copy a sequence, removing consecutive values using a predicate.
4394 * @ingroup mutating_algorithms
4395 * @param __first An input iterator.
4396 * @param __last An input iterator.
4397 * @param __result An output iterator.
4398 * @param __binary_pred A binary predicate.
4399 * @return An iterator designating the end of the resulting sequence.
4401 * Copies each element in the range @p [__first,__last) to the range
4402 * beginning at @p __result, except that only the first element is copied
4403 * from groups of consecutive elements for which @p __binary_pred returns
4405 * unique_copy() is stable, so the relative order of elements that are
4406 * copied is unchanged.
4408 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4409 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4411 template<typename _InputIterator
, typename _OutputIterator
,
4412 typename _BinaryPredicate
>
4413 inline _OutputIterator
4414 unique_copy(_InputIterator __first
, _InputIterator __last
,
4415 _OutputIterator __result
,
4416 _BinaryPredicate __binary_pred
)
4418 // concept requirements -- predicates checked later
4419 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4420 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4421 typename iterator_traits
<_InputIterator
>::value_type
>)
4422 __glibcxx_requires_valid_range(__first
, __last
);
4424 if (__first
== __last
)
4426 return std::__unique_copy(__first
, __last
, __result
,
4427 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred
),
4428 std::__iterator_category(__first
),
4429 std::__iterator_category(__result
));
4434 * @brief Randomly shuffle the elements of a sequence.
4435 * @ingroup mutating_algorithms
4436 * @param __first A forward iterator.
4437 * @param __last A forward iterator.
4440 * Reorder the elements in the range @p [__first,__last) using a random
4441 * distribution, so that every possible ordering of the sequence is
4444 template<typename _RandomAccessIterator
>
4446 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4448 // concept requirements
4449 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4450 _RandomAccessIterator
>)
4451 __glibcxx_requires_valid_range(__first
, __last
);
4453 if (__first
!= __last
)
4454 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4456 // XXX rand() % N is not uniformly distributed
4457 _RandomAccessIterator __j
= __first
4458 + std::rand() % ((__i
- __first
) + 1);
4460 std::iter_swap(__i
, __j
);
4466 * @brief Shuffle the elements of a sequence using a random number
4468 * @ingroup mutating_algorithms
4469 * @param __first A forward iterator.
4470 * @param __last A forward iterator.
4471 * @param __rand The RNG functor or function.
4474 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4475 * provide a random distribution. Calling @p __rand(N) for a positive
4476 * integer @p N should return a randomly chosen integer from the
4479 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
4481 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4482 #if __cplusplus >= 201103L
4483 _RandomNumberGenerator
&& __rand
)
4485 _RandomNumberGenerator
& __rand
)
4488 // concept requirements
4489 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4490 _RandomAccessIterator
>)
4491 __glibcxx_requires_valid_range(__first
, __last
);
4493 if (__first
== __last
)
4495 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4497 _RandomAccessIterator __j
= __first
+ __rand((__i
- __first
) + 1);
4499 std::iter_swap(__i
, __j
);
4505 * @brief Move elements for which a predicate is true to the beginning
4507 * @ingroup mutating_algorithms
4508 * @param __first A forward iterator.
4509 * @param __last A forward iterator.
4510 * @param __pred A predicate functor.
4511 * @return An iterator @p middle such that @p __pred(i) is true for each
4512 * iterator @p i in the range @p [__first,middle) and false for each @p i
4513 * in the range @p [middle,__last).
4515 * @p __pred must not modify its operand. @p partition() does not preserve
4516 * the relative ordering of elements in each group, use
4517 * @p stable_partition() if this is needed.
4519 template<typename _ForwardIterator
, typename _Predicate
>
4520 inline _ForwardIterator
4521 partition(_ForwardIterator __first
, _ForwardIterator __last
,
4524 // concept requirements
4525 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
4527 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4528 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4529 __glibcxx_requires_valid_range(__first
, __last
);
4531 return std::__partition(__first
, __last
, __pred
,
4532 std::__iterator_category(__first
));
4537 * @brief Sort the smallest elements of a sequence.
4538 * @ingroup sorting_algorithms
4539 * @param __first An iterator.
4540 * @param __middle Another iterator.
4541 * @param __last Another iterator.
4544 * Sorts the smallest @p (__middle-__first) elements in the range
4545 * @p [first,last) and moves them to the range @p [__first,__middle). The
4546 * order of the remaining elements in the range @p [__middle,__last) is
4548 * After the sort if @e i and @e j are iterators in the range
4549 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4550 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4552 template<typename _RandomAccessIterator
>
4554 partial_sort(_RandomAccessIterator __first
,
4555 _RandomAccessIterator __middle
,
4556 _RandomAccessIterator __last
)
4558 // concept requirements
4559 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4560 _RandomAccessIterator
>)
4561 __glibcxx_function_requires(_LessThanComparableConcept
<
4562 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4563 __glibcxx_requires_valid_range(__first
, __middle
);
4564 __glibcxx_requires_valid_range(__middle
, __last
);
4565 __glibcxx_requires_irreflexive(__first
, __last
);
4567 std::__partial_sort(__first
, __middle
, __last
,
4568 __gnu_cxx::__ops::__iter_less_iter());
4572 * @brief Sort the smallest elements of a sequence using a predicate
4574 * @ingroup sorting_algorithms
4575 * @param __first An iterator.
4576 * @param __middle Another iterator.
4577 * @param __last Another iterator.
4578 * @param __comp A comparison functor.
4581 * Sorts the smallest @p (__middle-__first) elements in the range
4582 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4583 * order of the remaining elements in the range @p [__middle,__last) is
4585 * After the sort if @e i and @e j are iterators in the range
4586 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4587 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4590 template<typename _RandomAccessIterator
, typename _Compare
>
4592 partial_sort(_RandomAccessIterator __first
,
4593 _RandomAccessIterator __middle
,
4594 _RandomAccessIterator __last
,
4597 // concept requirements
4598 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4599 _RandomAccessIterator
>)
4600 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4601 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4602 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4603 __glibcxx_requires_valid_range(__first
, __middle
);
4604 __glibcxx_requires_valid_range(__middle
, __last
);
4605 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4607 std::__partial_sort(__first
, __middle
, __last
,
4608 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4612 * @brief Sort a sequence just enough to find a particular position.
4613 * @ingroup sorting_algorithms
4614 * @param __first An iterator.
4615 * @param __nth Another iterator.
4616 * @param __last Another iterator.
4619 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4620 * is the same element that would have been in that position had the
4621 * whole sequence been sorted. The elements either side of @p *__nth are
4622 * not completely sorted, but for any iterator @e i in the range
4623 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4624 * holds that *j < *i is false.
4626 template<typename _RandomAccessIterator
>
4628 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4629 _RandomAccessIterator __last
)
4631 // concept requirements
4632 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4633 _RandomAccessIterator
>)
4634 __glibcxx_function_requires(_LessThanComparableConcept
<
4635 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4636 __glibcxx_requires_valid_range(__first
, __nth
);
4637 __glibcxx_requires_valid_range(__nth
, __last
);
4638 __glibcxx_requires_irreflexive(__first
, __last
);
4640 if (__first
== __last
|| __nth
== __last
)
4643 std::__introselect(__first
, __nth
, __last
,
4644 std::__lg(__last
- __first
) * 2,
4645 __gnu_cxx::__ops::__iter_less_iter());
4649 * @brief Sort a sequence just enough to find a particular position
4650 * using a predicate for comparison.
4651 * @ingroup sorting_algorithms
4652 * @param __first An iterator.
4653 * @param __nth Another iterator.
4654 * @param __last Another iterator.
4655 * @param __comp A comparison functor.
4658 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4659 * is the same element that would have been in that position had the
4660 * whole sequence been sorted. The elements either side of @p *__nth are
4661 * not completely sorted, but for any iterator @e i in the range
4662 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4663 * holds that @p __comp(*j,*i) is false.
4665 template<typename _RandomAccessIterator
, typename _Compare
>
4667 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4668 _RandomAccessIterator __last
, _Compare __comp
)
4670 // concept requirements
4671 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4672 _RandomAccessIterator
>)
4673 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4674 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4675 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4676 __glibcxx_requires_valid_range(__first
, __nth
);
4677 __glibcxx_requires_valid_range(__nth
, __last
);
4678 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4680 if (__first
== __last
|| __nth
== __last
)
4683 std::__introselect(__first
, __nth
, __last
,
4684 std::__lg(__last
- __first
) * 2,
4685 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4689 * @brief Sort the elements of a sequence.
4690 * @ingroup sorting_algorithms
4691 * @param __first An iterator.
4692 * @param __last Another iterator.
4695 * Sorts the elements in the range @p [__first,__last) in ascending order,
4696 * such that for each iterator @e i in the range @p [__first,__last-1),
4697 * *(i+1)<*i is false.
4699 * The relative ordering of equivalent elements is not preserved, use
4700 * @p stable_sort() if this is needed.
4702 template<typename _RandomAccessIterator
>
4704 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4706 // concept requirements
4707 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4708 _RandomAccessIterator
>)
4709 __glibcxx_function_requires(_LessThanComparableConcept
<
4710 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4711 __glibcxx_requires_valid_range(__first
, __last
);
4712 __glibcxx_requires_irreflexive(__first
, __last
);
4714 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_less_iter());
4718 * @brief Sort the elements of a sequence using a predicate for comparison.
4719 * @ingroup sorting_algorithms
4720 * @param __first An iterator.
4721 * @param __last Another iterator.
4722 * @param __comp A comparison functor.
4725 * Sorts the elements in the range @p [__first,__last) in ascending order,
4726 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4727 * range @p [__first,__last-1).
4729 * The relative ordering of equivalent elements is not preserved, use
4730 * @p stable_sort() if this is needed.
4732 template<typename _RandomAccessIterator
, typename _Compare
>
4734 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4737 // concept requirements
4738 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4739 _RandomAccessIterator
>)
4740 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4741 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4742 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4743 __glibcxx_requires_valid_range(__first
, __last
);
4744 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4746 std::__sort(__first
, __last
, __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4749 template<typename _InputIterator1
, typename _InputIterator2
,
4750 typename _OutputIterator
, typename _Compare
>
4752 __merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4753 _InputIterator2 __first2
, _InputIterator2 __last2
,
4754 _OutputIterator __result
, _Compare __comp
)
4756 while (__first1
!= __last1
&& __first2
!= __last2
)
4758 if (__comp(__first2
, __first1
))
4760 *__result
= *__first2
;
4765 *__result
= *__first1
;
4770 return std::copy(__first2
, __last2
,
4771 std::copy(__first1
, __last1
, __result
));
4775 * @brief Merges two sorted ranges.
4776 * @ingroup sorting_algorithms
4777 * @param __first1 An iterator.
4778 * @param __first2 Another iterator.
4779 * @param __last1 Another iterator.
4780 * @param __last2 Another iterator.
4781 * @param __result An iterator pointing to the end of the merged range.
4782 * @return An iterator pointing to the first element <em>not less
4785 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4786 * the sorted range @p [__result, __result + (__last1-__first1) +
4787 * (__last2-__first2)). Both input ranges must be sorted, and the
4788 * output range must not overlap with either of the input ranges.
4789 * The sort is @e stable, that is, for equivalent elements in the
4790 * two ranges, elements from the first range will always come
4791 * before elements from the second.
4793 template<typename _InputIterator1
, typename _InputIterator2
,
4794 typename _OutputIterator
>
4795 inline _OutputIterator
4796 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4797 _InputIterator2 __first2
, _InputIterator2 __last2
,
4798 _OutputIterator __result
)
4800 // concept requirements
4801 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4802 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4803 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4804 typename iterator_traits
<_InputIterator1
>::value_type
>)
4805 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4806 typename iterator_traits
<_InputIterator2
>::value_type
>)
4807 __glibcxx_function_requires(_LessThanOpConcept
<
4808 typename iterator_traits
<_InputIterator2
>::value_type
,
4809 typename iterator_traits
<_InputIterator1
>::value_type
>)
4810 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
4811 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
4812 __glibcxx_requires_irreflexive2(__first1
, __last1
);
4813 __glibcxx_requires_irreflexive2(__first2
, __last2
);
4815 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4816 __first2
, __last2
, __result
,
4817 __gnu_cxx::__ops::__iter_less_iter());
4821 * @brief Merges two sorted ranges.
4822 * @ingroup sorting_algorithms
4823 * @param __first1 An iterator.
4824 * @param __first2 Another iterator.
4825 * @param __last1 Another iterator.
4826 * @param __last2 Another iterator.
4827 * @param __result An iterator pointing to the end of the merged range.
4828 * @param __comp A functor to use for comparisons.
4829 * @return An iterator pointing to the first element "not less
4832 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4833 * the sorted range @p [__result, __result + (__last1-__first1) +
4834 * (__last2-__first2)). Both input ranges must be sorted, and the
4835 * output range must not overlap with either of the input ranges.
4836 * The sort is @e stable, that is, for equivalent elements in the
4837 * two ranges, elements from the first range will always come
4838 * before elements from the second.
4840 * The comparison function should have the same effects on ordering as
4841 * the function used for the initial sort.
4843 template<typename _InputIterator1
, typename _InputIterator2
,
4844 typename _OutputIterator
, typename _Compare
>
4845 inline _OutputIterator
4846 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
4847 _InputIterator2 __first2
, _InputIterator2 __last2
,
4848 _OutputIterator __result
, _Compare __comp
)
4850 // concept requirements
4851 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4852 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4853 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4854 typename iterator_traits
<_InputIterator1
>::value_type
>)
4855 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4856 typename iterator_traits
<_InputIterator2
>::value_type
>)
4857 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4858 typename iterator_traits
<_InputIterator2
>::value_type
,
4859 typename iterator_traits
<_InputIterator1
>::value_type
>)
4860 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
4861 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
4862 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
4863 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
4865 return _GLIBCXX_STD_A::__merge(__first1
, __last1
,
4866 __first2
, __last2
, __result
,
4867 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4870 template<typename _RandomAccessIterator
, typename _Compare
>
4872 __stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4875 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4877 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4880 typedef _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> _TmpBuf
;
4881 _TmpBuf
__buf(__first
, __last
);
4883 if (__buf
.begin() == 0)
4884 std::__inplace_stable_sort(__first
, __last
, __comp
);
4886 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4887 _DistanceType(__buf
.size()), __comp
);
4891 * @brief Sort the elements of a sequence, preserving the relative order
4892 * of equivalent elements.
4893 * @ingroup sorting_algorithms
4894 * @param __first An iterator.
4895 * @param __last Another iterator.
4898 * Sorts the elements in the range @p [__first,__last) in ascending order,
4899 * such that for each iterator @p i in the range @p [__first,__last-1),
4900 * @p *(i+1)<*i is false.
4902 * The relative ordering of equivalent elements is preserved, so any two
4903 * elements @p x and @p y in the range @p [__first,__last) such that
4904 * @p x<y is false and @p y<x is false will have the same relative
4905 * ordering after calling @p stable_sort().
4907 template<typename _RandomAccessIterator
>
4909 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4911 // concept requirements
4912 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4913 _RandomAccessIterator
>)
4914 __glibcxx_function_requires(_LessThanComparableConcept
<
4915 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4916 __glibcxx_requires_valid_range(__first
, __last
);
4917 __glibcxx_requires_irreflexive(__first
, __last
);
4919 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4920 __gnu_cxx::__ops::__iter_less_iter());
4924 * @brief Sort the elements of a sequence using a predicate for comparison,
4925 * preserving the relative order of equivalent elements.
4926 * @ingroup sorting_algorithms
4927 * @param __first An iterator.
4928 * @param __last Another iterator.
4929 * @param __comp A comparison functor.
4932 * Sorts the elements in the range @p [__first,__last) in ascending order,
4933 * such that for each iterator @p i in the range @p [__first,__last-1),
4934 * @p __comp(*(i+1),*i) is false.
4936 * The relative ordering of equivalent elements is preserved, so any two
4937 * elements @p x and @p y in the range @p [__first,__last) such that
4938 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
4939 * relative ordering after calling @p stable_sort().
4941 template<typename _RandomAccessIterator
, typename _Compare
>
4943 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4946 // concept requirements
4947 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4948 _RandomAccessIterator
>)
4949 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4950 typename iterator_traits
<_RandomAccessIterator
>::value_type
,
4951 typename iterator_traits
<_RandomAccessIterator
>::value_type
>)
4952 __glibcxx_requires_valid_range(__first
, __last
);
4953 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
4955 _GLIBCXX_STD_A::__stable_sort(__first
, __last
,
4956 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
4959 template<typename _InputIterator1
, typename _InputIterator2
,
4960 typename _OutputIterator
,
4963 __set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4964 _InputIterator2 __first2
, _InputIterator2 __last2
,
4965 _OutputIterator __result
, _Compare __comp
)
4967 while (__first1
!= __last1
&& __first2
!= __last2
)
4969 if (__comp(__first1
, __first2
))
4971 *__result
= *__first1
;
4974 else if (__comp(__first2
, __first1
))
4976 *__result
= *__first2
;
4981 *__result
= *__first1
;
4987 return std::copy(__first2
, __last2
,
4988 std::copy(__first1
, __last1
, __result
));
4992 * @brief Return the union of two sorted ranges.
4993 * @ingroup set_algorithms
4994 * @param __first1 Start of first range.
4995 * @param __last1 End of first range.
4996 * @param __first2 Start of second range.
4997 * @param __last2 End of second range.
4998 * @return End of the output range.
4999 * @ingroup set_algorithms
5001 * This operation iterates over both ranges, copying elements present in
5002 * each range in order to the output range. Iterators increment for each
5003 * range. When the current element of one range is less than the other,
5004 * that element is copied and the iterator advanced. If an element is
5005 * contained in both ranges, the element from the first range is copied and
5006 * both ranges advance. The output range may not overlap either input
5009 template<typename _InputIterator1
, typename _InputIterator2
,
5010 typename _OutputIterator
>
5011 inline _OutputIterator
5012 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5013 _InputIterator2 __first2
, _InputIterator2 __last2
,
5014 _OutputIterator __result
)
5016 // concept requirements
5017 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5018 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5019 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5020 typename iterator_traits
<_InputIterator1
>::value_type
>)
5021 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5022 typename iterator_traits
<_InputIterator2
>::value_type
>)
5023 __glibcxx_function_requires(_LessThanOpConcept
<
5024 typename iterator_traits
<_InputIterator1
>::value_type
,
5025 typename iterator_traits
<_InputIterator2
>::value_type
>)
5026 __glibcxx_function_requires(_LessThanOpConcept
<
5027 typename iterator_traits
<_InputIterator2
>::value_type
,
5028 typename iterator_traits
<_InputIterator1
>::value_type
>)
5029 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5030 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5031 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5032 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5034 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5035 __first2
, __last2
, __result
,
5036 __gnu_cxx::__ops::__iter_less_iter());
5040 * @brief Return the union of two sorted ranges using a comparison functor.
5041 * @ingroup set_algorithms
5042 * @param __first1 Start of first range.
5043 * @param __last1 End of first range.
5044 * @param __first2 Start of second range.
5045 * @param __last2 End of second range.
5046 * @param __comp The comparison functor.
5047 * @return End of the output range.
5048 * @ingroup set_algorithms
5050 * This operation iterates over both ranges, copying elements present in
5051 * each range in order to the output range. Iterators increment for each
5052 * range. When the current element of one range is less than the other
5053 * according to @p __comp, that element is copied and the iterator advanced.
5054 * If an equivalent element according to @p __comp is contained in both
5055 * ranges, the element from the first range is copied and both ranges
5056 * advance. The output range may not overlap either input range.
5058 template<typename _InputIterator1
, typename _InputIterator2
,
5059 typename _OutputIterator
, typename _Compare
>
5060 inline _OutputIterator
5061 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5062 _InputIterator2 __first2
, _InputIterator2 __last2
,
5063 _OutputIterator __result
, _Compare __comp
)
5065 // concept requirements
5066 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5067 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5068 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5069 typename iterator_traits
<_InputIterator1
>::value_type
>)
5070 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5071 typename iterator_traits
<_InputIterator2
>::value_type
>)
5072 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5073 typename iterator_traits
<_InputIterator1
>::value_type
,
5074 typename iterator_traits
<_InputIterator2
>::value_type
>)
5075 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5076 typename iterator_traits
<_InputIterator2
>::value_type
,
5077 typename iterator_traits
<_InputIterator1
>::value_type
>)
5078 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5079 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5080 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5081 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5083 return _GLIBCXX_STD_A::__set_union(__first1
, __last1
,
5084 __first2
, __last2
, __result
,
5085 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5088 template<typename _InputIterator1
, typename _InputIterator2
,
5089 typename _OutputIterator
,
5092 __set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5093 _InputIterator2 __first2
, _InputIterator2 __last2
,
5094 _OutputIterator __result
, _Compare __comp
)
5096 while (__first1
!= __last1
&& __first2
!= __last2
)
5097 if (__comp(__first1
, __first2
))
5099 else if (__comp(__first2
, __first1
))
5103 *__result
= *__first1
;
5112 * @brief Return the intersection of two sorted ranges.
5113 * @ingroup set_algorithms
5114 * @param __first1 Start of first range.
5115 * @param __last1 End of first range.
5116 * @param __first2 Start of second range.
5117 * @param __last2 End of second range.
5118 * @return End of the output range.
5119 * @ingroup set_algorithms
5121 * This operation iterates over both ranges, copying elements present in
5122 * both ranges in order to the output range. Iterators increment for each
5123 * range. When the current element of one range is less than the other,
5124 * that iterator advances. If an element is contained in both ranges, the
5125 * element from the first range is copied and both ranges advance. The
5126 * output range may not overlap either input range.
5128 template<typename _InputIterator1
, typename _InputIterator2
,
5129 typename _OutputIterator
>
5130 inline _OutputIterator
5131 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5132 _InputIterator2 __first2
, _InputIterator2 __last2
,
5133 _OutputIterator __result
)
5135 // concept requirements
5136 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5138 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5139 typename iterator_traits
<_InputIterator1
>::value_type
>)
5140 __glibcxx_function_requires(_LessThanOpConcept
<
5141 typename iterator_traits
<_InputIterator1
>::value_type
,
5142 typename iterator_traits
<_InputIterator2
>::value_type
>)
5143 __glibcxx_function_requires(_LessThanOpConcept
<
5144 typename iterator_traits
<_InputIterator2
>::value_type
,
5145 typename iterator_traits
<_InputIterator1
>::value_type
>)
5146 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5147 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5148 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5149 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5151 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5152 __first2
, __last2
, __result
,
5153 __gnu_cxx::__ops::__iter_less_iter());
5157 * @brief Return the intersection of two sorted ranges using comparison
5159 * @ingroup set_algorithms
5160 * @param __first1 Start of first range.
5161 * @param __last1 End of first range.
5162 * @param __first2 Start of second range.
5163 * @param __last2 End of second range.
5164 * @param __comp The comparison functor.
5165 * @return End of the output range.
5166 * @ingroup set_algorithms
5168 * This operation iterates over both ranges, copying elements present in
5169 * both ranges in order to the output range. Iterators increment for each
5170 * range. When the current element of one range is less than the other
5171 * according to @p __comp, that iterator advances. If an element is
5172 * contained in both ranges according to @p __comp, the element from the
5173 * first range is copied and both ranges advance. The output range may not
5174 * overlap either input range.
5176 template<typename _InputIterator1
, typename _InputIterator2
,
5177 typename _OutputIterator
, typename _Compare
>
5178 inline _OutputIterator
5179 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5180 _InputIterator2 __first2
, _InputIterator2 __last2
,
5181 _OutputIterator __result
, _Compare __comp
)
5183 // concept requirements
5184 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5185 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5186 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5187 typename iterator_traits
<_InputIterator1
>::value_type
>)
5188 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5189 typename iterator_traits
<_InputIterator1
>::value_type
,
5190 typename iterator_traits
<_InputIterator2
>::value_type
>)
5191 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5192 typename iterator_traits
<_InputIterator2
>::value_type
,
5193 typename iterator_traits
<_InputIterator1
>::value_type
>)
5194 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5195 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5196 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5197 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5199 return _GLIBCXX_STD_A::__set_intersection(__first1
, __last1
,
5200 __first2
, __last2
, __result
,
5201 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5204 template<typename _InputIterator1
, typename _InputIterator2
,
5205 typename _OutputIterator
,
5208 __set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5209 _InputIterator2 __first2
, _InputIterator2 __last2
,
5210 _OutputIterator __result
, _Compare __comp
)
5212 while (__first1
!= __last1
&& __first2
!= __last2
)
5213 if (__comp(__first1
, __first2
))
5215 *__result
= *__first1
;
5219 else if (__comp(__first2
, __first1
))
5226 return std::copy(__first1
, __last1
, __result
);
5230 * @brief Return the difference of two sorted ranges.
5231 * @ingroup set_algorithms
5232 * @param __first1 Start of first range.
5233 * @param __last1 End of first range.
5234 * @param __first2 Start of second range.
5235 * @param __last2 End of second range.
5236 * @return End of the output range.
5237 * @ingroup set_algorithms
5239 * This operation iterates over both ranges, copying elements present in
5240 * the first range but not the second in order to the output range.
5241 * Iterators increment for each range. When the current element of the
5242 * first range is less than the second, that element is copied and the
5243 * iterator advances. If the current element of the second range is less,
5244 * the iterator advances, but no element is copied. If an element is
5245 * contained in both ranges, no elements are copied and both ranges
5246 * advance. The output range may not overlap either input range.
5248 template<typename _InputIterator1
, typename _InputIterator2
,
5249 typename _OutputIterator
>
5250 inline _OutputIterator
5251 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5252 _InputIterator2 __first2
, _InputIterator2 __last2
,
5253 _OutputIterator __result
)
5255 // concept requirements
5256 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5257 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5258 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5259 typename iterator_traits
<_InputIterator1
>::value_type
>)
5260 __glibcxx_function_requires(_LessThanOpConcept
<
5261 typename iterator_traits
<_InputIterator1
>::value_type
,
5262 typename iterator_traits
<_InputIterator2
>::value_type
>)
5263 __glibcxx_function_requires(_LessThanOpConcept
<
5264 typename iterator_traits
<_InputIterator2
>::value_type
,
5265 typename iterator_traits
<_InputIterator1
>::value_type
>)
5266 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5267 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5268 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5269 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5271 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5272 __first2
, __last2
, __result
,
5273 __gnu_cxx::__ops::__iter_less_iter());
5277 * @brief Return the difference of two sorted ranges using comparison
5279 * @ingroup set_algorithms
5280 * @param __first1 Start of first range.
5281 * @param __last1 End of first range.
5282 * @param __first2 Start of second range.
5283 * @param __last2 End of second range.
5284 * @param __comp The comparison functor.
5285 * @return End of the output range.
5286 * @ingroup set_algorithms
5288 * This operation iterates over both ranges, copying elements present in
5289 * the first range but not the second in order to the output range.
5290 * Iterators increment for each range. When the current element of the
5291 * first range is less than the second according to @p __comp, that element
5292 * is copied and the iterator advances. If the current element of the
5293 * second range is less, no element is copied and the iterator advances.
5294 * If an element is contained in both ranges according to @p __comp, no
5295 * elements are copied and both ranges advance. The output range may not
5296 * overlap either input range.
5298 template<typename _InputIterator1
, typename _InputIterator2
,
5299 typename _OutputIterator
, typename _Compare
>
5300 inline _OutputIterator
5301 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5302 _InputIterator2 __first2
, _InputIterator2 __last2
,
5303 _OutputIterator __result
, _Compare __comp
)
5305 // concept requirements
5306 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5307 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5308 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5309 typename iterator_traits
<_InputIterator1
>::value_type
>)
5310 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5311 typename iterator_traits
<_InputIterator1
>::value_type
,
5312 typename iterator_traits
<_InputIterator2
>::value_type
>)
5313 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5314 typename iterator_traits
<_InputIterator2
>::value_type
,
5315 typename iterator_traits
<_InputIterator1
>::value_type
>)
5316 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5317 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5318 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5319 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5321 return _GLIBCXX_STD_A::__set_difference(__first1
, __last1
,
5322 __first2
, __last2
, __result
,
5323 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5326 template<typename _InputIterator1
, typename _InputIterator2
,
5327 typename _OutputIterator
,
5330 __set_symmetric_difference(_InputIterator1 __first1
,
5331 _InputIterator1 __last1
,
5332 _InputIterator2 __first2
,
5333 _InputIterator2 __last2
,
5334 _OutputIterator __result
,
5337 while (__first1
!= __last1
&& __first2
!= __last2
)
5338 if (__comp(__first1
, __first2
))
5340 *__result
= *__first1
;
5344 else if (__comp(__first2
, __first1
))
5346 *__result
= *__first2
;
5355 return std::copy(__first2
, __last2
,
5356 std::copy(__first1
, __last1
, __result
));
5360 * @brief Return the symmetric difference of two sorted ranges.
5361 * @ingroup set_algorithms
5362 * @param __first1 Start of first range.
5363 * @param __last1 End of first range.
5364 * @param __first2 Start of second range.
5365 * @param __last2 End of second range.
5366 * @return End of the output range.
5367 * @ingroup set_algorithms
5369 * This operation iterates over both ranges, copying elements present in
5370 * one range but not the other in order to the output range. Iterators
5371 * increment for each range. When the current element of one range is less
5372 * than the other, that element is copied and the iterator advances. If an
5373 * element is contained in both ranges, no elements are copied and both
5374 * ranges advance. The output range may not overlap either input range.
5376 template<typename _InputIterator1
, typename _InputIterator2
,
5377 typename _OutputIterator
>
5378 inline _OutputIterator
5379 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5380 _InputIterator2 __first2
, _InputIterator2 __last2
,
5381 _OutputIterator __result
)
5383 // concept requirements
5384 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5385 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5386 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5387 typename iterator_traits
<_InputIterator1
>::value_type
>)
5388 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5389 typename iterator_traits
<_InputIterator2
>::value_type
>)
5390 __glibcxx_function_requires(_LessThanOpConcept
<
5391 typename iterator_traits
<_InputIterator1
>::value_type
,
5392 typename iterator_traits
<_InputIterator2
>::value_type
>)
5393 __glibcxx_function_requires(_LessThanOpConcept
<
5394 typename iterator_traits
<_InputIterator2
>::value_type
,
5395 typename iterator_traits
<_InputIterator1
>::value_type
>)
5396 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5397 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5398 __glibcxx_requires_irreflexive2(__first1
, __last1
);
5399 __glibcxx_requires_irreflexive2(__first2
, __last2
);
5401 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5402 __first2
, __last2
, __result
,
5403 __gnu_cxx::__ops::__iter_less_iter());
5407 * @brief Return the symmetric difference of two sorted ranges using
5408 * comparison functor.
5409 * @ingroup set_algorithms
5410 * @param __first1 Start of first range.
5411 * @param __last1 End of first range.
5412 * @param __first2 Start of second range.
5413 * @param __last2 End of second range.
5414 * @param __comp The comparison functor.
5415 * @return End of the output range.
5416 * @ingroup set_algorithms
5418 * This operation iterates over both ranges, copying elements present in
5419 * one range but not the other in order to the output range. Iterators
5420 * increment for each range. When the current element of one range is less
5421 * than the other according to @p comp, that element is copied and the
5422 * iterator advances. If an element is contained in both ranges according
5423 * to @p __comp, no elements are copied and both ranges advance. The output
5424 * range may not overlap either input range.
5426 template<typename _InputIterator1
, typename _InputIterator2
,
5427 typename _OutputIterator
, typename _Compare
>
5428 inline _OutputIterator
5429 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5430 _InputIterator2 __first2
, _InputIterator2 __last2
,
5431 _OutputIterator __result
,
5434 // concept requirements
5435 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5437 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5438 typename iterator_traits
<_InputIterator1
>::value_type
>)
5439 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5440 typename iterator_traits
<_InputIterator2
>::value_type
>)
5441 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5442 typename iterator_traits
<_InputIterator1
>::value_type
,
5443 typename iterator_traits
<_InputIterator2
>::value_type
>)
5444 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5445 typename iterator_traits
<_InputIterator2
>::value_type
,
5446 typename iterator_traits
<_InputIterator1
>::value_type
>)
5447 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5448 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5449 __glibcxx_requires_irreflexive_pred2(__first1
, __last1
, __comp
);
5450 __glibcxx_requires_irreflexive_pred2(__first2
, __last2
, __comp
);
5452 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1
, __last1
,
5453 __first2
, __last2
, __result
,
5454 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5457 template<typename _ForwardIterator
, typename _Compare
>
5458 _GLIBCXX14_CONSTEXPR
5460 __min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5463 if (__first
== __last
)
5465 _ForwardIterator __result
= __first
;
5466 while (++__first
!= __last
)
5467 if (__comp(__first
, __result
))
5473 * @brief Return the minimum element in a range.
5474 * @ingroup sorting_algorithms
5475 * @param __first Start of range.
5476 * @param __last End of range.
5477 * @return Iterator referencing the first instance of the smallest value.
5479 template<typename _ForwardIterator
>
5480 _GLIBCXX14_CONSTEXPR
5482 inline min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5484 // concept requirements
5485 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5486 __glibcxx_function_requires(_LessThanComparableConcept
<
5487 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5488 __glibcxx_requires_valid_range(__first
, __last
);
5489 __glibcxx_requires_irreflexive(__first
, __last
);
5491 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5492 __gnu_cxx::__ops::__iter_less_iter());
5496 * @brief Return the minimum element in a range using comparison functor.
5497 * @ingroup sorting_algorithms
5498 * @param __first Start of range.
5499 * @param __last End of range.
5500 * @param __comp Comparison functor.
5501 * @return Iterator referencing the first instance of the smallest value
5502 * according to __comp.
5504 template<typename _ForwardIterator
, typename _Compare
>
5505 _GLIBCXX14_CONSTEXPR
5506 inline _ForwardIterator
5507 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5510 // concept requirements
5511 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5512 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5513 typename iterator_traits
<_ForwardIterator
>::value_type
,
5514 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5515 __glibcxx_requires_valid_range(__first
, __last
);
5516 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5518 return _GLIBCXX_STD_A::__min_element(__first
, __last
,
5519 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5522 template<typename _ForwardIterator
, typename _Compare
>
5523 _GLIBCXX14_CONSTEXPR
5525 __max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5528 if (__first
== __last
) return __first
;
5529 _ForwardIterator __result
= __first
;
5530 while (++__first
!= __last
)
5531 if (__comp(__result
, __first
))
5537 * @brief Return the maximum element in a range.
5538 * @ingroup sorting_algorithms
5539 * @param __first Start of range.
5540 * @param __last End of range.
5541 * @return Iterator referencing the first instance of the largest value.
5543 template<typename _ForwardIterator
>
5544 _GLIBCXX14_CONSTEXPR
5545 inline _ForwardIterator
5546 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5548 // concept requirements
5549 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5550 __glibcxx_function_requires(_LessThanComparableConcept
<
5551 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5552 __glibcxx_requires_valid_range(__first
, __last
);
5553 __glibcxx_requires_irreflexive(__first
, __last
);
5555 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5556 __gnu_cxx::__ops::__iter_less_iter());
5560 * @brief Return the maximum element in a range using comparison functor.
5561 * @ingroup sorting_algorithms
5562 * @param __first Start of range.
5563 * @param __last End of range.
5564 * @param __comp Comparison functor.
5565 * @return Iterator referencing the first instance of the largest value
5566 * according to __comp.
5568 template<typename _ForwardIterator
, typename _Compare
>
5569 _GLIBCXX14_CONSTEXPR
5570 inline _ForwardIterator
5571 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5574 // concept requirements
5575 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5576 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5577 typename iterator_traits
<_ForwardIterator
>::value_type
,
5578 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5579 __glibcxx_requires_valid_range(__first
, __last
);
5580 __glibcxx_requires_irreflexive_pred(__first
, __last
, __comp
);
5582 return _GLIBCXX_STD_A::__max_element(__first
, __last
,
5583 __gnu_cxx::__ops::__iter_comp_iter(__comp
));
5586 _GLIBCXX_END_NAMESPACE_ALGO
5589 #endif /* _STL_ALGO_H */