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 _DistanceType __remainder
= __count
;
390 while (__remainder
<= __tailSize
) // the main loop...
392 __first
+= __remainder
;
393 __tailSize
-= __remainder
;
394 // __first here is always pointing to one past the last element of
395 // next possible match.
396 _RandomAccessIter __backTrack
= __first
;
397 while (*--__backTrack
== __val
)
399 if (--__remainder
== 0)
400 return (__first
- __count
); // Success
402 __remainder
= __count
+ 1 - (__first
- __backTrack
);
404 return __last
; // Failure
410 * This is an uglified
411 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
413 * overloaded for forward iterators.
415 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
416 typename _BinaryPredicate
>
418 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
419 _Integer __count
, const _Tp
& __val
,
420 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
422 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
425 while (__first
!= __last
)
427 typename iterator_traits
<_ForwardIterator
>::difference_type
429 _ForwardIterator __i
= __first
;
431 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
441 while (__first
!= __last
442 && !bool(__binary_pred(*__first
, __val
)))
449 * This is an uglified
450 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
452 * overloaded for random access iterators.
454 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
455 typename _BinaryPredicate
>
457 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
458 _Integer __count
, const _Tp
& __val
,
459 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
462 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
465 _DistanceType __tailSize
= __last
- __first
;
466 _DistanceType __remainder
= __count
;
468 while (__remainder
<= __tailSize
) // the main loop...
470 __first
+= __remainder
;
471 __tailSize
-= __remainder
;
472 // __first here is always pointing to one past the last element of
473 // next possible match.
474 _RandomAccessIter __backTrack
= __first
;
475 while (__binary_pred(*--__backTrack
, __val
))
477 if (--__remainder
== 0)
478 return (__first
- __count
); // Success
480 __remainder
= __count
+ 1 - (__first
- __backTrack
);
482 return __last
; // Failure
485 // find_end for forward iterators.
486 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
488 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
489 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
490 forward_iterator_tag
, forward_iterator_tag
)
492 if (__first2
== __last2
)
496 _ForwardIterator1 __result
= __last1
;
499 _ForwardIterator1 __new_result
500 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
, __last2
);
501 if (__new_result
== __last1
)
505 __result
= __new_result
;
506 __first1
= __new_result
;
513 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
514 typename _BinaryPredicate
>
516 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
517 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
518 forward_iterator_tag
, forward_iterator_tag
,
519 _BinaryPredicate __comp
)
521 if (__first2
== __last2
)
525 _ForwardIterator1 __result
= __last1
;
528 _ForwardIterator1 __new_result
529 = _GLIBCXX_STD_A::search(__first1
, __last1
, __first2
,
531 if (__new_result
== __last1
)
535 __result
= __new_result
;
536 __first1
= __new_result
;
543 // find_end for bidirectional iterators (much faster).
544 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
545 _BidirectionalIterator1
546 __find_end(_BidirectionalIterator1 __first1
,
547 _BidirectionalIterator1 __last1
,
548 _BidirectionalIterator2 __first2
,
549 _BidirectionalIterator2 __last2
,
550 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
552 // concept requirements
553 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
554 _BidirectionalIterator1
>)
555 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
556 _BidirectionalIterator2
>)
558 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
559 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
561 _RevIterator1
__rlast1(__first1
);
562 _RevIterator2
__rlast2(__first2
);
563 _RevIterator1 __rresult
= _GLIBCXX_STD_A::search(_RevIterator1(__last1
),
565 _RevIterator2(__last2
),
568 if (__rresult
== __rlast1
)
572 _BidirectionalIterator1 __result
= __rresult
.base();
573 std::advance(__result
, -std::distance(__first2
, __last2
));
578 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
579 typename _BinaryPredicate
>
580 _BidirectionalIterator1
581 __find_end(_BidirectionalIterator1 __first1
,
582 _BidirectionalIterator1 __last1
,
583 _BidirectionalIterator2 __first2
,
584 _BidirectionalIterator2 __last2
,
585 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
586 _BinaryPredicate __comp
)
588 // concept requirements
589 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
590 _BidirectionalIterator1
>)
591 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
592 _BidirectionalIterator2
>)
594 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
595 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
597 _RevIterator1
__rlast1(__first1
);
598 _RevIterator2
__rlast2(__first2
);
599 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
600 _RevIterator2(__last2
), __rlast2
,
603 if (__rresult
== __rlast1
)
607 _BidirectionalIterator1 __result
= __rresult
.base();
608 std::advance(__result
, -std::distance(__first2
, __last2
));
614 * @brief Find last matching subsequence in a sequence.
615 * @ingroup non_mutating_algorithms
616 * @param __first1 Start of range to search.
617 * @param __last1 End of range to search.
618 * @param __first2 Start of sequence to match.
619 * @param __last2 End of sequence to match.
620 * @return The last iterator @c i in the range
621 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
622 * @p *(__first2+N) for each @c N in the range @p
623 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
625 * Searches the range @p [__first1,__last1) for a sub-sequence that
626 * compares equal value-by-value with the sequence given by @p
627 * [__first2,__last2) and returns an iterator to the __first
628 * element of the sub-sequence, or @p __last1 if the sub-sequence
629 * is not found. The sub-sequence will be the last such
630 * subsequence contained in [__first,__last1).
632 * Because the sub-sequence must lie completely within the range @p
633 * [__first1,__last1) it must start at a position less than @p
634 * __last1-(__last2-__first2) where @p __last2-__first2 is the
635 * length of the sub-sequence. This means that the returned
636 * iterator @c i will be in the range @p
637 * [__first1,__last1-(__last2-__first2))
639 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
640 inline _ForwardIterator1
641 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
642 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
644 // concept requirements
645 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
646 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
647 __glibcxx_function_requires(_EqualOpConcept
<
648 typename iterator_traits
<_ForwardIterator1
>::value_type
,
649 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
650 __glibcxx_requires_valid_range(__first1
, __last1
);
651 __glibcxx_requires_valid_range(__first2
, __last2
);
653 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
654 std::__iterator_category(__first1
),
655 std::__iterator_category(__first2
));
659 * @brief Find last matching subsequence in a sequence using a predicate.
660 * @ingroup non_mutating_algorithms
661 * @param __first1 Start of range to search.
662 * @param __last1 End of range to search.
663 * @param __first2 Start of sequence to match.
664 * @param __last2 End of sequence to match.
665 * @param __comp The predicate to use.
666 * @return The last iterator @c i in the range @p
667 * [__first1,__last1-(__last2-__first2)) such that @c
668 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
669 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
672 * Searches the range @p [__first1,__last1) for a sub-sequence that
673 * compares equal value-by-value with the sequence given by @p
674 * [__first2,__last2) using comp as a predicate and returns an
675 * iterator to the first element of the sub-sequence, or @p __last1
676 * if the sub-sequence is not found. The sub-sequence will be the
677 * last such subsequence contained in [__first,__last1).
679 * Because the sub-sequence must lie completely within the range @p
680 * [__first1,__last1) it must start at a position less than @p
681 * __last1-(__last2-__first2) where @p __last2-__first2 is the
682 * length of the sub-sequence. This means that the returned
683 * iterator @c i will be in the range @p
684 * [__first1,__last1-(__last2-__first2))
686 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
687 typename _BinaryPredicate
>
688 inline _ForwardIterator1
689 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
690 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
691 _BinaryPredicate __comp
)
693 // concept requirements
694 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
695 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
696 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
697 typename iterator_traits
<_ForwardIterator1
>::value_type
,
698 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
699 __glibcxx_requires_valid_range(__first1
, __last1
);
700 __glibcxx_requires_valid_range(__first2
, __last2
);
702 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
703 std::__iterator_category(__first1
),
704 std::__iterator_category(__first2
),
708 #if __cplusplus >= 201103L
710 * @brief Checks that a predicate is true for all the elements
712 * @ingroup non_mutating_algorithms
713 * @param __first An input iterator.
714 * @param __last An input iterator.
715 * @param __pred A predicate.
716 * @return True if the check is true, false otherwise.
718 * Returns true if @p __pred is true for each element in the range
719 * @p [__first,__last), and false otherwise.
721 template<typename _InputIterator
, typename _Predicate
>
723 all_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
724 { return __last
== std::find_if_not(__first
, __last
, __pred
); }
727 * @brief Checks that a predicate is false for all the elements
729 * @ingroup non_mutating_algorithms
730 * @param __first An input iterator.
731 * @param __last An input iterator.
732 * @param __pred A predicate.
733 * @return True if the check is true, false otherwise.
735 * Returns true if @p __pred is false for each element in the range
736 * @p [__first,__last), and false otherwise.
738 template<typename _InputIterator
, typename _Predicate
>
740 none_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
741 { return __last
== _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
); }
744 * @brief Checks that a predicate is false for at least an element
746 * @ingroup non_mutating_algorithms
747 * @param __first An input iterator.
748 * @param __last An input iterator.
749 * @param __pred A predicate.
750 * @return True if the check is true, false otherwise.
752 * Returns true if an element exists in the range @p
753 * [__first,__last) such that @p __pred is true, and false
756 template<typename _InputIterator
, typename _Predicate
>
758 any_of(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
759 { return !std::none_of(__first
, __last
, __pred
); }
762 * @brief Find the first element in a sequence for which a
763 * predicate is false.
764 * @ingroup non_mutating_algorithms
765 * @param __first An input iterator.
766 * @param __last An input iterator.
767 * @param __pred A predicate.
768 * @return The first iterator @c i in the range @p [__first,__last)
769 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
771 template<typename _InputIterator
, typename _Predicate
>
772 inline _InputIterator
773 find_if_not(_InputIterator __first
, _InputIterator __last
,
776 // concept requirements
777 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
778 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
779 typename iterator_traits
<_InputIterator
>::value_type
>)
780 __glibcxx_requires_valid_range(__first
, __last
);
781 return std::__find_if_not(__first
, __last
, __pred
);
785 * @brief Checks whether the sequence is partitioned.
786 * @ingroup mutating_algorithms
787 * @param __first An input iterator.
788 * @param __last An input iterator.
789 * @param __pred A predicate.
790 * @return True if the range @p [__first,__last) is partioned by @p __pred,
791 * i.e. if all elements that satisfy @p __pred appear before those that
794 template<typename _InputIterator
, typename _Predicate
>
796 is_partitioned(_InputIterator __first
, _InputIterator __last
,
799 __first
= std::find_if_not(__first
, __last
, __pred
);
800 return std::none_of(__first
, __last
, __pred
);
804 * @brief Find the partition point of a partitioned range.
805 * @ingroup mutating_algorithms
806 * @param __first An iterator.
807 * @param __last Another iterator.
808 * @param __pred A predicate.
809 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
810 * and @p none_of(mid, __last, __pred) are both true.
812 template<typename _ForwardIterator
, typename _Predicate
>
814 partition_point(_ForwardIterator __first
, _ForwardIterator __last
,
817 // concept requirements
818 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
819 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
820 typename iterator_traits
<_ForwardIterator
>::value_type
>)
822 // A specific debug-mode test will be necessary...
823 __glibcxx_requires_valid_range(__first
, __last
);
825 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
828 _DistanceType __len
= std::distance(__first
, __last
);
829 _DistanceType __half
;
830 _ForwardIterator __middle
;
836 std::advance(__middle
, __half
);
837 if (__pred(*__middle
))
841 __len
= __len
- __half
- 1;
852 * @brief Copy a sequence, removing elements of a given value.
853 * @ingroup mutating_algorithms
854 * @param __first An input iterator.
855 * @param __last An input iterator.
856 * @param __result An output iterator.
857 * @param __value The value to be removed.
858 * @return An iterator designating the end of the resulting sequence.
860 * Copies each element in the range @p [__first,__last) not equal
861 * to @p __value to the range beginning at @p __result.
862 * remove_copy() is stable, so the relative order of elements that
863 * are copied is unchanged.
865 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
867 remove_copy(_InputIterator __first
, _InputIterator __last
,
868 _OutputIterator __result
, const _Tp
& __value
)
870 // concept requirements
871 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
872 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
873 typename iterator_traits
<_InputIterator
>::value_type
>)
874 __glibcxx_function_requires(_EqualOpConcept
<
875 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
876 __glibcxx_requires_valid_range(__first
, __last
);
878 for (; __first
!= __last
; ++__first
)
879 if (!(*__first
== __value
))
881 *__result
= *__first
;
888 * @brief Copy a sequence, removing elements for which a predicate is true.
889 * @ingroup mutating_algorithms
890 * @param __first An input iterator.
891 * @param __last An input iterator.
892 * @param __result An output iterator.
893 * @param __pred A predicate.
894 * @return An iterator designating the end of the resulting sequence.
896 * Copies each element in the range @p [__first,__last) for which
897 * @p __pred returns false to the range beginning at @p __result.
899 * remove_copy_if() is stable, so the relative order of elements that are
900 * copied is unchanged.
902 template<typename _InputIterator
, typename _OutputIterator
,
905 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
906 _OutputIterator __result
, _Predicate __pred
)
908 // concept requirements
909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
910 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
911 typename iterator_traits
<_InputIterator
>::value_type
>)
912 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
913 typename iterator_traits
<_InputIterator
>::value_type
>)
914 __glibcxx_requires_valid_range(__first
, __last
);
916 for (; __first
!= __last
; ++__first
)
917 if (!bool(__pred(*__first
)))
919 *__result
= *__first
;
925 #if __cplusplus >= 201103L
927 * @brief Copy the elements of a sequence for which a predicate is true.
928 * @ingroup mutating_algorithms
929 * @param __first An input iterator.
930 * @param __last An input iterator.
931 * @param __result An output iterator.
932 * @param __pred A predicate.
933 * @return An iterator designating the end of the resulting sequence.
935 * Copies each element in the range @p [__first,__last) for which
936 * @p __pred returns true to the range beginning at @p __result.
938 * copy_if() is stable, so the relative order of elements that are
939 * copied is unchanged.
941 template<typename _InputIterator
, typename _OutputIterator
,
944 copy_if(_InputIterator __first
, _InputIterator __last
,
945 _OutputIterator __result
, _Predicate __pred
)
947 // concept requirements
948 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
949 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
950 typename iterator_traits
<_InputIterator
>::value_type
>)
951 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
952 typename iterator_traits
<_InputIterator
>::value_type
>)
953 __glibcxx_requires_valid_range(__first
, __last
);
955 for (; __first
!= __last
; ++__first
)
956 if (__pred(*__first
))
958 *__result
= *__first
;
965 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
967 __copy_n(_InputIterator __first
, _Size __n
,
968 _OutputIterator __result
, input_iterator_tag
)
974 *__result
= *__first
;
985 template<typename _RandomAccessIterator
, typename _Size
,
986 typename _OutputIterator
>
987 inline _OutputIterator
988 __copy_n(_RandomAccessIterator __first
, _Size __n
,
989 _OutputIterator __result
, random_access_iterator_tag
)
990 { return std::copy(__first
, __first
+ __n
, __result
); }
993 * @brief Copies the range [first,first+n) into [result,result+n).
994 * @ingroup mutating_algorithms
995 * @param __first An input iterator.
996 * @param __n The number of elements to copy.
997 * @param __result An output iterator.
1000 * This inline function will boil down to a call to @c memmove whenever
1001 * possible. Failing that, if random access iterators are passed, then the
1002 * loop count will be known (and therefore a candidate for compiler
1003 * optimizations such as unrolling).
1005 template<typename _InputIterator
, typename _Size
, typename _OutputIterator
>
1006 inline _OutputIterator
1007 copy_n(_InputIterator __first
, _Size __n
, _OutputIterator __result
)
1009 // concept requirements
1010 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1011 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1012 typename iterator_traits
<_InputIterator
>::value_type
>)
1014 return std::__copy_n(__first
, __n
, __result
,
1015 std::__iterator_category(__first
));
1019 * @brief Copy the elements of a sequence to separate output sequences
1020 * depending on the truth value of a predicate.
1021 * @ingroup mutating_algorithms
1022 * @param __first An input iterator.
1023 * @param __last An input iterator.
1024 * @param __out_true An output iterator.
1025 * @param __out_false An output iterator.
1026 * @param __pred A predicate.
1027 * @return A pair designating the ends of the resulting sequences.
1029 * Copies each element in the range @p [__first,__last) for which
1030 * @p __pred returns true to the range beginning at @p out_true
1031 * and each element for which @p __pred returns false to @p __out_false.
1033 template<typename _InputIterator
, typename _OutputIterator1
,
1034 typename _OutputIterator2
, typename _Predicate
>
1035 pair
<_OutputIterator1
, _OutputIterator2
>
1036 partition_copy(_InputIterator __first
, _InputIterator __last
,
1037 _OutputIterator1 __out_true
, _OutputIterator2 __out_false
,
1040 // concept requirements
1041 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1042 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator1
,
1043 typename iterator_traits
<_InputIterator
>::value_type
>)
1044 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator2
,
1045 typename iterator_traits
<_InputIterator
>::value_type
>)
1046 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1047 typename iterator_traits
<_InputIterator
>::value_type
>)
1048 __glibcxx_requires_valid_range(__first
, __last
);
1050 for (; __first
!= __last
; ++__first
)
1051 if (__pred(*__first
))
1053 *__out_true
= *__first
;
1058 *__out_false
= *__first
;
1062 return pair
<_OutputIterator1
, _OutputIterator2
>(__out_true
, __out_false
);
1067 * @brief Remove elements from a sequence.
1068 * @ingroup mutating_algorithms
1069 * @param __first An input iterator.
1070 * @param __last An input iterator.
1071 * @param __value The value to be removed.
1072 * @return An iterator designating the end of the resulting sequence.
1074 * All elements equal to @p __value are removed from the range
1075 * @p [__first,__last).
1077 * remove() is stable, so the relative order of elements that are
1078 * not removed is unchanged.
1080 * Elements between the end of the resulting sequence and @p __last
1081 * are still present, but their value is unspecified.
1083 template<typename _ForwardIterator
, typename _Tp
>
1085 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1088 // concept requirements
1089 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1091 __glibcxx_function_requires(_EqualOpConcept
<
1092 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1093 __glibcxx_requires_valid_range(__first
, __last
);
1095 __first
= _GLIBCXX_STD_A::find(__first
, __last
, __value
);
1096 if(__first
== __last
)
1098 _ForwardIterator __result
= __first
;
1100 for(; __first
!= __last
; ++__first
)
1101 if(!(*__first
== __value
))
1103 *__result
= _GLIBCXX_MOVE(*__first
);
1110 * @brief Remove elements from a sequence using a predicate.
1111 * @ingroup mutating_algorithms
1112 * @param __first A forward iterator.
1113 * @param __last A forward iterator.
1114 * @param __pred A predicate.
1115 * @return An iterator designating the end of the resulting sequence.
1117 * All elements for which @p __pred returns true are removed from the range
1118 * @p [__first,__last).
1120 * remove_if() is stable, so the relative order of elements that are
1121 * not removed is unchanged.
1123 * Elements between the end of the resulting sequence and @p __last
1124 * are still present, but their value is unspecified.
1126 template<typename _ForwardIterator
, typename _Predicate
>
1128 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1131 // concept requirements
1132 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1134 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1135 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1136 __glibcxx_requires_valid_range(__first
, __last
);
1138 __first
= _GLIBCXX_STD_A::find_if(__first
, __last
, __pred
);
1139 if(__first
== __last
)
1141 _ForwardIterator __result
= __first
;
1143 for(; __first
!= __last
; ++__first
)
1144 if(!bool(__pred(*__first
)))
1146 *__result
= _GLIBCXX_MOVE(*__first
);
1153 * @brief Remove consecutive duplicate values from a sequence.
1154 * @ingroup mutating_algorithms
1155 * @param __first A forward iterator.
1156 * @param __last A forward iterator.
1157 * @return An iterator designating the end of the resulting sequence.
1159 * Removes all but the first element from each group of consecutive
1160 * values that compare equal.
1161 * unique() is stable, so the relative order of elements that are
1162 * not removed is unchanged.
1163 * Elements between the end of the resulting sequence and @p __last
1164 * are still present, but their value is unspecified.
1166 template<typename _ForwardIterator
>
1168 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1170 // concept requirements
1171 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1173 __glibcxx_function_requires(_EqualityComparableConcept
<
1174 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1175 __glibcxx_requires_valid_range(__first
, __last
);
1177 // Skip the beginning, if already unique.
1178 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
);
1179 if (__first
== __last
)
1182 // Do the real copy work.
1183 _ForwardIterator __dest
= __first
;
1185 while (++__first
!= __last
)
1186 if (!(*__dest
== *__first
))
1187 *++__dest
= _GLIBCXX_MOVE(*__first
);
1192 * @brief Remove consecutive values from a sequence using a predicate.
1193 * @ingroup mutating_algorithms
1194 * @param __first A forward iterator.
1195 * @param __last A forward iterator.
1196 * @param __binary_pred A binary predicate.
1197 * @return An iterator designating the end of the resulting sequence.
1199 * Removes all but the first element from each group of consecutive
1200 * values for which @p __binary_pred returns true.
1201 * unique() is stable, so the relative order of elements that are
1202 * not removed is unchanged.
1203 * Elements between the end of the resulting sequence and @p __last
1204 * are still present, but their value is unspecified.
1206 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1208 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1209 _BinaryPredicate __binary_pred
)
1211 // concept requirements
1212 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1214 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1215 typename iterator_traits
<_ForwardIterator
>::value_type
,
1216 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1217 __glibcxx_requires_valid_range(__first
, __last
);
1219 // Skip the beginning, if already unique.
1220 __first
= _GLIBCXX_STD_A::adjacent_find(__first
, __last
, __binary_pred
);
1221 if (__first
== __last
)
1224 // Do the real copy work.
1225 _ForwardIterator __dest
= __first
;
1227 while (++__first
!= __last
)
1228 if (!bool(__binary_pred(*__dest
, *__first
)))
1229 *++__dest
= _GLIBCXX_MOVE(*__first
);
1234 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1236 * overloaded for forward iterators and output iterator as result.
1238 template<typename _ForwardIterator
, typename _OutputIterator
>
1240 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1241 _OutputIterator __result
,
1242 forward_iterator_tag
, output_iterator_tag
)
1244 // concept requirements -- taken care of in dispatching function
1245 _ForwardIterator __next
= __first
;
1246 *__result
= *__first
;
1247 while (++__next
!= __last
)
1248 if (!(*__first
== *__next
))
1251 *++__result
= *__first
;
1257 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1259 * overloaded for input iterators and output iterator as result.
1261 template<typename _InputIterator
, typename _OutputIterator
>
1263 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1264 _OutputIterator __result
,
1265 input_iterator_tag
, output_iterator_tag
)
1267 // concept requirements -- taken care of in dispatching function
1268 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1269 *__result
= __value
;
1270 while (++__first
!= __last
)
1271 if (!(__value
== *__first
))
1274 *++__result
= __value
;
1280 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1282 * overloaded for input iterators and forward iterator as result.
1284 template<typename _InputIterator
, typename _ForwardIterator
>
1286 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1287 _ForwardIterator __result
,
1288 input_iterator_tag
, forward_iterator_tag
)
1290 // concept requirements -- taken care of in dispatching function
1291 *__result
= *__first
;
1292 while (++__first
!= __last
)
1293 if (!(*__result
== *__first
))
1294 *++__result
= *__first
;
1299 * This is an uglified
1300 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1302 * overloaded for forward iterators and output iterator as result.
1304 template<typename _ForwardIterator
, typename _OutputIterator
,
1305 typename _BinaryPredicate
>
1307 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1308 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1309 forward_iterator_tag
, output_iterator_tag
)
1311 // concept requirements -- iterators already checked
1312 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1313 typename iterator_traits
<_ForwardIterator
>::value_type
,
1314 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1316 _ForwardIterator __next
= __first
;
1317 *__result
= *__first
;
1318 while (++__next
!= __last
)
1319 if (!bool(__binary_pred(*__first
, *__next
)))
1322 *++__result
= *__first
;
1328 * This is an uglified
1329 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1331 * overloaded for input iterators and output iterator as result.
1333 template<typename _InputIterator
, typename _OutputIterator
,
1334 typename _BinaryPredicate
>
1336 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1337 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1338 input_iterator_tag
, output_iterator_tag
)
1340 // concept requirements -- iterators already checked
1341 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1342 typename iterator_traits
<_InputIterator
>::value_type
,
1343 typename iterator_traits
<_InputIterator
>::value_type
>)
1345 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1346 *__result
= __value
;
1347 while (++__first
!= __last
)
1348 if (!bool(__binary_pred(__value
, *__first
)))
1351 *++__result
= __value
;
1357 * This is an uglified
1358 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1360 * overloaded for input iterators and forward iterator as result.
1362 template<typename _InputIterator
, typename _ForwardIterator
,
1363 typename _BinaryPredicate
>
1365 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1366 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1367 input_iterator_tag
, forward_iterator_tag
)
1369 // concept requirements -- iterators already checked
1370 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1371 typename iterator_traits
<_ForwardIterator
>::value_type
,
1372 typename iterator_traits
<_InputIterator
>::value_type
>)
1374 *__result
= *__first
;
1375 while (++__first
!= __last
)
1376 if (!bool(__binary_pred(*__result
, *__first
)))
1377 *++__result
= *__first
;
1382 * This is an uglified reverse(_BidirectionalIterator,
1383 * _BidirectionalIterator)
1384 * overloaded for bidirectional iterators.
1386 template<typename _BidirectionalIterator
>
1388 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1389 bidirectional_iterator_tag
)
1392 if (__first
== __last
|| __first
== --__last
)
1396 std::iter_swap(__first
, __last
);
1402 * This is an uglified reverse(_BidirectionalIterator,
1403 * _BidirectionalIterator)
1404 * overloaded for random access iterators.
1406 template<typename _RandomAccessIterator
>
1408 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1409 random_access_iterator_tag
)
1411 if (__first
== __last
)
1414 while (__first
< __last
)
1416 std::iter_swap(__first
, __last
);
1423 * @brief Reverse a sequence.
1424 * @ingroup mutating_algorithms
1425 * @param __first A bidirectional iterator.
1426 * @param __last A bidirectional iterator.
1427 * @return reverse() returns no value.
1429 * Reverses the order of the elements in the range @p [__first,__last),
1430 * so that the first element becomes the last etc.
1431 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1432 * swaps @p *(__first+i) and @p *(__last-(i+1))
1434 template<typename _BidirectionalIterator
>
1436 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1438 // concept requirements
1439 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1440 _BidirectionalIterator
>)
1441 __glibcxx_requires_valid_range(__first
, __last
);
1442 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1446 * @brief Copy a sequence, reversing its elements.
1447 * @ingroup mutating_algorithms
1448 * @param __first A bidirectional iterator.
1449 * @param __last A bidirectional iterator.
1450 * @param __result An output iterator.
1451 * @return An iterator designating the end of the resulting sequence.
1453 * Copies the elements in the range @p [__first,__last) to the
1454 * range @p [__result,__result+(__last-__first)) such that the
1455 * order of the elements is reversed. For every @c i such that @p
1456 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1457 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1458 * The ranges @p [__first,__last) and @p
1459 * [__result,__result+(__last-__first)) must not overlap.
1461 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1463 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1464 _OutputIterator __result
)
1466 // concept requirements
1467 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1468 _BidirectionalIterator
>)
1469 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1470 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1471 __glibcxx_requires_valid_range(__first
, __last
);
1473 while (__first
!= __last
)
1476 *__result
= *__last
;
1483 * This is a helper function for the rotate algorithm specialized on RAIs.
1484 * It returns the greatest common divisor of two integer values.
1486 template<typename _EuclideanRingElement
>
1487 _EuclideanRingElement
1488 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1492 _EuclideanRingElement __t
= __m
% __n
;
1499 /// This is a helper function for the rotate algorithm.
1500 template<typename _ForwardIterator
>
1502 __rotate(_ForwardIterator __first
,
1503 _ForwardIterator __middle
,
1504 _ForwardIterator __last
,
1505 forward_iterator_tag
)
1507 if (__first
== __middle
|| __last
== __middle
)
1510 _ForwardIterator __first2
= __middle
;
1513 std::iter_swap(__first
, __first2
);
1516 if (__first
== __middle
)
1517 __middle
= __first2
;
1519 while (__first2
!= __last
);
1521 __first2
= __middle
;
1523 while (__first2
!= __last
)
1525 std::iter_swap(__first
, __first2
);
1528 if (__first
== __middle
)
1529 __middle
= __first2
;
1530 else if (__first2
== __last
)
1531 __first2
= __middle
;
1535 /// This is a helper function for the rotate algorithm.
1536 template<typename _BidirectionalIterator
>
1538 __rotate(_BidirectionalIterator __first
,
1539 _BidirectionalIterator __middle
,
1540 _BidirectionalIterator __last
,
1541 bidirectional_iterator_tag
)
1543 // concept requirements
1544 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1545 _BidirectionalIterator
>)
1547 if (__first
== __middle
|| __last
== __middle
)
1550 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1551 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1553 while (__first
!= __middle
&& __middle
!= __last
)
1555 std::iter_swap(__first
, --__last
);
1559 if (__first
== __middle
)
1560 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1562 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1565 /// This is a helper function for the rotate algorithm.
1566 template<typename _RandomAccessIterator
>
1568 __rotate(_RandomAccessIterator __first
,
1569 _RandomAccessIterator __middle
,
1570 _RandomAccessIterator __last
,
1571 random_access_iterator_tag
)
1573 // concept requirements
1574 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1575 _RandomAccessIterator
>)
1577 if (__first
== __middle
|| __last
== __middle
)
1580 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1582 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1585 _Distance __n
= __last
- __first
;
1586 _Distance __k
= __middle
- __first
;
1588 if (__k
== __n
- __k
)
1590 std::swap_ranges(__first
, __middle
, __middle
);
1594 _RandomAccessIterator __p
= __first
;
1598 if (__k
< __n
- __k
)
1600 if (__is_pod(_ValueType
) && __k
== 1)
1602 _ValueType __t
= _GLIBCXX_MOVE(*__p
);
1603 _GLIBCXX_MOVE3(__p
+ 1, __p
+ __n
, __p
);
1604 *(__p
+ __n
- 1) = _GLIBCXX_MOVE(__t
);
1607 _RandomAccessIterator __q
= __p
+ __k
;
1608 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1610 std::iter_swap(__p
, __q
);
1617 std::swap(__n
, __k
);
1623 if (__is_pod(_ValueType
) && __k
== 1)
1625 _ValueType __t
= _GLIBCXX_MOVE(*(__p
+ __n
- 1));
1626 _GLIBCXX_MOVE_BACKWARD3(__p
, __p
+ __n
- 1, __p
+ __n
);
1627 *__p
= _GLIBCXX_MOVE(__t
);
1630 _RandomAccessIterator __q
= __p
+ __n
;
1632 for (_Distance __i
= 0; __i
< __n
- __k
; ++ __i
)
1636 std::iter_swap(__p
, __q
);
1641 std::swap(__n
, __k
);
1647 * @brief Rotate the elements of a sequence.
1648 * @ingroup mutating_algorithms
1649 * @param __first A forward iterator.
1650 * @param __middle A forward iterator.
1651 * @param __last A forward iterator.
1654 * Rotates the elements of the range @p [__first,__last) by
1655 * @p (__middle - __first) positions so that the element at @p __middle
1656 * is moved to @p __first, the element at @p __middle+1 is moved to
1657 * @p __first+1 and so on for each element in the range
1658 * @p [__first,__last).
1660 * This effectively swaps the ranges @p [__first,__middle) and
1661 * @p [__middle,__last).
1664 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1665 * for each @p n in the range @p [0,__last-__first).
1667 template<typename _ForwardIterator
>
1669 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1670 _ForwardIterator __last
)
1672 // concept requirements
1673 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1675 __glibcxx_requires_valid_range(__first
, __middle
);
1676 __glibcxx_requires_valid_range(__middle
, __last
);
1678 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1680 std::__rotate(__first
, __middle
, __last
, _IterType());
1684 * @brief Copy a sequence, rotating its elements.
1685 * @ingroup mutating_algorithms
1686 * @param __first A forward iterator.
1687 * @param __middle A forward iterator.
1688 * @param __last A forward iterator.
1689 * @param __result An output iterator.
1690 * @return An iterator designating the end of the resulting sequence.
1692 * Copies the elements of the range @p [__first,__last) to the
1693 * range beginning at @result, rotating the copied elements by
1694 * @p (__middle-__first) positions so that the element at @p __middle
1695 * is moved to @p __result, the element at @p __middle+1 is moved
1696 * to @p __result+1 and so on for each element in the range @p
1700 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1701 * for each @p n in the range @p [0,__last-__first).
1703 template<typename _ForwardIterator
, typename _OutputIterator
>
1705 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1706 _ForwardIterator __last
, _OutputIterator __result
)
1708 // concept requirements
1709 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1710 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1711 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1712 __glibcxx_requires_valid_range(__first
, __middle
);
1713 __glibcxx_requires_valid_range(__middle
, __last
);
1715 return std::copy(__first
, __middle
,
1716 std::copy(__middle
, __last
, __result
));
1719 /// This is a helper function...
1720 template<typename _ForwardIterator
, typename _Predicate
>
1722 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1723 _Predicate __pred
, forward_iterator_tag
)
1725 if (__first
== __last
)
1728 while (__pred(*__first
))
1729 if (++__first
== __last
)
1732 _ForwardIterator __next
= __first
;
1734 while (++__next
!= __last
)
1735 if (__pred(*__next
))
1737 std::iter_swap(__first
, __next
);
1744 /// This is a helper function...
1745 template<typename _BidirectionalIterator
, typename _Predicate
>
1746 _BidirectionalIterator
1747 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1748 _Predicate __pred
, bidirectional_iterator_tag
)
1753 if (__first
== __last
)
1755 else if (__pred(*__first
))
1761 if (__first
== __last
)
1763 else if (!bool(__pred(*__last
)))
1767 std::iter_swap(__first
, __last
);
1774 /// This is a helper function...
1775 /// Requires __len != 0 and !__pred(*__first),
1776 /// same as __stable_partition_adaptive.
1777 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1779 __inplace_stable_partition(_ForwardIterator __first
,
1780 _Predicate __pred
, _Distance __len
)
1784 _ForwardIterator __middle
= __first
;
1785 std::advance(__middle
, __len
/ 2);
1786 _ForwardIterator __left_split
=
1787 std::__inplace_stable_partition(__first
, __pred
, __len
/ 2);
1788 // Advance past true-predicate values to satisfy this
1789 // function's preconditions.
1790 _Distance __right_len
= __len
- __len
/ 2;
1791 _ForwardIterator __right_split
=
1792 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1794 __right_split
= std::__inplace_stable_partition(__middle
,
1797 std::rotate(__left_split
, __middle
, __right_split
);
1798 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1799 return __left_split
;
1802 /// This is a helper function...
1803 /// Requires __first != __last and !__pred(*__first)
1804 /// and __len == distance(__first, __last).
1806 /// !__pred(*__first) allows us to guarantee that we don't
1807 /// move-assign an element onto itself.
1808 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1811 __stable_partition_adaptive(_ForwardIterator __first
,
1812 _ForwardIterator __last
,
1813 _Predicate __pred
, _Distance __len
,
1815 _Distance __buffer_size
)
1817 if (__len
<= __buffer_size
)
1819 _ForwardIterator __result1
= __first
;
1820 _Pointer __result2
= __buffer
;
1821 // The precondition guarantees that !__pred(*__first), so
1822 // move that element to the buffer before starting the loop.
1823 // This ensures that we only call __pred once per element.
1824 *__result2
= _GLIBCXX_MOVE(*__first
);
1827 for (; __first
!= __last
; ++__first
)
1828 if (__pred(*__first
))
1830 *__result1
= _GLIBCXX_MOVE(*__first
);
1835 *__result2
= _GLIBCXX_MOVE(*__first
);
1838 _GLIBCXX_MOVE3(__buffer
, __result2
, __result1
);
1843 _ForwardIterator __middle
= __first
;
1844 std::advance(__middle
, __len
/ 2);
1845 _ForwardIterator __left_split
=
1846 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1847 __len
/ 2, __buffer
,
1849 // Advance past true-predicate values to satisfy this
1850 // function's preconditions.
1851 _Distance __right_len
= __len
- __len
/ 2;
1852 _ForwardIterator __right_split
=
1853 std::__find_if_not_n(__middle
, __right_len
, __pred
);
1856 std::__stable_partition_adaptive(__right_split
, __last
, __pred
,
1858 __buffer
, __buffer_size
);
1859 std::rotate(__left_split
, __middle
, __right_split
);
1860 std::advance(__left_split
, std::distance(__middle
, __right_split
));
1861 return __left_split
;
1866 * @brief Move elements for which a predicate is true to the beginning
1867 * of a sequence, preserving relative ordering.
1868 * @ingroup mutating_algorithms
1869 * @param __first A forward iterator.
1870 * @param __last A forward iterator.
1871 * @param __pred A predicate functor.
1872 * @return An iterator @p middle such that @p __pred(i) is true for each
1873 * iterator @p i in the range @p [first,middle) and false for each @p i
1874 * in the range @p [middle,last).
1876 * Performs the same function as @p partition() with the additional
1877 * guarantee that the relative ordering of elements in each group is
1878 * preserved, so any two elements @p x and @p y in the range
1879 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1880 * relative ordering after calling @p stable_partition().
1882 template<typename _ForwardIterator
, typename _Predicate
>
1884 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1887 // concept requirements
1888 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1890 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1891 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1892 __glibcxx_requires_valid_range(__first
, __last
);
1894 __first
= std::__find_if_not(__first
, __last
, __pred
);
1896 if (__first
== __last
)
1900 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1902 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1905 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1907 if (__buf
.size() > 0)
1909 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1910 _DistanceType(__buf
.requested_size()),
1912 _DistanceType(__buf
.size()));
1915 std::__inplace_stable_partition(__first
, __pred
,
1916 _DistanceType(__buf
.requested_size()));
1920 /// This is a helper function for the sort routines.
1921 template<typename _RandomAccessIterator
>
1923 __heap_select(_RandomAccessIterator __first
,
1924 _RandomAccessIterator __middle
,
1925 _RandomAccessIterator __last
)
1927 std::make_heap(__first
, __middle
);
1928 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1929 if (*__i
< *__first
)
1930 std::__pop_heap(__first
, __middle
, __i
);
1933 /// This is a helper function for the sort routines.
1934 template<typename _RandomAccessIterator
, typename _Compare
>
1936 __heap_select(_RandomAccessIterator __first
,
1937 _RandomAccessIterator __middle
,
1938 _RandomAccessIterator __last
, _Compare __comp
)
1940 std::make_heap(__first
, __middle
, __comp
);
1941 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
1942 if (__comp(*__i
, *__first
))
1943 std::__pop_heap(__first
, __middle
, __i
, __comp
);
1949 * @brief Copy the smallest elements of a sequence.
1950 * @ingroup sorting_algorithms
1951 * @param __first An iterator.
1952 * @param __last Another iterator.
1953 * @param __result_first A random-access iterator.
1954 * @param __result_last Another random-access iterator.
1955 * @return An iterator indicating the end of the resulting sequence.
1957 * Copies and sorts the smallest N values from the range @p [__first,__last)
1958 * to the range beginning at @p __result_first, where the number of
1959 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1960 * @p (__result_last-__result_first).
1961 * After the sort if @e i and @e j are iterators in the range
1962 * @p [__result_first,__result_first+N) such that i precedes j then
1964 * The value returned is @p __result_first+N.
1966 template<typename _InputIterator
, typename _RandomAccessIterator
>
1967 _RandomAccessIterator
1968 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
1969 _RandomAccessIterator __result_first
,
1970 _RandomAccessIterator __result_last
)
1972 typedef typename iterator_traits
<_InputIterator
>::value_type
1974 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1976 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1979 // concept requirements
1980 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1981 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
1983 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
1985 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
1986 __glibcxx_requires_valid_range(__first
, __last
);
1987 __glibcxx_requires_valid_range(__result_first
, __result_last
);
1989 if (__result_first
== __result_last
)
1990 return __result_last
;
1991 _RandomAccessIterator __result_real_last
= __result_first
;
1992 while(__first
!= __last
&& __result_real_last
!= __result_last
)
1994 *__result_real_last
= *__first
;
1995 ++__result_real_last
;
1998 std::make_heap(__result_first
, __result_real_last
);
1999 while (__first
!= __last
)
2001 if (*__first
< *__result_first
)
2002 std::__adjust_heap(__result_first
, _DistanceType(0),
2003 _DistanceType(__result_real_last
2005 _InputValueType(*__first
));
2008 std::sort_heap(__result_first
, __result_real_last
);
2009 return __result_real_last
;
2013 * @brief Copy the smallest elements of a sequence using a predicate for
2015 * @ingroup sorting_algorithms
2016 * @param __first An input iterator.
2017 * @param __last Another input iterator.
2018 * @param __result_first A random-access iterator.
2019 * @param __result_last Another random-access iterator.
2020 * @param __comp A comparison functor.
2021 * @return An iterator indicating the end of the resulting sequence.
2023 * Copies and sorts the smallest N values from the range @p [__first,__last)
2024 * to the range beginning at @p result_first, where the number of
2025 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
2026 * @p (__result_last-__result_first).
2027 * After the sort if @e i and @e j are iterators in the range
2028 * @p [__result_first,__result_first+N) such that i precedes j then
2029 * @p __comp(*j,*i) is false.
2030 * The value returned is @p __result_first+N.
2032 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2033 _RandomAccessIterator
2034 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2035 _RandomAccessIterator __result_first
,
2036 _RandomAccessIterator __result_last
,
2039 typedef typename iterator_traits
<_InputIterator
>::value_type
2041 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2043 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2046 // concept requirements
2047 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2048 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2049 _RandomAccessIterator
>)
2050 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2052 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2053 _InputValueType
, _OutputValueType
>)
2054 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2055 _OutputValueType
, _OutputValueType
>)
2056 __glibcxx_requires_valid_range(__first
, __last
);
2057 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2059 if (__result_first
== __result_last
)
2060 return __result_last
;
2061 _RandomAccessIterator __result_real_last
= __result_first
;
2062 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2064 *__result_real_last
= *__first
;
2065 ++__result_real_last
;
2068 std::make_heap(__result_first
, __result_real_last
, __comp
);
2069 while (__first
!= __last
)
2071 if (__comp(*__first
, *__result_first
))
2072 std::__adjust_heap(__result_first
, _DistanceType(0),
2073 _DistanceType(__result_real_last
2075 _InputValueType(*__first
),
2079 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2080 return __result_real_last
;
2083 /// This is a helper function for the sort routine.
2084 template<typename _RandomAccessIterator
>
2086 __unguarded_linear_insert(_RandomAccessIterator __last
)
2088 typename iterator_traits
<_RandomAccessIterator
>::value_type
2089 __val
= _GLIBCXX_MOVE(*__last
);
2090 _RandomAccessIterator __next
= __last
;
2092 while (__val
< *__next
)
2094 *__last
= _GLIBCXX_MOVE(*__next
);
2098 *__last
= _GLIBCXX_MOVE(__val
);
2101 /// This is a helper function for the sort routine.
2102 template<typename _RandomAccessIterator
, typename _Compare
>
2104 __unguarded_linear_insert(_RandomAccessIterator __last
,
2107 typename iterator_traits
<_RandomAccessIterator
>::value_type
2108 __val
= _GLIBCXX_MOVE(*__last
);
2109 _RandomAccessIterator __next
= __last
;
2111 while (__comp(__val
, *__next
))
2113 *__last
= _GLIBCXX_MOVE(*__next
);
2117 *__last
= _GLIBCXX_MOVE(__val
);
2120 /// This is a helper function for the sort routine.
2121 template<typename _RandomAccessIterator
>
2123 __insertion_sort(_RandomAccessIterator __first
,
2124 _RandomAccessIterator __last
)
2126 if (__first
== __last
)
2129 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2131 if (*__i
< *__first
)
2133 typename iterator_traits
<_RandomAccessIterator
>::value_type
2134 __val
= _GLIBCXX_MOVE(*__i
);
2135 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2136 *__first
= _GLIBCXX_MOVE(__val
);
2139 std::__unguarded_linear_insert(__i
);
2143 /// This is a helper function for the sort routine.
2144 template<typename _RandomAccessIterator
, typename _Compare
>
2146 __insertion_sort(_RandomAccessIterator __first
,
2147 _RandomAccessIterator __last
, _Compare __comp
)
2149 if (__first
== __last
) return;
2151 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2153 if (__comp(*__i
, *__first
))
2155 typename iterator_traits
<_RandomAccessIterator
>::value_type
2156 __val
= _GLIBCXX_MOVE(*__i
);
2157 _GLIBCXX_MOVE_BACKWARD3(__first
, __i
, __i
+ 1);
2158 *__first
= _GLIBCXX_MOVE(__val
);
2161 std::__unguarded_linear_insert(__i
, __comp
);
2165 /// This is a helper function for the sort routine.
2166 template<typename _RandomAccessIterator
>
2168 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2169 _RandomAccessIterator __last
)
2171 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2174 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2175 std::__unguarded_linear_insert(__i
);
2178 /// This is a helper function for the sort routine.
2179 template<typename _RandomAccessIterator
, typename _Compare
>
2181 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2182 _RandomAccessIterator __last
, _Compare __comp
)
2184 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2187 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2188 std::__unguarded_linear_insert(__i
, __comp
);
2193 * This controls some aspect of the sort routines.
2195 enum { _S_threshold
= 16 };
2197 /// This is a helper function for the sort routine.
2198 template<typename _RandomAccessIterator
>
2200 __final_insertion_sort(_RandomAccessIterator __first
,
2201 _RandomAccessIterator __last
)
2203 if (__last
- __first
> int(_S_threshold
))
2205 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2206 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2209 std::__insertion_sort(__first
, __last
);
2212 /// This is a helper function for the sort routine.
2213 template<typename _RandomAccessIterator
, typename _Compare
>
2215 __final_insertion_sort(_RandomAccessIterator __first
,
2216 _RandomAccessIterator __last
, _Compare __comp
)
2218 if (__last
- __first
> int(_S_threshold
))
2220 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2221 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2225 std::__insertion_sort(__first
, __last
, __comp
);
2228 /// This is a helper function...
2229 template<typename _RandomAccessIterator
, typename _Tp
>
2230 _RandomAccessIterator
2231 __unguarded_partition(_RandomAccessIterator __first
,
2232 _RandomAccessIterator __last
, const _Tp
& __pivot
)
2236 while (*__first
< __pivot
)
2239 while (__pivot
< *__last
)
2241 if (!(__first
< __last
))
2243 std::iter_swap(__first
, __last
);
2248 /// This is a helper function...
2249 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2250 _RandomAccessIterator
2251 __unguarded_partition(_RandomAccessIterator __first
,
2252 _RandomAccessIterator __last
,
2253 const _Tp
& __pivot
, _Compare __comp
)
2257 while (__comp(*__first
, __pivot
))
2260 while (__comp(__pivot
, *__last
))
2262 if (!(__first
< __last
))
2264 std::iter_swap(__first
, __last
);
2269 /// This is a helper function...
2270 template<typename _RandomAccessIterator
>
2271 inline _RandomAccessIterator
2272 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2273 _RandomAccessIterator __last
)
2275 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2276 std::__move_median_first(__first
, __mid
, (__last
- 1));
2277 return std::__unguarded_partition(__first
+ 1, __last
, *__first
);
2281 /// This is a helper function...
2282 template<typename _RandomAccessIterator
, typename _Compare
>
2283 inline _RandomAccessIterator
2284 __unguarded_partition_pivot(_RandomAccessIterator __first
,
2285 _RandomAccessIterator __last
, _Compare __comp
)
2287 _RandomAccessIterator __mid
= __first
+ (__last
- __first
) / 2;
2288 std::__move_median_first(__first
, __mid
, (__last
- 1), __comp
);
2289 return std::__unguarded_partition(__first
+ 1, __last
, *__first
, __comp
);
2292 /// This is a helper function for the sort routine.
2293 template<typename _RandomAccessIterator
, typename _Size
>
2295 __introsort_loop(_RandomAccessIterator __first
,
2296 _RandomAccessIterator __last
,
2297 _Size __depth_limit
)
2299 while (__last
- __first
> int(_S_threshold
))
2301 if (__depth_limit
== 0)
2303 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
);
2307 _RandomAccessIterator __cut
=
2308 std::__unguarded_partition_pivot(__first
, __last
);
2309 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2314 /// This is a helper function for the sort routine.
2315 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2317 __introsort_loop(_RandomAccessIterator __first
,
2318 _RandomAccessIterator __last
,
2319 _Size __depth_limit
, _Compare __comp
)
2321 while (__last
- __first
> int(_S_threshold
))
2323 if (__depth_limit
== 0)
2325 _GLIBCXX_STD_A::partial_sort(__first
, __last
, __last
, __comp
);
2329 _RandomAccessIterator __cut
=
2330 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2331 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2338 template<typename _RandomAccessIterator
, typename _Size
>
2340 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2341 _RandomAccessIterator __last
, _Size __depth_limit
)
2343 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2346 while (__last
- __first
> 3)
2348 if (__depth_limit
== 0)
2350 std::__heap_select(__first
, __nth
+ 1, __last
);
2352 // Place the nth largest element in its final position.
2353 std::iter_swap(__first
, __nth
);
2357 _RandomAccessIterator __cut
=
2358 std::__unguarded_partition_pivot(__first
, __last
);
2364 std::__insertion_sort(__first
, __last
);
2367 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2369 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
2370 _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
, __comp
);
2381 // Place the nth largest element in its final position.
2382 std::iter_swap(__first
, __nth
);
2386 _RandomAccessIterator __cut
=
2387 std::__unguarded_partition_pivot(__first
, __last
, __comp
);
2393 std::__insertion_sort(__first
, __last
, __comp
);
2398 // lower_bound moved to stl_algobase.h
2401 * @brief Finds the first position in which @p __val could be inserted
2402 * without changing the ordering.
2403 * @ingroup binary_search_algorithms
2404 * @param __first An iterator.
2405 * @param __last Another iterator.
2406 * @param __val The search term.
2407 * @param __comp A functor to use for comparisons.
2408 * @return An iterator pointing to the first element <em>not less
2409 * than</em> @p __val, or end() if every element is less
2411 * @ingroup binary_search_algorithms
2413 * The comparison function should have the same effects on ordering as
2414 * the function used for the initial sort.
2416 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2418 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2419 const _Tp
& __val
, _Compare __comp
)
2421 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2423 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2426 // concept requirements
2427 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2428 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2430 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2433 _DistanceType __len
= std::distance(__first
, __last
);
2437 _DistanceType __half
= __len
>> 1;
2438 _ForwardIterator __middle
= __first
;
2439 std::advance(__middle
, __half
);
2440 if (__comp(*__middle
, __val
))
2444 __len
= __len
- __half
- 1;
2453 * @brief Finds the last position in which @p __val could be inserted
2454 * without changing the ordering.
2455 * @ingroup binary_search_algorithms
2456 * @param __first An iterator.
2457 * @param __last Another iterator.
2458 * @param __val The search term.
2459 * @return An iterator pointing to the first element greater than @p __val,
2460 * or end() if no elements are greater than @p __val.
2461 * @ingroup binary_search_algorithms
2463 template<typename _ForwardIterator
, typename _Tp
>
2465 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2468 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2470 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2473 // concept requirements
2474 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2475 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2476 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2478 _DistanceType __len
= std::distance(__first
, __last
);
2482 _DistanceType __half
= __len
>> 1;
2483 _ForwardIterator __middle
= __first
;
2484 std::advance(__middle
, __half
);
2485 if (__val
< *__middle
)
2491 __len
= __len
- __half
- 1;
2498 * @brief Finds the last position in which @p __val could be inserted
2499 * without changing the ordering.
2500 * @ingroup binary_search_algorithms
2501 * @param __first An iterator.
2502 * @param __last Another iterator.
2503 * @param __val The search term.
2504 * @param __comp A functor to use for comparisons.
2505 * @return An iterator pointing to the first element greater than @p __val,
2506 * or end() if no elements are greater than @p __val.
2507 * @ingroup binary_search_algorithms
2509 * The comparison function should have the same effects on ordering as
2510 * the function used for the initial sort.
2512 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2514 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2515 const _Tp
& __val
, _Compare __comp
)
2517 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2519 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2522 // concept requirements
2523 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2524 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2526 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2529 _DistanceType __len
= std::distance(__first
, __last
);
2533 _DistanceType __half
= __len
>> 1;
2534 _ForwardIterator __middle
= __first
;
2535 std::advance(__middle
, __half
);
2536 if (__comp(__val
, *__middle
))
2542 __len
= __len
- __half
- 1;
2549 * @brief Finds the largest subrange in which @p __val could be inserted
2550 * at any place in it without changing the ordering.
2551 * @ingroup binary_search_algorithms
2552 * @param __first An iterator.
2553 * @param __last Another iterator.
2554 * @param __val The search term.
2555 * @return An pair of iterators defining the subrange.
2556 * @ingroup binary_search_algorithms
2558 * This is equivalent to
2560 * std::make_pair(lower_bound(__first, __last, __val),
2561 * upper_bound(__first, __last, __val))
2563 * but does not actually call those functions.
2565 template<typename _ForwardIterator
, typename _Tp
>
2566 pair
<_ForwardIterator
, _ForwardIterator
>
2567 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2570 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2572 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2575 // concept requirements
2576 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2577 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2578 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2579 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2580 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2582 _DistanceType __len
= std::distance(__first
, __last
);
2586 _DistanceType __half
= __len
>> 1;
2587 _ForwardIterator __middle
= __first
;
2588 std::advance(__middle
, __half
);
2589 if (*__middle
< __val
)
2593 __len
= __len
- __half
- 1;
2595 else if (__val
< *__middle
)
2599 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2601 std::advance(__first
, __len
);
2602 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2604 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2607 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2611 * @brief Finds the largest subrange in which @p __val could be inserted
2612 * at any place in it without changing the ordering.
2613 * @param __first An iterator.
2614 * @param __last Another iterator.
2615 * @param __val The search term.
2616 * @param __comp A functor to use for comparisons.
2617 * @return An pair of iterators defining the subrange.
2618 * @ingroup binary_search_algorithms
2620 * This is equivalent to
2622 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2623 * upper_bound(__first, __last, __val, __comp))
2625 * but does not actually call those functions.
2627 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2628 pair
<_ForwardIterator
, _ForwardIterator
>
2629 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
2630 const _Tp
& __val
, _Compare __comp
)
2632 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2634 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2637 // concept requirements
2638 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2639 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2641 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2643 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2645 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2648 _DistanceType __len
= std::distance(__first
, __last
);
2652 _DistanceType __half
= __len
>> 1;
2653 _ForwardIterator __middle
= __first
;
2654 std::advance(__middle
, __half
);
2655 if (__comp(*__middle
, __val
))
2659 __len
= __len
- __half
- 1;
2661 else if (__comp(__val
, *__middle
))
2665 _ForwardIterator __left
= std::lower_bound(__first
, __middle
,
2667 std::advance(__first
, __len
);
2668 _ForwardIterator __right
= std::upper_bound(++__middle
, __first
,
2670 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
2673 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
2677 * @brief Determines whether an element exists in a range.
2678 * @ingroup binary_search_algorithms
2679 * @param __first An iterator.
2680 * @param __last Another iterator.
2681 * @param __val The search term.
2682 * @return True if @p __val (or its equivalent) is in [@p
2683 * __first,@p __last ].
2685 * Note that this does not actually return an iterator to @p __val. For
2686 * that, use std::find or a container's specialized find member functions.
2688 template<typename _ForwardIterator
, typename _Tp
>
2690 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2693 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2696 // concept requirements
2697 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2698 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2699 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
2700 __glibcxx_requires_partitioned_upper(__first
, __last
, __val
);
2702 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
2703 return __i
!= __last
&& !(__val
< *__i
);
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 * @param __comp A functor to use for comparisons.
2713 * @return True if @p __val (or its equivalent) is in @p [__first,__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 * The comparison function should have the same effects on ordering as
2719 * the function used for the initial sort.
2721 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2723 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
2724 const _Tp
& __val
, _Compare __comp
)
2726 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2729 // concept requirements
2730 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2731 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2733 __glibcxx_requires_partitioned_lower_pred(__first
, __last
,
2735 __glibcxx_requires_partitioned_upper_pred(__first
, __last
,
2738 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
2739 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
2744 /// This is a helper function for the __merge_adaptive routines.
2745 template<typename _InputIterator1
, typename _InputIterator2
,
2746 typename _OutputIterator
>
2748 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2749 _InputIterator2 __first2
, _InputIterator2 __last2
,
2750 _OutputIterator __result
)
2752 while (__first1
!= __last1
&& __first2
!= __last2
)
2754 if (*__first2
< *__first1
)
2756 *__result
= _GLIBCXX_MOVE(*__first2
);
2761 *__result
= _GLIBCXX_MOVE(*__first1
);
2766 if (__first1
!= __last1
)
2767 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2770 /// This is a helper function for the __merge_adaptive routines.
2771 template<typename _InputIterator1
, typename _InputIterator2
,
2772 typename _OutputIterator
, typename _Compare
>
2774 __move_merge_adaptive(_InputIterator1 __first1
, _InputIterator1 __last1
,
2775 _InputIterator2 __first2
, _InputIterator2 __last2
,
2776 _OutputIterator __result
, _Compare __comp
)
2778 while (__first1
!= __last1
&& __first2
!= __last2
)
2780 if (__comp(*__first2
, *__first1
))
2782 *__result
= _GLIBCXX_MOVE(*__first2
);
2787 *__result
= _GLIBCXX_MOVE(*__first1
);
2792 if (__first1
!= __last1
)
2793 _GLIBCXX_MOVE3(__first1
, __last1
, __result
);
2796 /// This is a helper function for the __merge_adaptive routines.
2797 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2798 typename _BidirectionalIterator3
>
2800 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2801 _BidirectionalIterator1 __last1
,
2802 _BidirectionalIterator2 __first2
,
2803 _BidirectionalIterator2 __last2
,
2804 _BidirectionalIterator3 __result
)
2806 if (__first1
== __last1
)
2808 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2811 else if (__first2
== __last2
)
2818 if (*__last2
< *__last1
)
2820 *--__result
= _GLIBCXX_MOVE(*__last1
);
2821 if (__first1
== __last1
)
2823 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2830 *--__result
= _GLIBCXX_MOVE(*__last2
);
2831 if (__first2
== __last2
)
2838 /// This is a helper function for the __merge_adaptive routines.
2839 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2840 typename _BidirectionalIterator3
, typename _Compare
>
2842 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1
,
2843 _BidirectionalIterator1 __last1
,
2844 _BidirectionalIterator2 __first2
,
2845 _BidirectionalIterator2 __last2
,
2846 _BidirectionalIterator3 __result
,
2849 if (__first1
== __last1
)
2851 _GLIBCXX_MOVE_BACKWARD3(__first2
, __last2
, __result
);
2854 else if (__first2
== __last2
)
2861 if (__comp(*__last2
, *__last1
))
2863 *--__result
= _GLIBCXX_MOVE(*__last1
);
2864 if (__first1
== __last1
)
2866 _GLIBCXX_MOVE_BACKWARD3(__first2
, ++__last2
, __result
);
2873 *--__result
= _GLIBCXX_MOVE(*__last2
);
2874 if (__first2
== __last2
)
2881 /// This is a helper function for the merge routines.
2882 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2884 _BidirectionalIterator1
2885 __rotate_adaptive(_BidirectionalIterator1 __first
,
2886 _BidirectionalIterator1 __middle
,
2887 _BidirectionalIterator1 __last
,
2888 _Distance __len1
, _Distance __len2
,
2889 _BidirectionalIterator2 __buffer
,
2890 _Distance __buffer_size
)
2892 _BidirectionalIterator2 __buffer_end
;
2893 if (__len1
> __len2
&& __len2
<= __buffer_size
)
2897 __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2898 _GLIBCXX_MOVE_BACKWARD3(__first
, __middle
, __last
);
2899 return _GLIBCXX_MOVE3(__buffer
, __buffer_end
, __first
);
2904 else if (__len1
<= __buffer_size
)
2908 __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2909 _GLIBCXX_MOVE3(__middle
, __last
, __first
);
2910 return _GLIBCXX_MOVE_BACKWARD3(__buffer
, __buffer_end
, __last
);
2917 std::rotate(__first
, __middle
, __last
);
2918 std::advance(__first
, std::distance(__middle
, __last
));
2923 /// This is a helper function for the merge routines.
2924 template<typename _BidirectionalIterator
, typename _Distance
,
2927 __merge_adaptive(_BidirectionalIterator __first
,
2928 _BidirectionalIterator __middle
,
2929 _BidirectionalIterator __last
,
2930 _Distance __len1
, _Distance __len2
,
2931 _Pointer __buffer
, _Distance __buffer_size
)
2933 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2935 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2936 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2939 else if (__len2
<= __buffer_size
)
2941 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2942 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
2943 __buffer_end
, __last
);
2947 _BidirectionalIterator __first_cut
= __first
;
2948 _BidirectionalIterator __second_cut
= __middle
;
2949 _Distance __len11
= 0;
2950 _Distance __len22
= 0;
2951 if (__len1
> __len2
)
2953 __len11
= __len1
/ 2;
2954 std::advance(__first_cut
, __len11
);
2955 __second_cut
= std::lower_bound(__middle
, __last
,
2957 __len22
= std::distance(__middle
, __second_cut
);
2961 __len22
= __len2
/ 2;
2962 std::advance(__second_cut
, __len22
);
2963 __first_cut
= std::upper_bound(__first
, __middle
,
2965 __len11
= std::distance(__first
, __first_cut
);
2967 _BidirectionalIterator __new_middle
=
2968 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
2969 __len1
- __len11
, __len22
, __buffer
,
2971 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
2972 __len22
, __buffer
, __buffer_size
);
2973 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
2975 __len2
- __len22
, __buffer
, __buffer_size
);
2979 /// This is a helper function for the merge routines.
2980 template<typename _BidirectionalIterator
, typename _Distance
,
2981 typename _Pointer
, typename _Compare
>
2983 __merge_adaptive(_BidirectionalIterator __first
,
2984 _BidirectionalIterator __middle
,
2985 _BidirectionalIterator __last
,
2986 _Distance __len1
, _Distance __len2
,
2987 _Pointer __buffer
, _Distance __buffer_size
,
2990 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
2992 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__first
, __middle
, __buffer
);
2993 std::__move_merge_adaptive(__buffer
, __buffer_end
, __middle
, __last
,
2996 else if (__len2
<= __buffer_size
)
2998 _Pointer __buffer_end
= _GLIBCXX_MOVE3(__middle
, __last
, __buffer
);
2999 std::__move_merge_adaptive_backward(__first
, __middle
, __buffer
,
3000 __buffer_end
, __last
, __comp
);
3004 _BidirectionalIterator __first_cut
= __first
;
3005 _BidirectionalIterator __second_cut
= __middle
;
3006 _Distance __len11
= 0;
3007 _Distance __len22
= 0;
3008 if (__len1
> __len2
)
3010 __len11
= __len1
/ 2;
3011 std::advance(__first_cut
, __len11
);
3012 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3014 __len22
= std::distance(__middle
, __second_cut
);
3018 __len22
= __len2
/ 2;
3019 std::advance(__second_cut
, __len22
);
3020 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3022 __len11
= std::distance(__first
, __first_cut
);
3024 _BidirectionalIterator __new_middle
=
3025 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3026 __len1
- __len11
, __len22
, __buffer
,
3028 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3029 __len22
, __buffer
, __buffer_size
, __comp
);
3030 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3032 __len2
- __len22
, __buffer
,
3033 __buffer_size
, __comp
);
3037 /// This is a helper function for the merge routines.
3038 template<typename _BidirectionalIterator
, typename _Distance
>
3040 __merge_without_buffer(_BidirectionalIterator __first
,
3041 _BidirectionalIterator __middle
,
3042 _BidirectionalIterator __last
,
3043 _Distance __len1
, _Distance __len2
)
3045 if (__len1
== 0 || __len2
== 0)
3047 if (__len1
+ __len2
== 2)
3049 if (*__middle
< *__first
)
3050 std::iter_swap(__first
, __middle
);
3053 _BidirectionalIterator __first_cut
= __first
;
3054 _BidirectionalIterator __second_cut
= __middle
;
3055 _Distance __len11
= 0;
3056 _Distance __len22
= 0;
3057 if (__len1
> __len2
)
3059 __len11
= __len1
/ 2;
3060 std::advance(__first_cut
, __len11
);
3061 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3062 __len22
= std::distance(__middle
, __second_cut
);
3066 __len22
= __len2
/ 2;
3067 std::advance(__second_cut
, __len22
);
3068 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3069 __len11
= std::distance(__first
, __first_cut
);
3071 std::rotate(__first_cut
, __middle
, __second_cut
);
3072 _BidirectionalIterator __new_middle
= __first_cut
;
3073 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3074 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3076 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3077 __len1
- __len11
, __len2
- __len22
);
3080 /// This is a helper function for the merge routines.
3081 template<typename _BidirectionalIterator
, typename _Distance
,
3084 __merge_without_buffer(_BidirectionalIterator __first
,
3085 _BidirectionalIterator __middle
,
3086 _BidirectionalIterator __last
,
3087 _Distance __len1
, _Distance __len2
,
3090 if (__len1
== 0 || __len2
== 0)
3092 if (__len1
+ __len2
== 2)
3094 if (__comp(*__middle
, *__first
))
3095 std::iter_swap(__first
, __middle
);
3098 _BidirectionalIterator __first_cut
= __first
;
3099 _BidirectionalIterator __second_cut
= __middle
;
3100 _Distance __len11
= 0;
3101 _Distance __len22
= 0;
3102 if (__len1
> __len2
)
3104 __len11
= __len1
/ 2;
3105 std::advance(__first_cut
, __len11
);
3106 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3108 __len22
= std::distance(__middle
, __second_cut
);
3112 __len22
= __len2
/ 2;
3113 std::advance(__second_cut
, __len22
);
3114 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3116 __len11
= std::distance(__first
, __first_cut
);
3118 std::rotate(__first_cut
, __middle
, __second_cut
);
3119 _BidirectionalIterator __new_middle
= __first_cut
;
3120 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3121 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3122 __len11
, __len22
, __comp
);
3123 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3124 __len1
- __len11
, __len2
- __len22
, __comp
);
3128 * @brief Merges two sorted ranges in place.
3129 * @ingroup sorting_algorithms
3130 * @param __first An iterator.
3131 * @param __middle Another iterator.
3132 * @param __last Another iterator.
3135 * Merges two sorted and consecutive ranges, [__first,__middle) and
3136 * [__middle,__last), and puts the result in [__first,__last). The
3137 * output will be sorted. The sort is @e stable, that is, for
3138 * equivalent elements in the two ranges, elements from the first
3139 * range will always come before elements from the second.
3141 * If enough additional memory is available, this takes (__last-__first)-1
3142 * comparisons. Otherwise an NlogN algorithm is used, where N is
3143 * distance(__first,__last).
3145 template<typename _BidirectionalIterator
>
3147 inplace_merge(_BidirectionalIterator __first
,
3148 _BidirectionalIterator __middle
,
3149 _BidirectionalIterator __last
)
3151 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3153 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3156 // concept requirements
3157 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3158 _BidirectionalIterator
>)
3159 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3160 __glibcxx_requires_sorted(__first
, __middle
);
3161 __glibcxx_requires_sorted(__middle
, __last
);
3163 if (__first
== __middle
|| __middle
== __last
)
3166 _DistanceType __len1
= std::distance(__first
, __middle
);
3167 _DistanceType __len2
= std::distance(__middle
, __last
);
3169 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3171 if (__buf
.begin() == 0)
3172 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3174 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3175 __buf
.begin(), _DistanceType(__buf
.size()));
3179 * @brief Merges two sorted ranges in place.
3180 * @ingroup sorting_algorithms
3181 * @param __first An iterator.
3182 * @param __middle Another iterator.
3183 * @param __last Another iterator.
3184 * @param __comp A functor to use for comparisons.
3187 * Merges two sorted and consecutive ranges, [__first,__middle) and
3188 * [middle,last), and puts the result in [__first,__last). The output will
3189 * be sorted. The sort is @e stable, that is, for equivalent
3190 * elements in the two ranges, elements from the first range will always
3191 * come before elements from the second.
3193 * If enough additional memory is available, this takes (__last-__first)-1
3194 * comparisons. Otherwise an NlogN algorithm is used, where N is
3195 * distance(__first,__last).
3197 * The comparison function should have the same effects on ordering as
3198 * the function used for the initial sort.
3200 template<typename _BidirectionalIterator
, typename _Compare
>
3202 inplace_merge(_BidirectionalIterator __first
,
3203 _BidirectionalIterator __middle
,
3204 _BidirectionalIterator __last
,
3207 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3209 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3212 // concept requirements
3213 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3214 _BidirectionalIterator
>)
3215 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3216 _ValueType
, _ValueType
>)
3217 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3218 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3220 if (__first
== __middle
|| __middle
== __last
)
3223 const _DistanceType __len1
= std::distance(__first
, __middle
);
3224 const _DistanceType __len2
= std::distance(__middle
, __last
);
3226 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3228 if (__buf
.begin() == 0)
3229 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3232 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3233 __buf
.begin(), _DistanceType(__buf
.size()),
3238 /// This is a helper function for the __merge_sort_loop routines.
3239 template<typename _InputIterator1
, typename _InputIterator2
,
3240 typename _OutputIterator
>
3242 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3243 _InputIterator2 __first2
, _InputIterator2 __last2
,
3244 _OutputIterator __result
)
3246 while (__first1
!= __last1
&& __first2
!= __last2
)
3248 if (*__first2
< *__first1
)
3250 *__result
= _GLIBCXX_MOVE(*__first2
);
3255 *__result
= _GLIBCXX_MOVE(*__first1
);
3260 return _GLIBCXX_MOVE3(__first2
, __last2
,
3261 _GLIBCXX_MOVE3(__first1
, __last1
,
3265 /// This is a helper function for the __merge_sort_loop routines.
3266 template<typename _InputIterator1
, typename _InputIterator2
,
3267 typename _OutputIterator
, typename _Compare
>
3269 __move_merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3270 _InputIterator2 __first2
, _InputIterator2 __last2
,
3271 _OutputIterator __result
, _Compare __comp
)
3273 while (__first1
!= __last1
&& __first2
!= __last2
)
3275 if (__comp(*__first2
, *__first1
))
3277 *__result
= _GLIBCXX_MOVE(*__first2
);
3282 *__result
= _GLIBCXX_MOVE(*__first1
);
3287 return _GLIBCXX_MOVE3(__first2
, __last2
,
3288 _GLIBCXX_MOVE3(__first1
, __last1
,
3292 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3295 __merge_sort_loop(_RandomAccessIterator1 __first
,
3296 _RandomAccessIterator1 __last
,
3297 _RandomAccessIterator2 __result
,
3298 _Distance __step_size
)
3300 const _Distance __two_step
= 2 * __step_size
;
3302 while (__last
- __first
>= __two_step
)
3304 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3305 __first
+ __step_size
,
3306 __first
+ __two_step
, __result
);
3307 __first
+= __two_step
;
3310 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3311 std::__move_merge(__first
, __first
+ __step_size
,
3312 __first
+ __step_size
, __last
, __result
);
3315 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3316 typename _Distance
, typename _Compare
>
3318 __merge_sort_loop(_RandomAccessIterator1 __first
,
3319 _RandomAccessIterator1 __last
,
3320 _RandomAccessIterator2 __result
, _Distance __step_size
,
3323 const _Distance __two_step
= 2 * __step_size
;
3325 while (__last
- __first
>= __two_step
)
3327 __result
= std::__move_merge(__first
, __first
+ __step_size
,
3328 __first
+ __step_size
,
3329 __first
+ __two_step
,
3331 __first
+= __two_step
;
3333 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3335 std::__move_merge(__first
,__first
+ __step_size
,
3336 __first
+ __step_size
, __last
, __result
, __comp
);
3339 template<typename _RandomAccessIterator
, typename _Distance
>
3341 __chunk_insertion_sort(_RandomAccessIterator __first
,
3342 _RandomAccessIterator __last
,
3343 _Distance __chunk_size
)
3345 while (__last
- __first
>= __chunk_size
)
3347 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3348 __first
+= __chunk_size
;
3350 std::__insertion_sort(__first
, __last
);
3353 template<typename _RandomAccessIterator
, typename _Distance
,
3356 __chunk_insertion_sort(_RandomAccessIterator __first
,
3357 _RandomAccessIterator __last
,
3358 _Distance __chunk_size
, _Compare __comp
)
3360 while (__last
- __first
>= __chunk_size
)
3362 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3363 __first
+= __chunk_size
;
3365 std::__insertion_sort(__first
, __last
, __comp
);
3368 enum { _S_chunk_size
= 7 };
3370 template<typename _RandomAccessIterator
, typename _Pointer
>
3372 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3373 _RandomAccessIterator __last
,
3376 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3379 const _Distance __len
= __last
- __first
;
3380 const _Pointer __buffer_last
= __buffer
+ __len
;
3382 _Distance __step_size
= _S_chunk_size
;
3383 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3385 while (__step_size
< __len
)
3387 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3389 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3394 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3396 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3397 _RandomAccessIterator __last
,
3398 _Pointer __buffer
, _Compare __comp
)
3400 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3403 const _Distance __len
= __last
- __first
;
3404 const _Pointer __buffer_last
= __buffer
+ __len
;
3406 _Distance __step_size
= _S_chunk_size
;
3407 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3409 while (__step_size
< __len
)
3411 std::__merge_sort_loop(__first
, __last
, __buffer
,
3412 __step_size
, __comp
);
3414 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3415 __step_size
, __comp
);
3420 template<typename _RandomAccessIterator
, typename _Pointer
,
3423 __stable_sort_adaptive(_RandomAccessIterator __first
,
3424 _RandomAccessIterator __last
,
3425 _Pointer __buffer
, _Distance __buffer_size
)
3427 const _Distance __len
= (__last
- __first
+ 1) / 2;
3428 const _RandomAccessIterator __middle
= __first
+ __len
;
3429 if (__len
> __buffer_size
)
3431 std::__stable_sort_adaptive(__first
, __middle
,
3432 __buffer
, __buffer_size
);
3433 std::__stable_sort_adaptive(__middle
, __last
,
3434 __buffer
, __buffer_size
);
3438 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3439 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3441 std::__merge_adaptive(__first
, __middle
, __last
,
3442 _Distance(__middle
- __first
),
3443 _Distance(__last
- __middle
),
3444 __buffer
, __buffer_size
);
3447 template<typename _RandomAccessIterator
, typename _Pointer
,
3448 typename _Distance
, typename _Compare
>
3450 __stable_sort_adaptive(_RandomAccessIterator __first
,
3451 _RandomAccessIterator __last
,
3452 _Pointer __buffer
, _Distance __buffer_size
,
3455 const _Distance __len
= (__last
- __first
+ 1) / 2;
3456 const _RandomAccessIterator __middle
= __first
+ __len
;
3457 if (__len
> __buffer_size
)
3459 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3460 __buffer_size
, __comp
);
3461 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3462 __buffer_size
, __comp
);
3466 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3467 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3469 std::__merge_adaptive(__first
, __middle
, __last
,
3470 _Distance(__middle
- __first
),
3471 _Distance(__last
- __middle
),
3472 __buffer
, __buffer_size
,
3476 /// This is a helper function for the stable sorting routines.
3477 template<typename _RandomAccessIterator
>
3479 __inplace_stable_sort(_RandomAccessIterator __first
,
3480 _RandomAccessIterator __last
)
3482 if (__last
- __first
< 15)
3484 std::__insertion_sort(__first
, __last
);
3487 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3488 std::__inplace_stable_sort(__first
, __middle
);
3489 std::__inplace_stable_sort(__middle
, __last
);
3490 std::__merge_without_buffer(__first
, __middle
, __last
,
3495 /// This is a helper function for the stable sorting routines.
3496 template<typename _RandomAccessIterator
, typename _Compare
>
3498 __inplace_stable_sort(_RandomAccessIterator __first
,
3499 _RandomAccessIterator __last
, _Compare __comp
)
3501 if (__last
- __first
< 15)
3503 std::__insertion_sort(__first
, __last
, __comp
);
3506 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3507 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3508 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3509 std::__merge_without_buffer(__first
, __middle
, __last
,
3517 // Set algorithms: includes, set_union, set_intersection, set_difference,
3518 // set_symmetric_difference. All of these algorithms have the precondition
3519 // that their input ranges are sorted and the postcondition that their output
3520 // ranges are sorted.
3523 * @brief Determines whether all elements of a sequence exists in a range.
3524 * @param __first1 Start of search range.
3525 * @param __last1 End of search range.
3526 * @param __first2 Start of sequence
3527 * @param __last2 End of sequence.
3528 * @return True if each element in [__first2,__last2) is contained in order
3529 * within [__first1,__last1). False otherwise.
3530 * @ingroup set_algorithms
3532 * This operation expects both [__first1,__last1) and
3533 * [__first2,__last2) to be sorted. Searches for the presence of
3534 * each element in [__first2,__last2) within [__first1,__last1).
3535 * The iterators over each range only move forward, so this is a
3536 * linear algorithm. If an element in [__first2,__last2) is not
3537 * found before the search iterator reaches @p __last2, false is
3540 template<typename _InputIterator1
, typename _InputIterator2
>
3542 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3543 _InputIterator2 __first2
, _InputIterator2 __last2
)
3545 typedef typename iterator_traits
<_InputIterator1
>::value_type
3547 typedef typename iterator_traits
<_InputIterator2
>::value_type
3550 // concept requirements
3551 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3552 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3553 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
3554 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3555 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
3556 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
3558 while (__first1
!= __last1
&& __first2
!= __last2
)
3559 if (*__first2
< *__first1
)
3561 else if(*__first1
< *__first2
)
3564 ++__first1
, ++__first2
;
3566 return __first2
== __last2
;
3570 * @brief Determines whether all elements of a sequence exists in a range
3572 * @ingroup set_algorithms
3573 * @param __first1 Start of search range.
3574 * @param __last1 End of search range.
3575 * @param __first2 Start of sequence
3576 * @param __last2 End of sequence.
3577 * @param __comp Comparison function to use.
3578 * @return True if each element in [__first2,__last2) is contained
3579 * in order within [__first1,__last1) according to comp. False
3580 * otherwise. @ingroup set_algorithms
3582 * This operation expects both [__first1,__last1) and
3583 * [__first2,__last2) to be sorted. Searches for the presence of
3584 * each element in [__first2,__last2) within [__first1,__last1),
3585 * using comp to decide. The iterators over each range only move
3586 * forward, so this is a linear algorithm. If an element in
3587 * [__first2,__last2) is not found before the search iterator
3588 * reaches @p __last2, false is returned.
3590 template<typename _InputIterator1
, typename _InputIterator2
,
3593 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3594 _InputIterator2 __first2
, _InputIterator2 __last2
,
3597 typedef typename iterator_traits
<_InputIterator1
>::value_type
3599 typedef typename iterator_traits
<_InputIterator2
>::value_type
3602 // concept requirements
3603 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3604 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3605 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3606 _ValueType1
, _ValueType2
>)
3607 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3608 _ValueType2
, _ValueType1
>)
3609 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
3610 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
3612 while (__first1
!= __last1
&& __first2
!= __last2
)
3613 if (__comp(*__first2
, *__first1
))
3615 else if(__comp(*__first1
, *__first2
))
3618 ++__first1
, ++__first2
;
3620 return __first2
== __last2
;
3629 // set_symmetric_difference
3634 * @brief Permute range into the next @e dictionary ordering.
3635 * @ingroup sorting_algorithms
3636 * @param __first Start of range.
3637 * @param __last End of range.
3638 * @return False if wrapped to first permutation, true otherwise.
3640 * Treats all permutations of the range as a set of @e dictionary sorted
3641 * sequences. Permutes the current sequence into the next one of this set.
3642 * Returns true if there are more sequences to generate. If the sequence
3643 * is the largest of the set, the smallest is generated and false returned.
3645 template<typename _BidirectionalIterator
>
3647 next_permutation(_BidirectionalIterator __first
,
3648 _BidirectionalIterator __last
)
3650 // concept requirements
3651 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3652 _BidirectionalIterator
>)
3653 __glibcxx_function_requires(_LessThanComparableConcept
<
3654 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3655 __glibcxx_requires_valid_range(__first
, __last
);
3657 if (__first
== __last
)
3659 _BidirectionalIterator __i
= __first
;
3668 _BidirectionalIterator __ii
= __i
;
3672 _BidirectionalIterator __j
= __last
;
3673 while (!(*__i
< *--__j
))
3675 std::iter_swap(__i
, __j
);
3676 std::reverse(__ii
, __last
);
3681 std::reverse(__first
, __last
);
3688 * @brief Permute range into the next @e dictionary ordering using
3689 * comparison functor.
3690 * @ingroup sorting_algorithms
3691 * @param __first Start of range.
3692 * @param __last End of range.
3693 * @param __comp A comparison functor.
3694 * @return False if wrapped to first permutation, true otherwise.
3696 * Treats all permutations of the range [__first,__last) as a set of
3697 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3698 * sequence into the next one of this set. Returns true if there are more
3699 * sequences to generate. If the sequence is the largest of the set, the
3700 * smallest is generated and false returned.
3702 template<typename _BidirectionalIterator
, typename _Compare
>
3704 next_permutation(_BidirectionalIterator __first
,
3705 _BidirectionalIterator __last
, _Compare __comp
)
3707 // concept requirements
3708 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3709 _BidirectionalIterator
>)
3710 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3711 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3712 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3713 __glibcxx_requires_valid_range(__first
, __last
);
3715 if (__first
== __last
)
3717 _BidirectionalIterator __i
= __first
;
3726 _BidirectionalIterator __ii
= __i
;
3728 if (__comp(*__i
, *__ii
))
3730 _BidirectionalIterator __j
= __last
;
3731 while (!bool(__comp(*__i
, *--__j
)))
3733 std::iter_swap(__i
, __j
);
3734 std::reverse(__ii
, __last
);
3739 std::reverse(__first
, __last
);
3746 * @brief Permute range into the previous @e dictionary ordering.
3747 * @ingroup sorting_algorithms
3748 * @param __first Start of range.
3749 * @param __last End of range.
3750 * @return False if wrapped to last permutation, true otherwise.
3752 * Treats all permutations of the range as a set of @e dictionary sorted
3753 * sequences. Permutes the current sequence into the previous one of this
3754 * set. Returns true if there are more sequences to generate. If the
3755 * sequence is the smallest of the set, the largest is generated and false
3758 template<typename _BidirectionalIterator
>
3760 prev_permutation(_BidirectionalIterator __first
,
3761 _BidirectionalIterator __last
)
3763 // concept requirements
3764 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3765 _BidirectionalIterator
>)
3766 __glibcxx_function_requires(_LessThanComparableConcept
<
3767 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3768 __glibcxx_requires_valid_range(__first
, __last
);
3770 if (__first
== __last
)
3772 _BidirectionalIterator __i
= __first
;
3781 _BidirectionalIterator __ii
= __i
;
3785 _BidirectionalIterator __j
= __last
;
3786 while (!(*--__j
< *__i
))
3788 std::iter_swap(__i
, __j
);
3789 std::reverse(__ii
, __last
);
3794 std::reverse(__first
, __last
);
3801 * @brief Permute range into the previous @e dictionary ordering using
3802 * comparison functor.
3803 * @ingroup sorting_algorithms
3804 * @param __first Start of range.
3805 * @param __last End of range.
3806 * @param __comp A comparison functor.
3807 * @return False if wrapped to last permutation, true otherwise.
3809 * Treats all permutations of the range [__first,__last) as a set of
3810 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3811 * sequence into the previous one of this set. Returns true if there are
3812 * more sequences to generate. If the sequence is the smallest of the set,
3813 * the largest is generated and false returned.
3815 template<typename _BidirectionalIterator
, typename _Compare
>
3817 prev_permutation(_BidirectionalIterator __first
,
3818 _BidirectionalIterator __last
, _Compare __comp
)
3820 // concept requirements
3821 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
3822 _BidirectionalIterator
>)
3823 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3824 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
3825 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
3826 __glibcxx_requires_valid_range(__first
, __last
);
3828 if (__first
== __last
)
3830 _BidirectionalIterator __i
= __first
;
3839 _BidirectionalIterator __ii
= __i
;
3841 if (__comp(*__ii
, *__i
))
3843 _BidirectionalIterator __j
= __last
;
3844 while (!bool(__comp(*--__j
, *__i
)))
3846 std::iter_swap(__i
, __j
);
3847 std::reverse(__ii
, __last
);
3852 std::reverse(__first
, __last
);
3862 * @brief Copy a sequence, replacing each element of one value with another
3864 * @param __first An input iterator.
3865 * @param __last An input iterator.
3866 * @param __result An output iterator.
3867 * @param __old_value The value to be replaced.
3868 * @param __new_value The replacement value.
3869 * @return The end of the output sequence, @p result+(last-first).
3871 * Copies each element in the input range @p [__first,__last) to the
3872 * output range @p [__result,__result+(__last-__first)) replacing elements
3873 * equal to @p __old_value with @p __new_value.
3875 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
3877 replace_copy(_InputIterator __first
, _InputIterator __last
,
3878 _OutputIterator __result
,
3879 const _Tp
& __old_value
, const _Tp
& __new_value
)
3881 // concept requirements
3882 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3883 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3884 typename iterator_traits
<_InputIterator
>::value_type
>)
3885 __glibcxx_function_requires(_EqualOpConcept
<
3886 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
3887 __glibcxx_requires_valid_range(__first
, __last
);
3889 for (; __first
!= __last
; ++__first
, ++__result
)
3890 if (*__first
== __old_value
)
3891 *__result
= __new_value
;
3893 *__result
= *__first
;
3898 * @brief Copy a sequence, replacing each value for which a predicate
3899 * returns true with another value.
3900 * @ingroup mutating_algorithms
3901 * @param __first An input iterator.
3902 * @param __last An input iterator.
3903 * @param __result An output iterator.
3904 * @param __pred A predicate.
3905 * @param __new_value The replacement value.
3906 * @return The end of the output sequence, @p __result+(__last-__first).
3908 * Copies each element in the range @p [__first,__last) to the range
3909 * @p [__result,__result+(__last-__first)) replacing elements for which
3910 * @p __pred returns true with @p __new_value.
3912 template<typename _InputIterator
, typename _OutputIterator
,
3913 typename _Predicate
, typename _Tp
>
3915 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
3916 _OutputIterator __result
,
3917 _Predicate __pred
, const _Tp
& __new_value
)
3919 // concept requirements
3920 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
3921 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3922 typename iterator_traits
<_InputIterator
>::value_type
>)
3923 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
3924 typename iterator_traits
<_InputIterator
>::value_type
>)
3925 __glibcxx_requires_valid_range(__first
, __last
);
3927 for (; __first
!= __last
; ++__first
, ++__result
)
3928 if (__pred(*__first
))
3929 *__result
= __new_value
;
3931 *__result
= *__first
;
3935 #if __cplusplus >= 201103L
3937 * @brief Determines whether the elements of a sequence are sorted.
3938 * @ingroup sorting_algorithms
3939 * @param __first An iterator.
3940 * @param __last Another iterator.
3941 * @return True if the elements are sorted, false otherwise.
3943 template<typename _ForwardIterator
>
3945 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
)
3946 { return std::is_sorted_until(__first
, __last
) == __last
; }
3949 * @brief Determines whether the elements of a sequence are sorted
3950 * according to a comparison functor.
3951 * @ingroup sorting_algorithms
3952 * @param __first An iterator.
3953 * @param __last Another iterator.
3954 * @param __comp A comparison functor.
3955 * @return True if the elements are sorted, false otherwise.
3957 template<typename _ForwardIterator
, typename _Compare
>
3959 is_sorted(_ForwardIterator __first
, _ForwardIterator __last
,
3961 { return std::is_sorted_until(__first
, __last
, __comp
) == __last
; }
3964 * @brief Determines the end of a sorted sequence.
3965 * @ingroup sorting_algorithms
3966 * @param __first An iterator.
3967 * @param __last Another iterator.
3968 * @return An iterator pointing to the last iterator i in [__first, __last)
3969 * for which the range [__first, i) is sorted.
3971 template<typename _ForwardIterator
>
3973 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
)
3975 // concept requirements
3976 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3977 __glibcxx_function_requires(_LessThanComparableConcept
<
3978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3979 __glibcxx_requires_valid_range(__first
, __last
);
3981 if (__first
== __last
)
3984 _ForwardIterator __next
= __first
;
3985 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
3986 if (*__next
< *__first
)
3992 * @brief Determines the end of a sorted sequence using comparison functor.
3993 * @ingroup sorting_algorithms
3994 * @param __first An iterator.
3995 * @param __last Another iterator.
3996 * @param __comp A comparison functor.
3997 * @return An iterator pointing to the last iterator i in [__first, __last)
3998 * for which the range [__first, i) is sorted.
4000 template<typename _ForwardIterator
, typename _Compare
>
4002 is_sorted_until(_ForwardIterator __first
, _ForwardIterator __last
,
4005 // concept requirements
4006 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4007 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4008 typename iterator_traits
<_ForwardIterator
>::value_type
,
4009 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4010 __glibcxx_requires_valid_range(__first
, __last
);
4012 if (__first
== __last
)
4015 _ForwardIterator __next
= __first
;
4016 for (++__next
; __next
!= __last
; __first
= __next
, ++__next
)
4017 if (__comp(*__next
, *__first
))
4023 * @brief Determines min and max at once as an ordered pair.
4024 * @ingroup sorting_algorithms
4025 * @param __a A thing of arbitrary type.
4026 * @param __b Another thing of arbitrary type.
4027 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4030 template<typename _Tp
>
4031 inline pair
<const _Tp
&, const _Tp
&>
4032 minmax(const _Tp
& __a
, const _Tp
& __b
)
4034 // concept requirements
4035 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
4037 return __b
< __a
? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4038 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4042 * @brief Determines min and max at once as an ordered pair.
4043 * @ingroup sorting_algorithms
4044 * @param __a A thing of arbitrary type.
4045 * @param __b Another thing of arbitrary type.
4046 * @param __comp A @link comparison_functors comparison functor @endlink.
4047 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
4050 template<typename _Tp
, typename _Compare
>
4051 inline pair
<const _Tp
&, const _Tp
&>
4052 minmax(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
4054 return __comp(__b
, __a
) ? pair
<const _Tp
&, const _Tp
&>(__b
, __a
)
4055 : pair
<const _Tp
&, const _Tp
&>(__a
, __b
);
4059 * @brief Return a pair of iterators pointing to the minimum and maximum
4060 * elements in a range.
4061 * @ingroup sorting_algorithms
4062 * @param __first Start of range.
4063 * @param __last End of range.
4064 * @return make_pair(m, M), where m is the first iterator i in
4065 * [__first, __last) such that no other element in the range is
4066 * smaller, and where M is the last iterator i in [__first, __last)
4067 * such that no other element in the range is larger.
4069 template<typename _ForwardIterator
>
4070 pair
<_ForwardIterator
, _ForwardIterator
>
4071 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
)
4073 // concept requirements
4074 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4075 __glibcxx_function_requires(_LessThanComparableConcept
<
4076 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4077 __glibcxx_requires_valid_range(__first
, __last
);
4079 _ForwardIterator __next
= __first
;
4080 if (__first
== __last
4081 || ++__next
== __last
)
4082 return std::make_pair(__first
, __first
);
4084 _ForwardIterator __min
, __max
;
4085 if (*__next
< *__first
)
4099 while (__first
!= __last
)
4102 if (++__next
== __last
)
4104 if (*__first
< *__min
)
4106 else if (!(*__first
< *__max
))
4111 if (*__next
< *__first
)
4113 if (*__next
< *__min
)
4115 if (!(*__first
< *__max
))
4120 if (*__first
< *__min
)
4122 if (!(*__next
< *__max
))
4130 return std::make_pair(__min
, __max
);
4134 * @brief Return a pair of iterators pointing to the minimum and maximum
4135 * elements in a range.
4136 * @ingroup sorting_algorithms
4137 * @param __first Start of range.
4138 * @param __last End of range.
4139 * @param __comp Comparison functor.
4140 * @return make_pair(m, M), where m is the first iterator i in
4141 * [__first, __last) such that no other element in the range is
4142 * smaller, and where M is the last iterator i in [__first, __last)
4143 * such that no other element in the range is larger.
4145 template<typename _ForwardIterator
, typename _Compare
>
4146 pair
<_ForwardIterator
, _ForwardIterator
>
4147 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
,
4150 // concept requirements
4151 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4152 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4153 typename iterator_traits
<_ForwardIterator
>::value_type
,
4154 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4155 __glibcxx_requires_valid_range(__first
, __last
);
4157 _ForwardIterator __next
= __first
;
4158 if (__first
== __last
4159 || ++__next
== __last
)
4160 return std::make_pair(__first
, __first
);
4162 _ForwardIterator __min
, __max
;
4163 if (__comp(*__next
, *__first
))
4177 while (__first
!= __last
)
4180 if (++__next
== __last
)
4182 if (__comp(*__first
, *__min
))
4184 else if (!__comp(*__first
, *__max
))
4189 if (__comp(*__next
, *__first
))
4191 if (__comp(*__next
, *__min
))
4193 if (!__comp(*__first
, *__max
))
4198 if (__comp(*__first
, *__min
))
4200 if (!__comp(*__next
, *__max
))
4208 return std::make_pair(__min
, __max
);
4212 template<typename _Tp
>
4214 min(initializer_list
<_Tp
> __l
)
4215 { return *std::min_element(__l
.begin(), __l
.end()); }
4217 template<typename _Tp
, typename _Compare
>
4219 min(initializer_list
<_Tp
> __l
, _Compare __comp
)
4220 { return *std::min_element(__l
.begin(), __l
.end(), __comp
); }
4222 template<typename _Tp
>
4224 max(initializer_list
<_Tp
> __l
)
4225 { return *std::max_element(__l
.begin(), __l
.end()); }
4227 template<typename _Tp
, typename _Compare
>
4229 max(initializer_list
<_Tp
> __l
, _Compare __comp
)
4230 { return *std::max_element(__l
.begin(), __l
.end(), __comp
); }
4232 template<typename _Tp
>
4233 inline pair
<_Tp
, _Tp
>
4234 minmax(initializer_list
<_Tp
> __l
)
4236 pair
<const _Tp
*, const _Tp
*> __p
=
4237 std::minmax_element(__l
.begin(), __l
.end());
4238 return std::make_pair(*__p
.first
, *__p
.second
);
4241 template<typename _Tp
, typename _Compare
>
4242 inline pair
<_Tp
, _Tp
>
4243 minmax(initializer_list
<_Tp
> __l
, _Compare __comp
)
4245 pair
<const _Tp
*, const _Tp
*> __p
=
4246 std::minmax_element(__l
.begin(), __l
.end(), __comp
);
4247 return std::make_pair(*__p
.first
, *__p
.second
);
4251 * @brief Checks whether a permutaion of the second sequence is equal
4252 * to the first sequence.
4253 * @ingroup non_mutating_algorithms
4254 * @param __first1 Start of first range.
4255 * @param __last1 End of first range.
4256 * @param __first2 Start of second range.
4257 * @return true if there exists a permutation of the elements in the range
4258 * [__first2, __first2 + (__last1 - __first1)), beginning with
4259 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
4260 * returns true; otherwise, returns false.
4262 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4264 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4265 _ForwardIterator2 __first2
)
4267 // Efficiently compare identical prefixes: O(N) if sequences
4268 // have the same elements in the same order.
4269 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4270 if (!(*__first1
== *__first2
))
4273 if (__first1
== __last1
)
4276 // Establish __last2 assuming equal ranges by iterating over the
4277 // rest of the list.
4278 _ForwardIterator2 __last2
= __first2
;
4279 std::advance(__last2
, std::distance(__first1
, __last1
));
4280 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4282 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4283 continue; // We've seen this one before.
4285 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4287 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4294 * @brief Checks whether a permutation of the second sequence is equal
4295 * to the first sequence.
4296 * @ingroup non_mutating_algorithms
4297 * @param __first1 Start of first range.
4298 * @param __last1 End of first range.
4299 * @param __first2 Start of second range.
4300 * @param __pred A binary predicate.
4301 * @return true if there exists a permutation of the elements in
4302 * the range [__first2, __first2 + (__last1 - __first1)),
4303 * beginning with ForwardIterator2 begin, such that
4304 * equal(__first1, __last1, __begin, __pred) returns true;
4305 * otherwise, returns false.
4307 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4308 typename _BinaryPredicate
>
4310 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4311 _ForwardIterator2 __first2
, _BinaryPredicate __pred
)
4313 // Efficiently compare identical prefixes: O(N) if sequences
4314 // have the same elements in the same order.
4315 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4316 if (!bool(__pred(*__first1
, *__first2
)))
4319 if (__first1
== __last1
)
4322 // Establish __last2 assuming equal ranges by iterating over the
4323 // rest of the list.
4324 _ForwardIterator2 __last2
= __first2
;
4325 std::advance(__last2
, std::distance(__first1
, __last1
));
4326 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4328 using std::placeholders::_1
;
4330 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4331 std::bind(__pred
, _1
, *__scan
)))
4332 continue; // We've seen this one before.
4334 auto __matches
= std::count_if(__first2
, __last2
,
4335 std::bind(__pred
, _1
, *__scan
));
4337 || std::count_if(__scan
, __last1
,
4338 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4344 #if __cplusplus > 201103L
4346 * @brief Checks whether a permutaion of the second sequence is equal
4347 * to the first sequence.
4348 * @ingroup non_mutating_algorithms
4349 * @param __first1 Start of first range.
4350 * @param __last1 End of first range.
4351 * @param __first2 Start of second range.
4352 * @param __last2 End of first range.
4353 * @return true if there exists a permutation of the elements in the range
4354 * [__first2, __last2), beginning with ForwardIterator2 begin,
4355 * such that equal(__first1, __last1, begin) returns true;
4356 * otherwise, returns false.
4358 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4360 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4361 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4364 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
4366 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
4367 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
4368 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
4369 if (_It1_is_RA() && _It2_is_RA())
4371 auto __d1
= std::distance(__first1
, __last1
);
4372 auto __d2
= std::distance(__first2
, __last2
);
4377 // Efficiently compare identical prefixes: O(N) if sequences
4378 // have the same elements in the same order.
4379 for (; __first1
!= __last1
&& __first2
!= __last2
; ++__first1
, ++__first2
)
4380 if (!(*__first1
== *__first2
))
4383 if (__first1
== __last1
&& __first2
== __last2
)
4386 if (std::distance(__first1
, __last1
) != std::distance(__first2
, __last2
))
4389 for (auto __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4391 if (__scan
!= _GLIBCXX_STD_A::find(__first1
, __scan
, *__scan
))
4392 continue; // We've seen this one before.
4394 auto __matches
= std::count(__first2
, __last2
, *__scan
);
4396 || std::count(__scan
, __last1
, *__scan
) != __matches
)
4403 * @brief Checks whether a permutation of the second sequence is equal
4404 * to the first sequence.
4405 * @ingroup non_mutating_algorithms
4406 * @param __first1 Start of first range.
4407 * @param __last1 End of first range.
4408 * @param __first2 Start of second range.
4409 * @param __last2 End of first range.
4410 * @param __pred A binary predicate.
4411 * @return true if there exists a permutation of the elements in the range
4412 * [__first2, __last2), beginning with ForwardIterator2 begin,
4413 * such that equal(__first1, __last1, __begin, __pred) returns true;
4414 * otherwise, returns false.
4416 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4417 typename _BinaryPredicate
>
4419 is_permutation(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4420 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4421 _BinaryPredicate __pred
)
4424 = typename iterator_traits
<_ForwardIterator1
>::iterator_category
;
4426 = typename iterator_traits
<_ForwardIterator2
>::iterator_category
;
4427 using _It1_is_RA
= is_same
<_Cat1
, random_access_iterator_tag
>;
4428 using _It2_is_RA
= is_same
<_Cat2
, random_access_iterator_tag
>;
4429 constexpr bool __ra_iters
= _It1_is_RA() && _It2_is_RA();
4432 auto __d1
= std::distance(__first1
, __last1
);
4433 auto __d2
= std::distance(__first2
, __last2
);
4438 // Efficiently compare identical prefixes: O(N) if sequences
4439 // have the same elements in the same order.
4440 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
4441 if (!bool(__pred(*__first1
, *__first2
)))
4446 if (__first1
== __last1
)
4451 auto __d1
= std::distance(__first1
, __last1
);
4452 auto __d2
= std::distance(__first2
, __last2
);
4453 if (__d1
== 0 && __d2
== 0)
4459 for (_ForwardIterator1 __scan
= __first1
; __scan
!= __last1
; ++__scan
)
4461 using std::placeholders::_1
;
4463 if (__scan
!= _GLIBCXX_STD_A::find_if(__first1
, __scan
,
4464 std::bind(__pred
, _1
, *__scan
)))
4465 continue; // We've seen this one before.
4467 auto __matches
= std::count_if(__first2
, __last2
,
4468 std::bind(__pred
, _1
, *__scan
));
4470 || std::count_if(__scan
, __last1
,
4471 std::bind(__pred
, _1
, *__scan
)) != __matches
)
4478 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4480 * @brief Shuffle the elements of a sequence using a uniform random
4482 * @ingroup mutating_algorithms
4483 * @param __first A forward iterator.
4484 * @param __last A forward iterator.
4485 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
4488 * Reorders the elements in the range @p [__first,__last) using @p __g to
4489 * provide random numbers.
4491 template<typename _RandomAccessIterator
,
4492 typename _UniformRandomNumberGenerator
>
4494 shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4495 _UniformRandomNumberGenerator
&& __g
)
4497 // concept requirements
4498 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4499 _RandomAccessIterator
>)
4500 __glibcxx_requires_valid_range(__first
, __last
);
4502 if (__first
== __last
)
4505 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4508 typedef typename
std::make_unsigned
<_DistanceType
>::type __ud_type
;
4509 typedef typename
std::uniform_int_distribution
<__ud_type
> __distr_type
;
4510 typedef typename
__distr_type::param_type __p_type
;
4513 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
4514 std::iter_swap(__i
, __first
+ __d(__g
, __p_type(0, __i
- __first
)));
4520 _GLIBCXX_END_NAMESPACE_VERSION
4522 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4525 * @brief Apply a function to every element of a sequence.
4526 * @ingroup non_mutating_algorithms
4527 * @param __first An input iterator.
4528 * @param __last An input iterator.
4529 * @param __f A unary function object.
4530 * @return @p __f (std::move(@p __f) in C++0x).
4532 * Applies the function object @p __f to each element in the range
4533 * @p [first,last). @p __f must not modify the order of the sequence.
4534 * If @p __f has a return value it is ignored.
4536 template<typename _InputIterator
, typename _Function
>
4538 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
4540 // concept requirements
4541 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4542 __glibcxx_requires_valid_range(__first
, __last
);
4543 for (; __first
!= __last
; ++__first
)
4545 return _GLIBCXX_MOVE(__f
);
4549 * @brief Find the first occurrence of a value in a sequence.
4550 * @ingroup non_mutating_algorithms
4551 * @param __first An input iterator.
4552 * @param __last An input iterator.
4553 * @param __val The value to find.
4554 * @return The first iterator @c i in the range @p [__first,__last)
4555 * such that @c *i == @p __val, or @p __last if no such iterator exists.
4557 template<typename _InputIterator
, typename _Tp
>
4558 inline _InputIterator
4559 find(_InputIterator __first
, _InputIterator __last
,
4562 // concept requirements
4563 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4564 __glibcxx_function_requires(_EqualOpConcept
<
4565 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4566 __glibcxx_requires_valid_range(__first
, __last
);
4567 return std::__find(__first
, __last
, __val
,
4568 std::__iterator_category(__first
));
4572 * @brief Find the first element in a sequence for which a
4573 * predicate is true.
4574 * @ingroup non_mutating_algorithms
4575 * @param __first An input iterator.
4576 * @param __last An input iterator.
4577 * @param __pred A predicate.
4578 * @return The first iterator @c i in the range @p [__first,__last)
4579 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
4581 template<typename _InputIterator
, typename _Predicate
>
4582 inline _InputIterator
4583 find_if(_InputIterator __first
, _InputIterator __last
,
4586 // concept requirements
4587 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4588 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4589 typename iterator_traits
<_InputIterator
>::value_type
>)
4590 __glibcxx_requires_valid_range(__first
, __last
);
4591 return std::__find_if(__first
, __last
, __pred
,
4592 std::__iterator_category(__first
));
4596 * @brief Find element from a set in a sequence.
4597 * @ingroup non_mutating_algorithms
4598 * @param __first1 Start of range to search.
4599 * @param __last1 End of range to search.
4600 * @param __first2 Start of match candidates.
4601 * @param __last2 End of match candidates.
4602 * @return The first iterator @c i in the range
4603 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
4604 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
4606 * Searches the range @p [__first1,__last1) for an element that is
4607 * equal to some element in the range [__first2,__last2). If
4608 * found, returns an iterator in the range [__first1,__last1),
4609 * otherwise returns @p __last1.
4611 template<typename _InputIterator
, typename _ForwardIterator
>
4613 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4614 _ForwardIterator __first2
, _ForwardIterator __last2
)
4616 // concept requirements
4617 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4618 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4619 __glibcxx_function_requires(_EqualOpConcept
<
4620 typename iterator_traits
<_InputIterator
>::value_type
,
4621 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4622 __glibcxx_requires_valid_range(__first1
, __last1
);
4623 __glibcxx_requires_valid_range(__first2
, __last2
);
4625 for (; __first1
!= __last1
; ++__first1
)
4626 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4627 if (*__first1
== *__iter
)
4633 * @brief Find element from a set in a sequence using a predicate.
4634 * @ingroup non_mutating_algorithms
4635 * @param __first1 Start of range to search.
4636 * @param __last1 End of range to search.
4637 * @param __first2 Start of match candidates.
4638 * @param __last2 End of match candidates.
4639 * @param __comp Predicate to use.
4640 * @return The first iterator @c i in the range
4641 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
4642 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
4643 * such iterator exists.
4646 * Searches the range @p [__first1,__last1) for an element that is
4647 * equal to some element in the range [__first2,__last2). If
4648 * found, returns an iterator in the range [__first1,__last1),
4649 * otherwise returns @p __last1.
4651 template<typename _InputIterator
, typename _ForwardIterator
,
4652 typename _BinaryPredicate
>
4654 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4655 _ForwardIterator __first2
, _ForwardIterator __last2
,
4656 _BinaryPredicate __comp
)
4658 // concept requirements
4659 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4660 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4661 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4662 typename iterator_traits
<_InputIterator
>::value_type
,
4663 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4664 __glibcxx_requires_valid_range(__first1
, __last1
);
4665 __glibcxx_requires_valid_range(__first2
, __last2
);
4667 for (; __first1
!= __last1
; ++__first1
)
4668 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4669 if (__comp(*__first1
, *__iter
))
4675 * @brief Find two adjacent values in a sequence that are equal.
4676 * @ingroup non_mutating_algorithms
4677 * @param __first A forward iterator.
4678 * @param __last A forward iterator.
4679 * @return The first iterator @c i such that @c i and @c i+1 are both
4680 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4681 * or @p __last if no such iterator exists.
4683 template<typename _ForwardIterator
>
4685 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
4687 // concept requirements
4688 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4689 __glibcxx_function_requires(_EqualityComparableConcept
<
4690 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4691 __glibcxx_requires_valid_range(__first
, __last
);
4692 if (__first
== __last
)
4694 _ForwardIterator __next
= __first
;
4695 while(++__next
!= __last
)
4697 if (*__first
== *__next
)
4705 * @brief Find two adjacent values in a sequence using a predicate.
4706 * @ingroup non_mutating_algorithms
4707 * @param __first A forward iterator.
4708 * @param __last A forward iterator.
4709 * @param __binary_pred A binary predicate.
4710 * @return The first iterator @c i such that @c i and @c i+1 are both
4711 * valid iterators in @p [__first,__last) and such that
4712 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4715 template<typename _ForwardIterator
, typename _BinaryPredicate
>
4717 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
4718 _BinaryPredicate __binary_pred
)
4720 // concept requirements
4721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4722 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4723 typename iterator_traits
<_ForwardIterator
>::value_type
,
4724 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4725 __glibcxx_requires_valid_range(__first
, __last
);
4726 if (__first
== __last
)
4728 _ForwardIterator __next
= __first
;
4729 while(++__next
!= __last
)
4731 if (__binary_pred(*__first
, *__next
))
4739 * @brief Count the number of copies of a value in a sequence.
4740 * @ingroup non_mutating_algorithms
4741 * @param __first An input iterator.
4742 * @param __last An input iterator.
4743 * @param __value The value to be counted.
4744 * @return The number of iterators @c i in the range @p [__first,__last)
4745 * for which @c *i == @p __value
4747 template<typename _InputIterator
, typename _Tp
>
4748 typename iterator_traits
<_InputIterator
>::difference_type
4749 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
4751 // concept requirements
4752 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4753 __glibcxx_function_requires(_EqualOpConcept
<
4754 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
4755 __glibcxx_requires_valid_range(__first
, __last
);
4756 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4757 for (; __first
!= __last
; ++__first
)
4758 if (*__first
== __value
)
4764 * @brief Count the elements of a sequence for which a predicate is true.
4765 * @ingroup non_mutating_algorithms
4766 * @param __first An input iterator.
4767 * @param __last An input iterator.
4768 * @param __pred A predicate.
4769 * @return The number of iterators @c i in the range @p [__first,__last)
4770 * for which @p __pred(*i) is true.
4772 template<typename _InputIterator
, typename _Predicate
>
4773 typename iterator_traits
<_InputIterator
>::difference_type
4774 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
4776 // concept requirements
4777 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4778 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
4779 typename iterator_traits
<_InputIterator
>::value_type
>)
4780 __glibcxx_requires_valid_range(__first
, __last
);
4781 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
4782 for (; __first
!= __last
; ++__first
)
4783 if (__pred(*__first
))
4789 * @brief Search a sequence for a matching sub-sequence.
4790 * @ingroup non_mutating_algorithms
4791 * @param __first1 A forward iterator.
4792 * @param __last1 A forward iterator.
4793 * @param __first2 A forward iterator.
4794 * @param __last2 A forward iterator.
4795 * @return The first iterator @c i in the range @p
4796 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4797 * *(__first2+N) for each @c N in the range @p
4798 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4800 * Searches the range @p [__first1,__last1) for a sub-sequence that
4801 * compares equal value-by-value with the sequence given by @p
4802 * [__first2,__last2) and returns an iterator to the first element
4803 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4806 * Because the sub-sequence must lie completely within the range @p
4807 * [__first1,__last1) it must start at a position less than @p
4808 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4809 * length of the sub-sequence.
4811 * This means that the returned iterator @c i will be in the range
4812 * @p [__first1,__last1-(__last2-__first2))
4814 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4816 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4817 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4819 // concept requirements
4820 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4821 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4822 __glibcxx_function_requires(_EqualOpConcept
<
4823 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4824 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4825 __glibcxx_requires_valid_range(__first1
, __last1
);
4826 __glibcxx_requires_valid_range(__first2
, __last2
);
4828 // Test for empty ranges
4829 if (__first1
== __last1
|| __first2
== __last2
)
4832 // Test for a pattern of length 1.
4833 _ForwardIterator2
__p1(__first2
);
4834 if (++__p1
== __last2
)
4835 return _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4838 _ForwardIterator2 __p
;
4839 _ForwardIterator1 __current
= __first1
;
4843 __first1
= _GLIBCXX_STD_A::find(__first1
, __last1
, *__first2
);
4844 if (__first1
== __last1
)
4848 __current
= __first1
;
4849 if (++__current
== __last1
)
4852 while (*__current
== *__p
)
4854 if (++__p
== __last2
)
4856 if (++__current
== __last1
)
4865 * @brief Search a sequence for a matching sub-sequence using a predicate.
4866 * @ingroup non_mutating_algorithms
4867 * @param __first1 A forward iterator.
4868 * @param __last1 A forward iterator.
4869 * @param __first2 A forward iterator.
4870 * @param __last2 A forward iterator.
4871 * @param __predicate A binary predicate.
4872 * @return The first iterator @c i in the range
4873 * @p [__first1,__last1-(__last2-__first2)) such that
4874 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4875 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4877 * Searches the range @p [__first1,__last1) for a sub-sequence that
4878 * compares equal value-by-value with the sequence given by @p
4879 * [__first2,__last2), using @p __predicate to determine equality,
4880 * and returns an iterator to the first element of the
4881 * sub-sequence, or @p __last1 if no such iterator exists.
4883 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4885 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4886 typename _BinaryPredicate
>
4888 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4889 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4890 _BinaryPredicate __predicate
)
4892 // concept requirements
4893 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4894 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4895 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4896 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4897 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4898 __glibcxx_requires_valid_range(__first1
, __last1
);
4899 __glibcxx_requires_valid_range(__first2
, __last2
);
4901 // Test for empty ranges
4902 if (__first1
== __last1
|| __first2
== __last2
)
4905 // Test for a pattern of length 1.
4906 _ForwardIterator2
__p1(__first2
);
4907 if (++__p1
== __last2
)
4909 while (__first1
!= __last1
4910 && !bool(__predicate(*__first1
, *__first2
)))
4916 _ForwardIterator2 __p
;
4917 _ForwardIterator1 __current
= __first1
;
4921 while (__first1
!= __last1
4922 && !bool(__predicate(*__first1
, *__first2
)))
4924 if (__first1
== __last1
)
4928 __current
= __first1
;
4929 if (++__current
== __last1
)
4932 while (__predicate(*__current
, *__p
))
4934 if (++__p
== __last2
)
4936 if (++__current
== __last1
)
4946 * @brief Search a sequence for a number of consecutive values.
4947 * @ingroup non_mutating_algorithms
4948 * @param __first A forward iterator.
4949 * @param __last A forward iterator.
4950 * @param __count The number of consecutive values.
4951 * @param __val The value to find.
4952 * @return The first iterator @c i in the range @p
4953 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4954 * each @c N in the range @p [0,__count), or @p __last if no such
4957 * Searches the range @p [__first,__last) for @p count consecutive elements
4958 * equal to @p __val.
4960 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
4962 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
4963 _Integer __count
, const _Tp
& __val
)
4965 // concept requirements
4966 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4967 __glibcxx_function_requires(_EqualOpConcept
<
4968 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
4969 __glibcxx_requires_valid_range(__first
, __last
);
4974 return _GLIBCXX_STD_A::find(__first
, __last
, __val
);
4975 return std::__search_n(__first
, __last
, __count
, __val
,
4976 std::__iterator_category(__first
));
4981 * @brief Search a sequence for a number of consecutive values using a
4983 * @ingroup non_mutating_algorithms
4984 * @param __first A forward iterator.
4985 * @param __last A forward iterator.
4986 * @param __count The number of consecutive values.
4987 * @param __val The value to find.
4988 * @param __binary_pred A binary predicate.
4989 * @return The first iterator @c i in the range @p
4990 * [__first,__last-__count) such that @p
4991 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4992 * @p [0,__count), or @p __last if no such iterator exists.
4994 * Searches the range @p [__first,__last) for @p __count
4995 * consecutive elements for which the predicate returns true.
4997 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
4998 typename _BinaryPredicate
>
5000 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
5001 _Integer __count
, const _Tp
& __val
,
5002 _BinaryPredicate __binary_pred
)
5004 // concept requirements
5005 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5006 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5007 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
5008 __glibcxx_requires_valid_range(__first
, __last
);
5014 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
5018 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
5019 std::__iterator_category(__first
));
5024 * @brief Perform an operation on a sequence.
5025 * @ingroup mutating_algorithms
5026 * @param __first An input iterator.
5027 * @param __last An input iterator.
5028 * @param __result An output iterator.
5029 * @param __unary_op A unary operator.
5030 * @return An output iterator equal to @p __result+(__last-__first).
5032 * Applies the operator to each element in the input range and assigns
5033 * the results to successive elements of the output sequence.
5034 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
5035 * range @p [0,__last-__first).
5037 * @p unary_op must not alter its argument.
5039 template<typename _InputIterator
, typename _OutputIterator
,
5040 typename _UnaryOperation
>
5042 transform(_InputIterator __first
, _InputIterator __last
,
5043 _OutputIterator __result
, _UnaryOperation __unary_op
)
5045 // concept requirements
5046 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5047 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5048 // "the type returned by a _UnaryOperation"
5049 __typeof__(__unary_op(*__first
))>)
5050 __glibcxx_requires_valid_range(__first
, __last
);
5052 for (; __first
!= __last
; ++__first
, ++__result
)
5053 *__result
= __unary_op(*__first
);
5058 * @brief Perform an operation on corresponding elements of two sequences.
5059 * @ingroup mutating_algorithms
5060 * @param __first1 An input iterator.
5061 * @param __last1 An input iterator.
5062 * @param __first2 An input iterator.
5063 * @param __result An output iterator.
5064 * @param __binary_op A binary operator.
5065 * @return An output iterator equal to @p result+(last-first).
5067 * Applies the operator to the corresponding elements in the two
5068 * input ranges and assigns the results to successive elements of the
5071 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
5072 * @c N in the range @p [0,__last1-__first1).
5074 * @p binary_op must not alter either of its arguments.
5076 template<typename _InputIterator1
, typename _InputIterator2
,
5077 typename _OutputIterator
, typename _BinaryOperation
>
5079 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
5080 _InputIterator2 __first2
, _OutputIterator __result
,
5081 _BinaryOperation __binary_op
)
5083 // concept requirements
5084 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5085 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5086 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5087 // "the type returned by a _BinaryOperation"
5088 __typeof__(__binary_op(*__first1
,*__first2
))>)
5089 __glibcxx_requires_valid_range(__first1
, __last1
);
5091 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
5092 *__result
= __binary_op(*__first1
, *__first2
);
5097 * @brief Replace each occurrence of one value in a sequence with another
5099 * @ingroup mutating_algorithms
5100 * @param __first A forward iterator.
5101 * @param __last A forward iterator.
5102 * @param __old_value The value to be replaced.
5103 * @param __new_value The replacement value.
5104 * @return replace() returns no value.
5106 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
5107 * @p __old_value then the assignment @c *i = @p __new_value is performed.
5109 template<typename _ForwardIterator
, typename _Tp
>
5111 replace(_ForwardIterator __first
, _ForwardIterator __last
,
5112 const _Tp
& __old_value
, const _Tp
& __new_value
)
5114 // concept requirements
5115 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5117 __glibcxx_function_requires(_EqualOpConcept
<
5118 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
5119 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
5120 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5121 __glibcxx_requires_valid_range(__first
, __last
);
5123 for (; __first
!= __last
; ++__first
)
5124 if (*__first
== __old_value
)
5125 *__first
= __new_value
;
5129 * @brief Replace each value in a sequence for which a predicate returns
5130 * true with another value.
5131 * @ingroup mutating_algorithms
5132 * @param __first A forward iterator.
5133 * @param __last A forward iterator.
5134 * @param __pred A predicate.
5135 * @param __new_value The replacement value.
5136 * @return replace_if() returns no value.
5138 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
5139 * is true then the assignment @c *i = @p __new_value is performed.
5141 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
5143 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
5144 _Predicate __pred
, const _Tp
& __new_value
)
5146 // concept requirements
5147 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5149 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
5150 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5151 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5152 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5153 __glibcxx_requires_valid_range(__first
, __last
);
5155 for (; __first
!= __last
; ++__first
)
5156 if (__pred(*__first
))
5157 *__first
= __new_value
;
5161 * @brief Assign the result of a function object to each value in a
5163 * @ingroup mutating_algorithms
5164 * @param __first A forward iterator.
5165 * @param __last A forward iterator.
5166 * @param __gen A function object taking no arguments and returning
5167 * std::iterator_traits<_ForwardIterator>::value_type
5168 * @return generate() returns no value.
5170 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5171 * @p [__first,__last).
5173 template<typename _ForwardIterator
, typename _Generator
>
5175 generate(_ForwardIterator __first
, _ForwardIterator __last
,
5178 // concept requirements
5179 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5180 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
5181 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5182 __glibcxx_requires_valid_range(__first
, __last
);
5184 for (; __first
!= __last
; ++__first
)
5189 * @brief Assign the result of a function object to each value in a
5191 * @ingroup mutating_algorithms
5192 * @param __first A forward iterator.
5193 * @param __n The length of the sequence.
5194 * @param __gen A function object taking no arguments and returning
5195 * std::iterator_traits<_ForwardIterator>::value_type
5196 * @return The end of the sequence, @p __first+__n
5198 * Performs the assignment @c *i = @p __gen() for each @c i in the range
5199 * @p [__first,__first+__n).
5201 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5202 * DR 865. More algorithms that throw away information
5204 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
5206 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
5208 // concept requirements
5209 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5210 // "the type returned by a _Generator"
5211 __typeof__(__gen())>)
5213 for (__decltype(__n
+ 0) __niter
= __n
;
5214 __niter
> 0; --__niter
, ++__first
)
5221 * @brief Copy a sequence, removing consecutive duplicate values.
5222 * @ingroup mutating_algorithms
5223 * @param __first An input iterator.
5224 * @param __last An input iterator.
5225 * @param __result An output iterator.
5226 * @return An iterator designating the end of the resulting sequence.
5228 * Copies each element in the range @p [__first,__last) to the range
5229 * beginning at @p __result, except that only the first element is copied
5230 * from groups of consecutive elements that compare equal.
5231 * unique_copy() is stable, so the relative order of elements that are
5232 * copied is unchanged.
5234 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5235 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5237 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5238 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
5241 template<typename _InputIterator
, typename _OutputIterator
>
5242 inline _OutputIterator
5243 unique_copy(_InputIterator __first
, _InputIterator __last
,
5244 _OutputIterator __result
)
5246 // concept requirements
5247 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5248 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5249 typename iterator_traits
<_InputIterator
>::value_type
>)
5250 __glibcxx_function_requires(_EqualityComparableConcept
<
5251 typename iterator_traits
<_InputIterator
>::value_type
>)
5252 __glibcxx_requires_valid_range(__first
, __last
);
5254 if (__first
== __last
)
5256 return std::__unique_copy(__first
, __last
, __result
,
5257 std::__iterator_category(__first
),
5258 std::__iterator_category(__result
));
5262 * @brief Copy a sequence, removing consecutive values using a predicate.
5263 * @ingroup mutating_algorithms
5264 * @param __first An input iterator.
5265 * @param __last An input iterator.
5266 * @param __result An output iterator.
5267 * @param __binary_pred A binary predicate.
5268 * @return An iterator designating the end of the resulting sequence.
5270 * Copies each element in the range @p [__first,__last) to the range
5271 * beginning at @p __result, except that only the first element is copied
5272 * from groups of consecutive elements for which @p __binary_pred returns
5274 * unique_copy() is stable, so the relative order of elements that are
5275 * copied is unchanged.
5277 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5278 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
5280 template<typename _InputIterator
, typename _OutputIterator
,
5281 typename _BinaryPredicate
>
5282 inline _OutputIterator
5283 unique_copy(_InputIterator __first
, _InputIterator __last
,
5284 _OutputIterator __result
,
5285 _BinaryPredicate __binary_pred
)
5287 // concept requirements -- predicates checked later
5288 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5289 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5290 typename iterator_traits
<_InputIterator
>::value_type
>)
5291 __glibcxx_requires_valid_range(__first
, __last
);
5293 if (__first
== __last
)
5295 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
5296 std::__iterator_category(__first
),
5297 std::__iterator_category(__result
));
5302 * @brief Randomly shuffle the elements of a sequence.
5303 * @ingroup mutating_algorithms
5304 * @param __first A forward iterator.
5305 * @param __last A forward iterator.
5308 * Reorder the elements in the range @p [__first,__last) using a random
5309 * distribution, so that every possible ordering of the sequence is
5312 template<typename _RandomAccessIterator
>
5314 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5316 // concept requirements
5317 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5318 _RandomAccessIterator
>)
5319 __glibcxx_requires_valid_range(__first
, __last
);
5321 if (__first
!= __last
)
5322 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5323 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
5327 * @brief Shuffle the elements of a sequence using a random number
5329 * @ingroup mutating_algorithms
5330 * @param __first A forward iterator.
5331 * @param __last A forward iterator.
5332 * @param __rand The RNG functor or function.
5335 * Reorders the elements in the range @p [__first,__last) using @p __rand to
5336 * provide a random distribution. Calling @p __rand(N) for a positive
5337 * integer @p N should return a randomly chosen integer from the
5340 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
5342 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5343 #if __cplusplus >= 201103L
5344 _RandomNumberGenerator
&& __rand
)
5346 _RandomNumberGenerator
& __rand
)
5349 // concept requirements
5350 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5351 _RandomAccessIterator
>)
5352 __glibcxx_requires_valid_range(__first
, __last
);
5354 if (__first
== __last
)
5356 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
5357 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
5362 * @brief Move elements for which a predicate is true to the beginning
5364 * @ingroup mutating_algorithms
5365 * @param __first A forward iterator.
5366 * @param __last A forward iterator.
5367 * @param __pred A predicate functor.
5368 * @return An iterator @p middle such that @p __pred(i) is true for each
5369 * iterator @p i in the range @p [__first,middle) and false for each @p i
5370 * in the range @p [middle,__last).
5372 * @p __pred must not modify its operand. @p partition() does not preserve
5373 * the relative ordering of elements in each group, use
5374 * @p stable_partition() if this is needed.
5376 template<typename _ForwardIterator
, typename _Predicate
>
5377 inline _ForwardIterator
5378 partition(_ForwardIterator __first
, _ForwardIterator __last
,
5381 // concept requirements
5382 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
5384 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
5385 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5386 __glibcxx_requires_valid_range(__first
, __last
);
5388 return std::__partition(__first
, __last
, __pred
,
5389 std::__iterator_category(__first
));
5395 * @brief Sort the smallest elements of a sequence.
5396 * @ingroup sorting_algorithms
5397 * @param __first An iterator.
5398 * @param __middle Another iterator.
5399 * @param __last Another iterator.
5402 * Sorts the smallest @p (__middle-__first) elements in the range
5403 * @p [first,last) and moves them to the range @p [__first,__middle). The
5404 * order of the remaining elements in the range @p [__middle,__last) is
5406 * After the sort if @e i and @e j are iterators in the range
5407 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
5408 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
5410 template<typename _RandomAccessIterator
>
5412 partial_sort(_RandomAccessIterator __first
,
5413 _RandomAccessIterator __middle
,
5414 _RandomAccessIterator __last
)
5416 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5419 // concept requirements
5420 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5421 _RandomAccessIterator
>)
5422 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5423 __glibcxx_requires_valid_range(__first
, __middle
);
5424 __glibcxx_requires_valid_range(__middle
, __last
);
5426 std::__heap_select(__first
, __middle
, __last
);
5427 std::sort_heap(__first
, __middle
);
5431 * @brief Sort the smallest elements of a sequence using a predicate
5433 * @ingroup sorting_algorithms
5434 * @param __first An iterator.
5435 * @param __middle Another iterator.
5436 * @param __last Another iterator.
5437 * @param __comp A comparison functor.
5440 * Sorts the smallest @p (__middle-__first) elements in the range
5441 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
5442 * order of the remaining elements in the range @p [__middle,__last) is
5444 * After the sort if @e i and @e j are iterators in the range
5445 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
5446 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
5449 template<typename _RandomAccessIterator
, typename _Compare
>
5451 partial_sort(_RandomAccessIterator __first
,
5452 _RandomAccessIterator __middle
,
5453 _RandomAccessIterator __last
,
5456 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5459 // concept requirements
5460 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5461 _RandomAccessIterator
>)
5462 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5463 _ValueType
, _ValueType
>)
5464 __glibcxx_requires_valid_range(__first
, __middle
);
5465 __glibcxx_requires_valid_range(__middle
, __last
);
5467 std::__heap_select(__first
, __middle
, __last
, __comp
);
5468 std::sort_heap(__first
, __middle
, __comp
);
5472 * @brief Sort a sequence just enough to find a particular position.
5473 * @ingroup sorting_algorithms
5474 * @param __first An iterator.
5475 * @param __nth Another iterator.
5476 * @param __last Another iterator.
5479 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5480 * is the same element that would have been in that position had the
5481 * whole sequence been sorted. The elements either side of @p *__nth are
5482 * not completely sorted, but for any iterator @e i in the range
5483 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
5484 * holds that *j < *i is false.
5486 template<typename _RandomAccessIterator
>
5488 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5489 _RandomAccessIterator __last
)
5491 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5494 // concept requirements
5495 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5496 _RandomAccessIterator
>)
5497 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5498 __glibcxx_requires_valid_range(__first
, __nth
);
5499 __glibcxx_requires_valid_range(__nth
, __last
);
5501 if (__first
== __last
|| __nth
== __last
)
5504 std::__introselect(__first
, __nth
, __last
,
5505 std::__lg(__last
- __first
) * 2);
5509 * @brief Sort a sequence just enough to find a particular position
5510 * using a predicate for comparison.
5511 * @ingroup sorting_algorithms
5512 * @param __first An iterator.
5513 * @param __nth Another iterator.
5514 * @param __last Another iterator.
5515 * @param __comp A comparison functor.
5518 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
5519 * is the same element that would have been in that position had the
5520 * whole sequence been sorted. The elements either side of @p *__nth are
5521 * not completely sorted, but for any iterator @e i in the range
5522 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
5523 * holds that @p __comp(*j,*i) is false.
5525 template<typename _RandomAccessIterator
, typename _Compare
>
5527 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
5528 _RandomAccessIterator __last
, _Compare __comp
)
5530 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5533 // concept requirements
5534 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5535 _RandomAccessIterator
>)
5536 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5537 _ValueType
, _ValueType
>)
5538 __glibcxx_requires_valid_range(__first
, __nth
);
5539 __glibcxx_requires_valid_range(__nth
, __last
);
5541 if (__first
== __last
|| __nth
== __last
)
5544 std::__introselect(__first
, __nth
, __last
,
5545 std::__lg(__last
- __first
) * 2, __comp
);
5550 * @brief Sort the elements of a sequence.
5551 * @ingroup sorting_algorithms
5552 * @param __first An iterator.
5553 * @param __last Another iterator.
5556 * Sorts the elements in the range @p [__first,__last) in ascending order,
5557 * such that for each iterator @e i in the range @p [__first,__last-1),
5558 * *(i+1)<*i is false.
5560 * The relative ordering of equivalent elements is not preserved, use
5561 * @p stable_sort() if this is needed.
5563 template<typename _RandomAccessIterator
>
5565 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5567 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5570 // concept requirements
5571 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5572 _RandomAccessIterator
>)
5573 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5574 __glibcxx_requires_valid_range(__first
, __last
);
5576 if (__first
!= __last
)
5578 std::__introsort_loop(__first
, __last
,
5579 std::__lg(__last
- __first
) * 2);
5580 std::__final_insertion_sort(__first
, __last
);
5585 * @brief Sort the elements of a sequence using a predicate for comparison.
5586 * @ingroup sorting_algorithms
5587 * @param __first An iterator.
5588 * @param __last Another iterator.
5589 * @param __comp A comparison functor.
5592 * Sorts the elements in the range @p [__first,__last) in ascending order,
5593 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
5594 * range @p [__first,__last-1).
5596 * The relative ordering of equivalent elements is not preserved, use
5597 * @p stable_sort() if this is needed.
5599 template<typename _RandomAccessIterator
, typename _Compare
>
5601 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5604 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5607 // concept requirements
5608 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5609 _RandomAccessIterator
>)
5610 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
5612 __glibcxx_requires_valid_range(__first
, __last
);
5614 if (__first
!= __last
)
5616 std::__introsort_loop(__first
, __last
,
5617 std::__lg(__last
- __first
) * 2, __comp
);
5618 std::__final_insertion_sort(__first
, __last
, __comp
);
5623 * @brief Merges two sorted ranges.
5624 * @ingroup sorting_algorithms
5625 * @param __first1 An iterator.
5626 * @param __first2 Another iterator.
5627 * @param __last1 Another iterator.
5628 * @param __last2 Another iterator.
5629 * @param __result An iterator pointing to the end of the merged range.
5630 * @return An iterator pointing to the first element <em>not less
5633 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5634 * the sorted range @p [__result, __result + (__last1-__first1) +
5635 * (__last2-__first2)). Both input ranges must be sorted, and the
5636 * output range must not overlap with either of the input ranges.
5637 * The sort is @e stable, that is, for equivalent elements in the
5638 * two ranges, elements from the first range will always come
5639 * before elements from the second.
5641 template<typename _InputIterator1
, typename _InputIterator2
,
5642 typename _OutputIterator
>
5644 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5645 _InputIterator2 __first2
, _InputIterator2 __last2
,
5646 _OutputIterator __result
)
5648 typedef typename iterator_traits
<_InputIterator1
>::value_type
5650 typedef typename iterator_traits
<_InputIterator2
>::value_type
5653 // concept requirements
5654 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5655 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5656 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5658 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5660 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5661 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5662 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5664 while (__first1
!= __last1
&& __first2
!= __last2
)
5666 if (*__first2
< *__first1
)
5668 *__result
= *__first2
;
5673 *__result
= *__first1
;
5678 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5683 * @brief Merges two sorted ranges.
5684 * @ingroup sorting_algorithms
5685 * @param __first1 An iterator.
5686 * @param __first2 Another iterator.
5687 * @param __last1 Another iterator.
5688 * @param __last2 Another iterator.
5689 * @param __result An iterator pointing to the end of the merged range.
5690 * @param __comp A functor to use for comparisons.
5691 * @return An iterator pointing to the first element "not less
5694 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
5695 * the sorted range @p [__result, __result + (__last1-__first1) +
5696 * (__last2-__first2)). Both input ranges must be sorted, and the
5697 * output range must not overlap with either of the input ranges.
5698 * The sort is @e stable, that is, for equivalent elements in the
5699 * two ranges, elements from the first range will always come
5700 * before elements from the second.
5702 * The comparison function should have the same effects on ordering as
5703 * the function used for the initial sort.
5705 template<typename _InputIterator1
, typename _InputIterator2
,
5706 typename _OutputIterator
, typename _Compare
>
5708 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
5709 _InputIterator2 __first2
, _InputIterator2 __last2
,
5710 _OutputIterator __result
, _Compare __comp
)
5712 typedef typename iterator_traits
<_InputIterator1
>::value_type
5714 typedef typename iterator_traits
<_InputIterator2
>::value_type
5717 // concept requirements
5718 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5719 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5720 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5722 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5724 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5725 _ValueType2
, _ValueType1
>)
5726 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5727 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5729 while (__first1
!= __last1
&& __first2
!= __last2
)
5731 if (__comp(*__first2
, *__first1
))
5733 *__result
= *__first2
;
5738 *__result
= *__first1
;
5743 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5749 * @brief Sort the elements of a sequence, preserving the relative order
5750 * of equivalent elements.
5751 * @ingroup sorting_algorithms
5752 * @param __first An iterator.
5753 * @param __last Another iterator.
5756 * Sorts the elements in the range @p [__first,__last) in ascending order,
5757 * such that for each iterator @p i in the range @p [__first,__last-1),
5758 * @p *(i+1)<*i is false.
5760 * The relative ordering of equivalent elements is preserved, so any two
5761 * elements @p x and @p y in the range @p [__first,__last) such that
5762 * @p x<y is false and @p y<x is false will have the same relative
5763 * ordering after calling @p stable_sort().
5765 template<typename _RandomAccessIterator
>
5767 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
5769 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5771 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5774 // concept requirements
5775 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5776 _RandomAccessIterator
>)
5777 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
5778 __glibcxx_requires_valid_range(__first
, __last
);
5780 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5782 if (__buf
.begin() == 0)
5783 std::__inplace_stable_sort(__first
, __last
);
5785 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5786 _DistanceType(__buf
.size()));
5790 * @brief Sort the elements of a sequence using a predicate for comparison,
5791 * preserving the relative order of equivalent elements.
5792 * @ingroup sorting_algorithms
5793 * @param __first An iterator.
5794 * @param __last Another iterator.
5795 * @param __comp A comparison functor.
5798 * Sorts the elements in the range @p [__first,__last) in ascending order,
5799 * such that for each iterator @p i in the range @p [__first,__last-1),
5800 * @p __comp(*(i+1),*i) is false.
5802 * The relative ordering of equivalent elements is preserved, so any two
5803 * elements @p x and @p y in the range @p [__first,__last) such that
5804 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5805 * relative ordering after calling @p stable_sort().
5807 template<typename _RandomAccessIterator
, typename _Compare
>
5809 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
5812 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
5814 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
5817 // concept requirements
5818 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
5819 _RandomAccessIterator
>)
5820 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5823 __glibcxx_requires_valid_range(__first
, __last
);
5825 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
5827 if (__buf
.begin() == 0)
5828 std::__inplace_stable_sort(__first
, __last
, __comp
);
5830 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
5831 _DistanceType(__buf
.size()), __comp
);
5836 * @brief Return the union of two sorted ranges.
5837 * @ingroup set_algorithms
5838 * @param __first1 Start of first range.
5839 * @param __last1 End of first range.
5840 * @param __first2 Start of second range.
5841 * @param __last2 End of second range.
5842 * @return End of the output range.
5843 * @ingroup set_algorithms
5845 * This operation iterates over both ranges, copying elements present in
5846 * each range in order to the output range. Iterators increment for each
5847 * range. When the current element of one range is less than the other,
5848 * that element is copied and the iterator advanced. If an element is
5849 * contained in both ranges, the element from the first range is copied and
5850 * both ranges advance. The output range may not overlap either input
5853 template<typename _InputIterator1
, typename _InputIterator2
,
5854 typename _OutputIterator
>
5856 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5857 _InputIterator2 __first2
, _InputIterator2 __last2
,
5858 _OutputIterator __result
)
5860 typedef typename iterator_traits
<_InputIterator1
>::value_type
5862 typedef typename iterator_traits
<_InputIterator2
>::value_type
5865 // concept requirements
5866 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5867 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5868 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5870 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5872 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5873 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5874 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
5875 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
5877 while (__first1
!= __last1
&& __first2
!= __last2
)
5879 if (*__first1
< *__first2
)
5881 *__result
= *__first1
;
5884 else if (*__first2
< *__first1
)
5886 *__result
= *__first2
;
5891 *__result
= *__first1
;
5897 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5902 * @brief Return the union of two sorted ranges using a comparison functor.
5903 * @ingroup set_algorithms
5904 * @param __first1 Start of first range.
5905 * @param __last1 End of first range.
5906 * @param __first2 Start of second range.
5907 * @param __last2 End of second range.
5908 * @param __comp The comparison functor.
5909 * @return End of the output range.
5910 * @ingroup set_algorithms
5912 * This operation iterates over both ranges, copying elements present in
5913 * each range in order to the output range. Iterators increment for each
5914 * range. When the current element of one range is less than the other
5915 * according to @p __comp, that element is copied and the iterator advanced.
5916 * If an equivalent element according to @p __comp is contained in both
5917 * ranges, the element from the first range is copied and both ranges
5918 * advance. The output range may not overlap either input range.
5920 template<typename _InputIterator1
, typename _InputIterator2
,
5921 typename _OutputIterator
, typename _Compare
>
5923 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
5924 _InputIterator2 __first2
, _InputIterator2 __last2
,
5925 _OutputIterator __result
, _Compare __comp
)
5927 typedef typename iterator_traits
<_InputIterator1
>::value_type
5929 typedef typename iterator_traits
<_InputIterator2
>::value_type
5932 // concept requirements
5933 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5934 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5935 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5937 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5939 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5940 _ValueType1
, _ValueType2
>)
5941 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5942 _ValueType2
, _ValueType1
>)
5943 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
5944 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
5946 while (__first1
!= __last1
&& __first2
!= __last2
)
5948 if (__comp(*__first1
, *__first2
))
5950 *__result
= *__first1
;
5953 else if (__comp(*__first2
, *__first1
))
5955 *__result
= *__first2
;
5960 *__result
= *__first1
;
5966 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
5971 * @brief Return the intersection of two sorted ranges.
5972 * @ingroup set_algorithms
5973 * @param __first1 Start of first range.
5974 * @param __last1 End of first range.
5975 * @param __first2 Start of second range.
5976 * @param __last2 End of second range.
5977 * @return End of the output range.
5978 * @ingroup set_algorithms
5980 * This operation iterates over both ranges, copying elements present in
5981 * both ranges in order to the output range. Iterators increment for each
5982 * range. When the current element of one range is less than the other,
5983 * that iterator advances. If an element is contained in both ranges, the
5984 * element from the first range is copied and both ranges advance. The
5985 * output range may not overlap either input range.
5987 template<typename _InputIterator1
, typename _InputIterator2
,
5988 typename _OutputIterator
>
5990 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
5991 _InputIterator2 __first2
, _InputIterator2 __last2
,
5992 _OutputIterator __result
)
5994 typedef typename iterator_traits
<_InputIterator1
>::value_type
5996 typedef typename iterator_traits
<_InputIterator2
>::value_type
5999 // concept requirements
6000 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6001 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6002 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6004 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6005 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6006 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6007 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6009 while (__first1
!= __last1
&& __first2
!= __last2
)
6010 if (*__first1
< *__first2
)
6012 else if (*__first2
< *__first1
)
6016 *__result
= *__first1
;
6025 * @brief Return the intersection of two sorted ranges using comparison
6027 * @ingroup set_algorithms
6028 * @param __first1 Start of first range.
6029 * @param __last1 End of first range.
6030 * @param __first2 Start of second range.
6031 * @param __last2 End of second range.
6032 * @param __comp The comparison functor.
6033 * @return End of the output range.
6034 * @ingroup set_algorithms
6036 * This operation iterates over both ranges, copying elements present in
6037 * both ranges in order to the output range. Iterators increment for each
6038 * range. When the current element of one range is less than the other
6039 * according to @p __comp, that iterator advances. If an element is
6040 * contained in both ranges according to @p __comp, the element from the
6041 * first range is copied and both ranges advance. The output range may not
6042 * overlap either input range.
6044 template<typename _InputIterator1
, typename _InputIterator2
,
6045 typename _OutputIterator
, typename _Compare
>
6047 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
6048 _InputIterator2 __first2
, _InputIterator2 __last2
,
6049 _OutputIterator __result
, _Compare __comp
)
6051 typedef typename iterator_traits
<_InputIterator1
>::value_type
6053 typedef typename iterator_traits
<_InputIterator2
>::value_type
6056 // concept requirements
6057 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6058 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6059 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6061 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6062 _ValueType1
, _ValueType2
>)
6063 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6064 _ValueType2
, _ValueType1
>)
6065 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6066 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6068 while (__first1
!= __last1
&& __first2
!= __last2
)
6069 if (__comp(*__first1
, *__first2
))
6071 else if (__comp(*__first2
, *__first1
))
6075 *__result
= *__first1
;
6084 * @brief Return the difference of two sorted ranges.
6085 * @ingroup set_algorithms
6086 * @param __first1 Start of first range.
6087 * @param __last1 End of first range.
6088 * @param __first2 Start of second range.
6089 * @param __last2 End of second range.
6090 * @return End of the output range.
6091 * @ingroup set_algorithms
6093 * This operation iterates over both ranges, copying elements present in
6094 * the first range but not the second in order to the output range.
6095 * Iterators increment for each range. When the current element of the
6096 * first range is less than the second, that element is copied and the
6097 * iterator advances. If the current element of the second range is less,
6098 * the iterator advances, but no element is copied. If an element is
6099 * contained in both ranges, no elements are copied and both ranges
6100 * advance. The output range may not overlap either input range.
6102 template<typename _InputIterator1
, typename _InputIterator2
,
6103 typename _OutputIterator
>
6105 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6106 _InputIterator2 __first2
, _InputIterator2 __last2
,
6107 _OutputIterator __result
)
6109 typedef typename iterator_traits
<_InputIterator1
>::value_type
6111 typedef typename iterator_traits
<_InputIterator2
>::value_type
6114 // concept requirements
6115 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6116 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6117 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6119 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6120 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6121 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6122 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6124 while (__first1
!= __last1
&& __first2
!= __last2
)
6125 if (*__first1
< *__first2
)
6127 *__result
= *__first1
;
6131 else if (*__first2
< *__first1
)
6138 return std::copy(__first1
, __last1
, __result
);
6142 * @brief Return the difference of two sorted ranges using comparison
6144 * @ingroup set_algorithms
6145 * @param __first1 Start of first range.
6146 * @param __last1 End of first range.
6147 * @param __first2 Start of second range.
6148 * @param __last2 End of second range.
6149 * @param __comp The comparison functor.
6150 * @return End of the output range.
6151 * @ingroup set_algorithms
6153 * This operation iterates over both ranges, copying elements present in
6154 * the first range but not the second in order to the output range.
6155 * Iterators increment for each range. When the current element of the
6156 * first range is less than the second according to @p __comp, that element
6157 * is copied and the iterator advances. If the current element of the
6158 * second range is less, no element is copied and the iterator advances.
6159 * If an element is contained in both ranges according to @p __comp, no
6160 * elements are copied and both ranges advance. The output range may not
6161 * overlap either input range.
6163 template<typename _InputIterator1
, typename _InputIterator2
,
6164 typename _OutputIterator
, typename _Compare
>
6166 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6167 _InputIterator2 __first2
, _InputIterator2 __last2
,
6168 _OutputIterator __result
, _Compare __comp
)
6170 typedef typename iterator_traits
<_InputIterator1
>::value_type
6172 typedef typename iterator_traits
<_InputIterator2
>::value_type
6175 // concept requirements
6176 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6177 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6178 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6180 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6181 _ValueType1
, _ValueType2
>)
6182 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6183 _ValueType2
, _ValueType1
>)
6184 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6185 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6187 while (__first1
!= __last1
&& __first2
!= __last2
)
6188 if (__comp(*__first1
, *__first2
))
6190 *__result
= *__first1
;
6194 else if (__comp(*__first2
, *__first1
))
6201 return std::copy(__first1
, __last1
, __result
);
6205 * @brief Return the symmetric difference of two sorted ranges.
6206 * @ingroup set_algorithms
6207 * @param __first1 Start of first range.
6208 * @param __last1 End of first range.
6209 * @param __first2 Start of second range.
6210 * @param __last2 End of second range.
6211 * @return End of the output range.
6212 * @ingroup set_algorithms
6214 * This operation iterates over both ranges, copying elements present in
6215 * one range but not the other in order to the output range. Iterators
6216 * increment for each range. When the current element of one range is less
6217 * than the other, that element is copied and the iterator advances. If an
6218 * element is contained in both ranges, no elements are copied and both
6219 * ranges advance. The output range may not overlap either input range.
6221 template<typename _InputIterator1
, typename _InputIterator2
,
6222 typename _OutputIterator
>
6224 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6225 _InputIterator2 __first2
, _InputIterator2 __last2
,
6226 _OutputIterator __result
)
6228 typedef typename iterator_traits
<_InputIterator1
>::value_type
6230 typedef typename iterator_traits
<_InputIterator2
>::value_type
6233 // concept requirements
6234 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6235 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6236 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6238 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6240 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
6241 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
6242 __glibcxx_requires_sorted_set(__first1
, __last1
, __first2
);
6243 __glibcxx_requires_sorted_set(__first2
, __last2
, __first1
);
6245 while (__first1
!= __last1
&& __first2
!= __last2
)
6246 if (*__first1
< *__first2
)
6248 *__result
= *__first1
;
6252 else if (*__first2
< *__first1
)
6254 *__result
= *__first2
;
6263 return std::copy(__first2
, __last2
, std::copy(__first1
,
6264 __last1
, __result
));
6268 * @brief Return the symmetric difference of two sorted ranges using
6269 * comparison functor.
6270 * @ingroup set_algorithms
6271 * @param __first1 Start of first range.
6272 * @param __last1 End of first range.
6273 * @param __first2 Start of second range.
6274 * @param __last2 End of second range.
6275 * @param __comp The comparison functor.
6276 * @return End of the output range.
6277 * @ingroup set_algorithms
6279 * This operation iterates over both ranges, copying elements present in
6280 * one range but not the other in order to the output range. Iterators
6281 * increment for each range. When the current element of one range is less
6282 * than the other according to @p comp, that element is copied and the
6283 * iterator advances. If an element is contained in both ranges according
6284 * to @p __comp, no elements are copied and both ranges advance. The output
6285 * range may not overlap either input range.
6287 template<typename _InputIterator1
, typename _InputIterator2
,
6288 typename _OutputIterator
, typename _Compare
>
6290 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
6291 _InputIterator2 __first2
, _InputIterator2 __last2
,
6292 _OutputIterator __result
,
6295 typedef typename iterator_traits
<_InputIterator1
>::value_type
6297 typedef typename iterator_traits
<_InputIterator2
>::value_type
6300 // concept requirements
6301 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
6302 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
6303 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6305 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
6307 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6308 _ValueType1
, _ValueType2
>)
6309 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6310 _ValueType2
, _ValueType1
>)
6311 __glibcxx_requires_sorted_set_pred(__first1
, __last1
, __first2
, __comp
);
6312 __glibcxx_requires_sorted_set_pred(__first2
, __last2
, __first1
, __comp
);
6314 while (__first1
!= __last1
&& __first2
!= __last2
)
6315 if (__comp(*__first1
, *__first2
))
6317 *__result
= *__first1
;
6321 else if (__comp(*__first2
, *__first1
))
6323 *__result
= *__first2
;
6332 return std::copy(__first2
, __last2
,
6333 std::copy(__first1
, __last1
, __result
));
6338 * @brief Return the minimum element in a range.
6339 * @ingroup sorting_algorithms
6340 * @param __first Start of range.
6341 * @param __last End of range.
6342 * @return Iterator referencing the first instance of the smallest value.
6344 template<typename _ForwardIterator
>
6346 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
6348 // concept requirements
6349 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6350 __glibcxx_function_requires(_LessThanComparableConcept
<
6351 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6352 __glibcxx_requires_valid_range(__first
, __last
);
6354 if (__first
== __last
)
6356 _ForwardIterator __result
= __first
;
6357 while (++__first
!= __last
)
6358 if (*__first
< *__result
)
6364 * @brief Return the minimum element in a range using comparison functor.
6365 * @ingroup sorting_algorithms
6366 * @param __first Start of range.
6367 * @param __last End of range.
6368 * @param __comp Comparison functor.
6369 * @return Iterator referencing the first instance of the smallest value
6370 * according to __comp.
6372 template<typename _ForwardIterator
, typename _Compare
>
6374 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
6377 // concept requirements
6378 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6379 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6380 typename iterator_traits
<_ForwardIterator
>::value_type
,
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 (__comp(*__first
, *__result
))
6394 * @brief Return the maximum element in a range.
6395 * @ingroup sorting_algorithms
6396 * @param __first Start of range.
6397 * @param __last End of range.
6398 * @return Iterator referencing the first instance of the largest value.
6400 template<typename _ForwardIterator
>
6402 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
6404 // concept requirements
6405 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6406 __glibcxx_function_requires(_LessThanComparableConcept
<
6407 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6408 __glibcxx_requires_valid_range(__first
, __last
);
6410 if (__first
== __last
)
6412 _ForwardIterator __result
= __first
;
6413 while (++__first
!= __last
)
6414 if (*__result
< *__first
)
6420 * @brief Return the maximum element in a range using comparison functor.
6421 * @ingroup sorting_algorithms
6422 * @param __first Start of range.
6423 * @param __last End of range.
6424 * @param __comp Comparison functor.
6425 * @return Iterator referencing the first instance of the largest value
6426 * according to __comp.
6428 template<typename _ForwardIterator
, typename _Compare
>
6430 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
6433 // concept requirements
6434 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
6435 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
6436 typename iterator_traits
<_ForwardIterator
>::value_type
,
6437 typename iterator_traits
<_ForwardIterator
>::value_type
>)
6438 __glibcxx_requires_valid_range(__first
, __last
);
6440 if (__first
== __last
) return __first
;
6441 _ForwardIterator __result
= __first
;
6442 while (++__first
!= __last
)
6443 if (__comp(*__result
, *__first
))
6448 _GLIBCXX_END_NAMESPACE_ALGO
6451 #endif /* _STL_ALGO_H */