1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_algo.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
59 #include <cstdlib> // for rand
60 #include <bits/algorithmfwd.h>
61 #include <bits/stl_heap.h>
62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
64 #if __cplusplus >= 201103L
65 #include <random> // for std::uniform_int_distribution
66 #include <functional> // for std::bind
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 to *__a
76 template<typename _Iterator
>
78 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
)
80 // concept requirements
81 __glibcxx_function_requires(_LessThanComparableConcept
<
82 typename iterator_traits
<_Iterator
>::value_type
>)
87 std::iter_swap(__a
, __b
);
89 std::iter_swap(__a
, __c
);
94 std::iter_swap(__a
, __c
);
96 std::iter_swap(__a
, __b
);
99 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
100 template<typename _Iterator
, typename _Compare
>
102 __move_median_first(_Iterator __a
, _Iterator __b
, _Iterator __c
,
105 // concept requirements
106 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
, bool,
107 typename iterator_traits
<_Iterator
>::value_type
,
108 typename iterator_traits
<_Iterator
>::value_type
>)
110 if (__comp(*__a
, *__b
))
112 if (__comp(*__b
, *__c
))
113 std::iter_swap(__a
, __b
);
114 else if (__comp(*__a
, *__c
))
115 std::iter_swap(__a
, __c
);
117 else if (__comp(*__a
, *__c
))
119 else if (__comp(*__b
, *__c
))
120 std::iter_swap(__a
, __c
);
122 std::iter_swap(__a
, __b
);
127 /// This is an overload used by find() for the Input Iterator case.
128 template<typename _InputIterator
, typename _Tp
>
129 inline _InputIterator
130 __find(_InputIterator __first
, _InputIterator __last
,
131 const _Tp
& __val
, input_iterator_tag
)
133 while (__first
!= __last
&& !(*__first
== __val
))
138 /// This is an overload used by find_if() for the Input Iterator case.
139 template<typename _InputIterator
, typename _Predicate
>
140 inline _InputIterator
141 __find_if(_InputIterator __first
, _InputIterator __last
,
142 _Predicate __pred
, input_iterator_tag
)
144 while (__first
!= __last
&& !bool(__pred(*__first
)))
149 /// This is an overload used by find() for the RAI case.
150 template<typename _RandomAccessIterator
, typename _Tp
>
151 _RandomAccessIterator
152 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
153 const _Tp
& __val
, random_access_iterator_tag
)
155 typename iterator_traits
<_RandomAccessIterator
>::difference_type
156 __trip_count
= (__last
- __first
) >> 2;
158 for (; __trip_count
> 0; --__trip_count
)
160 if (*__first
== __val
)
164 if (*__first
== __val
)
168 if (*__first
== __val
)
172 if (*__first
== __val
)
177 switch (__last
- __first
)
180 if (*__first
== __val
)
184 if (*__first
== __val
)
188 if (*__first
== __val
)
197 /// This is an overload used by find_if() for the RAI case.
198 template<typename _RandomAccessIterator
, typename _Predicate
>
199 _RandomAccessIterator
200 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
201 _Predicate __pred
, random_access_iterator_tag
)
203 typename iterator_traits
<_RandomAccessIterator
>::difference_type
204 __trip_count
= (__last
- __first
) >> 2;
206 for (; __trip_count
> 0; --__trip_count
)
208 if (__pred(*__first
))
212 if (__pred(*__first
))
216 if (__pred(*__first
))
220 if (__pred(*__first
))
225 switch (__last
- __first
)
228 if (__pred(*__first
))
232 if (__pred(*__first
))
236 if (__pred(*__first
))
245 /// This is an overload used by find_if_not() for the Input Iterator case.
246 template<typename _InputIterator
, typename _Predicate
>
247 inline _InputIterator
248 __find_if_not(_InputIterator __first
, _InputIterator __last
,
249 _Predicate __pred
, input_iterator_tag
)
251 while (__first
!= __last
&& bool(__pred(*__first
)))
256 /// This is an overload used by find_if_not() for the RAI case.
257 template<typename _RandomAccessIterator
, typename _Predicate
>
258 _RandomAccessIterator
259 __find_if_not(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
260 _Predicate __pred
, random_access_iterator_tag
)
262 typename iterator_traits
<_RandomAccessIterator
>::difference_type
263 __trip_count
= (__last
- __first
) >> 2;
265 for (; __trip_count
> 0; --__trip_count
)
267 if (!bool(__pred(*__first
)))
271 if (!bool(__pred(*__first
)))
275 if (!bool(__pred(*__first
)))
279 if (!bool(__pred(*__first
)))
284 switch (__last
- __first
)
287 if (!bool(__pred(*__first
)))
291 if (!bool(__pred(*__first
)))
295 if (!bool(__pred(*__first
)))
304 /// Provided for stable_partition to use.
305 template<typename _InputIterator
, typename _Predicate
>
306 inline _InputIterator
307 __find_if_not(_InputIterator __first
, _InputIterator __last
,
310 return std::__find_if_not(__first
, __last
, __pred
,
311 std::__iterator_category(__first
));
314 /// Like find_if_not(), but uses and updates a count of the
315 /// remaining range length instead of comparing against an end
317 template<typename _InputIterator
, typename _Predicate
, typename _Distance
>
319 __find_if_not_n(_InputIterator __first
, _Distance
& __len
, _Predicate __pred
)
321 for (; __len
; --__len
, ++__first
)
322 if (!bool(__pred(*__first
)))
329 // set_symmetric_difference
341 * This is an uglified
342 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
343 * overloaded for forward iterators.
345 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
347 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
348 _Integer __count
, const _Tp
& __val
,
349 std::forward_iterator_tag
)
351 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __val
);
352 while (__first
!= __last
)
354 typename iterator_traits
<_ForwardIterator
>::difference_type
356 _ForwardIterator __i
= __first
;
358 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
367 __first
= _GLIBCXX_STD_A::find(++__i
, __last
, __val
);
373 * This is an uglified
374 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
375 * overloaded for random access iterators.
377 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
379 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
380 _Integer __count
, const _Tp
& __val
,
381 std::random_access_iterator_tag
)
384 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
387 _DistanceType __tailSize
= __last
- __first
;
388 const _DistanceType __pattSize
= __count
;
390 if (__tailSize
< __pattSize
)
393 const _DistanceType __skipOffset
= __pattSize
- 1;
394 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
395 __tailSize
-= __pattSize
;
397 while (1) // the main loop...
399 // __lookAhead here is always pointing to the last element of next
401 while (!(*__lookAhead
== __val
)) // the skip loop...
403 if (__tailSize
< __pattSize
)
404 return __last
; // Failure
405 __lookAhead
+= __pattSize
;
406 __tailSize
-= __pattSize
;
408 _DistanceType __remainder
= __skipOffset
;
409 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
410 *__backTrack
== __val
; --__backTrack
)
412 if (--__remainder
== 0)
413 return (__lookAhead
- __skipOffset
); // Success
415 if (__remainder
> __tailSize
)
416 return __last
; // Failure
417 __lookAhead
+= __remainder
;
418 __tailSize
-= __remainder
;
425 * This is an uglified
426 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
428 * overloaded for forward iterators.
430 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
431 typename _BinaryPredicate
>
433 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
434 _Integer __count
, const _Tp
& __val
,
435 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
437 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
440 while (__first
!= __last
)
442 typename iterator_traits
<_ForwardIterator
>::difference_type
444 _ForwardIterator __i
= __first
;
446 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
456 while (__first
!= __last
457 && !bool(__binary_pred(*__first
, __val
)))
464 * This is an uglified
465 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
467 * overloaded for random access iterators.
469 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
470 typename _BinaryPredicate
>
472 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
473 _Integer __count
, const _Tp
& __val
,
474 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
477 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
480 _DistanceType __tailSize
= __last
- __first
;
481 const _DistanceType __pattSize
= __count
;
483 if (__tailSize
< __pattSize
)
486 const _DistanceType __skipOffset
= __pattSize
- 1;
487 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
488 __tailSize
-= __pattSize
;
490 while (1) // the main loop...
492 // __lookAhead here is always pointing to the last element of next
494 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
496 if (__tailSize
< __pattSize
)
497 return __last
; // Failure
498 __lookAhead
+= __pattSize
;
499 __tailSize
-= __pattSize
;
501 _DistanceType __remainder
= __skipOffset
;
502 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
503 __binary_pred(*__backTrack
, __val
); --__backTrack
)
505 if (--__remainder
== 0)
506 return (__lookAhead
- __skipOffset
); // Success
508 if (__remainder
> __tailSize
)
509 return __last
; // Failure
510 __lookAhead
+= __remainder
;
511 __tailSize
-= __remainder
;
515 // find_end for forward iterators.
516 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
518 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
519 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
520 forward_iterator_tag
, forward_iterator_tag
)
522 if (__first2
== __last2
)
526 _ForwardIterator1 __result
= __last1
;
529 _ForwardIterator1 __new_result
530 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
, __last2
);
531 if (__new_result
== __last1
)
535 __result
= __new_result
;
536 __first1
= __new_result
;
543 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
544 typename _BinaryPredicate
>
546 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
547 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
548 forward_iterator_tag
, forward_iterator_tag
,
549 _BinaryPredicate __comp
)
551 if (__first2
== __last2
)
555 _ForwardIterator1 __result
= __last1
;
558 _ForwardIterator1 __new_result
559 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
,
561 if (__new_result
== __last1
)
565 __result
= __new_result
;
566 __first1
= __new_result
;
573 // find_end for bidirectional iterators (much faster).
574 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
575 _BidirectionalIterator1
576 __find_end(_BidirectionalIterator1 __first1
,
577 _BidirectionalIterator1 __last1
,
578 _BidirectionalIterator2 __first2
,
579 _BidirectionalIterator2 __last2
,
580 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
582 // concept requirements
583 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
584 _BidirectionalIterator1
>)
585 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
586 _BidirectionalIterator2
>)
588 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
589 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
591 _RevIterator1
__rlast1(__first1
);
592 _RevIterator2
__rlast2(__first2
);
593 _RevIterator1 __rresult
= _GLIBCXX_STD_A::search(_RevIterator1(__last1
),
595 _RevIterator2(__last2
),
598 if (__rresult
== __rlast1
)
602 _BidirectionalIterator1 __result
= __rresult
.base();
603 std::advance(__result
, -std::distance(__first2
, __last2
));
608 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
609 typename _BinaryPredicate
>
610 _BidirectionalIterator1
611 __find_end(_BidirectionalIterator1 __first1
,
612 _BidirectionalIterator1 __last1
,
613 _BidirectionalIterator2 __first2
,
614 _BidirectionalIterator2 __last2
,
615 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
616 _BinaryPredicate __comp
)
618 // concept requirements
619 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
620 _BidirectionalIterator1
>)
621 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
622 _BidirectionalIterator2
>)
624 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
625 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
627 _RevIterator1
__rlast1(__first1
);
628 _RevIterator2
__rlast2(__first2
);
629 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
630 _RevIterator2(__last2
), __rlast2
,
633 if (__rresult
== __rlast1
)
637 _BidirectionalIterator1 __result
= __rresult
.base();
638 std::advance(__result
, -std::distance(__first2
, __last2
));
644 * @brief Find last matching subsequence in a sequence.
645 * @ingroup non_mutating_algorithms
646 * @param __first1 Start of range to search.
647 * @param __last1 End of range to search.
648 * @param __first2 Start of sequence to match.
649 * @param __last2 End of sequence to match.
650 * @return The last iterator @c i in the range
651 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
652 * @p *(__first2+N) for each @c N in the range @p
653 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
655 * Searches the range @p [__first1,__last1) for a sub-sequence that
656 * compares equal value-by-value with the sequence given by @p
657 * [__first2,__last2) and returns an iterator to the __first
658 * element of the sub-sequence, or @p __last1 if the sub-sequence
659 * is not found. The sub-sequence will be the last such
660 * subsequence contained in [__first,__last1).
662 * Because the sub-sequence must lie completely within the range @p
663 * [__first1,__last1) it must start at a position less than @p
664 * __last1-(__last2-__first2) where @p __last2-__first2 is the
665 * length of the sub-sequence. This means that the returned
666 * iterator @c i will be in the range @p
667 * [__first1,__last1-(__last2-__first2))
669 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
670 inline _ForwardIterator1
671 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
672 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
674 // concept requirements
675 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
676 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
677 __glibcxx_function_requires(_EqualOpConcept
<
678 typename iterator_traits
<_ForwardIterator1
>::value_type
,
679 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
680 __glibcxx_requires_valid_range(__first1
, __last1
);
681 __glibcxx_requires_valid_range(__first2
, __last2
);
683 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
684 std::__iterator_category(__first1
),
685 std::__iterator_category(__first2
));
689 * @brief Find last matching subsequence in a sequence using a predicate.
690 * @ingroup non_mutating_algorithms
691 * @param __first1 Start of range to search.
692 * @param __last1 End of range to search.
693 * @param __first2 Start of sequence to match.
694 * @param __last2 End of sequence to match.
695 * @param __comp The predicate to use.
696 * @return The last iterator @c i in the range @p
697 * [__first1,__last1-(__last2-__first2)) such that @c
698 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
699 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
702 * Searches the range @p [__first1,__last1) for a sub-sequence that
703 * compares equal value-by-value with the sequence given by @p
704 * [__first2,__last2) using comp as a predicate and returns an
705 * iterator to the first element of the sub-sequence, or @p __last1
706 * if the sub-sequence is not found. The sub-sequence will be the
707 * last such subsequence contained in [__first,__last1).
709 * Because the sub-sequence must lie completely within the range @p
710 * [__first1,__last1) it must start at a position less than @p
711 * __last1-(__last2-__first2) where @p __last2-__first2 is the
712 * length of the sub-sequence. This means that the returned
713 * iterator @c i will be in the range @p
714 * [__first1,__last1-(__last2-__first2))
716 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
717 typename _BinaryPredicate
>
718 inline _ForwardIterator1
719 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
720 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
721 _BinaryPredicate __comp
)
723 // concept requirements
724 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
725 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
726 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
727 typename iterator_traits
<_ForwardIterator1
>::value_type
,
728 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
729 __glibcxx_requires_valid_range(__first1
, __last1
);
730 __glibcxx_requires_valid_range(__first2
, __last2
);
732 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
733 std::__iterator_category(__first1
),
734 std::__iterator_category(__first2
),
738 #if __cplusplus >= 201103L
740 * @brief Checks that a predicate is true for all the elements
742 * @ingroup non_mutating_algorithms
743 * @param __first An input iterator.
744 * @param __last An input iterator.
745 * @param __pred A predicate.
746 * @return True if the check is true, false otherwise.
748 * Returns true if @p __pred is true for each element in the range
749 * @p [__first,__last), and false otherwise.
751 template<typename _InputIterator
, typename _Predicate
>
753 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
754 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
757 * @brief Checks that a predicate is false for all the elements
759 * @ingroup non_mutating_algorithms
760 * @param __first An input iterator.
761 * @param __last An input iterator.
762 * @param __pred A predicate.
763 * @return True if the check is true, false otherwise.
765 * Returns true if @p __pred is false for each element in the range
766 * @p [__first,__last), and false otherwise.
768 template<typename _InputIterator
, typename _Predicate
>
770 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
771 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
774 * @brief Checks that a predicate is false for at least an element
776 * @ingroup non_mutating_algorithms
777 * @param __first An input iterator.
778 * @param __last An input iterator.
779 * @param __pred A predicate.
780 * @return True if the check is true, false otherwise.
782 * Returns true if an element exists in the range @p
783 * [__first,__last) such that @p __pred is true, and false
786 template<typename _InputIterator
, typename _Predicate
>
788 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
789 { return !std::none_of(__first
, __last
, __pred
); }
792 * @brief Find the first element in a sequence for which a
793 * predicate is false.
794 * @ingroup non_mutating_algorithms
795 * @param __first An input iterator.
796 * @param __last An input iterator.
797 * @param __pred A predicate.
798 * @return The first iterator @c i in the range @p [__first,__last)
799 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
801 template<typename _InputIterator
, typename _Predicate
>
802 inline _InputIterator
803 find_if_not(_InputIterator __first
, _InputIterator __last
,
806 // concept requirements
807 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
808 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
809 typename iterator_traits
<_InputIterator
>::value_type
>)
810 __glibcxx_requires_valid_range(__first
, __last
);
811 return std::__find_if_not(__first
, __last
, __pred
);
815 * @brief Checks whether the sequence is partitioned.
816 * @ingroup mutating_algorithms
817 * @param __first An input iterator.
818 * @param __last An input iterator.
819 * @param __pred A predicate.
820 * @return True if the range @p [__first,__last) is partioned by @p __pred,
821 * i.e. if all elements that satisfy @p __pred appear before those that
824 template<typename _InputIterator
, typename _Predicate
>
826 is_partitioned(_InputIterator __first
, _InputIterator __last
,
829 __first
= std::find_if_not(__first
, __last
, __pred
);
830 return std::none_of(__first
, __last
, __pred
);
834 * @brief Find the partition point of a partitioned range.
835 * @ingroup mutating_algorithms
836 * @param __first An iterator.
837 * @param __last Another iterator.
838 * @param __pred A predicate.
839 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
840 * and @p none_of(mid, __last, __pred) are both true.
842 template<typename _ForwardIterator
, typename _Predicate
>
844 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
847 // concept requirements
848 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
849 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
850 typename iterator_traits
<_ForwardIterator
>::value_type
>)
852 // A specific debug-mode test will be necessary...
853 __glibcxx_requires_valid_range(__first
, __last
);
855 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
858 _DistanceType __len
= std::distance(__first
, __last
);
859 _DistanceType __half
;
860 _ForwardIterator __middle
;
866 std::advance(__middle
, __half
);
867 if (__pred(*__middle
))
871 __len
= __len
- __half
- 1;
882 * @brief Copy a sequence, removing elements of a given value.
883 * @ingroup mutating_algorithms
884 * @param __first An input iterator.
885 * @param __last An input iterator.
886 * @param __result An output iterator.
887 * @param __value The value to be removed.
888 * @return An iterator designating the end of the resulting sequence.
890 * Copies each element in the range @p [__first,__last) not equal
891 * to @p __value to the range beginning at @p __result.
892 * remove_copy() is stable, so the relative order of elements that
893 * are copied is unchanged.
895 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
897 remove_copy(_InputIterator __first
, _InputIterator __last
,
898 _OutputIterator __result
, const _Tp
& __value
)
900 // concept requirements
901 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
902 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
903 typename iterator_traits
<_InputIterator
>::value_type
>)
904 __glibcxx_function_requires(_EqualOpConcept
<
905 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
906 __glibcxx_requires_valid_range(__first
, __last
);
908 for (; __first
!= __last
; ++__first
)
909 if (!(*__first
== __value
))
911 *__result
= *__first
;
918 * @brief Copy a sequence, removing elements for which a predicate is true.
919 * @ingroup mutating_algorithms
920 * @param __first An input iterator.
921 * @param __last An input iterator.
922 * @param __result An output iterator.
923 * @param __pred A predicate.
924 * @return An iterator designating the end of the resulting sequence.
926 * Copies each element in the range @p [__first,__last) for which
927 * @p __pred returns false to the range beginning at @p __result.
929 * remove_copy_if() is stable, so the relative order of elements that are
930 * copied is unchanged.
932 template<typename _InputIterator
, typename _OutputIterator
,
935 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
936 _OutputIterator __result
, _Predicate __pred
)
938 // concept requirements
939 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
940 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
941 typename iterator_traits
<_InputIterator
>::value_type
>)
942 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
943 typename iterator_traits
<_InputIterator
>::value_type
>)
944 __glibcxx_requires_valid_range(__first
, __last
);
946 for (; __first
!= __last
; ++__first
)
947 if (!bool(__pred(*__first
)))
949 *__result
= *__first
;
955 #if __cplusplus >= 201103L
957 * @brief Copy the elements of a sequence for which a predicate is true.
958 * @ingroup mutating_algorithms
959 * @param __first An input iterator.
960 * @param __last An input iterator.
961 * @param __result An output iterator.
962 * @param __pred A predicate.
963 * @return An iterator designating the end of the resulting sequence.
965 * Copies each element in the range @p [__first,__last) for which
966 * @p __pred returns true to the range beginning at @p __result.
968 * copy_if() is stable, so the relative order of elements that are
969 * copied is unchanged.
971 template<typename _InputIterator
, typename _OutputIterator
,
974 copy_if(_InputIterator __first
, _InputIterator __last
,
975 _OutputIterator __result
, _Predicate __pred
)
977 // concept requirements
978 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
979 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
980 typename iterator_traits
<_InputIterator
>::value_type
>)
981 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
982 typename iterator_traits
<_InputIterator
>::value_type
>)
983 __glibcxx_requires_valid_range(__first
, __last
);
985 for (; __first
!= __last
; ++__first
)
986 if (__pred(*__first
))
988 *__result
= *__first
;
995 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
997 __copy_n(_InputIterator __first
, _Size __n
,
998 _OutputIterator __result
, input_iterator_tag
)
1004 *__result
= *__first
;
1015 template<typename _RandomAccessIterator
, typename _Size
,
1016 typename _OutputIterator
>
1017 inline _OutputIterator
1018 __copy_n(_RandomAccessIterator __first
, _Size __n
,
1019 _OutputIterator __result
, random_access_iterator_tag
)
1020 { return std::copy(__first
, __first
+ __n
, __result
); }
1023 * @brief Copies the range [first,first+n) into [result,result+n).
1024 * @ingroup mutating_algorithms
1025 * @param __first An input iterator.
1026 * @param __n The number of elements to copy.
1027 * @param __result An output iterator.
1030 * This inline function will boil down to a call to @c memmove whenever
1031 * possible. Failing that, if random access iterators are passed, then the
1032 * loop count will be known (and therefore a candidate for compiler
1033 * optimizations such as unrolling).
1035 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1036 inline _OutputIterator
1037 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1039 // concept requirements
1040 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1041 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1042 typename iterator_traits
<_InputIterator
>::value_type
>)
1044 return std::__copy_n(__first
, __n
, __result
,
1045 std::__iterator_category(__first
));
1049 * @brief Copy the elements of a sequence to separate output sequences
1050 * depending on the truth value of a predicate.
1051 * @ingroup mutating_algorithms
1052 * @param __first An input iterator.
1053 * @param __last An input iterator.
1054 * @param __out_true An output iterator.
1055 * @param __out_false An output iterator.
1056 * @param __pred A predicate.
1057 * @return A pair designating the ends of the resulting sequences.
1059 * Copies each element in the range @p [__first,__last) for which
1060 * @p __pred returns true to the range beginning at @p out_true
1061 * and each element for which @p __pred returns false to @p __out_false.
1063 template<typename _InputIterator
, typename _OutputIterator1
,
1064 typename _OutputIterator2
, typename _Predicate
>
1065 pair
<_OutputIterator1
, _OutputIterator2
>
1066 partition_copy(_InputIterator __first
, _InputIterator __last
,
1067 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1070 // concept requirements
1071 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1072 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1073 typename iterator_traits
<_InputIterator
>::value_type
>)
1074 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1075 typename iterator_traits
<_InputIterator
>::value_type
>)
1076 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1077 typename iterator_traits
<_InputIterator
>::value_type
>)
1078 __glibcxx_requires_valid_range(__first
, __last
);
1080 for (; __first
!= __last
; ++__first
)
1081 if (__pred(*__first
))
1083 *__out_true
= *__first
;
1088 *__out_false
= *__first
;
1092 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1097 * @brief Remove elements from a sequence.
1098 * @ingroup mutating_algorithms
1099 * @param __first An input iterator.
1100 * @param __last An input iterator.
1101 * @param __value The value to be removed.
1102 * @return An iterator designating the end of the resulting sequence.
1104 * All elements equal to @p __value are removed from the range
1105 * @p [__first,__last).
1107 * remove() is stable, so the relative order of elements that are
1108 * not removed is unchanged.
1110 * Elements between the end of the resulting sequence and @p __last
1111 * are still present, but their value is unspecified.
1113 template<typename _ForwardIterator
, typename _Tp
>
1115 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1118 // concept requirements
1119 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1121 __glibcxx_function_requires(_EqualOpConcept
<
1122 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1123 __glibcxx_requires_valid_range(__first
, __last
);
1125 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __value
);
1126 if(__first
== __last
)
1128 _ForwardIterator __result
= __first
;
1130 for(; __first
!= __last
; ++__first
)
1131 if(!(*__first
== __value
))
1133 *__result
= _GLIBCXX_MOVE(*__first
);
1140 * @brief Remove elements from a sequence using a predicate.
1141 * @ingroup mutating_algorithms
1142 * @param __first A forward iterator.
1143 * @param __last A forward iterator.
1144 * @param __pred A predicate.
1145 * @return An iterator designating the end of the resulting sequence.
1147 * All elements for which @p __pred returns true are removed from the range
1148 * @p [__first,__last).
1150 * remove_if() is stable, so the relative order of elements that are
1151 * not removed is unchanged.
1153 * Elements between the end of the resulting sequence and @p __last
1154 * are still present, but their value is unspecified.
1156 template<typename _ForwardIterator
, typename _Predicate
>
1158 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1161 // concept requirements
1162 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1164 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1165 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1166 __glibcxx_requires_valid_range(__first
, __last
);
1168 __first
= _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
);
1169 if(__first
== __last
)
1171 _ForwardIterator __result
= __first
;
1173 for(; __first
!= __last
; ++__first
)
1174 if(!bool(__pred(*__first
)))
1176 *__result
= _GLIBCXX_MOVE(*__first
);
1183 * @brief Remove consecutive duplicate values from a sequence.
1184 * @ingroup mutating_algorithms
1185 * @param __first A forward iterator.
1186 * @param __last A forward iterator.
1187 * @return An iterator designating the end of the resulting sequence.
1189 * Removes all but the first element from each group of consecutive
1190 * values that compare equal.
1191 * unique() is stable, so the relative order of elements that are
1192 * not removed is unchanged.
1193 * Elements between the end of the resulting sequence and @p __last
1194 * are still present, but their value is unspecified.
1196 template<typename _ForwardIterator
>
1198 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1200 // concept requirements
1201 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1203 __glibcxx_function_requires(_EqualityComparableConcept
<
1204 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1205 __glibcxx_requires_valid_range(__first
, __last
);
1207 // Skip the beginning, if already unique.
1208 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
);
1209 if (__first
== __last
)
1212 // Do the real copy work.
1213 _ForwardIterator __dest
= __first
;
1215 while (++__first
!= __last
)
1216 if (!(*__dest
== *__first
))
1217 *++__dest
= _GLIBCXX_MOVE(*__first
);
1222 * @brief Remove consecutive values from a sequence using a predicate.
1223 * @ingroup mutating_algorithms
1224 * @param __first A forward iterator.
1225 * @param __last A forward iterator.
1226 * @param __binary_pred A binary predicate.
1227 * @return An iterator designating the end of the resulting sequence.
1229 * Removes all but the first element from each group of consecutive
1230 * values for which @p __binary_pred returns true.
1231 * unique() is stable, so the relative order of elements that are
1232 * not removed is unchanged.
1233 * Elements between the end of the resulting sequence and @p __last
1234 * are still present, but their value is unspecified.
1236 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1238 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1239 _BinaryPredicate __binary_pred
)
1241 // concept requirements
1242 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1244 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1245 typename iterator_traits
<_ForwardIterator
>::value_type
,
1246 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1247 __glibcxx_requires_valid_range(__first
, __last
);
1249 // Skip the beginning, if already unique.
1250 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
, __binary_pred
);
1251 if (__first
== __last
)
1254 // Do the real copy work.
1255 _ForwardIterator __dest
= __first
;
1257 while (++__first
!= __last
)
1258 if (!bool(__binary_pred(*__dest
, *__first
)))
1259 *++__dest
= _GLIBCXX_MOVE(*__first
);
1264 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1266 * overloaded for forward iterators and output iterator as result.
1268 template<typename _ForwardIterator
, typename _OutputIterator
>
1270 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1271 _OutputIterator __result
,
1272 forward_iterator_tag
, output_iterator_tag
)
1274 // concept requirements -- taken care of in dispatching function
1275 _ForwardIterator __next
= __first
;
1276 *__result
= *__first
;
1277 while (++__next
!= __last
)
1278 if (!(*__first
== *__next
))
1281 *++__result
= *__first
;
1287 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1289 * overloaded for input iterators and output iterator as result.
1291 template<typename _InputIterator
, typename _OutputIterator
>
1293 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1294 _OutputIterator __result
,
1295 input_iterator_tag
, output_iterator_tag
)
1297 // concept requirements -- taken care of in dispatching function
1298 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1299 *__result
= __value
;
1300 while (++__first
!= __last
)
1301 if (!(__value
== *__first
))
1304 *++__result
= __value
;
1310 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1312 * overloaded for input iterators and forward iterator as result.
1314 template<typename _InputIterator
, typename _ForwardIterator
>
1316 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1317 _ForwardIterator __result
,
1318 input_iterator_tag
, forward_iterator_tag
)
1320 // concept requirements -- taken care of in dispatching function
1321 *__result
= *__first
;
1322 while (++__first
!= __last
)
1323 if (!(*__result
== *__first
))
1324 *++__result
= *__first
;
1329 * This is an uglified
1330 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1332 * overloaded for forward iterators and output iterator as result.
1334 template<typename _ForwardIterator
, typename _OutputIterator
,
1335 typename _BinaryPredicate
>
1337 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1338 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1339 forward_iterator_tag
, output_iterator_tag
)
1341 // concept requirements -- iterators already checked
1342 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1343 typename iterator_traits
<_ForwardIterator
>::value_type
,
1344 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1346 _ForwardIterator __next
= __first
;
1347 *__result
= *__first
;
1348 while (++__next
!= __last
)
1349 if (!bool(__binary_pred(*__first
, *__next
)))
1352 *++__result
= *__first
;
1358 * This is an uglified
1359 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1361 * overloaded for input iterators and output iterator as result.
1363 template<typename _InputIterator
, typename _OutputIterator
,
1364 typename _BinaryPredicate
>
1366 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1367 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1368 input_iterator_tag
, output_iterator_tag
)
1370 // concept requirements -- iterators already checked
1371 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1372 typename iterator_traits
<_InputIterator
>::value_type
,
1373 typename iterator_traits
<_InputIterator
>::value_type
>)
1375 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1376 *__result
= __value
;
1377 while (++__first
!= __last
)
1378 if (!bool(__binary_pred(__value
, *__first
)))
1381 *++__result
= __value
;
1387 * This is an uglified
1388 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1390 * overloaded for input iterators and forward iterator as result.
1392 template<typename _InputIterator
, typename _ForwardIterator
,
1393 typename _BinaryPredicate
>
1395 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1396 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1397 input_iterator_tag
, forward_iterator_tag
)
1399 // concept requirements -- iterators already checked
1400 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1401 typename iterator_traits
<_ForwardIterator
>::value_type
,
1402 typename iterator_traits
<_InputIterator
>::value_type
>)
1404 *__result
= *__first
;
1405 while (++__first
!= __last
)
1406 if (!bool(__binary_pred(*__result
, *__first
)))
1407 *++__result
= *__first
;
1412 * This is an uglified reverse(_BidirectionalIterator,
1413 * _BidirectionalIterator)
1414 * overloaded for bidirectional iterators.
1416 template<typename _BidirectionalIterator
>
1418 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1419 bidirectional_iterator_tag
)
1422 if (__first
== __last
|| __first
== --__last
)
1426 std::iter_swap(__first
, __last
);
1432 * This is an uglified reverse(_BidirectionalIterator,
1433 * _BidirectionalIterator)
1434 * overloaded for random access iterators.
1436 template<typename _RandomAccessIterator
>
1438 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1439 random_access_iterator_tag
)
1441 if (__first
== __last
)
1444 while (__first
< __last
)
1446 std::iter_swap(__first
, __last
);
1453 * @brief Reverse a sequence.
1454 * @ingroup mutating_algorithms
1455 * @param __first A bidirectional iterator.
1456 * @param __last A bidirectional iterator.
1457 * @return reverse() returns no value.
1459 * Reverses the order of the elements in the range @p [__first,__last),
1460 * so that the first element becomes the last etc.
1461 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1462 * swaps @p *(__first+i) and @p *(__last-(i+1))
1464 template<typename _BidirectionalIterator
>
1466 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1468 // concept requirements
1469 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1470 _BidirectionalIterator
>)
1471 __glibcxx_requires_valid_range(__first
, __last
);
1472 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1476 * @brief Copy a sequence, reversing its elements.
1477 * @ingroup mutating_algorithms
1478 * @param __first A bidirectional iterator.
1479 * @param __last A bidirectional iterator.
1480 * @param __result An output iterator.
1481 * @return An iterator designating the end of the resulting sequence.
1483 * Copies the elements in the range @p [__first,__last) to the
1484 * range @p [__result,__result+(__last-__first)) such that the
1485 * order of the elements is reversed. For every @c i such that @p
1486 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1487 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1488 * The ranges @p [__first,__last) and @p
1489 * [__result,__result+(__last-__first)) must not overlap.
1491 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1493 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1494 _OutputIterator __result
)
1496 // concept requirements
1497 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1498 _BidirectionalIterator
>)
1499 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1500 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1501 __glibcxx_requires_valid_range(__first
, __last
);
1503 while (__first
!= __last
)
1506 *__result
= *__last
;
1513 * This is a helper function for the rotate algorithm specialized on RAIs.
1514 * It returns the greatest common divisor of two integer values.
1516 template<typename _EuclideanRingElement
>
1517 _EuclideanRingElement
1518 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1522 _EuclideanRingElement __t
= __m
% __n
;
1529 /// This is a helper function for the rotate algorithm.
1530 template<typename _ForwardIterator
>
1532 __rotate(_ForwardIterator __first
,
1533 _ForwardIterator __middle
,
1534 _ForwardIterator __last
,
1535 forward_iterator_tag
)
1537 if (__first
== __middle
|| __last
== __middle
)
1540 _ForwardIterator __first2
= __middle
;
1543 std::iter_swap(__first
, __first2
);
1546 if (__first
== __middle
)
1547 __middle
= __first2
;
1549 while (__first2
!= __last
);
1551 __first2
= __middle
;
1553 while (__first2
!= __last
)
1555 std::iter_swap(__first
, __first2
);
1558 if (__first
== __middle
)
1559 __middle
= __first2
;
1560 else if (__first2
== __last
)
1561 __first2
= __middle
;
1565 /// This is a helper function for the rotate algorithm.
1566 template<typename _BidirectionalIterator
>
1568 __rotate(_BidirectionalIterator __first
,
1569 _BidirectionalIterator __middle
,
1570 _BidirectionalIterator __last
,
1571 bidirectional_iterator_tag
)
1573 // concept requirements
1574 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1575 _BidirectionalIterator
>)
1577 if (__first
== __middle
|| __last
== __middle
)
1580 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1581 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1583 while (__first
!= __middle
&& __middle
!= __last
)
1585 std::iter_swap(__first
, --__last
);
1589 if (__first
== __middle
)
1590 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1592 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1595 /// This is a helper function for the rotate algorithm.
1596 template<typename _RandomAccessIterator
>
1598 __rotate(_RandomAccessIterator __first
,
1599 _RandomAccessIterator __middle
,
1600 _RandomAccessIterator __last
,
1601 random_access_iterator_tag
)
1603 // concept requirements
1604 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1605 _RandomAccessIterator
>)
1607 if (__first
== __middle
|| __last
== __middle
)
1610 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1612 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1615 _Distance __n
= __last
- __first
;
1616 _Distance __k
= __middle
- __first
;
1618 if (__k
== __n
- __k
)
1620 std::swap_ranges(__first
, __middle
, __middle
);
1624 _RandomAccessIterator __p
= __first
;
1628 if (__k
< __n
- __k
)
1630 if (__is_pod(_ValueType
) && __k
== 1)
1632 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1633 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1634 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1637 _RandomAccessIterator __q
= __p
+ __k
;
1638 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1640 std::iter_swap(__p
, __q
);
1647 std::swap(__n
, __k
);
1653 if (__is_pod(_ValueType
) && __k
== 1)
1655 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1656 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1657 *__p
= _GLIBCXX_MOVE(__t
);
1660 _RandomAccessIterator __q
= __p
+ __n
;
1662 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1666 std::iter_swap(__p
, __q
);
1671 std::swap(__n
, __k
);
1677 * @brief Rotate the elements of a sequence.
1678 * @ingroup mutating_algorithms
1679 * @param __first A forward iterator.
1680 * @param __middle A forward iterator.
1681 * @param __last A forward iterator.
1684 * Rotates the elements of the range @p [__first,__last) by
1685 * @p (__middle - __first) positions so that the element at @p __middle
1686 * is moved to @p __first, the element at @p __middle+1 is moved to
1687 * @p __first+1 and so on for each element in the range
1688 * @p [__first,__last).
1690 * This effectively swaps the ranges @p [__first,__middle) and
1691 * @p [__middle,__last).
1694 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1695 * for each @p n in the range @p [0,__last-__first).
1697 template<typename _ForwardIterator
>
1699 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1700 _ForwardIterator __last
)
1702 // concept requirements
1703 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1705 __glibcxx_requires_valid_range(__first
, __middle
);
1706 __glibcxx_requires_valid_range(__middle
, __last
);
1708 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1710 std::__rotate(__first
, __middle
, __last
, _IterType());
1714 * @brief Copy a sequence, rotating its elements.
1715 * @ingroup mutating_algorithms
1716 * @param __first A forward iterator.
1717 * @param __middle A forward iterator.
1718 * @param __last A forward iterator.
1719 * @param __result An output iterator.
1720 * @return An iterator designating the end of the resulting sequence.
1722 * Copies the elements of the range @p [__first,__last) to the
1723 * range beginning at @result, rotating the copied elements by
1724 * @p (__middle-__first) positions so that the element at @p __middle
1725 * is moved to @p __result, the element at @p __middle+1 is moved
1726 * to @p __result+1 and so on for each element in the range @p
1730 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1731 * for each @p n in the range @p [0,__last-__first).
1733 template<typename _ForwardIterator
, typename _OutputIterator
>
1735 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1736 _ForwardIterator __last
, _OutputIterator __result
)
1738 // concept requirements
1739 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1740 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1741 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1742 __glibcxx_requires_valid_range(__first
, __middle
);
1743 __glibcxx_requires_valid_range(__middle
, __last
);
1745 return std::copy(__first
, __middle
,
1746 std::copy(__middle
, __last
, __result
));
1749 /// This is a helper function...
1750 template<typename _ForwardIterator
, typename _Predicate
>
1752 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1753 _Predicate __pred
, forward_iterator_tag
)
1755 if (__first
== __last
)
1758 while (__pred(*__first
))
1759 if (++__first
== __last
)
1762 _ForwardIterator __next
= __first
;
1764 while (++__next
!= __last
)
1765 if (__pred(*__next
))
1767 std::iter_swap(__first
, __next
);
1774 /// This is a helper function...
1775 template<typename _BidirectionalIterator
, typename _Predicate
>
1776 _BidirectionalIterator
1777 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1778 _Predicate __pred
, bidirectional_iterator_tag
)
1783 if (__first
== __last
)
1785 else if (__pred(*__first
))
1791 if (__first
== __last
)
1793 else if (!bool(__pred(*__last
)))
1797 std::iter_swap(__first
, __last
);
1804 /// This is a helper function...
1805 /// Requires __len != 0 and !__pred(*__first),
1806 /// same as __stable_partition_adaptive.
1807 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1809 __inplace_stable_partition(_ForwardIterator __first
,
1810 _Predicate __pred
, _Distance __len
)
1814 _ForwardIterator __middle
= __first
;
1815 std::advance(__middle
, __len
/ 2);
1816 _ForwardIterator __left_split
=
1817 std::__inplace_stable_partition(__first
, __pred
, __len
/ 2);
1818 // Advance past true-predicate values to satisfy this
1819 // function's preconditions.
1820 _Distance __right_len
= __len
- __len
/ 2;
1821 _ForwardIterator __right_split
=
1822 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1824 __right_split
= std::__inplace_stable_partition(__middle
,
1827 std::rotate(__left_split
, __middle
, __right_split
);
1828 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1829 return __left_split
;
1832 /// This is a helper function...
1833 /// Requires __first != __last and !__pred(*__first)
1834 /// and __len == distance(__first, __last).
1836 /// !__pred(*__first) allows us to guarantee that we don't
1837 /// move-assign an element onto itself.
1838 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1841 __stable_partition_adaptive(_ForwardIterator __first
,
1842 _ForwardIterator __last
,
1843 _Predicate __pred
, _Distance __len
,
1845 _Distance __buffer_size
)
1847 if (__len
<= __buffer_size
)
1849 _ForwardIterator __result1
= __first
;
1850 _Pointer __result2
= __buffer
;
1851 // The precondition guarantees that !__pred(*__first), so
1852 // move that element to the buffer before starting the loop.
1853 // This ensures that we only call __pred once per element.
1854 *__result2
= _GLIBCXX_MOVE(*__first
);
1857 for (; __first
!= __last
; ++__first
)
1858 if (__pred(*__first
))
1860 *__result1
= _GLIBCXX_MOVE(*__first
);
1865 *__result2
= _GLIBCXX_MOVE(*__first
);
1868 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1873 _ForwardIterator __middle
= __first
;
1874 std::advance(__middle
, __len
/ 2);
1875 _ForwardIterator __left_split
=
1876 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1877 __len
/ 2, __buffer
,
1879 // Advance past true-predicate values to satisfy this
1880 // function's preconditions.
1881 _Distance __right_len
= __len
- __len
/ 2;
1882 _ForwardIterator __right_split
=
1883 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1886 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1888 __buffer
, __buffer_size
);
1889 std::rotate(__left_split
, __middle
, __right_split
);
1890 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1891 return __left_split
;
1896 * @brief Move elements for which a predicate is true to the beginning
1897 * of a sequence, preserving relative ordering.
1898 * @ingroup mutating_algorithms
1899 * @param __first A forward iterator.
1900 * @param __last A forward iterator.
1901 * @param __pred A predicate functor.
1902 * @return An iterator @p middle such that @p __pred(i) is true for each
1903 * iterator @p i in the range @p [first,middle) and false for each @p i
1904 * in the range @p [middle,last).
1906 * Performs the same function as @p partition() with the additional
1907 * guarantee that the relative ordering of elements in each group is
1908 * preserved, so any two elements @p x and @p y in the range
1909 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1910 * relative ordering after calling @p stable_partition().
1912 template<typename _ForwardIterator
, typename _Predicate
>
1914 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1917 // concept requirements
1918 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1920 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1921 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1922 __glibcxx_requires_valid_range(__first
, __last
);
1924 __first
= std::__find_if_not(__first
, __last
, __pred
);
1926 if (__first
== __last
)
1930 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1932 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1935 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1937 if (__buf
.size() > 0)
1939 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1940 _DistanceType(__buf
.requested_size()),
1942 _DistanceType(__buf
.size()));
1945 std::__inplace_stable_partition(__first
, __pred
,
1946 _DistanceType(__buf
.requested_size()));
1950 /// This is a helper function for the sort routines.
1951 template<typename _RandomAccessIterator
>
1953 __heap_select(_RandomAccessIterator __first
,
1954 _RandomAccessIterator __middle
,
1955 _RandomAccessIterator __last
)
1957 std::make_heap(__first
, __middle
);
1958 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1959 if (*__i
< *__first
)
1960 std::__pop_heap(__first
, __middle
, __i
);
1963 /// This is a helper function for the sort routines.
1964 template<typename _RandomAccessIterator
, typename _Compare
>
1966 __heap_select(_RandomAccessIterator __first
,
1967 _RandomAccessIterator __middle
,
1968 _RandomAccessIterator __last
, _Compare __comp
)
1970 std::make_heap(__first
, __middle
, __comp
);
1971 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1972 if (__comp(*__i
, *__first
))
1973 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1979 * @brief Copy the smallest elements of a sequence.
1980 * @ingroup sorting_algorithms
1981 * @param __first An iterator.
1982 * @param __last Another iterator.
1983 * @param __result_first A random-access iterator.
1984 * @param __result_last Another random-access iterator.
1985 * @return An iterator indicating the end of the resulting sequence.
1987 * Copies and sorts the smallest N values from the range @p [__first,__last)
1988 * to the range beginning at @p __result_first, where the number of
1989 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1990 * @p (__result_last-__result_first).
1991 * After the sort if @e i and @e j are iterators in the range
1992 * @p [__result_first,__result_first+N) such that i precedes j then
1994 * The value returned is @p __result_first+N.
1996 template<typename _InputIterator
, typename _RandomAccessIterator
>
1997 _RandomAccessIterator
1998 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1999 _RandomAccessIterator __result_first
,
2000 _RandomAccessIterator __result_last
)
2002 typedef typename iterator_traits
<_InputIterator
>::value_type
2004 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2006 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2009 // concept requirements
2010 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2011 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2013 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2015 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2016 __glibcxx_requires_valid_range(__first
, __last
);
2017 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2019 if (__result_first
== __result_last
)
2020 return __result_last
;
2021 _RandomAccessIterator __result_real_last
= __result_first
;
2022 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2024 *__result_real_last
= *__first
;
2025 ++__result_real_last
;
2028 std::make_heap(__result_first
, __result_real_last
);
2029 while (__first
!= __last
)
2031 if (*__first
< *__result_first
)
2032 std::__adjust_heap(__result_first
, _DistanceType(0),
2033 _DistanceType(__result_real_last
2035 _InputValueType(*__first
));
2038 std::sort_heap(__result_first
, __result_real_last
);
2039 return __result_real_last
;
2043 * @brief Copy the smallest elements of a sequence using a predicate for
2045 * @ingroup sorting_algorithms
2046 * @param __first An input iterator.
2047 * @param __last Another input iterator.
2048 * @param __result_first A random-access iterator.
2049 * @param __result_last Another random-access iterator.
2050 * @param __comp A comparison functor.
2051 * @return An iterator indicating the end of the resulting sequence.
2053 * Copies and sorts the smallest N values from the range @p [__first,__last)
2054 * to the range beginning at @p result_first, where the number of
2055 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
2056 * @p (__result_last-__result_first).
2057 * After the sort if @e i and @e j are iterators in the range
2058 * @p [__result_first,__result_first+N) such that i precedes j then
2059 * @p __comp(*j,*i) is false.
2060 * The value returned is @p __result_first+N.
2062 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2063 _RandomAccessIterator
2064 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2065 _RandomAccessIterator __result_first
,
2066 _RandomAccessIterator __result_last
,
2069 typedef typename iterator_traits
<_InputIterator
>::value_type
2071 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2073 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2076 // concept requirements
2077 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2078 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2079 _RandomAccessIterator
>)
2080 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2082 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2083 _InputValueType
, _OutputValueType
>)
2084 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2085 _OutputValueType
, _OutputValueType
>)
2086 __glibcxx_requires_valid_range(__first
, __last
);
2087 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2089 if (__result_first
== __result_last
)
2090 return __result_last
;
2091 _RandomAccessIterator __result_real_last
= __result_first
;
2092 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2094 *__result_real_last
= *__first
;
2095 ++__result_real_last
;
2098 std::make_heap(__result_first
, __result_real_last
, __comp
);
2099 while (__first
!= __last
)
2101 if (__comp(*__first
, *__result_first
))
2102 std::__adjust_heap(__result_first
, _DistanceType(0),
2103 _DistanceType(__result_real_last
2105 _InputValueType(*__first
),
2109 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2110 return __result_real_last
;
2113 /// This is a helper function for the sort routine.
2114 template<typename _RandomAccessIterator
>
2116 __unguarded_linear_insert(_RandomAccessIterator __last
)
2118 typename iterator_traits
<_RandomAccessIterator
>::value_type
2119 __val
= _GLIBCXX_MOVE(*__last
);
2120 _RandomAccessIterator __next
= __last
;
2122 while (__val
< *__next
)
2124 *__last
= _GLIBCXX_MOVE(*__next
);
2128 *__last
= _GLIBCXX_MOVE(__val
);
2131 /// This is a helper function for the sort routine.
2132 template<typename _RandomAccessIterator
, typename _Compare
>
2134 __unguarded_linear_insert(_RandomAccessIterator __last
,
2137 typename iterator_traits
<_RandomAccessIterator
>::value_type
2138 __val
= _GLIBCXX_MOVE(*__last
);
2139 _RandomAccessIterator __next
= __last
;
2141 while (__comp(__val
, *__next
))
2143 *__last
= _GLIBCXX_MOVE(*__next
);
2147 *__last
= _GLIBCXX_MOVE(__val
);
2150 /// This is a helper function for the sort routine.
2151 template<typename _RandomAccessIterator
>
2153 __insertion_sort(_RandomAccessIterator __first
,
2154 _RandomAccessIterator __last
)
2156 if (__first
== __last
)
2159 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2161 if (*__i
< *__first
)
2163 typename iterator_traits
<_RandomAccessIterator
>::value_type
2164 __val
= _GLIBCXX_MOVE(*__i
);
2165 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2166 *__first
= _GLIBCXX_MOVE(__val
);
2169 std::__unguarded_linear_insert(__i
);
2173 /// This is a helper function for the sort routine.
2174 template<typename _RandomAccessIterator
, typename _Compare
>
2176 __insertion_sort(_RandomAccessIterator __first
,
2177 _RandomAccessIterator __last
, _Compare __comp
)
2179 if (__first
== __last
) return;
2181 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2183 if (__comp(*__i
, *__first
))
2185 typename iterator_traits
<_RandomAccessIterator
>::value_type
2186 __val
= _GLIBCXX_MOVE(*__i
);
2187 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2188 *__first
= _GLIBCXX_MOVE(__val
);
2191 std::__unguarded_linear_insert(__i
, __comp
);
2195 /// This is a helper function for the sort routine.
2196 template<typename _RandomAccessIterator
>
2198 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2199 _RandomAccessIterator __last
)
2201 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2204 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2205 std::__unguarded_linear_insert(__i
);
2208 /// This is a helper function for the sort routine.
2209 template<typename _RandomAccessIterator
, typename _Compare
>
2211 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2212 _RandomAccessIterator __last
, _Compare __comp
)
2214 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2217 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2218 std::__unguarded_linear_insert(__i
, __comp
);
2223 * This controls some aspect of the sort routines.
2225 enum { _S_threshold
= 16 };
2227 /// This is a helper function for the sort routine.
2228 template<typename _RandomAccessIterator
>
2230 __final_insertion_sort(_RandomAccessIterator __first
,
2231 _RandomAccessIterator __last
)
2233 if (__last
- __first
> int(_S_threshold
))
2235 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2236 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2239 std::__insertion_sort(__first
, __last
);
2242 /// This is a helper function for the sort routine.
2243 template<typename _RandomAccessIterator
, typename _Compare
>
2245 __final_insertion_sort(_RandomAccessIterator __first
,
2246 _RandomAccessIterator __last
, _Compare __comp
)
2248 if (__last
- __first
> int(_S_threshold
))
2250 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2251 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2255 std::__insertion_sort(__first
, __last
, __comp
);
2258 /// This is a helper function...
2259 template<typename _RandomAccessIterator
, typename _Tp
>
2260 _RandomAccessIterator
2261 __unguarded_partition(_RandomAccessIterator __first
,
2262 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2266 while (*__first
< __pivot
)
2269 while (__pivot
< *__last
)
2271 if (!(__first
< __last
))
2273 std::iter_swap(__first
, __last
);
2278 /// This is a helper function...
2279 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2280 _RandomAccessIterator
2281 __unguarded_partition(_RandomAccessIterator __first
,
2282 _RandomAccessIterator __last
,
2283 const _Tp
& __pivot
, _Compare __comp
)
2287 while (__comp(*__first
, __pivot
))
2290 while (__comp(__pivot
, *__last
))
2292 if (!(__first
< __last
))
2294 std::iter_swap(__first
, __last
);
2299 /// This is a helper function...
2300 template<typename _RandomAccessIterator
>
2301 inline _RandomAccessIterator
2302 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2303 _RandomAccessIterator __last
)
2305 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2306 std::__move_median_first(__first
, __mid
, (__last
- 1));
2307 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2311 /// This is a helper function...
2312 template<typename _RandomAccessIterator
, typename _Compare
>
2313 inline _RandomAccessIterator
2314 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2315 _RandomAccessIterator __last
, _Compare __comp
)
2317 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2318 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2319 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2322 /// This is a helper function for the sort routine.
2323 template<typename _RandomAccessIterator
, typename _Size
>
2325 __introsort_loop(_RandomAccessIterator __first
,
2326 _RandomAccessIterator __last
,
2327 _Size __depth_limit
)
2329 while (__last
- __first
> int(_S_threshold
))
2331 if (__depth_limit
== 0)
2333 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
);
2337 _RandomAccessIterator __cut
=
2338 std::__unguarded_partition_pivot(__first
, __last
);
2339 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2344 /// This is a helper function for the sort routine.
2345 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2347 __introsort_loop(_RandomAccessIterator __first
,
2348 _RandomAccessIterator __last
,
2349 _Size __depth_limit
, _Compare __comp
)
2351 while (__last
- __first
> int(_S_threshold
))
2353 if (__depth_limit
== 0)
2355 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
, __comp
);
2359 _RandomAccessIterator __cut
=
2360 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2361 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2368 template<typename _RandomAccessIterator
, typename _Size
>
2370 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2371 _RandomAccessIterator __last
, _Size __depth_limit
)
2373 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2376 while (__last
- __first
> 3)
2378 if (__depth_limit
== 0)
2380 std::__heap_select(__first
, __nth
+ 1, __last
);
2382 // Place the nth largest element in its final position.
2383 std::iter_swap(__first
, __nth
);
2387 _RandomAccessIterator __cut
=
2388 std::__unguarded_partition_pivot(__first
, __last
);
2394 std::__insertion_sort(__first
, __last
);
2397 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2399 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2400 _RandomAccessIterator __last
, _Size __depth_limit
,
2403 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2406 while (__last
- __first
> 3)
2408 if (__depth_limit
== 0)
2410 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
2411 // Place the nth largest element in its final position.
2412 std::iter_swap(__first
, __nth
);
2416 _RandomAccessIterator __cut
=
2417 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2423 std::__insertion_sort(__first
, __last
, __comp
);
2428 // lower_bound moved to stl_algobase.h
2431 * @brief Finds the first position in which @p __val could be inserted
2432 * without changing the ordering.
2433 * @ingroup binary_search_algorithms
2434 * @param __first An iterator.
2435 * @param __last Another iterator.
2436 * @param __val The search term.
2437 * @param __comp A functor to use for comparisons.
2438 * @return An iterator pointing to the first element <em>not less
2439 * than</em> @p __val, or end() if every element is less
2441 * @ingroup binary_search_algorithms
2443 * The comparison function should have the same effects on ordering as
2444 * the function used for the initial sort.
2446 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2448 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2449 const _Tp
& __val
, _Compare __comp
)
2451 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2453 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2456 // concept requirements
2457 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2458 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2460 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2463 _DistanceType __len
= std::distance(__first
, __last
);
2467 _DistanceType __half
= __len
>> 1;
2468 _ForwardIterator __middle
= __first
;
2469 std::advance(__middle
, __half
);
2470 if (__comp(*__middle
, __val
))
2474 __len
= __len
- __half
- 1;
2483 * @brief Finds the last position in which @p __val could be inserted
2484 * without changing the ordering.
2485 * @ingroup binary_search_algorithms
2486 * @param __first An iterator.
2487 * @param __last Another iterator.
2488 * @param __val The search term.
2489 * @return An iterator pointing to the first element greater than @p __val,
2490 * or end() if no elements are greater than @p __val.
2491 * @ingroup binary_search_algorithms
2493 template<typename _ForwardIterator
, typename _Tp
>
2495 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2498 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2500 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2503 // concept requirements
2504 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2505 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2506 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2508 _DistanceType __len
= std::distance(__first
, __last
);
2512 _DistanceType __half
= __len
>> 1;
2513 _ForwardIterator __middle
= __first
;
2514 std::advance(__middle
, __half
);
2515 if (__val
< *__middle
)
2521 __len
= __len
- __half
- 1;
2528 * @brief Finds the last position in which @p __val could be inserted
2529 * without changing the ordering.
2530 * @ingroup binary_search_algorithms
2531 * @param __first An iterator.
2532 * @param __last Another iterator.
2533 * @param __val The search term.
2534 * @param __comp A functor to use for comparisons.
2535 * @return An iterator pointing to the first element greater than @p __val,
2536 * or end() if no elements are greater than @p __val.
2537 * @ingroup binary_search_algorithms
2539 * The comparison function should have the same effects on ordering as
2540 * the function used for the initial sort.
2542 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2544 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2545 const _Tp
& __val
, _Compare __comp
)
2547 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2549 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2552 // concept requirements
2553 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2554 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2556 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2559 _DistanceType __len
= std::distance(__first
, __last
);
2563 _DistanceType __half
= __len
>> 1;
2564 _ForwardIterator __middle
= __first
;
2565 std::advance(__middle
, __half
);
2566 if (__comp(__val
, *__middle
))
2572 __len
= __len
- __half
- 1;
2579 * @brief Finds the largest subrange in which @p __val could be inserted
2580 * at any place in it without changing the ordering.
2581 * @ingroup binary_search_algorithms
2582 * @param __first An iterator.
2583 * @param __last Another iterator.
2584 * @param __val The search term.
2585 * @return An pair of iterators defining the subrange.
2586 * @ingroup binary_search_algorithms
2588 * This is equivalent to
2590 * std::make_pair(lower_bound(__first, __last, __val),
2591 * upper_bound(__first, __last, __val))
2593 * but does not actually call those functions.
2595 template<typename _ForwardIterator
, typename _Tp
>
2596 pair
<_ForwardIterator
, _ForwardIterator
>
2597 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2600 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2602 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2605 // concept requirements
2606 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2607 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2608 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2609 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2610 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2612 _DistanceType __len
= std::distance(__first
, __last
);
2616 _DistanceType __half
= __len
>> 1;
2617 _ForwardIterator __middle
= __first
;
2618 std::advance(__middle
, __half
);
2619 if (*__middle
< __val
)
2623 __len
= __len
- __half
- 1;
2625 else if (__val
< *__middle
)
2629 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2631 std::advance(__first
, __len
);
2632 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2634 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2637 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2641 * @brief Finds the largest subrange in which @p __val could be inserted
2642 * at any place in it without changing the ordering.
2643 * @param __first An iterator.
2644 * @param __last Another iterator.
2645 * @param __val The search term.
2646 * @param __comp A functor to use for comparisons.
2647 * @return An pair of iterators defining the subrange.
2648 * @ingroup binary_search_algorithms
2650 * This is equivalent to
2652 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2653 * upper_bound(__first, __last, __val, __comp))
2655 * but does not actually call those functions.
2657 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2658 pair
<_ForwardIterator
, _ForwardIterator
>
2659 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2660 const _Tp
& __val
, _Compare __comp
)
2662 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2664 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2667 // concept requirements
2668 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2669 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2671 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2673 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2675 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2678 _DistanceType __len
= std::distance(__first
, __last
);
2682 _DistanceType __half
= __len
>> 1;
2683 _ForwardIterator __middle
= __first
;
2684 std::advance(__middle
, __half
);
2685 if (__comp(*__middle
, __val
))
2689 __len
= __len
- __half
- 1;
2691 else if (__comp(__val
, *__middle
))
2695 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2697 std::advance(__first
, __len
);
2698 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2700 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2703 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2707 * @brief Determines whether an element exists in a range.
2708 * @ingroup binary_search_algorithms
2709 * @param __first An iterator.
2710 * @param __last Another iterator.
2711 * @param __val The search term.
2712 * @return True if @p __val (or its equivalent) is in [@p
2713 * __first,@p __last ].
2715 * Note that this does not actually return an iterator to @p __val. For
2716 * that, use std::find or a container's specialized find member functions.
2718 template<typename _ForwardIterator
, typename _Tp
>
2720 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2723 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2726 // concept requirements
2727 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2728 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2729 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2730 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2732 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2733 return __i
!= __last
&& !(__val
< *__i
);
2737 * @brief Determines whether an element exists in a range.
2738 * @ingroup binary_search_algorithms
2739 * @param __first An iterator.
2740 * @param __last Another iterator.
2741 * @param __val The search term.
2742 * @param __comp A functor to use for comparisons.
2743 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2745 * Note that this does not actually return an iterator to @p __val. For
2746 * that, use std::find or a container's specialized find member functions.
2748 * The comparison function should have the same effects on ordering as
2749 * the function used for the initial sort.
2751 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2753 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2754 const _Tp
& __val
, _Compare __comp
)
2756 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2759 // concept requirements
2760 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2761 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2763 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2765 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2768 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2769 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2774 /// This is a helper function for the __merge_adaptive routines.
2775 template<typename _InputIterator1
, typename _InputIterator2
,
2776 typename _OutputIterator
>
2778 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2779 _InputIterator2 __first2
, _InputIterator2 __last2
,
2780 _OutputIterator __result
)
2782 while (__first1
!= __last1
&& __first2
!= __last2
)
2784 if (*__first2
< *__first1
)
2786 *__result
= _GLIBCXX_MOVE(*__first2
);
2791 *__result
= _GLIBCXX_MOVE(*__first1
);
2796 if (__first1
!= __last1
)
2797 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2800 /// This is a helper function for the __merge_adaptive routines.
2801 template<typename _InputIterator1
, typename _InputIterator2
,
2802 typename _OutputIterator
, typename _Compare
>
2804 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2805 _InputIterator2 __first2
, _InputIterator2 __last2
,
2806 _OutputIterator __result
, _Compare __comp
)
2808 while (__first1
!= __last1
&& __first2
!= __last2
)
2810 if (__comp(*__first2
, *__first1
))
2812 *__result
= _GLIBCXX_MOVE(*__first2
);
2817 *__result
= _GLIBCXX_MOVE(*__first1
);
2822 if (__first1
!= __last1
)
2823 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2826 /// This is a helper function for the __merge_adaptive routines.
2827 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2828 typename _BidirectionalIterator3
>
2830 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2831 _BidirectionalIterator1 __last1
,
2832 _BidirectionalIterator2 __first2
,
2833 _BidirectionalIterator2 __last2
,
2834 _BidirectionalIterator3 __result
)
2836 if (__first1
== __last1
)
2838 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2841 else if (__first2
== __last2
)
2848 if (*__last2
< *__last1
)
2850 *--__result
= _GLIBCXX_MOVE(*__last1
);
2851 if (__first1
== __last1
)
2853 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2860 *--__result
= _GLIBCXX_MOVE(*__last2
);
2861 if (__first2
== __last2
)
2868 /// This is a helper function for the __merge_adaptive routines.
2869 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2870 typename _BidirectionalIterator3
, typename _Compare
>
2872 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2873 _BidirectionalIterator1 __last1
,
2874 _BidirectionalIterator2 __first2
,
2875 _BidirectionalIterator2 __last2
,
2876 _BidirectionalIterator3 __result
,
2879 if (__first1
== __last1
)
2881 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2884 else if (__first2
== __last2
)
2891 if (__comp(*__last2
, *__last1
))
2893 *--__result
= _GLIBCXX_MOVE(*__last1
);
2894 if (__first1
== __last1
)
2896 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2903 *--__result
= _GLIBCXX_MOVE(*__last2
);
2904 if (__first2
== __last2
)
2911 /// This is a helper function for the merge routines.
2912 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2914 _BidirectionalIterator1
2915 __rotate_adaptive(_BidirectionalIterator1 __first
,
2916 _BidirectionalIterator1 __middle
,
2917 _BidirectionalIterator1 __last
,
2918 _Distance __len1
, _Distance __len2
,
2919 _BidirectionalIterator2 __buffer
,
2920 _Distance __buffer_size
)
2922 _BidirectionalIterator2 __buffer_end
;
2923 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2927 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2928 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2929 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2934 else if (__len1
<= __buffer_size
)
2938 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2939 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2940 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2947 std::rotate(__first
, __middle
, __last
);
2948 std::advance(__first
, std::distance(__middle
, __last
));
2953 /// This is a helper function for the merge routines.
2954 template<typename _BidirectionalIterator
, typename _Distance
,
2957 __merge_adaptive(_BidirectionalIterator __first
,
2958 _BidirectionalIterator __middle
,
2959 _BidirectionalIterator __last
,
2960 _Distance __len1
, _Distance __len2
,
2961 _Pointer __buffer
, _Distance __buffer_size
)
2963 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2965 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2966 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2969 else if (__len2
<= __buffer_size
)
2971 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2972 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2973 __buffer_end
, __last
);
2977 _BidirectionalIterator __first_cut
= __first
;
2978 _BidirectionalIterator __second_cut
= __middle
;
2979 _Distance __len11
= 0;
2980 _Distance __len22
= 0;
2981 if (__len1
> __len2
)
2983 __len11
= __len1
/ 2;
2984 std::advance(__first_cut
, __len11
);
2985 __second_cut
= std::lower_bound(__middle
, __last
,
2987 __len22
= std::distance(__middle
, __second_cut
);
2991 __len22
= __len2
/ 2;
2992 std::advance(__second_cut
, __len22
);
2993 __first_cut
= std::upper_bound(__first
, __middle
,
2995 __len11
= std::distance(__first
, __first_cut
);
2997 _BidirectionalIterator __new_middle
=
2998 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2999 __len1
- __len11
, __len22
, __buffer
,
3001 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3002 __len22
, __buffer
, __buffer_size
);
3003 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3005 __len2
- __len22
, __buffer
, __buffer_size
);
3009 /// This is a helper function for the merge routines.
3010 template<typename _BidirectionalIterator
, typename _Distance
,
3011 typename _Pointer
, typename _Compare
>
3013 __merge_adaptive(_BidirectionalIterator __first
,
3014 _BidirectionalIterator __middle
,
3015 _BidirectionalIterator __last
,
3016 _Distance __len1
, _Distance __len2
,
3017 _Pointer __buffer
, _Distance __buffer_size
,
3020 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3022 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
3023 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
3026 else if (__len2
<= __buffer_size
)
3028 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
3029 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
3030 __buffer_end
, __last
, __comp
);
3034 _BidirectionalIterator __first_cut
= __first
;
3035 _BidirectionalIterator __second_cut
= __middle
;
3036 _Distance __len11
= 0;
3037 _Distance __len22
= 0;
3038 if (__len1
> __len2
)
3040 __len11
= __len1
/ 2;
3041 std::advance(__first_cut
, __len11
);
3042 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3044 __len22
= std::distance(__middle
, __second_cut
);
3048 __len22
= __len2
/ 2;
3049 std::advance(__second_cut
, __len22
);
3050 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3052 __len11
= std::distance(__first
, __first_cut
);
3054 _BidirectionalIterator __new_middle
=
3055 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3056 __len1
- __len11
, __len22
, __buffer
,
3058 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3059 __len22
, __buffer
, __buffer_size
, __comp
);
3060 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3062 __len2
- __len22
, __buffer
,
3063 __buffer_size
, __comp
);
3067 /// This is a helper function for the merge routines.
3068 template<typename _BidirectionalIterator
, typename _Distance
>
3070 __merge_without_buffer(_BidirectionalIterator __first
,
3071 _BidirectionalIterator __middle
,
3072 _BidirectionalIterator __last
,
3073 _Distance __len1
, _Distance __len2
)
3075 if (__len1
== 0 || __len2
== 0)
3077 if (__len1
+ __len2
== 2)
3079 if (*__middle
< *__first
)
3080 std::iter_swap(__first
, __middle
);
3083 _BidirectionalIterator __first_cut
= __first
;
3084 _BidirectionalIterator __second_cut
= __middle
;
3085 _Distance __len11
= 0;
3086 _Distance __len22
= 0;
3087 if (__len1
> __len2
)
3089 __len11
= __len1
/ 2;
3090 std::advance(__first_cut
, __len11
);
3091 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3092 __len22
= std::distance(__middle
, __second_cut
);
3096 __len22
= __len2
/ 2;
3097 std::advance(__second_cut
, __len22
);
3098 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3099 __len11
= std::distance(__first
, __first_cut
);
3101 std::rotate(__first_cut
, __middle
, __second_cut
);
3102 _BidirectionalIterator __new_middle
= __first_cut
;
3103 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3104 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3106 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3107 __len1
- __len11
, __len2
- __len22
);
3110 /// This is a helper function for the merge routines.
3111 template<typename _BidirectionalIterator
, typename _Distance
,
3114 __merge_without_buffer(_BidirectionalIterator __first
,
3115 _BidirectionalIterator __middle
,
3116 _BidirectionalIterator __last
,
3117 _Distance __len1
, _Distance __len2
,
3120 if (__len1
== 0 || __len2
== 0)
3122 if (__len1
+ __len2
== 2)
3124 if (__comp(*__middle
, *__first
))
3125 std::iter_swap(__first
, __middle
);
3128 _BidirectionalIterator __first_cut
= __first
;
3129 _BidirectionalIterator __second_cut
= __middle
;
3130 _Distance __len11
= 0;
3131 _Distance __len22
= 0;
3132 if (__len1
> __len2
)
3134 __len11
= __len1
/ 2;
3135 std::advance(__first_cut
, __len11
);
3136 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3138 __len22
= std::distance(__middle
, __second_cut
);
3142 __len22
= __len2
/ 2;
3143 std::advance(__second_cut
, __len22
);
3144 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3146 __len11
= std::distance(__first
, __first_cut
);
3148 std::rotate(__first_cut
, __middle
, __second_cut
);
3149 _BidirectionalIterator __new_middle
= __first_cut
;
3150 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3151 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3152 __len11
, __len22
, __comp
);
3153 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3154 __len1
- __len11
, __len2
- __len22
, __comp
);
3158 * @brief Merges two sorted ranges in place.
3159 * @ingroup sorting_algorithms
3160 * @param __first An iterator.
3161 * @param __middle Another iterator.
3162 * @param __last Another iterator.
3165 * Merges two sorted and consecutive ranges, [__first,__middle) and
3166 * [__middle,__last), and puts the result in [__first,__last). The
3167 * output will be sorted. The sort is @e stable, that is, for
3168 * equivalent elements in the two ranges, elements from the first
3169 * range will always come before elements from the second.
3171 * If enough additional memory is available, this takes (__last-__first)-1
3172 * comparisons. Otherwise an NlogN algorithm is used, where N is
3173 * distance(__first,__last).
3175 template<typename _BidirectionalIterator
>
3177 inplace_merge(_BidirectionalIterator __first
,
3178 _BidirectionalIterator __middle
,
3179 _BidirectionalIterator __last
)
3181 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3183 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3186 // concept requirements
3187 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3188 _BidirectionalIterator
>)
3189 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3190 __glibcxx_requires_sorted(__first
, __middle
);
3191 __glibcxx_requires_sorted(__middle
, __last
);
3193 if (__first
== __middle
|| __middle
== __last
)
3196 _DistanceType __len1
= std::distance(__first
, __middle
);
3197 _DistanceType __len2
= std::distance(__middle
, __last
);
3199 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3201 if (__buf
.begin() == 0)
3202 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3204 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3205 __buf
.begin(), _DistanceType(__buf
.size()));
3209 * @brief Merges two sorted ranges in place.
3210 * @ingroup sorting_algorithms
3211 * @param __first An iterator.
3212 * @param __middle Another iterator.
3213 * @param __last Another iterator.
3214 * @param __comp A functor to use for comparisons.
3217 * Merges two sorted and consecutive ranges, [__first,__middle) and
3218 * [middle,last), and puts the result in [__first,__last). The output will
3219 * be sorted. The sort is @e stable, that is, for equivalent
3220 * elements in the two ranges, elements from the first range will always
3221 * come before elements from the second.
3223 * If enough additional memory is available, this takes (__last-__first)-1
3224 * comparisons. Otherwise an NlogN algorithm is used, where N is
3225 * distance(__first,__last).
3227 * The comparison function should have the same effects on ordering as
3228 * the function used for the initial sort.
3230 template<typename _BidirectionalIterator
, typename _Compare
>
3232 inplace_merge(_BidirectionalIterator __first
,
3233 _BidirectionalIterator __middle
,
3234 _BidirectionalIterator __last
,
3237 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3239 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3242 // concept requirements
3243 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3244 _BidirectionalIterator
>)
3245 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3246 _ValueType
, _ValueType
>)
3247 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3248 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3250 if (__first
== __middle
|| __middle
== __last
)
3253 const _DistanceType __len1
= std::distance(__first
, __middle
);
3254 const _DistanceType __len2
= std::distance(__middle
, __last
);
3256 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3258 if (__buf
.begin() == 0)
3259 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3262 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3263 __buf
.begin(), _DistanceType(__buf
.size()),
3268 /// This is a helper function for the __merge_sort_loop routines.
3269 template<typename _InputIterator1
, typename _InputIterator2
,
3270 typename _OutputIterator
>
3272 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3273 _InputIterator2 __first2
, _InputIterator2 __last2
,
3274 _OutputIterator __result
)
3276 while (__first1
!= __last1
&& __first2
!= __last2
)
3278 if (*__first2
< *__first1
)
3280 *__result
= _GLIBCXX_MOVE(*__first2
);
3285 *__result
= _GLIBCXX_MOVE(*__first1
);
3290 return _GLIBCXX_MOVE3(__first2
, __last2
,
3291 _GLIBCXX_MOVE3(__first1
, __last1
,
3295 /// This is a helper function for the __merge_sort_loop routines.
3296 template<typename _InputIterator1
, typename _InputIterator2
,
3297 typename _OutputIterator
, typename _Compare
>
3299 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3300 _InputIterator2 __first2
, _InputIterator2 __last2
,
3301 _OutputIterator __result
, _Compare __comp
)
3303 while (__first1
!= __last1
&& __first2
!= __last2
)
3305 if (__comp(*__first2
, *__first1
))
3307 *__result
= _GLIBCXX_MOVE(*__first2
);
3312 *__result
= _GLIBCXX_MOVE(*__first1
);
3317 return _GLIBCXX_MOVE3(__first2
, __last2
,
3318 _GLIBCXX_MOVE3(__first1
, __last1
,
3322 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3325 __merge_sort_loop(_RandomAccessIterator1 __first
,
3326 _RandomAccessIterator1 __last
,
3327 _RandomAccessIterator2 __result
,
3328 _Distance __step_size
)
3330 const _Distance __two_step
= 2 * __step_size
;
3332 while (__last
- __first
>= __two_step
)
3334 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3335 __first
+ __step_size
,
3336 __first
+ __two_step
, __result
);
3337 __first
+= __two_step
;
3340 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3341 std::__move_merge(__first
, __first
+ __step_size
,
3342 __first
+ __step_size
, __last
, __result
);
3345 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3346 typename _Distance
, typename _Compare
>
3348 __merge_sort_loop(_RandomAccessIterator1 __first
,
3349 _RandomAccessIterator1 __last
,
3350 _RandomAccessIterator2 __result
, _Distance __step_size
,
3353 const _Distance __two_step
= 2 * __step_size
;
3355 while (__last
- __first
>= __two_step
)
3357 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3358 __first
+ __step_size
,
3359 __first
+ __two_step
,
3361 __first
+= __two_step
;
3363 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3365 std::__move_merge(__first
,__first
+ __step_size
,
3366 __first
+ __step_size
, __last
, __result
, __comp
);
3369 template<typename _RandomAccessIterator
, typename _Distance
>
3371 __chunk_insertion_sort(_RandomAccessIterator __first
,
3372 _RandomAccessIterator __last
,
3373 _Distance __chunk_size
)
3375 while (__last
- __first
>= __chunk_size
)
3377 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3378 __first
+= __chunk_size
;
3380 std::__insertion_sort(__first
, __last
);
3383 template<typename _RandomAccessIterator
, typename _Distance
,
3386 __chunk_insertion_sort(_RandomAccessIterator __first
,
3387 _RandomAccessIterator __last
,
3388 _Distance __chunk_size
, _Compare __comp
)
3390 while (__last
- __first
>= __chunk_size
)
3392 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3393 __first
+= __chunk_size
;
3395 std::__insertion_sort(__first
, __last
, __comp
);
3398 enum { _S_chunk_size
= 7 };
3400 template<typename _RandomAccessIterator
, typename _Pointer
>
3402 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3403 _RandomAccessIterator __last
,
3406 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3409 const _Distance __len
= __last
- __first
;
3410 const _Pointer __buffer_last
= __buffer
+ __len
;
3412 _Distance __step_size
= _S_chunk_size
;
3413 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3415 while (__step_size
< __len
)
3417 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3419 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3424 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3426 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3427 _RandomAccessIterator __last
,
3428 _Pointer __buffer
, _Compare __comp
)
3430 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3433 const _Distance __len
= __last
- __first
;
3434 const _Pointer __buffer_last
= __buffer
+ __len
;
3436 _Distance __step_size
= _S_chunk_size
;
3437 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3439 while (__step_size
< __len
)
3441 std::__merge_sort_loop(__first
, __last
, __buffer
,
3442 __step_size
, __comp
);
3444 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3445 __step_size
, __comp
);
3450 template<typename _RandomAccessIterator
, typename _Pointer
,
3453 __stable_sort_adaptive(_RandomAccessIterator __first
,
3454 _RandomAccessIterator __last
,
3455 _Pointer __buffer
, _Distance __buffer_size
)
3457 const _Distance __len
= (__last
- __first
+ 1) / 2;
3458 const _RandomAccessIterator __middle
= __first
+ __len
;
3459 if (__len
> __buffer_size
)
3461 std::__stable_sort_adaptive(__first
, __middle
,
3462 __buffer
, __buffer_size
);
3463 std::__stable_sort_adaptive(__middle
, __last
,
3464 __buffer
, __buffer_size
);
3468 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3469 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3471 std::__merge_adaptive(__first
, __middle
, __last
,
3472 _Distance(__middle
- __first
),
3473 _Distance(__last
- __middle
),
3474 __buffer
, __buffer_size
);
3477 template<typename _RandomAccessIterator
, typename _Pointer
,
3478 typename _Distance
, typename _Compare
>
3480 __stable_sort_adaptive(_RandomAccessIterator __first
,
3481 _RandomAccessIterator __last
,
3482 _Pointer __buffer
, _Distance __buffer_size
,
3485 const _Distance __len
= (__last
- __first
+ 1) / 2;
3486 const _RandomAccessIterator __middle
= __first
+ __len
;
3487 if (__len
> __buffer_size
)
3489 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3490 __buffer_size
, __comp
);
3491 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3492 __buffer_size
, __comp
);
3496 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3497 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3499 std::__merge_adaptive(__first
, __middle
, __last
,
3500 _Distance(__middle
- __first
),
3501 _Distance(__last
- __middle
),
3502 __buffer
, __buffer_size
,
3506 /// This is a helper function for the stable sorting routines.
3507 template<typename _RandomAccessIterator
>
3509 __inplace_stable_sort(_RandomAccessIterator __first
,
3510 _RandomAccessIterator __last
)
3512 if (__last
- __first
< 15)
3514 std::__insertion_sort(__first
, __last
);
3517 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3518 std::__inplace_stable_sort(__first
, __middle
);
3519 std::__inplace_stable_sort(__middle
, __last
);
3520 std::__merge_without_buffer(__first
, __middle
, __last
,
3525 /// This is a helper function for the stable sorting routines.
3526 template<typename _RandomAccessIterator
, typename _Compare
>
3528 __inplace_stable_sort(_RandomAccessIterator __first
,
3529 _RandomAccessIterator __last
, _Compare __comp
)
3531 if (__last
- __first
< 15)
3533 std::__insertion_sort(__first
, __last
, __comp
);
3536 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3537 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3538 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3539 std::__merge_without_buffer(__first
, __middle
, __last
,
3547 // Set algorithms: includes, set_union, set_intersection, set_difference,
3548 // set_symmetric_difference. All of these algorithms have the precondition
3549 // that their input ranges are sorted and the postcondition that their output
3550 // ranges are sorted.
3553 * @brief Determines whether all elements of a sequence exists in a range.
3554 * @param __first1 Start of search range.
3555 * @param __last1 End of search range.
3556 * @param __first2 Start of sequence
3557 * @param __last2 End of sequence.
3558 * @return True if each element in [__first2,__last2) is contained in order
3559 * within [__first1,__last1). False otherwise.
3560 * @ingroup set_algorithms
3562 * This operation expects both [__first1,__last1) and
3563 * [__first2,__last2) to be sorted. Searches for the presence of
3564 * each element in [__first2,__last2) within [__first1,__last1).
3565 * The iterators over each range only move forward, so this is a
3566 * linear algorithm. If an element in [__first2,__last2) is not
3567 * found before the search iterator reaches @p __last2, false is
3570 template<typename _InputIterator1
, typename _InputIterator2
>
3572 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3573 _InputIterator2 __first2
, _InputIterator2 __last2
)
3575 typedef typename iterator_traits
<_InputIterator1
>::value_type
3577 typedef typename iterator_traits
<_InputIterator2
>::value_type
3580 // concept requirements
3581 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3582 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3583 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3584 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3585 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3586 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3588 while (__first1
!= __last1
&& __first2
!= __last2
)
3589 if (*__first2
< *__first1
)
3591 else if(*__first1
< *__first2
)
3594 ++__first1
, ++__first2
;
3596 return __first2
== __last2
;
3600 * @brief Determines whether all elements of a sequence exists in a range
3602 * @ingroup set_algorithms
3603 * @param __first1 Start of search range.
3604 * @param __last1 End of search range.
3605 * @param __first2 Start of sequence
3606 * @param __last2 End of sequence.
3607 * @param __comp Comparison function to use.
3608 * @return True if each element in [__first2,__last2) is contained
3609 * in order within [__first1,__last1) according to comp. False
3610 * otherwise. @ingroup set_algorithms
3612 * This operation expects both [__first1,__last1) and
3613 * [__first2,__last2) to be sorted. Searches for the presence of
3614 * each element in [__first2,__last2) within [__first1,__last1),
3615 * using comp to decide. The iterators over each range only move
3616 * forward, so this is a linear algorithm. If an element in
3617 * [__first2,__last2) is not found before the search iterator
3618 * reaches @p __last2, false is returned.
3620 template<typename _InputIterator1
, typename _InputIterator2
,
3623 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3624 _InputIterator2 __first2
, _InputIterator2 __last2
,
3627 typedef typename iterator_traits
<_InputIterator1
>::value_type
3629 typedef typename iterator_traits
<_InputIterator2
>::value_type
3632 // concept requirements
3633 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3634 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3635 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3636 _ValueType1
, _ValueType2
>)
3637 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3638 _ValueType2
, _ValueType1
>)
3639 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3640 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3642 while (__first1
!= __last1
&& __first2
!= __last2
)
3643 if (__comp(*__first2
, *__first1
))
3645 else if(__comp(*__first1
, *__first2
))
3648 ++__first1
, ++__first2
;
3650 return __first2
== __last2
;
3659 // set_symmetric_difference
3664 * @brief Permute range into the next @e dictionary ordering.
3665 * @ingroup sorting_algorithms
3666 * @param __first Start of range.
3667 * @param __last End of range.
3668 * @return False if wrapped to first permutation, true otherwise.
3670 * Treats all permutations of the range as a set of @e dictionary sorted
3671 * sequences. Permutes the current sequence into the next one of this set.
3672 * Returns true if there are more sequences to generate. If the sequence
3673 * is the largest of the set, the smallest is generated and false returned.
3675 template<typename _BidirectionalIterator
>
3677 next_permutation(_BidirectionalIterator __first
,
3678 _BidirectionalIterator __last
)
3680 // concept requirements
3681 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3682 _BidirectionalIterator
>)
3683 __glibcxx_function_requires(_LessThanComparableConcept
<
3684 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3685 __glibcxx_requires_valid_range(__first
, __last
);
3687 if (__first
== __last
)
3689 _BidirectionalIterator __i
= __first
;
3698 _BidirectionalIterator __ii
= __i
;
3702 _BidirectionalIterator __j
= __last
;
3703 while (!(*__i
< *--__j
))
3705 std::iter_swap(__i
, __j
);
3706 std::reverse(__ii
, __last
);
3711 std::reverse(__first
, __last
);
3718 * @brief Permute range into the next @e dictionary ordering using
3719 * comparison functor.
3720 * @ingroup sorting_algorithms
3721 * @param __first Start of range.
3722 * @param __last End of range.
3723 * @param __comp A comparison functor.
3724 * @return False if wrapped to first permutation, true otherwise.
3726 * Treats all permutations of the range [__first,__last) as a set of
3727 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3728 * sequence into the next one of this set. Returns true if there are more
3729 * sequences to generate. If the sequence is the largest of the set, the
3730 * smallest is generated and false returned.
3732 template<typename _BidirectionalIterator
, typename _Compare
>
3734 next_permutation(_BidirectionalIterator __first
,
3735 _BidirectionalIterator __last
, _Compare __comp
)
3737 // concept requirements
3738 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3739 _BidirectionalIterator
>)
3740 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3741 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3742 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3743 __glibcxx_requires_valid_range(__first
, __last
);
3745 if (__first
== __last
)
3747 _BidirectionalIterator __i
= __first
;
3756 _BidirectionalIterator __ii
= __i
;
3758 if (__comp(*__i
, *__ii
))
3760 _BidirectionalIterator __j
= __last
;
3761 while (!bool(__comp(*__i
, *--__j
)))
3763 std::iter_swap(__i
, __j
);
3764 std::reverse(__ii
, __last
);
3769 std::reverse(__first
, __last
);
3776 * @brief Permute range into the previous @e dictionary ordering.
3777 * @ingroup sorting_algorithms
3778 * @param __first Start of range.
3779 * @param __last End of range.
3780 * @return False if wrapped to last permutation, true otherwise.
3782 * Treats all permutations of the range as a set of @e dictionary sorted
3783 * sequences. Permutes the current sequence into the previous one of this
3784 * set. Returns true if there are more sequences to generate. If the
3785 * sequence is the smallest of the set, the largest is generated and false
3788 template<typename _BidirectionalIterator
>
3790 prev_permutation(_BidirectionalIterator __first
,
3791 _BidirectionalIterator __last
)
3793 // concept requirements
3794 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3795 _BidirectionalIterator
>)
3796 __glibcxx_function_requires(_LessThanComparableConcept
<
3797 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3798 __glibcxx_requires_valid_range(__first
, __last
);
3800 if (__first
== __last
)
3802 _BidirectionalIterator __i
= __first
;
3811 _BidirectionalIterator __ii
= __i
;
3815 _BidirectionalIterator __j
= __last
;
3816 while (!(*--__j
< *__i
))
3818 std::iter_swap(__i
, __j
);
3819 std::reverse(__ii
, __last
);
3824 std::reverse(__first
, __last
);
3831 * @brief Permute range into the previous @e dictionary ordering using
3832 * comparison functor.
3833 * @ingroup sorting_algorithms
3834 * @param __first Start of range.
3835 * @param __last End of range.
3836 * @param __comp A comparison functor.
3837 * @return False if wrapped to last permutation, true otherwise.
3839 * Treats all permutations of the range [__first,__last) as a set of
3840 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3841 * sequence into the previous one of this set. Returns true if there are
3842 * more sequences to generate. If the sequence is the smallest of the set,
3843 * the largest is generated and false returned.
3845 template<typename _BidirectionalIterator
, typename _Compare
>
3847 prev_permutation(_BidirectionalIterator __first
,
3848 _BidirectionalIterator __last
, _Compare __comp
)
3850 // concept requirements
3851 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3852 _BidirectionalIterator
>)
3853 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3854 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3855 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3856 __glibcxx_requires_valid_range(__first
, __last
);
3858 if (__first
== __last
)
3860 _BidirectionalIterator __i
= __first
;
3869 _BidirectionalIterator __ii
= __i
;
3871 if (__comp(*__ii
, *__i
))
3873 _BidirectionalIterator __j
= __last
;
3874 while (!bool(__comp(*--__j
, *__i
)))
3876 std::iter_swap(__i
, __j
);
3877 std::reverse(__ii
, __last
);
3882 std::reverse(__first
, __last
);
3892 * @brief Copy a sequence, replacing each element of one value with another
3894 * @param __first An input iterator.
3895 * @param __last An input iterator.
3896 * @param __result An output iterator.
3897 * @param __old_value The value to be replaced.
3898 * @param __new_value The replacement value.
3899 * @return The end of the output sequence, @p result+(last-first).
3901 * Copies each element in the input range @p [__first,__last) to the
3902 * output range @p [__result,__result+(__last-__first)) replacing elements
3903 * equal to @p __old_value with @p __new_value.
3905 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3907 replace_copy(_InputIterator __first
, _InputIterator __last
,
3908 _OutputIterator __result
,
3909 const _Tp
& __old_value
, const _Tp
& __new_value
)
3911 // concept requirements
3912 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3913 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3914 typename iterator_traits
<_InputIterator
>::value_type
>)
3915 __glibcxx_function_requires(_EqualOpConcept
<
3916 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3917 __glibcxx_requires_valid_range(__first
, __last
);
3919 for (; __first
!= __last
; ++__first
, ++__result
)
3920 if (*__first
== __old_value
)
3921 *__result
= __new_value
;
3923 *__result
= *__first
;
3928 * @brief Copy a sequence, replacing each value for which a predicate
3929 * returns true with another value.
3930 * @ingroup mutating_algorithms
3931 * @param __first An input iterator.
3932 * @param __last An input iterator.
3933 * @param __result An output iterator.
3934 * @param __pred A predicate.
3935 * @param __new_value The replacement value.
3936 * @return The end of the output sequence, @p __result+(__last-__first).
3938 * Copies each element in the range @p [__first,__last) to the range
3939 * @p [__result,__result+(__last-__first)) replacing elements for which
3940 * @p __pred returns true with @p __new_value.
3942 template<typename _InputIterator
, typename _OutputIterator
,
3943 typename _Predicate
, typename _Tp
>
3945 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3946 _OutputIterator __result
,
3947 _Predicate __pred
, const _Tp
& __new_value
)
3949 // concept requirements
3950 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3951 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3952 typename iterator_traits
<_InputIterator
>::value_type
>)
3953 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3954 typename iterator_traits
<_InputIterator
>::value_type
>)
3955 __glibcxx_requires_valid_range(__first
, __last
);
3957 for (; __first
!= __last
; ++__first
, ++__result
)
3958 if (__pred(*__first
))
3959 *__result
= __new_value
;
3961 *__result
= *__first
;
3965 #if __cplusplus >= 201103L
3967 * @brief Determines whether the elements of a sequence are sorted.
3968 * @ingroup sorting_algorithms
3969 * @param __first An iterator.
3970 * @param __last Another iterator.
3971 * @return True if the elements are sorted, false otherwise.
3973 template<typename _ForwardIterator
>
3975 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3976 { return std::is_sorted_until(__first
, __last
) == __last
; }
3979 * @brief Determines whether the elements of a sequence are sorted
3980 * according to a comparison functor.
3981 * @ingroup sorting_algorithms
3982 * @param __first An iterator.
3983 * @param __last Another iterator.
3984 * @param __comp A comparison functor.
3985 * @return True if the elements are sorted, false otherwise.
3987 template<typename _ForwardIterator
, typename _Compare
>
3989 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3991 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3994 * @brief Determines the end of a sorted sequence.
3995 * @ingroup sorting_algorithms
3996 * @param __first An iterator.
3997 * @param __last Another iterator.
3998 * @return An iterator pointing to the last iterator i in [__first, __last)
3999 * for which the range [__first, i) is sorted.
4001 template<typename _ForwardIterator
>
4003 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
4005 // concept requirements
4006 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4007 __glibcxx_function_requires(_LessThanComparableConcept
<
4008 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4009 __glibcxx_requires_valid_range(__first
, __last
);
4011 if (__first
== __last
)
4014 _ForwardIterator __next
= __first
;
4015 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4016 if (*__next
< *__first
)
4022 * @brief Determines the end of a sorted sequence using comparison functor.
4023 * @ingroup sorting_algorithms
4024 * @param __first An iterator.
4025 * @param __last Another iterator.
4026 * @param __comp A comparison functor.
4027 * @return An iterator pointing to the last iterator i in [__first, __last)
4028 * for which the range [__first, i) is sorted.
4030 template<typename _ForwardIterator
, typename _Compare
>
4032 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
4035 // concept requirements
4036 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4037 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4038 typename iterator_traits
<_ForwardIterator
>::value_type
,
4039 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4040 __glibcxx_requires_valid_range(__first
, __last
);
4042 if (__first
== __last
)
4045 _ForwardIterator __next
= __first
;
4046 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4047 if (__comp(*__next
, *__first
))
4053 * @brief Determines min and max at once as an ordered pair.
4054 * @ingroup sorting_algorithms
4055 * @param __a A thing of arbitrary type.
4056 * @param __b Another thing of arbitrary type.
4057 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4060 template<typename _Tp
>
4061 inline pair
<const _Tp
&, const _Tp
&>
4062 minmax(const _Tp
& __a
, const _Tp
& __b
)
4064 // concept requirements
4065 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4067 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4068 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4072 * @brief Determines min and max at once as an ordered pair.
4073 * @ingroup sorting_algorithms
4074 * @param __a A thing of arbitrary type.
4075 * @param __b Another thing of arbitrary type.
4076 * @param __comp A @link comparison_functors comparison functor @endlink.
4077 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4080 template<typename _Tp
, typename _Compare
>
4081 inline pair
<const _Tp
&, const _Tp
&>
4082 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4084 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4085 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4089 * @brief Return a pair of iterators pointing to the minimum and maximum
4090 * elements in a range.
4091 * @ingroup sorting_algorithms
4092 * @param __first Start of range.
4093 * @param __last End of range.
4094 * @return make_pair(m, M), where m is the first iterator i in
4095 * [__first, __last) such that no other element in the range is
4096 * smaller, and where M is the last iterator i in [__first, __last)
4097 * such that no other element in the range is larger.
4099 template<typename _ForwardIterator
>
4100 pair
<_ForwardIterator
, _ForwardIterator
>
4101 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4103 // concept requirements
4104 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4105 __glibcxx_function_requires(_LessThanComparableConcept
<
4106 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4107 __glibcxx_requires_valid_range(__first
, __last
);
4109 _ForwardIterator __next
= __first
;
4110 if (__first
== __last
4111 || ++__next
== __last
)
4112 return std::make_pair(__first
, __first
);
4114 _ForwardIterator __min
, __max
;
4115 if (*__next
< *__first
)
4129 while (__first
!= __last
)
4132 if (++__next
== __last
)
4134 if (*__first
< *__min
)
4136 else if (!(*__first
< *__max
))
4141 if (*__next
< *__first
)
4143 if (*__next
< *__min
)
4145 if (!(*__first
< *__max
))
4150 if (*__first
< *__min
)
4152 if (!(*__next
< *__max
))
4160 return std::make_pair(__min
, __max
);
4164 * @brief Return a pair of iterators pointing to the minimum and maximum
4165 * elements in a range.
4166 * @ingroup sorting_algorithms
4167 * @param __first Start of range.
4168 * @param __last End of range.
4169 * @param __comp Comparison functor.
4170 * @return make_pair(m, M), where m is the first iterator i in
4171 * [__first, __last) such that no other element in the range is
4172 * smaller, and where M is the last iterator i in [__first, __last)
4173 * such that no other element in the range is larger.
4175 template<typename _ForwardIterator
, typename _Compare
>
4176 pair
<_ForwardIterator
, _ForwardIterator
>
4177 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4180 // concept requirements
4181 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4182 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4183 typename iterator_traits
<_ForwardIterator
>::value_type
,
4184 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4185 __glibcxx_requires_valid_range(__first
, __last
);
4187 _ForwardIterator __next
= __first
;
4188 if (__first
== __last
4189 || ++__next
== __last
)
4190 return std::make_pair(__first
, __first
);
4192 _ForwardIterator __min
, __max
;
4193 if (__comp(*__next
, *__first
))
4207 while (__first
!= __last
)
4210 if (++__next
== __last
)
4212 if (__comp(*__first
, *__min
))
4214 else if (!__comp(*__first
, *__max
))
4219 if (__comp(*__next
, *__first
))
4221 if (__comp(*__next
, *__min
))
4223 if (!__comp(*__first
, *__max
))
4228 if (__comp(*__first
, *__min
))
4230 if (!__comp(*__next
, *__max
))
4238 return std::make_pair(__min
, __max
);
4242 template<typename _Tp
>
4244 min(initializer_list
<_Tp
> __l
)
4245 { return *std::min_element(__l
.begin(), __l
.end()); }
4247 template<typename _Tp
, typename _Compare
>
4249 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4250 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4252 template<typename _Tp
>
4254 max(initializer_list
<_Tp
> __l
)
4255 { return *std::max_element(__l
.begin(), __l
.end()); }
4257 template<typename _Tp
, typename _Compare
>
4259 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4260 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4262 template<typename _Tp
>
4263 inline pair
<_Tp
, _Tp
>
4264 minmax(initializer_list
<_Tp
> __l
)
4266 pair
<const _Tp
*, const _Tp
*> __p
=
4267 std::minmax_element(__l
.begin(), __l
.end());
4268 return std::make_pair(*__p
.first
, *__p
.second
);
4271 template<typename _Tp
, typename _Compare
>
4272 inline pair
<_Tp
, _Tp
>
4273 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4275 pair
<const _Tp
*, const _Tp
*> __p
=
4276 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4277 return std::make_pair(*__p
.first
, *__p
.second
);
4281 * @brief Checks whether a permutaion of the second sequence is equal
4282 * to the first sequence.
4283 * @ingroup non_mutating_algorithms
4284 * @param __first1 Start of first range.
4285 * @param __last1 End of first range.
4286 * @param __first2 Start of second range.
4287 * @return true if there exists a permutation of the elements in the range
4288 * [__first2, __first2 + (__last1 - __first1)), beginning with
4289 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
4290 * returns true; otherwise, returns false.
4292 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4294 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4295 _ForwardIterator2 __first2
)
4297 // Efficiently compare identical prefixes: O(N) if sequences
4298 // have the same elements in the same order.
4299 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4300 if (!(*__first1
== *__first2
))
4303 if (__first1
== __last1
)
4306 // Establish __last2 assuming equal ranges by iterating over the
4307 // rest of the list.
4308 _ForwardIterator2 __last2
= __first2
;
4309 std::advance(__last2
, std::distance(__first1
, __last1
));
4310 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4312 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4313 continue; // We've seen this one before.
4315 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4317 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4324 * @brief Checks whether a permutation of the second sequence is equal
4325 * to the first sequence.
4326 * @ingroup non_mutating_algorithms
4327 * @param __first1 Start of first range.
4328 * @param __last1 End of first range.
4329 * @param __first2 Start of second range.
4330 * @param __pred A binary predicate.
4331 * @return true if there exists a permutation of the elements in
4332 * the range [__first2, __first2 + (__last1 - __first1)),
4333 * beginning with ForwardIterator2 begin, such that
4334 * equal(__first1, __last1, __begin, __pred) returns true;
4335 * otherwise, returns false.
4337 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4338 typename _BinaryPredicate
>
4340 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4341 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
4343 // Efficiently compare identical prefixes: O(N) if sequences
4344 // have the same elements in the same order.
4345 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4346 if (!bool(__pred(*__first1
, *__first2
)))
4349 if (__first1
== __last1
)
4352 // Establish __last2 assuming equal ranges by iterating over the
4353 // rest of the list.
4354 _ForwardIterator2 __last2
= __first2
;
4355 std::advance(__last2
, std::distance(__first1
, __last1
));
4356 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4358 using std::placeholders::_1
;
4360 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4361 std::bind(__pred
, _1
, *__scan
)))
4362 continue; // We've seen this one before.
4364 auto __matches
= std::count_if(__first2
, __last2
,
4365 std::bind(__pred
, _1
, *__scan
));
4367 || std::count_if(__scan
, __last1
,
4368 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4374 #if __cplusplus > 201103L
4376 * @brief Checks whether a permutaion of the second sequence is equal
4377 * to the first sequence.
4378 * @ingroup non_mutating_algorithms
4379 * @param __first1 Start of first range.
4380 * @param __last1 End of first range.
4381 * @param __first2 Start of second range.
4382 * @param __last2 End of first range.
4383 * @return true if there exists a permutation of the elements in the range
4384 * [__first2, __last2), beginning with ForwardIterator2 begin,
4385 * such that equal(__first1, __last1, begin) returns true;
4386 * otherwise, returns false.
4388 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4390 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4391 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4394 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
4396 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
4397 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
4398 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
4399 if (_It1_is_RA() && _It2_is_RA())
4401 auto __d1
= std::distance(__first1
, __last1
);
4402 auto __d2
= std::distance(__first2
, __last2
);
4407 // Efficiently compare identical prefixes: O(N) if sequences
4408 // have the same elements in the same order.
4409 for (; __first1
!= __last1
&& __first2
!= __last2
; ++__first1
, ++__first2
)
4410 if (!(*__first1
== *__first2
))
4413 if (__first1
== __last1
&& __first2
== __last2
)
4416 if (std::distance(__first1
, __last1
) != std::distance(__first2
, __last2
))
4419 for (auto __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4421 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4422 continue; // We've seen this one before.
4424 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4426 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4433 * @brief Checks whether a permutation of the second sequence is equal
4434 * to the first sequence.
4435 * @ingroup non_mutating_algorithms
4436 * @param __first1 Start of first range.
4437 * @param __last1 End of first range.
4438 * @param __first2 Start of second range.
4439 * @param __last2 End of first range.
4440 * @param __pred A binary predicate.
4441 * @return true if there exists a permutation of the elements in the range
4442 * [__first2, __last2), beginning with ForwardIterator2 begin,
4443 * such that equal(__first1, __last1, __begin, __pred) returns true;
4444 * otherwise, returns false.
4446 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4447 typename _BinaryPredicate
>
4449 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4450 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4451 _BinaryPredicate __pred
)
4454 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
4456 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
4457 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
4458 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
4459 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
4462 auto __d1
= std::distance(__first1
, __last1
);
4463 auto __d2
= std::distance(__first2
, __last2
);
4468 // Efficiently compare identical prefixes: O(N) if sequences
4469 // have the same elements in the same order.
4470 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4471 if (!bool(__pred(*__first1
, *__first2
)))
4476 if (__first1
== __last1
)
4481 auto __d1
= std::distance(__first1
, __last1
);
4482 auto __d2
= std::distance(__first2
, __last2
);
4483 if (__d1
== 0 && __d2
== 0)
4489 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4491 using std::placeholders::_1
;
4493 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4494 std::bind(__pred
, _1
, *__scan
)))
4495 continue; // We've seen this one before.
4497 auto __matches
= std::count_if(__first2
, __last2
,
4498 std::bind(__pred
, _1
, *__scan
));
4500 || std::count_if(__scan
, __last1
,
4501 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4508 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4510 * @brief Shuffle the elements of a sequence using a uniform random
4512 * @ingroup mutating_algorithms
4513 * @param __first A forward iterator.
4514 * @param __last A forward iterator.
4515 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
4518 * Reorders the elements in the range @p [__first,__last) using @p __g to
4519 * provide random numbers.
4521 template<typename _RandomAccessIterator
,
4522 typename _UniformRandomNumberGenerator
>
4524 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4525 _UniformRandomNumberGenerator
&& __g
)
4527 // concept requirements
4528 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4529 _RandomAccessIterator
>)
4530 __glibcxx_requires_valid_range(__first
, __last
);
4532 if (__first
== __last
)
4535 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4538 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4539 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4540 typedef typename
__distr_type::param_type __p_type
;
4543 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4544 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4550 _GLIBCXX_END_NAMESPACE_VERSION
4552 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4555 * @brief Apply a function to every element of a sequence.
4556 * @ingroup non_mutating_algorithms
4557 * @param __first An input iterator.
4558 * @param __last An input iterator.
4559 * @param __f A unary function object.
4560 * @return @p __f (std::move(@p __f) in C++0x).
4562 * Applies the function object @p __f to each element in the range
4563 * @p [first,last). @p __f must not modify the order of the sequence.
4564 * If @p __f has a return value it is ignored.
4566 template<typename _InputIterator
, typename _Function
>
4568 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4570 // concept requirements
4571 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4572 __glibcxx_requires_valid_range(__first
, __last
);
4573 for (; __first
!= __last
; ++__first
)
4575 return _GLIBCXX_MOVE(__f
);
4579 * @brief Find the first occurrence of a value in a sequence.
4580 * @ingroup non_mutating_algorithms
4581 * @param __first An input iterator.
4582 * @param __last An input iterator.
4583 * @param __val The value to find.
4584 * @return The first iterator @c i in the range @p [__first,__last)
4585 * such that @c *i == @p __val, or @p __last if no such iterator exists.
4587 template<typename _InputIterator
, typename _Tp
>
4588 inline _InputIterator
4589 find(_InputIterator __first
, _InputIterator __last
,
4592 // concept requirements
4593 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4594 __glibcxx_function_requires(_EqualOpConcept
<
4595 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4596 __glibcxx_requires_valid_range(__first
, __last
);
4597 return std::__find(__first
, __last
, __val
,
4598 std::__iterator_category(__first
));
4602 * @brief Find the first element in a sequence for which a
4603 * predicate is true.
4604 * @ingroup non_mutating_algorithms
4605 * @param __first An input iterator.
4606 * @param __last An input iterator.
4607 * @param __pred A predicate.
4608 * @return The first iterator @c i in the range @p [__first,__last)
4609 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4611 template<typename _InputIterator
, typename _Predicate
>
4612 inline _InputIterator
4613 find_if(_InputIterator __first
, _InputIterator __last
,
4616 // concept requirements
4617 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4618 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4619 typename iterator_traits
<_InputIterator
>::value_type
>)
4620 __glibcxx_requires_valid_range(__first
, __last
);
4621 return std::__find_if(__first
, __last
, __pred
,
4622 std::__iterator_category(__first
));
4626 * @brief Find element from a set in a sequence.
4627 * @ingroup non_mutating_algorithms
4628 * @param __first1 Start of range to search.
4629 * @param __last1 End of range to search.
4630 * @param __first2 Start of match candidates.
4631 * @param __last2 End of match candidates.
4632 * @return The first iterator @c i in the range
4633 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4634 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4636 * Searches the range @p [__first1,__last1) for an element that is
4637 * equal to some element in the range [__first2,__last2). If
4638 * found, returns an iterator in the range [__first1,__last1),
4639 * otherwise returns @p __last1.
4641 template<typename _InputIterator
, typename _ForwardIterator
>
4643 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4644 _ForwardIterator __first2
, _ForwardIterator __last2
)
4646 // concept requirements
4647 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4648 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4649 __glibcxx_function_requires(_EqualOpConcept
<
4650 typename iterator_traits
<_InputIterator
>::value_type
,
4651 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4652 __glibcxx_requires_valid_range(__first1
, __last1
);
4653 __glibcxx_requires_valid_range(__first2
, __last2
);
4655 for (; __first1
!= __last1
; ++__first1
)
4656 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4657 if (*__first1
== *__iter
)
4663 * @brief Find element from a set in a sequence using a predicate.
4664 * @ingroup non_mutating_algorithms
4665 * @param __first1 Start of range to search.
4666 * @param __last1 End of range to search.
4667 * @param __first2 Start of match candidates.
4668 * @param __last2 End of match candidates.
4669 * @param __comp Predicate to use.
4670 * @return The first iterator @c i in the range
4671 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4672 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4673 * such iterator exists.
4676 * Searches the range @p [__first1,__last1) for an element that is
4677 * equal to some element in the range [__first2,__last2). If
4678 * found, returns an iterator in the range [__first1,__last1),
4679 * otherwise returns @p __last1.
4681 template<typename _InputIterator
, typename _ForwardIterator
,
4682 typename _BinaryPredicate
>
4684 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4685 _ForwardIterator __first2
, _ForwardIterator __last2
,
4686 _BinaryPredicate __comp
)
4688 // concept requirements
4689 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4690 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4691 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4692 typename iterator_traits
<_InputIterator
>::value_type
,
4693 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4694 __glibcxx_requires_valid_range(__first1
, __last1
);
4695 __glibcxx_requires_valid_range(__first2
, __last2
);
4697 for (; __first1
!= __last1
; ++__first1
)
4698 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4699 if (__comp(*__first1
, *__iter
))
4705 * @brief Find two adjacent values in a sequence that are equal.
4706 * @ingroup non_mutating_algorithms
4707 * @param __first A forward iterator.
4708 * @param __last A forward iterator.
4709 * @return The first iterator @c i such that @c i and @c i+1 are both
4710 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4711 * or @p __last if no such iterator exists.
4713 template<typename _ForwardIterator
>
4715 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4717 // concept requirements
4718 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4719 __glibcxx_function_requires(_EqualityComparableConcept
<
4720 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4721 __glibcxx_requires_valid_range(__first
, __last
);
4722 if (__first
== __last
)
4724 _ForwardIterator __next
= __first
;
4725 while(++__next
!= __last
)
4727 if (*__first
== *__next
)
4735 * @brief Find two adjacent values in a sequence using a predicate.
4736 * @ingroup non_mutating_algorithms
4737 * @param __first A forward iterator.
4738 * @param __last A forward iterator.
4739 * @param __binary_pred A binary predicate.
4740 * @return The first iterator @c i such that @c i and @c i+1 are both
4741 * valid iterators in @p [__first,__last) and such that
4742 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4745 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4747 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4748 _BinaryPredicate __binary_pred
)
4750 // concept requirements
4751 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4752 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4753 typename iterator_traits
<_ForwardIterator
>::value_type
,
4754 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4755 __glibcxx_requires_valid_range(__first
, __last
);
4756 if (__first
== __last
)
4758 _ForwardIterator __next
= __first
;
4759 while(++__next
!= __last
)
4761 if (__binary_pred(*__first
, *__next
))
4769 * @brief Count the number of copies of a value in a sequence.
4770 * @ingroup non_mutating_algorithms
4771 * @param __first An input iterator.
4772 * @param __last An input iterator.
4773 * @param __value The value to be counted.
4774 * @return The number of iterators @c i in the range @p [__first,__last)
4775 * for which @c *i == @p __value
4777 template<typename _InputIterator
, typename _Tp
>
4778 typename iterator_traits
<_InputIterator
>::difference_type
4779 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4781 // concept requirements
4782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4783 __glibcxx_function_requires(_EqualOpConcept
<
4784 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4785 __glibcxx_requires_valid_range(__first
, __last
);
4786 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4787 for (; __first
!= __last
; ++__first
)
4788 if (*__first
== __value
)
4794 * @brief Count the elements of a sequence for which a predicate is true.
4795 * @ingroup non_mutating_algorithms
4796 * @param __first An input iterator.
4797 * @param __last An input iterator.
4798 * @param __pred A predicate.
4799 * @return The number of iterators @c i in the range @p [__first,__last)
4800 * for which @p __pred(*i) is true.
4802 template<typename _InputIterator
, typename _Predicate
>
4803 typename iterator_traits
<_InputIterator
>::difference_type
4804 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4806 // concept requirements
4807 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4808 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4809 typename iterator_traits
<_InputIterator
>::value_type
>)
4810 __glibcxx_requires_valid_range(__first
, __last
);
4811 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4812 for (; __first
!= __last
; ++__first
)
4813 if (__pred(*__first
))
4819 * @brief Search a sequence for a matching sub-sequence.
4820 * @ingroup non_mutating_algorithms
4821 * @param __first1 A forward iterator.
4822 * @param __last1 A forward iterator.
4823 * @param __first2 A forward iterator.
4824 * @param __last2 A forward iterator.
4825 * @return The first iterator @c i in the range @p
4826 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4827 * *(__first2+N) for each @c N in the range @p
4828 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4830 * Searches the range @p [__first1,__last1) for a sub-sequence that
4831 * compares equal value-by-value with the sequence given by @p
4832 * [__first2,__last2) and returns an iterator to the first element
4833 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4836 * Because the sub-sequence must lie completely within the range @p
4837 * [__first1,__last1) it must start at a position less than @p
4838 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4839 * length of the sub-sequence.
4841 * This means that the returned iterator @c i will be in the range
4842 * @p [__first1,__last1-(__last2-__first2))
4844 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4846 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4847 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4849 // concept requirements
4850 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4851 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4852 __glibcxx_function_requires(_EqualOpConcept
<
4853 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4854 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4855 __glibcxx_requires_valid_range(__first1
, __last1
);
4856 __glibcxx_requires_valid_range(__first2
, __last2
);
4858 // Test for empty ranges
4859 if (__first1
== __last1
|| __first2
== __last2
)
4862 // Test for a pattern of length 1.
4863 _ForwardIterator2
__p1(__first2
);
4864 if (++__p1
== __last2
)
4865 return _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4868 _ForwardIterator2 __p
;
4869 _ForwardIterator1 __current
= __first1
;
4873 __first1
= _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4874 if (__first1
== __last1
)
4878 __current
= __first1
;
4879 if (++__current
== __last1
)
4882 while (*__current
== *__p
)
4884 if (++__p
== __last2
)
4886 if (++__current
== __last1
)
4895 * @brief Search a sequence for a matching sub-sequence using a predicate.
4896 * @ingroup non_mutating_algorithms
4897 * @param __first1 A forward iterator.
4898 * @param __last1 A forward iterator.
4899 * @param __first2 A forward iterator.
4900 * @param __last2 A forward iterator.
4901 * @param __predicate A binary predicate.
4902 * @return The first iterator @c i in the range
4903 * @p [__first1,__last1-(__last2-__first2)) such that
4904 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4905 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4907 * Searches the range @p [__first1,__last1) for a sub-sequence that
4908 * compares equal value-by-value with the sequence given by @p
4909 * [__first2,__last2), using @p __predicate to determine equality,
4910 * and returns an iterator to the first element of the
4911 * sub-sequence, or @p __last1 if no such iterator exists.
4913 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4915 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4916 typename _BinaryPredicate
>
4918 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4919 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4920 _BinaryPredicate __predicate
)
4922 // concept requirements
4923 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4924 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4925 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4926 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4927 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4928 __glibcxx_requires_valid_range(__first1
, __last1
);
4929 __glibcxx_requires_valid_range(__first2
, __last2
);
4931 // Test for empty ranges
4932 if (__first1
== __last1
|| __first2
== __last2
)
4935 // Test for a pattern of length 1.
4936 _ForwardIterator2
__p1(__first2
);
4937 if (++__p1
== __last2
)
4939 while (__first1
!= __last1
4940 && !bool(__predicate(*__first1
, *__first2
)))
4946 _ForwardIterator2 __p
;
4947 _ForwardIterator1 __current
= __first1
;
4951 while (__first1
!= __last1
4952 && !bool(__predicate(*__first1
, *__first2
)))
4954 if (__first1
== __last1
)
4958 __current
= __first1
;
4959 if (++__current
== __last1
)
4962 while (__predicate(*__current
, *__p
))
4964 if (++__p
== __last2
)
4966 if (++__current
== __last1
)
4976 * @brief Search a sequence for a number of consecutive values.
4977 * @ingroup non_mutating_algorithms
4978 * @param __first A forward iterator.
4979 * @param __last A forward iterator.
4980 * @param __count The number of consecutive values.
4981 * @param __val The value to find.
4982 * @return The first iterator @c i in the range @p
4983 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4984 * each @c N in the range @p [0,__count), or @p __last if no such
4987 * Searches the range @p [__first,__last) for @p count consecutive elements
4988 * equal to @p __val.
4990 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4992 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4993 _Integer __count
, const _Tp
& __val
)
4995 // concept requirements
4996 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4997 __glibcxx_function_requires(_EqualOpConcept
<
4998 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4999 __glibcxx_requires_valid_range(__first
, __last
);
5004 return _GLIBCXX_STD_A::find(__first
, __last
, __val
);
5005 return std::__search_n(__first
, __last
, __count
, __val
,
5006 std::__iterator_category(__first
));
5011 * @brief Search a sequence for a number of consecutive values using a
5013 * @ingroup non_mutating_algorithms
5014 * @param __first A forward iterator.
5015 * @param __last A forward iterator.
5016 * @param __count The number of consecutive values.
5017 * @param __val The value to find.
5018 * @param __binary_pred A binary predicate.
5019 * @return The first iterator @c i in the range @p
5020 * [__first,__last-__count) such that @p
5021 * __binary_pred(*(i+N),__val) is true for each @c N in the range
5022 * @p [0,__count), or @p __last if no such iterator exists.
5024 * Searches the range @p [__first,__last) for @p __count
5025 * consecutive elements for which the predicate returns true.
5027 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
5028 typename _BinaryPredicate
>
5030 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
5031 _Integer __count
, const _Tp
& __val
,
5032 _BinaryPredicate __binary_pred
)
5034 // concept requirements
5035 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5036 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5037 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
5038 __glibcxx_requires_valid_range(__first
, __last
);
5044 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
5048 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
5049 std::__iterator_category(__first
));
5054 * @brief Perform an operation on a sequence.
5055 * @ingroup mutating_algorithms
5056 * @param __first An input iterator.
5057 * @param __last An input iterator.
5058 * @param __result An output iterator.
5059 * @param __unary_op A unary operator.
5060 * @return An output iterator equal to @p __result+(__last-__first).
5062 * Applies the operator to each element in the input range and assigns
5063 * the results to successive elements of the output sequence.
5064 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
5065 * range @p [0,__last-__first).
5067 * @p unary_op must not alter its argument.
5069 template<typename _InputIterator
, typename _OutputIterator
,
5070 typename _UnaryOperation
>
5072 transform(_InputIterator __first
, _InputIterator __last
,
5073 _OutputIterator __result
, _UnaryOperation __unary_op
)
5075 // concept requirements
5076 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5077 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5078 // "the type returned by a _UnaryOperation"
5079 __typeof__(__unary_op(*__first
))>)
5080 __glibcxx_requires_valid_range(__first
, __last
);
5082 for (; __first
!= __last
; ++__first
, ++__result
)
5083 *__result
= __unary_op(*__first
);
5088 * @brief Perform an operation on corresponding elements of two sequences.
5089 * @ingroup mutating_algorithms
5090 * @param __first1 An input iterator.
5091 * @param __last1 An input iterator.
5092 * @param __first2 An input iterator.
5093 * @param __result An output iterator.
5094 * @param __binary_op A binary operator.
5095 * @return An output iterator equal to @p result+(last-first).
5097 * Applies the operator to the corresponding elements in the two
5098 * input ranges and assigns the results to successive elements of the
5101 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
5102 * @c N in the range @p [0,__last1-__first1).
5104 * @p binary_op must not alter either of its arguments.
5106 template<typename _InputIterator1
, typename _InputIterator2
,
5107 typename _OutputIterator
, typename _BinaryOperation
>
5109 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
5110 _InputIterator2 __first2
, _OutputIterator __result
,
5111 _BinaryOperation __binary_op
)
5113 // concept requirements
5114 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5115 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5116 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5117 // "the type returned by a _BinaryOperation"
5118 __typeof__(__binary_op(*__first1
,*__first2
))>)
5119 __glibcxx_requires_valid_range(__first1
, __last1
);
5121 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
5122 *__result
= __binary_op(*__first1
, *__first2
);
5127 * @brief Replace each occurrence of one value in a sequence with another
5129 * @ingroup mutating_algorithms
5130 * @param __first A forward iterator.
5131 * @param __last A forward iterator.
5132 * @param __old_value The value to be replaced.
5133 * @param __new_value The replacement value.
5134 * @return replace() returns no value.
5136 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
5137 * @p __old_value then the assignment @c *i = @p __new_value is performed.
5139 template<typename _ForwardIterator
, typename _Tp
>
5141 replace(_ForwardIterator __first
, _ForwardIterator __last
,
5142 const _Tp
& __old_value
, const _Tp
& __new_value
)
5144 // concept requirements
5145 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5147 __glibcxx_function_requires(_EqualOpConcept
<
5148 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
5149 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
5150 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5151 __glibcxx_requires_valid_range(__first
, __last
);
5153 for (; __first
!= __last
; ++__first
)
5154 if (*__first
== __old_value
)
5155 *__first
= __new_value
;
5159 * @brief Replace each value in a sequence for which a predicate returns
5160 * true with another value.
5161 * @ingroup mutating_algorithms
5162 * @param __first A forward iterator.
5163 * @param __last A forward iterator.
5164 * @param __pred A predicate.
5165 * @param __new_value The replacement value.
5166 * @return replace_if() returns no value.
5168 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
5169 * is true then the assignment @c *i = @p __new_value is performed.
5171 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
5173 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
5174 _Predicate __pred
, const _Tp
& __new_value
)
5176 // concept requirements
5177 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5179 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
5180 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5181 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5182 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5183 __glibcxx_requires_valid_range(__first
, __last
);
5185 for (; __first
!= __last
; ++__first
)
5186 if (__pred(*__first
))
5187 *__first
= __new_value
;
5191 * @brief Assign the result of a function object to each value in a
5193 * @ingroup mutating_algorithms
5194 * @param __first A forward iterator.
5195 * @param __last A forward iterator.
5196 * @param __gen A function object taking no arguments and returning
5197 * std::iterator_traits<_ForwardIterator>::value_type
5198 * @return generate() returns no value.
5200 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5201 * @p [__first,__last).
5203 template<typename _ForwardIterator
, typename _Generator
>
5205 generate(_ForwardIterator __first
, _ForwardIterator __last
,
5208 // concept requirements
5209 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5210 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
5211 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5212 __glibcxx_requires_valid_range(__first
, __last
);
5214 for (; __first
!= __last
; ++__first
)
5219 * @brief Assign the result of a function object to each value in a
5221 * @ingroup mutating_algorithms
5222 * @param __first A forward iterator.
5223 * @param __n The length of the sequence.
5224 * @param __gen A function object taking no arguments and returning
5225 * std::iterator_traits<_ForwardIterator>::value_type
5226 * @return The end of the sequence, @p __first+__n
5228 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5229 * @p [__first,__first+__n).
5231 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5232 * DR 865. More algorithms that throw away information
5234 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
5236 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
5238 // concept requirements
5239 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5240 // "the type returned by a _Generator"
5241 __typeof__(__gen())>)
5243 for (__decltype(__n
+ 0) __niter
= __n
;
5244 __niter
> 0; --__niter
, ++__first
)
5251 * @brief Copy a sequence, removing consecutive duplicate values.
5252 * @ingroup mutating_algorithms
5253 * @param __first An input iterator.
5254 * @param __last An input iterator.
5255 * @param __result An output iterator.
5256 * @return An iterator designating the end of the resulting sequence.
5258 * Copies each element in the range @p [__first,__last) to the range
5259 * beginning at @p __result, except that only the first element is copied
5260 * from groups of consecutive elements that compare equal.
5261 * unique_copy() is stable, so the relative order of elements that are
5262 * copied is unchanged.
5264 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5265 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5267 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5268 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5271 template<typename _InputIterator
, typename _OutputIterator
>
5272 inline _OutputIterator
5273 unique_copy(_InputIterator __first
, _InputIterator __last
,
5274 _OutputIterator __result
)
5276 // concept requirements
5277 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5278 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5279 typename iterator_traits
<_InputIterator
>::value_type
>)
5280 __glibcxx_function_requires(_EqualityComparableConcept
<
5281 typename iterator_traits
<_InputIterator
>::value_type
>)
5282 __glibcxx_requires_valid_range(__first
, __last
);
5284 if (__first
== __last
)
5286 return std::__unique_copy(__first
, __last
, __result
,
5287 std::__iterator_category(__first
),
5288 std::__iterator_category(__result
));
5292 * @brief Copy a sequence, removing consecutive values using a predicate.
5293 * @ingroup mutating_algorithms
5294 * @param __first An input iterator.
5295 * @param __last An input iterator.
5296 * @param __result An output iterator.
5297 * @param __binary_pred A binary predicate.
5298 * @return An iterator designating the end of the resulting sequence.
5300 * Copies each element in the range @p [__first,__last) to the range
5301 * beginning at @p __result, except that only the first element is copied
5302 * from groups of consecutive elements for which @p __binary_pred returns
5304 * unique_copy() is stable, so the relative order of elements that are
5305 * copied is unchanged.
5307 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5308 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5310 template<typename _InputIterator
, typename _OutputIterator
,
5311 typename _BinaryPredicate
>
5312 inline _OutputIterator
5313 unique_copy(_InputIterator __first
, _InputIterator __last
,
5314 _OutputIterator __result
,
5315 _BinaryPredicate __binary_pred
)
5317 // concept requirements -- predicates checked later
5318 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5319 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5320 typename iterator_traits
<_InputIterator
>::value_type
>)
5321 __glibcxx_requires_valid_range(__first
, __last
);
5323 if (__first
== __last
)
5325 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5326 std::__iterator_category(__first
),
5327 std::__iterator_category(__result
));
5332 * @brief Randomly shuffle the elements of a sequence.
5333 * @ingroup mutating_algorithms
5334 * @param __first A forward iterator.
5335 * @param __last A forward iterator.
5338 * Reorder the elements in the range @p [__first,__last) using a random
5339 * distribution, so that every possible ordering of the sequence is
5342 template<typename _RandomAccessIterator
>
5344 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5346 // concept requirements
5347 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5348 _RandomAccessIterator
>)
5349 __glibcxx_requires_valid_range(__first
, __last
);
5351 if (__first
!= __last
)
5352 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5353 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5357 * @brief Shuffle the elements of a sequence using a random number
5359 * @ingroup mutating_algorithms
5360 * @param __first A forward iterator.
5361 * @param __last A forward iterator.
5362 * @param __rand The RNG functor or function.
5365 * Reorders the elements in the range @p [__first,__last) using @p __rand to
5366 * provide a random distribution. Calling @p __rand(N) for a positive
5367 * integer @p N should return a randomly chosen integer from the
5370 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5372 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5373 #if __cplusplus >= 201103L
5374 _RandomNumberGenerator
&& __rand
)
5376 _RandomNumberGenerator
& __rand
)
5379 // concept requirements
5380 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5381 _RandomAccessIterator
>)
5382 __glibcxx_requires_valid_range(__first
, __last
);
5384 if (__first
== __last
)
5386 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5387 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5392 * @brief Move elements for which a predicate is true to the beginning
5394 * @ingroup mutating_algorithms
5395 * @param __first A forward iterator.
5396 * @param __last A forward iterator.
5397 * @param __pred A predicate functor.
5398 * @return An iterator @p middle such that @p __pred(i) is true for each
5399 * iterator @p i in the range @p [__first,middle) and false for each @p i
5400 * in the range @p [middle,__last).
5402 * @p __pred must not modify its operand. @p partition() does not preserve
5403 * the relative ordering of elements in each group, use
5404 * @p stable_partition() if this is needed.
5406 template<typename _ForwardIterator
, typename _Predicate
>
5407 inline _ForwardIterator
5408 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5411 // concept requirements
5412 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5414 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5415 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5416 __glibcxx_requires_valid_range(__first
, __last
);
5418 return std::__partition(__first
, __last
, __pred
,
5419 std::__iterator_category(__first
));
5425 * @brief Sort the smallest elements of a sequence.
5426 * @ingroup sorting_algorithms
5427 * @param __first An iterator.
5428 * @param __middle Another iterator.
5429 * @param __last Another iterator.
5432 * Sorts the smallest @p (__middle-__first) elements in the range
5433 * @p [first,last) and moves them to the range @p [__first,__middle). The
5434 * order of the remaining elements in the range @p [__middle,__last) is
5436 * After the sort if @e i and @e j are iterators in the range
5437 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
5438 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
5440 template<typename _RandomAccessIterator
>
5442 partial_sort(_RandomAccessIterator __first
,
5443 _RandomAccessIterator __middle
,
5444 _RandomAccessIterator __last
)
5446 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5449 // concept requirements
5450 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5451 _RandomAccessIterator
>)
5452 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5453 __glibcxx_requires_valid_range(__first
, __middle
);
5454 __glibcxx_requires_valid_range(__middle
, __last
);
5456 std::__heap_select(__first
, __middle
, __last
);
5457 std::sort_heap(__first
, __middle
);
5461 * @brief Sort the smallest elements of a sequence using a predicate
5463 * @ingroup sorting_algorithms
5464 * @param __first An iterator.
5465 * @param __middle Another iterator.
5466 * @param __last Another iterator.
5467 * @param __comp A comparison functor.
5470 * Sorts the smallest @p (__middle-__first) elements in the range
5471 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
5472 * order of the remaining elements in the range @p [__middle,__last) is
5474 * After the sort if @e i and @e j are iterators in the range
5475 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
5476 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
5479 template<typename _RandomAccessIterator
, typename _Compare
>
5481 partial_sort(_RandomAccessIterator __first
,
5482 _RandomAccessIterator __middle
,
5483 _RandomAccessIterator __last
,
5486 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5489 // concept requirements
5490 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5491 _RandomAccessIterator
>)
5492 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5493 _ValueType
, _ValueType
>)
5494 __glibcxx_requires_valid_range(__first
, __middle
);
5495 __glibcxx_requires_valid_range(__middle
, __last
);
5497 std::__heap_select(__first
, __middle
, __last
, __comp
);
5498 std::sort_heap(__first
, __middle
, __comp
);
5502 * @brief Sort a sequence just enough to find a particular position.
5503 * @ingroup sorting_algorithms
5504 * @param __first An iterator.
5505 * @param __nth Another iterator.
5506 * @param __last Another iterator.
5509 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5510 * is the same element that would have been in that position had the
5511 * whole sequence been sorted. The elements either side of @p *__nth are
5512 * not completely sorted, but for any iterator @e i in the range
5513 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
5514 * holds that *j < *i is false.
5516 template<typename _RandomAccessIterator
>
5518 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5519 _RandomAccessIterator __last
)
5521 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5524 // concept requirements
5525 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5526 _RandomAccessIterator
>)
5527 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5528 __glibcxx_requires_valid_range(__first
, __nth
);
5529 __glibcxx_requires_valid_range(__nth
, __last
);
5531 if (__first
== __last
|| __nth
== __last
)
5534 std::__introselect(__first
, __nth
, __last
,
5535 std::__lg(__last
- __first
) * 2);
5539 * @brief Sort a sequence just enough to find a particular position
5540 * using a predicate for comparison.
5541 * @ingroup sorting_algorithms
5542 * @param __first An iterator.
5543 * @param __nth Another iterator.
5544 * @param __last Another iterator.
5545 * @param __comp A comparison functor.
5548 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5549 * is the same element that would have been in that position had the
5550 * whole sequence been sorted. The elements either side of @p *__nth are
5551 * not completely sorted, but for any iterator @e i in the range
5552 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
5553 * holds that @p __comp(*j,*i) is false.
5555 template<typename _RandomAccessIterator
, typename _Compare
>
5557 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5558 _RandomAccessIterator __last
, _Compare __comp
)
5560 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5563 // concept requirements
5564 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5565 _RandomAccessIterator
>)
5566 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5567 _ValueType
, _ValueType
>)
5568 __glibcxx_requires_valid_range(__first
, __nth
);
5569 __glibcxx_requires_valid_range(__nth
, __last
);
5571 if (__first
== __last
|| __nth
== __last
)
5574 std::__introselect(__first
, __nth
, __last
,
5575 std::__lg(__last
- __first
) * 2, __comp
);
5580 * @brief Sort the elements of a sequence.
5581 * @ingroup sorting_algorithms
5582 * @param __first An iterator.
5583 * @param __last Another iterator.
5586 * Sorts the elements in the range @p [__first,__last) in ascending order,
5587 * such that for each iterator @e i in the range @p [__first,__last-1),
5588 * *(i+1)<*i is false.
5590 * The relative ordering of equivalent elements is not preserved, use
5591 * @p stable_sort() if this is needed.
5593 template<typename _RandomAccessIterator
>
5595 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5597 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5600 // concept requirements
5601 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5602 _RandomAccessIterator
>)
5603 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5604 __glibcxx_requires_valid_range(__first
, __last
);
5606 if (__first
!= __last
)
5608 std::__introsort_loop(__first
, __last
,
5609 std::__lg(__last
- __first
) * 2);
5610 std::__final_insertion_sort(__first
, __last
);
5615 * @brief Sort the elements of a sequence using a predicate for comparison.
5616 * @ingroup sorting_algorithms
5617 * @param __first An iterator.
5618 * @param __last Another iterator.
5619 * @param __comp A comparison functor.
5622 * Sorts the elements in the range @p [__first,__last) in ascending order,
5623 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
5624 * range @p [__first,__last-1).
5626 * The relative ordering of equivalent elements is not preserved, use
5627 * @p stable_sort() if this is needed.
5629 template<typename _RandomAccessIterator
, typename _Compare
>
5631 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5634 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5637 // concept requirements
5638 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5639 _RandomAccessIterator
>)
5640 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5642 __glibcxx_requires_valid_range(__first
, __last
);
5644 if (__first
!= __last
)
5646 std::__introsort_loop(__first
, __last
,
5647 std::__lg(__last
- __first
) * 2, __comp
);
5648 std::__final_insertion_sort(__first
, __last
, __comp
);
5653 * @brief Merges two sorted ranges.
5654 * @ingroup sorting_algorithms
5655 * @param __first1 An iterator.
5656 * @param __first2 Another iterator.
5657 * @param __last1 Another iterator.
5658 * @param __last2 Another iterator.
5659 * @param __result An iterator pointing to the end of the merged range.
5660 * @return An iterator pointing to the first element <em>not less
5663 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5664 * the sorted range @p [__result, __result + (__last1-__first1) +
5665 * (__last2-__first2)). Both input ranges must be sorted, and the
5666 * output range must not overlap with either of the input ranges.
5667 * The sort is @e stable, that is, for equivalent elements in the
5668 * two ranges, elements from the first range will always come
5669 * before elements from the second.
5671 template<typename _InputIterator1
, typename _InputIterator2
,
5672 typename _OutputIterator
>
5674 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5675 _InputIterator2 __first2
, _InputIterator2 __last2
,
5676 _OutputIterator __result
)
5678 typedef typename iterator_traits
<_InputIterator1
>::value_type
5680 typedef typename iterator_traits
<_InputIterator2
>::value_type
5683 // concept requirements
5684 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5685 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5686 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5688 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5690 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5691 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5692 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5694 while (__first1
!= __last1
&& __first2
!= __last2
)
5696 if (*__first2
< *__first1
)
5698 *__result
= *__first2
;
5703 *__result
= *__first1
;
5708 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5713 * @brief Merges two sorted ranges.
5714 * @ingroup sorting_algorithms
5715 * @param __first1 An iterator.
5716 * @param __first2 Another iterator.
5717 * @param __last1 Another iterator.
5718 * @param __last2 Another iterator.
5719 * @param __result An iterator pointing to the end of the merged range.
5720 * @param __comp A functor to use for comparisons.
5721 * @return An iterator pointing to the first element "not less
5724 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5725 * the sorted range @p [__result, __result + (__last1-__first1) +
5726 * (__last2-__first2)). Both input ranges must be sorted, and the
5727 * output range must not overlap with either of the input ranges.
5728 * The sort is @e stable, that is, for equivalent elements in the
5729 * two ranges, elements from the first range will always come
5730 * before elements from the second.
5732 * The comparison function should have the same effects on ordering as
5733 * the function used for the initial sort.
5735 template<typename _InputIterator1
, typename _InputIterator2
,
5736 typename _OutputIterator
, typename _Compare
>
5738 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5739 _InputIterator2 __first2
, _InputIterator2 __last2
,
5740 _OutputIterator __result
, _Compare __comp
)
5742 typedef typename iterator_traits
<_InputIterator1
>::value_type
5744 typedef typename iterator_traits
<_InputIterator2
>::value_type
5747 // concept requirements
5748 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5749 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5750 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5752 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5754 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5755 _ValueType2
, _ValueType1
>)
5756 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5757 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5759 while (__first1
!= __last1
&& __first2
!= __last2
)
5761 if (__comp(*__first2
, *__first1
))
5763 *__result
= *__first2
;
5768 *__result
= *__first1
;
5773 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5779 * @brief Sort the elements of a sequence, preserving the relative order
5780 * of equivalent elements.
5781 * @ingroup sorting_algorithms
5782 * @param __first An iterator.
5783 * @param __last Another iterator.
5786 * Sorts the elements in the range @p [__first,__last) in ascending order,
5787 * such that for each iterator @p i in the range @p [__first,__last-1),
5788 * @p *(i+1)<*i is false.
5790 * The relative ordering of equivalent elements is preserved, so any two
5791 * elements @p x and @p y in the range @p [__first,__last) such that
5792 * @p x<y is false and @p y<x is false will have the same relative
5793 * ordering after calling @p stable_sort().
5795 template<typename _RandomAccessIterator
>
5797 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5799 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5801 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5804 // concept requirements
5805 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5806 _RandomAccessIterator
>)
5807 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5808 __glibcxx_requires_valid_range(__first
, __last
);
5810 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5812 if (__buf
.begin() == 0)
5813 std::__inplace_stable_sort(__first
, __last
);
5815 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5816 _DistanceType(__buf
.size()));
5820 * @brief Sort the elements of a sequence using a predicate for comparison,
5821 * preserving the relative order of equivalent elements.
5822 * @ingroup sorting_algorithms
5823 * @param __first An iterator.
5824 * @param __last Another iterator.
5825 * @param __comp A comparison functor.
5828 * Sorts the elements in the range @p [__first,__last) in ascending order,
5829 * such that for each iterator @p i in the range @p [__first,__last-1),
5830 * @p __comp(*(i+1),*i) is false.
5832 * The relative ordering of equivalent elements is preserved, so any two
5833 * elements @p x and @p y in the range @p [__first,__last) such that
5834 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5835 * relative ordering after calling @p stable_sort().
5837 template<typename _RandomAccessIterator
, typename _Compare
>
5839 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5842 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5844 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5847 // concept requirements
5848 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5849 _RandomAccessIterator
>)
5850 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5853 __glibcxx_requires_valid_range(__first
, __last
);
5855 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5857 if (__buf
.begin() == 0)
5858 std::__inplace_stable_sort(__first
, __last
, __comp
);
5860 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5861 _DistanceType(__buf
.size()), __comp
);
5866 * @brief Return the union of two sorted ranges.
5867 * @ingroup set_algorithms
5868 * @param __first1 Start of first range.
5869 * @param __last1 End of first range.
5870 * @param __first2 Start of second range.
5871 * @param __last2 End of second range.
5872 * @return End of the output range.
5873 * @ingroup set_algorithms
5875 * This operation iterates over both ranges, copying elements present in
5876 * each range in order to the output range. Iterators increment for each
5877 * range. When the current element of one range is less than the other,
5878 * that element is copied and the iterator advanced. If an element is
5879 * contained in both ranges, the element from the first range is copied and
5880 * both ranges advance. The output range may not overlap either input
5883 template<typename _InputIterator1
, typename _InputIterator2
,
5884 typename _OutputIterator
>
5886 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5887 _InputIterator2 __first2
, _InputIterator2 __last2
,
5888 _OutputIterator __result
)
5890 typedef typename iterator_traits
<_InputIterator1
>::value_type
5892 typedef typename iterator_traits
<_InputIterator2
>::value_type
5895 // concept requirements
5896 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5897 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5898 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5900 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5902 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5903 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5904 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5905 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5907 while (__first1
!= __last1
&& __first2
!= __last2
)
5909 if (*__first1
< *__first2
)
5911 *__result
= *__first1
;
5914 else if (*__first2
< *__first1
)
5916 *__result
= *__first2
;
5921 *__result
= *__first1
;
5927 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5932 * @brief Return the union of two sorted ranges using a comparison functor.
5933 * @ingroup set_algorithms
5934 * @param __first1 Start of first range.
5935 * @param __last1 End of first range.
5936 * @param __first2 Start of second range.
5937 * @param __last2 End of second range.
5938 * @param __comp The comparison functor.
5939 * @return End of the output range.
5940 * @ingroup set_algorithms
5942 * This operation iterates over both ranges, copying elements present in
5943 * each range in order to the output range. Iterators increment for each
5944 * range. When the current element of one range is less than the other
5945 * according to @p __comp, that element is copied and the iterator advanced.
5946 * If an equivalent element according to @p __comp is contained in both
5947 * ranges, the element from the first range is copied and both ranges
5948 * advance. The output range may not overlap either input range.
5950 template<typename _InputIterator1
, typename _InputIterator2
,
5951 typename _OutputIterator
, typename _Compare
>
5953 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5954 _InputIterator2 __first2
, _InputIterator2 __last2
,
5955 _OutputIterator __result
, _Compare __comp
)
5957 typedef typename iterator_traits
<_InputIterator1
>::value_type
5959 typedef typename iterator_traits
<_InputIterator2
>::value_type
5962 // concept requirements
5963 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5964 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5965 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5967 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5969 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5970 _ValueType1
, _ValueType2
>)
5971 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5972 _ValueType2
, _ValueType1
>)
5973 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5974 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5976 while (__first1
!= __last1
&& __first2
!= __last2
)
5978 if (__comp(*__first1
, *__first2
))
5980 *__result
= *__first1
;
5983 else if (__comp(*__first2
, *__first1
))
5985 *__result
= *__first2
;
5990 *__result
= *__first1
;
5996 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
6001 * @brief Return the intersection of two sorted ranges.
6002 * @ingroup set_algorithms
6003 * @param __first1 Start of first range.
6004 * @param __last1 End of first range.
6005 * @param __first2 Start of second range.
6006 * @param __last2 End of second range.
6007 * @return End of the output range.
6008 * @ingroup set_algorithms
6010 * This operation iterates over both ranges, copying elements present in
6011 * both ranges in order to the output range. Iterators increment for each
6012 * range. When the current element of one range is less than the other,
6013 * that iterator advances. If an element is contained in both ranges, the
6014 * element from the first range is copied and both ranges advance. The
6015 * output range may not overlap either input range.
6017 template<typename _InputIterator1
, typename _InputIterator2
,
6018 typename _OutputIterator
>
6020 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
6021 _InputIterator2 __first2
, _InputIterator2 __last2
,
6022 _OutputIterator __result
)
6024 typedef typename iterator_traits
<_InputIterator1
>::value_type
6026 typedef typename iterator_traits
<_InputIterator2
>::value_type
6029 // concept requirements
6030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6031 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6032 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6034 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6035 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6036 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6037 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6039 while (__first1
!= __last1
&& __first2
!= __last2
)
6040 if (*__first1
< *__first2
)
6042 else if (*__first2
< *__first1
)
6046 *__result
= *__first1
;
6055 * @brief Return the intersection of two sorted ranges using comparison
6057 * @ingroup set_algorithms
6058 * @param __first1 Start of first range.
6059 * @param __last1 End of first range.
6060 * @param __first2 Start of second range.
6061 * @param __last2 End of second range.
6062 * @param __comp The comparison functor.
6063 * @return End of the output range.
6064 * @ingroup set_algorithms
6066 * This operation iterates over both ranges, copying elements present in
6067 * both ranges in order to the output range. Iterators increment for each
6068 * range. When the current element of one range is less than the other
6069 * according to @p __comp, that iterator advances. If an element is
6070 * contained in both ranges according to @p __comp, the element from the
6071 * first range is copied and both ranges advance. The output range may not
6072 * overlap either input range.
6074 template<typename _InputIterator1
, typename _InputIterator2
,
6075 typename _OutputIterator
, typename _Compare
>
6077 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
6078 _InputIterator2 __first2
, _InputIterator2 __last2
,
6079 _OutputIterator __result
, _Compare __comp
)
6081 typedef typename iterator_traits
<_InputIterator1
>::value_type
6083 typedef typename iterator_traits
<_InputIterator2
>::value_type
6086 // concept requirements
6087 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6088 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6089 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6091 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6092 _ValueType1
, _ValueType2
>)
6093 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6094 _ValueType2
, _ValueType1
>)
6095 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6096 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6098 while (__first1
!= __last1
&& __first2
!= __last2
)
6099 if (__comp(*__first1
, *__first2
))
6101 else if (__comp(*__first2
, *__first1
))
6105 *__result
= *__first1
;
6114 * @brief Return the difference of two sorted ranges.
6115 * @ingroup set_algorithms
6116 * @param __first1 Start of first range.
6117 * @param __last1 End of first range.
6118 * @param __first2 Start of second range.
6119 * @param __last2 End of second range.
6120 * @return End of the output range.
6121 * @ingroup set_algorithms
6123 * This operation iterates over both ranges, copying elements present in
6124 * the first range but not the second in order to the output range.
6125 * Iterators increment for each range. When the current element of the
6126 * first range is less than the second, that element is copied and the
6127 * iterator advances. If the current element of the second range is less,
6128 * the iterator advances, but no element is copied. If an element is
6129 * contained in both ranges, no elements are copied and both ranges
6130 * advance. The output range may not overlap either input range.
6132 template<typename _InputIterator1
, typename _InputIterator2
,
6133 typename _OutputIterator
>
6135 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6136 _InputIterator2 __first2
, _InputIterator2 __last2
,
6137 _OutputIterator __result
)
6139 typedef typename iterator_traits
<_InputIterator1
>::value_type
6141 typedef typename iterator_traits
<_InputIterator2
>::value_type
6144 // concept requirements
6145 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6146 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6147 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6149 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6150 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6151 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6152 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6154 while (__first1
!= __last1
&& __first2
!= __last2
)
6155 if (*__first1
< *__first2
)
6157 *__result
= *__first1
;
6161 else if (*__first2
< *__first1
)
6168 return std::copy(__first1
, __last1
, __result
);
6172 * @brief Return the difference of two sorted ranges using comparison
6174 * @ingroup set_algorithms
6175 * @param __first1 Start of first range.
6176 * @param __last1 End of first range.
6177 * @param __first2 Start of second range.
6178 * @param __last2 End of second range.
6179 * @param __comp The comparison functor.
6180 * @return End of the output range.
6181 * @ingroup set_algorithms
6183 * This operation iterates over both ranges, copying elements present in
6184 * the first range but not the second in order to the output range.
6185 * Iterators increment for each range. When the current element of the
6186 * first range is less than the second according to @p __comp, that element
6187 * is copied and the iterator advances. If the current element of the
6188 * second range is less, no element is copied and the iterator advances.
6189 * If an element is contained in both ranges according to @p __comp, no
6190 * elements are copied and both ranges advance. The output range may not
6191 * overlap either input range.
6193 template<typename _InputIterator1
, typename _InputIterator2
,
6194 typename _OutputIterator
, typename _Compare
>
6196 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6197 _InputIterator2 __first2
, _InputIterator2 __last2
,
6198 _OutputIterator __result
, _Compare __comp
)
6200 typedef typename iterator_traits
<_InputIterator1
>::value_type
6202 typedef typename iterator_traits
<_InputIterator2
>::value_type
6205 // concept requirements
6206 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6207 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6208 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6210 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6211 _ValueType1
, _ValueType2
>)
6212 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6213 _ValueType2
, _ValueType1
>)
6214 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6215 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6217 while (__first1
!= __last1
&& __first2
!= __last2
)
6218 if (__comp(*__first1
, *__first2
))
6220 *__result
= *__first1
;
6224 else if (__comp(*__first2
, *__first1
))
6231 return std::copy(__first1
, __last1
, __result
);
6235 * @brief Return the symmetric difference of two sorted ranges.
6236 * @ingroup set_algorithms
6237 * @param __first1 Start of first range.
6238 * @param __last1 End of first range.
6239 * @param __first2 Start of second range.
6240 * @param __last2 End of second range.
6241 * @return End of the output range.
6242 * @ingroup set_algorithms
6244 * This operation iterates over both ranges, copying elements present in
6245 * one range but not the other in order to the output range. Iterators
6246 * increment for each range. When the current element of one range is less
6247 * than the other, that element is copied and the iterator advances. If an
6248 * element is contained in both ranges, no elements are copied and both
6249 * ranges advance. The output range may not overlap either input range.
6251 template<typename _InputIterator1
, typename _InputIterator2
,
6252 typename _OutputIterator
>
6254 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6255 _InputIterator2 __first2
, _InputIterator2 __last2
,
6256 _OutputIterator __result
)
6258 typedef typename iterator_traits
<_InputIterator1
>::value_type
6260 typedef typename iterator_traits
<_InputIterator2
>::value_type
6263 // concept requirements
6264 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6265 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6266 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6268 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6270 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6271 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6272 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6273 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6275 while (__first1
!= __last1
&& __first2
!= __last2
)
6276 if (*__first1
< *__first2
)
6278 *__result
= *__first1
;
6282 else if (*__first2
< *__first1
)
6284 *__result
= *__first2
;
6293 return std::copy(__first2
, __last2
, std::copy(__first1
,
6294 __last1
, __result
));
6298 * @brief Return the symmetric difference of two sorted ranges using
6299 * comparison functor.
6300 * @ingroup set_algorithms
6301 * @param __first1 Start of first range.
6302 * @param __last1 End of first range.
6303 * @param __first2 Start of second range.
6304 * @param __last2 End of second range.
6305 * @param __comp The comparison functor.
6306 * @return End of the output range.
6307 * @ingroup set_algorithms
6309 * This operation iterates over both ranges, copying elements present in
6310 * one range but not the other in order to the output range. Iterators
6311 * increment for each range. When the current element of one range is less
6312 * than the other according to @p comp, that element is copied and the
6313 * iterator advances. If an element is contained in both ranges according
6314 * to @p __comp, no elements are copied and both ranges advance. The output
6315 * range may not overlap either input range.
6317 template<typename _InputIterator1
, typename _InputIterator2
,
6318 typename _OutputIterator
, typename _Compare
>
6320 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6321 _InputIterator2 __first2
, _InputIterator2 __last2
,
6322 _OutputIterator __result
,
6325 typedef typename iterator_traits
<_InputIterator1
>::value_type
6327 typedef typename iterator_traits
<_InputIterator2
>::value_type
6330 // concept requirements
6331 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6333 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6335 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6337 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6338 _ValueType1
, _ValueType2
>)
6339 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6340 _ValueType2
, _ValueType1
>)
6341 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6342 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6344 while (__first1
!= __last1
&& __first2
!= __last2
)
6345 if (__comp(*__first1
, *__first2
))
6347 *__result
= *__first1
;
6351 else if (__comp(*__first2
, *__first1
))
6353 *__result
= *__first2
;
6362 return std::copy(__first2
, __last2
,
6363 std::copy(__first1
, __last1
, __result
));
6368 * @brief Return the minimum element in a range.
6369 * @ingroup sorting_algorithms
6370 * @param __first Start of range.
6371 * @param __last End of range.
6372 * @return Iterator referencing the first instance of the smallest value.
6374 template<typename _ForwardIterator
>
6376 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6378 // concept requirements
6379 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6380 __glibcxx_function_requires(_LessThanComparableConcept
<
6381 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6382 __glibcxx_requires_valid_range(__first
, __last
);
6384 if (__first
== __last
)
6386 _ForwardIterator __result
= __first
;
6387 while (++__first
!= __last
)
6388 if (*__first
< *__result
)
6394 * @brief Return the minimum element in a range using comparison functor.
6395 * @ingroup sorting_algorithms
6396 * @param __first Start of range.
6397 * @param __last End of range.
6398 * @param __comp Comparison functor.
6399 * @return Iterator referencing the first instance of the smallest value
6400 * according to __comp.
6402 template<typename _ForwardIterator
, typename _Compare
>
6404 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6407 // concept requirements
6408 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6409 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6410 typename iterator_traits
<_ForwardIterator
>::value_type
,
6411 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6412 __glibcxx_requires_valid_range(__first
, __last
);
6414 if (__first
== __last
)
6416 _ForwardIterator __result
= __first
;
6417 while (++__first
!= __last
)
6418 if (__comp(*__first
, *__result
))
6424 * @brief Return the maximum element in a range.
6425 * @ingroup sorting_algorithms
6426 * @param __first Start of range.
6427 * @param __last End of range.
6428 * @return Iterator referencing the first instance of the largest value.
6430 template<typename _ForwardIterator
>
6432 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6434 // concept requirements
6435 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6436 __glibcxx_function_requires(_LessThanComparableConcept
<
6437 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6438 __glibcxx_requires_valid_range(__first
, __last
);
6440 if (__first
== __last
)
6442 _ForwardIterator __result
= __first
;
6443 while (++__first
!= __last
)
6444 if (*__result
< *__first
)
6450 * @brief Return the maximum element in a range using comparison functor.
6451 * @ingroup sorting_algorithms
6452 * @param __first Start of range.
6453 * @param __last End of range.
6454 * @param __comp Comparison functor.
6455 * @return Iterator referencing the first instance of the largest value
6456 * according to __comp.
6458 template<typename _ForwardIterator
, typename _Compare
>
6460 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6463 // concept requirements
6464 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6465 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6466 typename iterator_traits
<_ForwardIterator
>::value_type
,
6467 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6468 __glibcxx_requires_valid_range(__first
, __last
);
6470 if (__first
== __last
) return __first
;
6471 _ForwardIterator __result
= __first
;
6472 while (++__first
!= __last
)
6473 if (__comp(*__result
, *__first
))
6478 _GLIBCXX_END_NAMESPACE_ALGO
6481 #endif /* _STL_ALGO_H */