This is an overload used by find() for the Input Iterator case.
This is an overload used by find_if() for the Input Iterator case.
This is an overload used by find() for the RAI case.
This is an overload used by find_if() for the RAI case.
This is an overload used by find_if_not() for the Input Iterator case.
This is an overload used by find_if_not() for the RAI case.
Provided for stable_partition to use.
Like find_if_not(), but uses and updates a count of the remaining range length instead of comparing against an end iterator.
This is an uglified search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&) overloaded for forward iterators.
This is an uglified search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&) overloaded for random access iterators.
This is an uglified search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&, _BinaryPredicate) overloaded for forward iterators.
This is an uglified search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&, _BinaryPredicate) overloaded for random access iterators.
Find last matching subsequence in a sequence.
Find last matching subsequence in a sequence using a predicate.
Copy a sequence, removing elements of a given value.
Copy a sequence, removing elements for which a predicate is true.
remove_copy_if() is stable, so the relative order of elements that are copied is unchanged.
Remove elements from a sequence.
remove() is stable, so the relative order of elements that are not removed is unchanged.
Remove elements from a sequence using a predicate.
remove_if() is stable, so the relative order of elements that are not removed is unchanged.
Remove consecutive duplicate values from a sequence.
Removes all but the first element from each group of consecutive values that compare equal. unique() is stable, so the relative order of elements that are not removed is unchanged. Elements between the end of the resulting sequence and __last
are still present, but their value is unspecified.
Remove consecutive values from a sequence using a predicate.
Removes all but the first element from each group of consecutive values for which __binary_pred
returns true. unique() is stable, so the relative order of elements that are not removed is unchanged. Elements between the end of the resulting sequence and __last
are still present, but their value is unspecified.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator) overloaded for forward iterators and output iterator as result.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator) overloaded for input iterators and output iterator as result.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator) overloaded for input iterators and forward iterator as result.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate) overloaded for forward iterators and output iterator as result.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate) overloaded for input iterators and output iterator as result.
This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate) overloaded for input iterators and forward iterator as result.
This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator) overloaded for bidirectional iterators.
This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator) overloaded for random access iterators.
Reverse a sequence.
Copy a sequence, reversing its elements.
This is a helper function for the rotate algorithm specialized on RAIs. It returns the greatest common divisor of two integer values.
This is a helper function for the rotate algorithm.
This is a helper function for the rotate algorithm.
This is a helper function for the rotate algorithm.
Rotate the elements of a sequence.
Copy a sequence, rotating its elements.
This is a helper function...
This is a helper function...
This is a helper function... Requires __len != 0 and !__pred(*__first), same as __stable_partition_adaptive.
This is a helper function... Requires __first != __last and !__pred(*__first) and __len == distance(__first, __last).
!__pred(*__first) allows us to guarantee that we don't move-assign an element onto itself.
Move elements for which a predicate is true to the beginning of a sequence, preserving relative ordering.
This is a helper function for the sort routines.
This is a helper function for the sort routines.
Copy the smallest elements of a sequence.
Copy the smallest elements of a sequence using a predicate for comparison.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This controls some aspect of the sort routines.
This is a helper function for the sort routine.
This is a helper function for the sort routine.
This is a helper function...
This is a helper function...
This is a helper function...
This is a helper function...
This is a helper function for the sort routine.
This is a helper function for the sort routine.
The comparison function should have the same effects on ordering as the function used for the initial sort.
The comparison function should have the same effects on ordering as the function used for the initial sort.
but does not actually call those functions.
but does not actually call those functions.
Determines whether an element exists in a range.
Determines whether an element exists in a range.
The comparison function should have the same effects on ordering as the function used for the initial sort.
This is a helper function for the __merge_adaptive routines.
This is a helper function for the __merge_adaptive routines.
This is a helper function for the __merge_adaptive routines.
This is a helper function for the __merge_adaptive routines.
This is a helper function for the merge routines.
This is a helper function for the merge routines.
This is a helper function for the merge routines.
This is a helper function for the merge routines.
This is a helper function for the merge routines.
Merges two sorted ranges in place.
Merges two sorted and consecutive ranges, [__first,__middle) and [__middle,__last), and puts the result in [__first,__last). The output will be sorted. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.
If enough additional memory is available, this takes (__last-__first)-1 comparisons. Otherwise an NlogN algorithm is used, where N is distance(__first,__last).
Merges two sorted ranges in place.
Merges two sorted and consecutive ranges, [__first,__middle) and [middle,last), and puts the result in [__first,__last). The output will be sorted. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.
If enough additional memory is available, this takes (__last-__first)-1 comparisons. Otherwise an NlogN algorithm is used, where N is distance(__first,__last).
The comparison function should have the same effects on ordering as the function used for the initial sort.
This is a helper function for the __merge_sort_loop routines.
This is a helper function for the __merge_sort_loop routines.
This is a helper function for the stable sorting routines.
This is a helper function for the stable sorting routines.
Determines whether all elements of a sequence exists in a range.
This operation expects both [__first1,__last1) and [__first2,__last2) to be sorted. Searches for the presence of each element in [__first2,__last2) within [__first1,__last1). The iterators over each range only move forward, so this is a linear algorithm. If an element in [__first2,__last2) is not found before the search iterator reaches __last2
, false is returned.
Determines whether all elements of a sequence exists in a range using comparison.
This operation expects both [__first1,__last1) and [__first2,__last2) to be sorted. Searches for the presence of each element in [__first2,__last2) within [__first1,__last1), using comp to decide. The iterators over each range only move forward, so this is a linear algorithm. If an element in [__first2,__last2) is not found before the search iterator reaches __last2
, false is returned.
Copy a sequence, replacing each element of one value with another value.
Copy a sequence, replacing each value for which a predicate returns true with another value.
Apply a function to every element of a sequence.
Find the first occurrence of a value in a sequence.
Find the first element in a sequence for which a predicate is true.
Find element from a set in a sequence.
Find element from a set in a sequence using a predicate.
Find two adjacent values in a sequence that are equal.
Find two adjacent values in a sequence using a predicate.
Count the number of copies of a value in a sequence.
Count the elements of a sequence for which a predicate is true.
Search a sequence for a matching sub-sequence.
Search a sequence for a matching sub-sequence using a predicate.
Search a sequence for a number of consecutive values.
Search a sequence for a number of consecutive values using a predicate.
Perform an operation on a sequence.
Applies the operator to each element in the input range and assigns the results to successive elements of the output sequence. Evaluates *
(__result+N)=unary_op(*(__first+N)) for each N
in the range
[0,__last-__first).
Perform an operation on corresponding elements of two sequences.
Applies the operator to the corresponding elements in the two input ranges and assigns the results to successive elements of the output sequence. Evaluates *
(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each N
in the range
[0,__last1-__first1).
Replace each occurrence of one value in a sequence with another value.
Replace each value in a sequence for which a predicate returns true with another value.
Assign the result of a function object to each value in a sequence.
Assign the result of a function object to each value in a sequence.
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 865. More algorithms that throw away information
Copy a sequence, removing consecutive duplicate values.
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 241. Does unique_copy() require CopyConstructible and Assignable?
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 538. 241 again: Does unique_copy() require CopyConstructible and Assignable?
Copy a sequence, removing consecutive values using a predicate.
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 241. Does unique_copy() require CopyConstructible and Assignable?
Randomly shuffle the elements of a sequence.
Shuffle the elements of a sequence using a random number generator.
Move elements for which a predicate is true to the beginning of a sequence.
Sort the smallest elements of a sequence.
Sort the smallest elements of a sequence using a predicate for comparison.
Sort a sequence just enough to find a particular position.
Sort a sequence just enough to find a particular position using a predicate for comparison.
Sort the elements of a sequence.
Sort the elements of a sequence using a predicate for comparison.
Merges two sorted ranges.
Merges two sorted ranges.
The comparison function should have the same effects on ordering as the function used for the initial sort.
Sort the elements of a sequence, preserving the relative order of equivalent elements.
Sort the elements of a sequence using a predicate for comparison, preserving the relative order of equivalent elements.
Return the union of two sorted ranges.
This operation iterates over both ranges, copying elements present in each range in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that element is copied and the iterator advanced. If an element is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.
Return the union of two sorted ranges using a comparison functor.
This operation iterates over both ranges, copying elements present in each range in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to __comp
, that element is copied and the iterator advanced. If an equivalent element according to __comp
is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.
Return the intersection of two sorted ranges.
This operation iterates over both ranges, copying elements present in both ranges in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that iterator advances. If an element is contained in both ranges, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.
Return the intersection of two sorted ranges using comparison functor.
This operation iterates over both ranges, copying elements present in both ranges in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to __comp
, that iterator advances. If an element is contained in both ranges according to __comp
, the element from the first range is copied and both ranges advance. The output range may not overlap either input range.
Return the difference of two sorted ranges.
This operation iterates over both ranges, copying elements present in the first range but not the second in order to the output range. Iterators increment for each range. When the current element of the first range is less than the second, that element is copied and the iterator advances. If the current element of the second range is less, the iterator advances, but no element is copied. If an element is contained in both ranges, no elements are copied and both ranges advance. The output range may not overlap either input range.
Return the difference of two sorted ranges using comparison functor.
This operation iterates over both ranges, copying elements present in the first range but not the second in order to the output range. Iterators increment for each range. When the current element of the first range is less than the second according to __comp
, that element is copied and the iterator advances. If the current element of the second range is less, no element is copied and the iterator advances. If an element is contained in both ranges according to __comp
, no elements are copied and both ranges advance. The output range may not overlap either input range.
Return the symmetric difference of two sorted ranges.
This operation iterates over both ranges, copying elements present in one range but not the other in order to the output range. Iterators increment for each range. When the current element of one range is less than the other, that element is copied and the iterator advances. If an element is contained in both ranges, no elements are copied and both ranges advance. The output range may not overlap either input range.
Return the symmetric difference of two sorted ranges using comparison functor.
This operation iterates over both ranges, copying elements present in one range but not the other in order to the output range. Iterators increment for each range. When the current element of one range is less than the other according to comp
, that element is copied and the iterator advances. If an element is contained in both ranges according to __comp
, no elements are copied and both ranges advance. The output range may not overlap either input range.
Return the minimum element in a range.
Return the minimum element in a range using comparison functor.
Return the maximum element in a range.
Return the maximum element in a range using comparison functor.
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 template<
typename _Iterator>
78 __move_median_to_first(_Iterator __result, _Iterator __a,
79 _Iterator __b, _Iterator __c)
83 typename iterator_traits<_Iterator>::value_type>)
88 std::iter_swap(__result, __b);
90 std::iter_swap(__result, __c);
92 std::iter_swap(__result, __a);
95 std::iter_swap(__result, __a);
97 std::iter_swap(__result, __c);
99 std::iter_swap(__result, __b);
103 template<
typename _Iterator,
typename _Compare>
105 __move_median_to_first(_Iterator __result, _Iterator __a,
106 _Iterator __b, _Iterator __c,
111 typename iterator_traits<_Iterator>::value_type,
112 typename iterator_traits<_Iterator>::value_type>)
114 if (__comp(*__a, *__b))
116 if (__comp(*__b, *__c))
117 std::iter_swap(__result, __b);
118 else if (__comp(*__a, *__c))
119 std::iter_swap(__result, __c);
121 std::iter_swap(__result, __a);
123 else if (__comp(*__a, *__c))
124 std::iter_swap(__result, __a);
125 else if (__comp(*__b, *__c))
126 std::iter_swap(__result, __c);
128 std::iter_swap(__result, __b);
134 template<
typename _InputIterator,
typename _Tp>
135 inline _InputIterator
136 __find(_InputIterator __first, _InputIterator __last,
137 const _Tp& __val, input_iterator_tag)
139 while (__first != __last && !(*__first == __val))
145 template<
typename _InputIterator,
typename _Predicate>
146 inline _InputIterator
147 __find_if(_InputIterator __first, _InputIterator __last,
148 _Predicate __pred, input_iterator_tag)
150 while (__first != __last && !
bool(__pred(*__first)))
156 template<
typename _RandomAccessIterator,
typename _Tp>
157 _RandomAccessIterator
158 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
159 const _Tp& __val, random_access_iterator_tag)
161 typename iterator_traits<_RandomAccessIterator>::difference_type
162 __trip_count = (__last - __first) >> 2;
164 for (; __trip_count > 0; --__trip_count)
166 if (*__first == __val)
170 if (*__first == __val)
174 if (*__first == __val)
178 if (*__first == __val)
183 switch (__last - __first)
186 if (*__first == __val)
190 if (*__first == __val)
194 if (*__first == __val)
204 template<
typename _RandomAccessIterator,
typename _Predicate>
205 _RandomAccessIterator
206 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
207 _Predicate __pred, random_access_iterator_tag)
209 typename iterator_traits<_RandomAccessIterator>::difference_type
210 __trip_count = (__last - __first) >> 2;
212 for (; __trip_count > 0; --__trip_count)
214 if (__pred(*__first))
218 if (__pred(*__first))
222 if (__pred(*__first))
226 if (__pred(*__first))
231 switch (__last - __first)
234 if (__pred(*__first))
238 if (__pred(*__first))
242 if (__pred(*__first))
252 template<
typename _InputIterator,
typename _Predicate>
253 inline _InputIterator
254 __find_if_not(_InputIterator __first, _InputIterator __last,
255 _Predicate __pred, input_iterator_tag)
257 while (__first != __last &&
bool(__pred(*__first)))
263 template<
typename _RandomAccessIterator,
typename _Predicate>
264 _RandomAccessIterator
265 __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
266 _Predicate __pred, random_access_iterator_tag)
268 typename iterator_traits<_RandomAccessIterator>::difference_type
269 __trip_count = (__last - __first) >> 2;
271 for (; __trip_count > 0; --__trip_count)
273 if (!
bool(__pred(*__first)))
277 if (!
bool(__pred(*__first)))
281 if (!
bool(__pred(*__first)))
285 if (!
bool(__pred(*__first)))
290 switch (__last - __first)
293 if (!
bool(__pred(*__first)))
297 if (!
bool(__pred(*__first)))
301 if (!
bool(__pred(*__first)))
311 template<
typename _InputIterator,
typename _Predicate>
312 inline _InputIterator
313 __find_if_not(_InputIterator __first, _InputIterator __last,
316 return std::__find_if_not(__first, __last, __pred,
317 std::__iterator_category(__first));
323 template<
typename _InputIterator,
typename _Predicate,
typename _Distance>
325 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
327 for (; __len; --__len, ++__first)
328 if (!
bool(__pred(*__first)))
351 template<
typename _ForwardIterator,
typename _Integer,
typename _Tp>
353 __search_n(_ForwardIterator __first, _ForwardIterator __last,
354 _Integer __count,
const _Tp& __val,
355 std::forward_iterator_tag)
357 __first = _GLIBCXX_STD_A::find(__first, __last, __val);
358 while (__first != __last)
360 typename iterator_traits<_ForwardIterator>::difference_type
362 _ForwardIterator __i = __first;
364 while (__i != __last && __n != 1 && *__i == __val)
373 __first = _GLIBCXX_STD_A::find(++__i, __last, __val);
383 template<
typename _RandomAccessIter,
typename _Integer,
typename _Tp>
385 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
386 _Integer __count,
const _Tp& __val,
387 std::random_access_iterator_tag)
390 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
393 _DistanceType __tailSize = __last - __first;
394 _DistanceType __remainder = __count;
396 while (__remainder <= __tailSize)
398 __first += __remainder;
399 __tailSize -= __remainder;
402 _RandomAccessIter __backTrack = __first;
403 while (*--__backTrack == __val)
405 if (--__remainder == 0)
406 return (__first - __count);
408 __remainder = __count + 1 - (__first - __backTrack);
421 template<
typename _ForwardIterator,
typename _Integer,
typename _Tp,
422 typename _BinaryPredicate>
424 __search_n(_ForwardIterator __first, _ForwardIterator __last,
425 _Integer __count,
const _Tp& __val,
426 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
428 while (__first != __last && !
bool(__binary_pred(*__first, __val)))
431 while (__first != __last)
433 typename iterator_traits<_ForwardIterator>::difference_type
435 _ForwardIterator __i = __first;
437 while (__i != __last && __n != 1 &&
bool(__binary_pred(*__i, __val)))
447 while (__first != __last
448 && !
bool(__binary_pred(*__first, __val)))
460 template<
typename _RandomAccessIter,
typename _Integer,
typename _Tp,
461 typename _BinaryPredicate>
463 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
464 _Integer __count,
const _Tp& __val,
465 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
468 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
471 _DistanceType __tailSize = __last - __first;
472 _DistanceType __remainder = __count;
474 while (__remainder <= __tailSize)
476 __first += __remainder;
477 __tailSize -= __remainder;
480 _RandomAccessIter __backTrack = __first;
481 while (__binary_pred(*--__backTrack, __val))
483 if (--__remainder == 0)
484 return (__first - __count);
486 __remainder = __count + 1 - (__first - __backTrack);
492 template<
typename _ForwardIterator1,
typename _ForwardIterator2>
494 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
495 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
496 forward_iterator_tag, forward_iterator_tag)
498 if (__first2 == __last2)
502 _ForwardIterator1 __result = __last1;
505 _ForwardIterator1 __new_result
506 = _GLIBCXX_STD_A::search(__first1, __last1, __first2, __last2);
507 if (__new_result == __last1)
511 __result = __new_result;
512 __first1 = __new_result;
519 template<
typename _ForwardIterator1,
typename _ForwardIterator2,
520 typename _BinaryPredicate>
522 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
523 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
524 forward_iterator_tag, forward_iterator_tag,
525 _BinaryPredicate __comp)
527 if (__first2 == __last2)
531 _ForwardIterator1 __result = __last1;
534 _ForwardIterator1 __new_result
535 = _GLIBCXX_STD_A::search(__first1, __last1, __first2,
537 if (__new_result == __last1)
541 __result = __new_result;
542 __first1 = __new_result;
550 template<
typename _B
idirectionalIterator1,
typename _B
idirectionalIterator2>
551 _BidirectionalIterator1
552 __find_end(_BidirectionalIterator1 __first1,
553 _BidirectionalIterator1 __last1,
554 _BidirectionalIterator2 __first2,
555 _BidirectionalIterator2 __last2,
556 bidirectional_iterator_tag, bidirectional_iterator_tag)
560 _BidirectionalIterator1>)
562 _BidirectionalIterator2>)
564 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
565 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
567 _RevIterator1 __rlast1(__first1);
568 _RevIterator2 __rlast2(__first2);
569 _RevIterator1 __rresult = _GLIBCXX_STD_A::search(_RevIterator1(__last1),
571 _RevIterator2(__last2),
574 if (__rresult == __rlast1)
578 _BidirectionalIterator1 __result = __rresult.base();
579 std::advance(__result, -std::distance(__first2, __last2));
584 template<
typename _BidirectionalIterator1,
typename _BidirectionalIterator2,
585 typename _BinaryPredicate>
586 _BidirectionalIterator1
587 __find_end(_BidirectionalIterator1 __first1,
588 _BidirectionalIterator1 __last1,
589 _BidirectionalIterator2 __first2,
590 _BidirectionalIterator2 __last2,
591 bidirectional_iterator_tag, bidirectional_iterator_tag,
592 _BinaryPredicate __comp)
596 _BidirectionalIterator1>)
598 _BidirectionalIterator2>)
600 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
601 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
603 _RevIterator1 __rlast1(__first1);
604 _RevIterator2 __rlast2(__first2);
605 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
606 _RevIterator2(__last2), __rlast2,
609 if (__rresult == __rlast1)
613 _BidirectionalIterator1 __result = __rresult.base();
614 std::advance(__result, -std::distance(__first2, __last2));
645 template<
typename _ForwardIterator1,
typename _ForwardIterator2>
646 inline _ForwardIterator1
647 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
648 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
654 typename iterator_traits<_ForwardIterator1>::value_type,
655 typename iterator_traits<_ForwardIterator2>::value_type>)
659 return std::__find_end(__first1, __last1, __first2, __last2,
660 std::__iterator_category(__first1),
661 std::__iterator_category(__first2));
692 template<typename _ForwardIterator1, typename _ForwardIterator2,
693 typename _BinaryPredicate>
694 inline _ForwardIterator1
695 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
696 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
697 _BinaryPredicate __comp)
703 typename iterator_traits<_ForwardIterator1>::value_type,
704 typename iterator_traits<_ForwardIterator2>::value_type>)
708 return std::__find_end(__first1, __last1, __first2, __last2,
709 std::__iterator_category(__first1),
710 std::__iterator_category(__first2),
714 #if __cplusplus >= 201103L
727 template<
typename _InputIterator,
typename _Predicate>
729 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
730 {
return __last == std::find_if_not(__first, __last, __pred); }
744 template<
typename _InputIterator,
typename _Predicate>
746 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
747 {
return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
762 template<
typename _InputIterator,
typename _Predicate>
764 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
765 {
return !std::none_of(__first, __last, __pred); }
777 template<
typename _InputIterator,
typename _Predicate>
778 inline _InputIterator
779 find_if_not(_InputIterator __first, _InputIterator __last,
785 typename iterator_traits<_InputIterator>::value_type>)
787 return std::__find_if_not(__first, __last, __pred);
800 template<typename _InputIterator, typename _Predicate>
802 is_partitioned(_InputIterator __first, _InputIterator __last,
805 __first = std::find_if_not(__first, __last, __pred);
806 return std::none_of(__first, __last, __pred);
818 template<
typename _ForwardIterator,
typename _Predicate>
820 partition_point(_ForwardIterator __first, _ForwardIterator __last,
826 typename iterator_traits<_ForwardIterator>::value_type>)
831 typedef typename iterator_traits<_ForwardIterator>::difference_type
834 _DistanceType __len = std::distance(__first, __last);
835 _DistanceType __half;
836 _ForwardIterator __middle;
842 std::advance(__middle, __half);
843 if (__pred(*__middle))
847 __len = __len - __half - 1;
871 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
873 remove_copy(_InputIterator __first, _InputIterator __last,
874 _OutputIterator __result,
const _Tp& __value)
879 typename iterator_traits<_InputIterator>::value_type>)
881 typename iterator_traits<_InputIterator>::value_type, _Tp>)
884 for (; __first != __last; ++__first)
885 if (!(*__first == __value))
887 *__result = *__first;
908 template<
typename _InputIterator,
typename _OutputIterator,
911 remove_copy_if(_InputIterator __first, _InputIterator __last,
912 _OutputIterator __result, _Predicate __pred)
917 typename iterator_traits<_InputIterator>::value_type>)
919 typename iterator_traits<_InputIterator>::value_type>)
922 for (; __first != __last; ++__first)
923 if (!
bool(__pred(*__first)))
925 *__result = *__first;
931 #if __cplusplus >= 201103L
947 template<
typename _InputIterator,
typename _OutputIterator,
950 copy_if(_InputIterator __first, _InputIterator __last,
951 _OutputIterator __result, _Predicate __pred)
956 typename iterator_traits<_InputIterator>::value_type>)
958 typename iterator_traits<_InputIterator>::value_type>)
961 for (; __first != __last; ++__first)
962 if (__pred(*__first))
964 *__result = *__first;
971 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
973 __copy_n(_InputIterator __first, _Size __n,
974 _OutputIterator __result, input_iterator_tag)
980 *__result = *__first;
991 template<
typename _RandomAccessIterator,
typename _Size,
992 typename _OutputIterator>
993 inline _OutputIterator
994 __copy_n(_RandomAccessIterator __first, _Size __n,
995 _OutputIterator __result, random_access_iterator_tag)
996 {
return std::copy(__first, __first + __n, __result); }
1011 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
1012 inline _OutputIterator
1013 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1018 typename iterator_traits<_InputIterator>::value_type>)
1020 return std::__copy_n(__first, __n, __result,
1021 std::__iterator_category(__first));
1039 template<typename _InputIterator, typename _OutputIterator1,
1040 typename _OutputIterator2, typename _Predicate>
1041 pair<_OutputIterator1, _OutputIterator2>
1042 partition_copy(_InputIterator __first, _InputIterator __last,
1043 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
1049 typename iterator_traits<_InputIterator>::value_type>)
1051 typename iterator_traits<_InputIterator>::value_type>)
1053 typename iterator_traits<_InputIterator>::value_type>)
1056 for (; __first != __last; ++__first)
1057 if (__pred(*__first))
1059 *__out_true = *__first;
1064 *__out_false = *__first;
1068 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
1089 template<
typename _ForwardIterator,
typename _Tp>
1091 remove(_ForwardIterator __first, _ForwardIterator __last,
1098 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1101 __first = _GLIBCXX_STD_A::find(__first, __last, __value);
1102 if(__first == __last)
1104 _ForwardIterator __result = __first;
1106 for(; __first != __last; ++__first)
1107 if(!(*__first == __value))
1132 template<
typename _ForwardIterator,
typename _Predicate>
1134 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1141 typename iterator_traits<_ForwardIterator>::value_type>)
1144 __first = _GLIBCXX_STD_A::find_if(__first, __last, __pred);
1145 if(__first == __last)
1147 _ForwardIterator __result = __first;
1149 for(; __first != __last; ++__first)
1150 if(!
bool(__pred(*__first)))
1172 template<
typename _ForwardIterator>
1174 unique(_ForwardIterator __first, _ForwardIterator __last)
1180 typename iterator_traits<_ForwardIterator>::value_type>)
1184 __first = _GLIBCXX_STD_A::adjacent_find(__first, __last);
1185 if (__first == __last)
1189 _ForwardIterator __dest = __first;
1191 while (++__first != __last)
1192 if (!(*__dest == *__first))
1212 template<typename _ForwardIterator, typename _BinaryPredicate>
1214 unique(_ForwardIterator __first, _ForwardIterator __last,
1215 _BinaryPredicate __binary_pred)
1221 typename iterator_traits<_ForwardIterator>::value_type,
1222 typename iterator_traits<_ForwardIterator>::value_type>)
1226 __first = _GLIBCXX_STD_A::adjacent_find(__first, __last, __binary_pred);
1227 if (__first == __last)
1231 _ForwardIterator __dest = __first;
1233 while (++__first != __last)
1234 if (!
bool(__binary_pred(*__dest, *__first)))
1244 template<typename _ForwardIterator, typename _OutputIterator>
1246 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1247 _OutputIterator __result,
1248 forward_iterator_tag, output_iterator_tag)
1251 _ForwardIterator __next = __first;
1252 *__result = *__first;
1253 while (++__next != __last)
1254 if (!(*__first == *__next))
1257 *++__result = *__first;
1267 template<
typename _InputIterator,
typename _OutputIterator>
1269 __unique_copy(_InputIterator __first, _InputIterator __last,
1270 _OutputIterator __result,
1271 input_iterator_tag, output_iterator_tag)
1274 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1275 *__result = __value;
1276 while (++__first != __last)
1277 if (!(__value == *__first))
1280 *++__result = __value;
1290 template<
typename _InputIterator,
typename _ForwardIterator>
1292 __unique_copy(_InputIterator __first, _InputIterator __last,
1293 _ForwardIterator __result,
1294 input_iterator_tag, forward_iterator_tag)
1297 *__result = *__first;
1298 while (++__first != __last)
1299 if (!(*__result == *__first))
1300 *++__result = *__first;
1310 template<
typename _ForwardIterator,
typename _OutputIterator,
1311 typename _BinaryPredicate>
1313 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1314 _OutputIterator __result, _BinaryPredicate __binary_pred,
1315 forward_iterator_tag, output_iterator_tag)
1319 typename iterator_traits<_ForwardIterator>::value_type,
1320 typename iterator_traits<_ForwardIterator>::value_type>)
1322 _ForwardIterator __next = __first;
1323 *__result = *__first;
1324 while (++__next != __last)
1325 if (!
bool(__binary_pred(*__first, *__next)))
1328 *++__result = *__first;
1339 template<
typename _InputIterator,
typename _OutputIterator,
1340 typename _BinaryPredicate>
1342 __unique_copy(_InputIterator __first, _InputIterator __last,
1343 _OutputIterator __result, _BinaryPredicate __binary_pred,
1344 input_iterator_tag, output_iterator_tag)
1348 typename iterator_traits<_InputIterator>::value_type,
1349 typename iterator_traits<_InputIterator>::value_type>)
1351 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1352 *__result = __value;
1353 while (++__first != __last)
1354 if (!
bool(__binary_pred(__value, *__first)))
1357 *++__result = __value;
1368 template<
typename _InputIterator,
typename _ForwardIterator,
1369 typename _BinaryPredicate>
1371 __unique_copy(_InputIterator __first, _InputIterator __last,
1372 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1373 input_iterator_tag, forward_iterator_tag)
1377 typename iterator_traits<_ForwardIterator>::value_type,
1378 typename iterator_traits<_InputIterator>::value_type>)
1380 *__result = *__first;
1381 while (++__first != __last)
1382 if (!
bool(__binary_pred(*__result, *__first)))
1383 *++__result = *__first;
1392 template<
typename _B
idirectionalIterator>
1394 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1395 bidirectional_iterator_tag)
1398 if (__first == __last || __first == --__last)
1402 std::iter_swap(__first, __last);
1412 template<
typename _RandomAccessIterator>
1414 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1415 random_access_iterator_tag)
1417 if (__first == __last)
1420 while (__first < __last)
1422 std::iter_swap(__first, __last);
1440 template<
typename _B
idirectionalIterator>
1442 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1446 _BidirectionalIterator>)
1448 std::__reverse(__first, __last, std::__iterator_category(__first));
1467 template<typename _BidirectionalIterator, typename _OutputIterator>
1469 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1470 _OutputIterator __result)
1474 _BidirectionalIterator>)
1476 typename iterator_traits<_BidirectionalIterator>::value_type>)
1479 while (__first != __last)
1482 *__result = *__last;
1492 template<
typename _Eucl
ideanRingElement>
1493 _EuclideanRingElement
1494 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1498 _EuclideanRingElement __t = __m % __n;
1506 template<
typename _ForwardIterator>
1508 __rotate(_ForwardIterator __first,
1509 _ForwardIterator __middle,
1510 _ForwardIterator __last,
1511 forward_iterator_tag)
1513 if (__first == __middle || __last == __middle)
1516 _ForwardIterator __first2 = __middle;
1519 std::iter_swap(__first, __first2);
1522 if (__first == __middle)
1523 __middle = __first2;
1525 while (__first2 != __last);
1527 __first2 = __middle;
1529 while (__first2 != __last)
1531 std::iter_swap(__first, __first2);
1534 if (__first == __middle)
1535 __middle = __first2;
1536 else if (__first2 == __last)
1537 __first2 = __middle;
1542 template<
typename _B
idirectionalIterator>
1544 __rotate(_BidirectionalIterator __first,
1545 _BidirectionalIterator __middle,
1546 _BidirectionalIterator __last,
1547 bidirectional_iterator_tag)
1551 _BidirectionalIterator>)
1553 if (__first == __middle || __last == __middle)
1556 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1557 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1559 while (__first != __middle && __middle != __last)
1561 std::iter_swap(__first, --__last);
1565 if (__first == __middle)
1566 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1568 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1572 template<
typename _RandomAccessIterator>
1574 __rotate(_RandomAccessIterator __first,
1575 _RandomAccessIterator __middle,
1576 _RandomAccessIterator __last,
1577 random_access_iterator_tag)
1581 _RandomAccessIterator>)
1583 if (__first == __middle || __last == __middle)
1586 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1588 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1591 _Distance __n = __last - __first;
1592 _Distance __k = __middle - __first;
1594 if (__k == __n - __k)
1596 std::swap_ranges(__first, __middle, __middle);
1600 _RandomAccessIterator __p = __first;
1604 if (__k < __n - __k)
1606 if (__is_pod(_ValueType) && __k == 1)
1613 _RandomAccessIterator __q = __p + __k;
1614 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1616 std::iter_swap(__p, __q);
1629 if (__is_pod(_ValueType) && __k == 1)
1636 _RandomAccessIterator __q = __p + __n;
1638 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1642 std::iter_swap(__p, __q);
1673 template<
typename _ForwardIterator>
1675 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1676 _ForwardIterator __last)
1684 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1686 std::__rotate(__first, __middle, __last, _IterType());
1709 template<typename _ForwardIterator, typename _OutputIterator>
1711 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1712 _ForwardIterator __last, _OutputIterator __result)
1717 typename iterator_traits<_ForwardIterator>::value_type>)
1721 return std::copy(__first, __middle,
1722 std::copy(__middle, __last, __result));
1726 template<typename _ForwardIterator, typename _Predicate>
1728 __partition(_ForwardIterator __first, _ForwardIterator __last,
1729 _Predicate __pred, forward_iterator_tag)
1731 if (__first == __last)
1734 while (__pred(*__first))
1735 if (++__first == __last)
1738 _ForwardIterator __next = __first;
1740 while (++__next != __last)
1741 if (__pred(*__next))
1743 std::iter_swap(__first, __next);
1751 template<
typename _B
idirectionalIterator,
typename _Predicate>
1752 _BidirectionalIterator
1753 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1754 _Predicate __pred, bidirectional_iterator_tag)
1759 if (__first == __last)
1761 else if (__pred(*__first))
1767 if (__first == __last)
1769 else if (!
bool(__pred(*__last)))
1773 std::iter_swap(__first, __last);
1783 template<
typename _ForwardIterator,
typename _Predicate,
typename _Distance>
1785 __inplace_stable_partition(_ForwardIterator __first,
1786 _Predicate __pred, _Distance __len)
1790 _ForwardIterator __middle = __first;
1791 std::advance(__middle, __len / 2);
1792 _ForwardIterator __left_split =
1793 std::__inplace_stable_partition(__first, __pred, __len / 2);
1796 _Distance __right_len = __len - __len / 2;
1797 _ForwardIterator __right_split =
1798 std::__find_if_not_n(__middle, __right_len, __pred);
1800 __right_split = std::__inplace_stable_partition(__middle,
1803 std::rotate(__left_split, __middle, __right_split);
1804 std::advance(__left_split, std::distance(__middle, __right_split));
1805 return __left_split;
1814 template<
typename _ForwardIterator,
typename _Pointer,
typename _Predicate,
1817 __stable_partition_adaptive(_ForwardIterator __first,
1818 _ForwardIterator __last,
1819 _Predicate __pred, _Distance __len,
1821 _Distance __buffer_size)
1823 if (__len <= __buffer_size)
1825 _ForwardIterator __result1 = __first;
1826 _Pointer __result2 = __buffer;
1833 for (; __first != __last; ++__first)
1834 if (__pred(*__first))
1849 _ForwardIterator __middle = __first;
1850 std::advance(__middle, __len / 2);
1851 _ForwardIterator __left_split =
1852 std::__stable_partition_adaptive(__first, __middle, __pred,
1853 __len / 2, __buffer,
1857 _Distance __right_len = __len - __len / 2;
1858 _ForwardIterator __right_split =
1859 std::__find_if_not_n(__middle, __right_len, __pred);
1862 std::__stable_partition_adaptive(__right_split, __last, __pred,
1864 __buffer, __buffer_size);
1865 std::rotate(__left_split, __middle, __right_split);
1866 std::advance(__left_split, std::distance(__middle, __right_split));
1867 return __left_split;
1888 template<
typename _ForwardIterator,
typename _Predicate>
1890 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1897 typename iterator_traits<_ForwardIterator>::value_type>)
1900 __first = std::__find_if_not(__first, __last, __pred);
1902 if (__first == __last)
1906 typedef typename iterator_traits<_ForwardIterator>::value_type
1908 typedef typename iterator_traits<_ForwardIterator>::difference_type
1911 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1913 if (__buf.size() > 0)
1915 std::__stable_partition_adaptive(__first, __last, __pred,
1916 _DistanceType(__buf.requested_size()),
1918 _DistanceType(__buf.size()));
1921 std::__inplace_stable_partition(__first, __pred,
1922 _DistanceType(__buf.requested_size()));
1927 template<
typename _RandomAccessIterator>
1929 __heap_select(_RandomAccessIterator __first,
1930 _RandomAccessIterator __middle,
1931 _RandomAccessIterator __last)
1933 std::make_heap(__first, __middle);
1934 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1935 if (*__i < *__first)
1936 std::__pop_heap(__first, __middle, __i);
1940 template<
typename _RandomAccessIterator,
typename _Compare>
1942 __heap_select(_RandomAccessIterator __first,
1943 _RandomAccessIterator __middle,
1944 _RandomAccessIterator __last, _Compare __comp)
1946 std::make_heap(__first, __middle, __comp);
1947 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1948 if (__comp(*__i, *__first))
1949 std::__pop_heap(__first, __middle, __i, __comp);
1972 template<
typename _InputIterator,
typename _RandomAccessIterator>
1973 _RandomAccessIterator
1974 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1975 _RandomAccessIterator __result_first,
1976 _RandomAccessIterator __result_last)
1978 typedef typename iterator_traits<_InputIterator>::value_type
1980 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1982 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1995 if (__result_first == __result_last)
1997 _RandomAccessIterator __result_real_last = __result_first;
1998 while(__first != __last && __result_real_last != __result_last)
2000 *__result_real_last = *__first;
2001 ++__result_real_last;
2004 std::make_heap(__result_first, __result_real_last);
2005 while (__first != __last)
2007 if (*__first < *__result_first)
2008 std::__adjust_heap(__result_first, _DistanceType(0),
2009 _DistanceType(__result_real_last
2011 _InputValueType(*__first));
2014 std::sort_heap(__result_first, __result_real_last);
2015 return __result_real_last;
2038 template<
typename _InputIterator,
typename _RandomAccessIterator,
typename _Compare>
2039 _RandomAccessIterator
2040 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2041 _RandomAccessIterator __result_first,
2042 _RandomAccessIterator __result_last,
2045 typedef typename iterator_traits<_InputIterator>::value_type
2047 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2049 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2055 _RandomAccessIterator>)
2059 _InputValueType, _OutputValueType>)
2061 _OutputValueType, _OutputValueType>)
2065 if (__result_first == __result_last)
2067 _RandomAccessIterator __result_real_last = __result_first;
2068 while(__first != __last && __result_real_last != __result_last)
2070 *__result_real_last = *__first;
2071 ++__result_real_last;
2074 std::make_heap(__result_first, __result_real_last, __comp);
2075 while (__first != __last)
2077 if (__comp(*__first, *__result_first))
2078 std::__adjust_heap(__result_first, _DistanceType(0),
2079 _DistanceType(__result_real_last
2081 _InputValueType(*__first),
2085 std::sort_heap(__result_first, __result_real_last, __comp);
2086 return __result_real_last;
2090 template<
typename _RandomAccessIterator>
2092 __unguarded_linear_insert(_RandomAccessIterator __last)
2094 typename iterator_traits<_RandomAccessIterator>::value_type
2096 _RandomAccessIterator __next = __last;
2098 while (__val < *__next)
2108 template<
typename _RandomAccessIterator,
typename _Compare>
2110 __unguarded_linear_insert(_RandomAccessIterator __last,
2113 typename iterator_traits<_RandomAccessIterator>::value_type
2115 _RandomAccessIterator __next = __last;
2117 while (__comp(__val, *__next))
2127 template<
typename _RandomAccessIterator>
2129 __insertion_sort(_RandomAccessIterator __first,
2130 _RandomAccessIterator __last)
2132 if (__first == __last)
2135 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2137 if (*__i < *__first)
2139 typename iterator_traits<_RandomAccessIterator>::value_type
2145 std::__unguarded_linear_insert(__i);
2150 template<
typename _RandomAccessIterator,
typename _Compare>
2152 __insertion_sort(_RandomAccessIterator __first,
2153 _RandomAccessIterator __last, _Compare __comp)
2155 if (__first == __last)
return;
2157 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2159 if (__comp(*__i, *__first))
2161 typename iterator_traits<_RandomAccessIterator>::value_type
2167 std::__unguarded_linear_insert(__i, __comp);
2172 template<
typename _RandomAccessIterator>
2174 __unguarded_insertion_sort(_RandomAccessIterator __first,
2175 _RandomAccessIterator __last)
2177 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2180 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2181 std::__unguarded_linear_insert(__i);
2185 template<
typename _RandomAccessIterator,
typename _Compare>
2187 __unguarded_insertion_sort(_RandomAccessIterator __first,
2188 _RandomAccessIterator __last, _Compare __comp)
2190 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2193 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2194 std::__unguarded_linear_insert(__i, __comp);
2201 enum { _S_threshold = 16 };
2204 template<
typename _RandomAccessIterator>
2206 __final_insertion_sort(_RandomAccessIterator __first,
2207 _RandomAccessIterator __last)
2209 if (__last - __first >
int(_S_threshold))
2211 std::__insertion_sort(__first, __first +
int(_S_threshold));
2212 std::__unguarded_insertion_sort(__first +
int(_S_threshold), __last);
2215 std::__insertion_sort(__first, __last);
2219 template<
typename _RandomAccessIterator,
typename _Compare>
2221 __final_insertion_sort(_RandomAccessIterator __first,
2222 _RandomAccessIterator __last, _Compare __comp)
2224 if (__last - __first >
int(_S_threshold))
2226 std::__insertion_sort(__first, __first +
int(_S_threshold), __comp);
2227 std::__unguarded_insertion_sort(__first +
int(_S_threshold), __last,
2231 std::__insertion_sort(__first, __last, __comp);
2235 template<
typename _RandomAccessIterator,
typename _Tp>
2236 _RandomAccessIterator
2237 __unguarded_partition(_RandomAccessIterator __first,
2238 _RandomAccessIterator __last,
const _Tp& __pivot)
2242 while (*__first < __pivot)
2245 while (__pivot < *__last)
2247 if (!(__first < __last))
2249 std::iter_swap(__first, __last);
2255 template<
typename _RandomAccessIterator,
typename _Tp,
typename _Compare>
2256 _RandomAccessIterator
2257 __unguarded_partition(_RandomAccessIterator __first,
2258 _RandomAccessIterator __last,
2259 const _Tp& __pivot, _Compare __comp)
2263 while (__comp(*__first, __pivot))
2266 while (__comp(__pivot, *__last))
2268 if (!(__first < __last))
2270 std::iter_swap(__first, __last);
2276 template<
typename _RandomAccessIterator>
2277 inline _RandomAccessIterator
2278 __unguarded_partition_pivot(_RandomAccessIterator __first,
2279 _RandomAccessIterator __last)
2281 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2282 std::__move_median_to_first(__first, __first + 1, __mid, __last - 1);
2283 return std::__unguarded_partition(__first + 1, __last, *__first);
2288 template<
typename _RandomAccessIterator,
typename _Compare>
2289 inline _RandomAccessIterator
2290 __unguarded_partition_pivot(_RandomAccessIterator __first,
2291 _RandomAccessIterator __last, _Compare __comp)
2293 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2294 std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
2296 return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
2300 template<
typename _RandomAccessIterator,
typename _Size>
2302 __introsort_loop(_RandomAccessIterator __first,
2303 _RandomAccessIterator __last,
2304 _Size __depth_limit)
2306 while (__last - __first >
int(_S_threshold))
2308 if (__depth_limit == 0)
2310 _GLIBCXX_STD_A::partial_sort(__first, __last, __last);
2314 _RandomAccessIterator __cut =
2315 std::__unguarded_partition_pivot(__first, __last);
2316 std::__introsort_loop(__cut, __last, __depth_limit);
2322 template<
typename _RandomAccessIterator,
typename _Size,
typename _Compare>
2324 __introsort_loop(_RandomAccessIterator __first,
2325 _RandomAccessIterator __last,
2326 _Size __depth_limit, _Compare __comp)
2328 while (__last - __first >
int(_S_threshold))
2330 if (__depth_limit == 0)
2332 _GLIBCXX_STD_A::partial_sort(__first, __last, __last, __comp);
2336 _RandomAccessIterator __cut =
2337 std::__unguarded_partition_pivot(__first, __last, __comp);
2338 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2345 template<
typename _RandomAccessIterator,
typename _Size>
2347 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2348 _RandomAccessIterator __last, _Size __depth_limit)
2350 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2353 while (__last - __first > 3)
2355 if (__depth_limit == 0)
2357 std::__heap_select(__first, __nth + 1, __last);
2360 std::iter_swap(__first, __nth);
2364 _RandomAccessIterator __cut =
2365 std::__unguarded_partition_pivot(__first, __last);
2371 std::__insertion_sort(__first, __last);
2374 template<
typename _RandomAccessIterator,
typename _Size,
typename _Compare>
2376 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2377 _RandomAccessIterator __last, _Size __depth_limit,
2380 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2383 while (__last - __first > 3)
2385 if (__depth_limit == 0)
2387 std::__heap_select(__first, __nth + 1, __last, __comp);
2389 std::iter_swap(__first, __nth);
2393 _RandomAccessIterator __cut =
2394 std::__unguarded_partition_pivot(__first, __last, __comp);
2400 std::__insertion_sort(__first, __last, __comp);
2423 template<
typename _ForwardIterator,
typename _Tp,
typename _Compare>
2425 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2426 const _Tp& __val, _Compare __comp)
2428 typedef typename iterator_traits<_ForwardIterator>::value_type
2430 typedef typename iterator_traits<_ForwardIterator>::difference_type
2440 _DistanceType __len = std::distance(__first, __last);
2444 _DistanceType __half = __len >> 1;
2445 _ForwardIterator __middle = __first;
2446 std::advance(__middle, __half);
2447 if (__comp(*__middle, __val))
2451 __len = __len - __half - 1;
2470 template<
typename _ForwardIterator,
typename _Tp>
2472 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2475 typedef typename iterator_traits<_ForwardIterator>::value_type
2477 typedef typename iterator_traits<_ForwardIterator>::difference_type
2485 _DistanceType __len = std::distance(__first, __last);
2489 _DistanceType __half = __len >> 1;
2490 _ForwardIterator __middle = __first;
2491 std::advance(__middle, __half);
2492 if (__val < *__middle)
2498 __len = __len - __half - 1;
2519 template<
typename _ForwardIterator,
typename _Tp,
typename _Compare>
2521 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2522 const _Tp& __val, _Compare __comp)
2524 typedef typename iterator_traits<_ForwardIterator>::value_type
2526 typedef typename iterator_traits<_ForwardIterator>::difference_type
2536 _DistanceType __len = std::distance(__first, __last);
2540 _DistanceType __half = __len >> 1;
2541 _ForwardIterator __middle = __first;
2542 std::advance(__middle, __half);
2543 if (__comp(__val, *__middle))
2549 __len = __len - __half - 1;
2572 template<
typename _ForwardIterator,
typename _Tp>
2573 pair<_ForwardIterator, _ForwardIterator>
2574 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2577 typedef typename iterator_traits<_ForwardIterator>::value_type
2579 typedef typename iterator_traits<_ForwardIterator>::difference_type
2589 _DistanceType __len = std::distance(__first, __last);
2593 _DistanceType __half = __len >> 1;
2594 _ForwardIterator __middle = __first;
2595 std::advance(__middle, __half);
2596 if (*__middle < __val)
2600 __len = __len - __half - 1;
2602 else if (__val < *__middle)
2606 _ForwardIterator __left = std::lower_bound(__first, __middle,
2608 std::advance(__first, __len);
2609 _ForwardIterator __right = std::upper_bound(++__middle, __first,
2611 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2614 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2634 template<
typename _ForwardIterator,
typename _Tp,
typename _Compare>
2635 pair<_ForwardIterator, _ForwardIterator>
2636 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2637 const _Tp& __val, _Compare __comp)
2639 typedef typename iterator_traits<_ForwardIterator>::value_type
2641 typedef typename iterator_traits<_ForwardIterator>::difference_type
2655 _DistanceType __len = std::distance(__first, __last);
2659 _DistanceType __half = __len >> 1;
2660 _ForwardIterator __middle = __first;
2661 std::advance(__middle, __half);
2662 if (__comp(*__middle, __val))
2666 __len = __len - __half - 1;
2668 else if (__comp(__val, *__middle))
2672 _ForwardIterator __left = std::lower_bound(__first, __middle,
2674 std::advance(__first, __len);
2675 _ForwardIterator __right = std::upper_bound(++__middle, __first,
2677 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2680 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2695 template<
typename _ForwardIterator,
typename _Tp>
2697 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2700 typedef typename iterator_traits<_ForwardIterator>::value_type
2709 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2710 return __i != __last && !(__val < *__i);
2728 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2730 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2731 const _Tp& __val, _Compare __comp)
2733 typedef typename iterator_traits<_ForwardIterator>::value_type
2745 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2746 return __i != __last && !
bool(__comp(__val, *__i));
2752 template<typename _InputIterator1, typename _InputIterator2,
2753 typename _OutputIterator>
2755 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2756 _InputIterator2 __first2, _InputIterator2 __last2,
2757 _OutputIterator __result)
2759 while (__first1 != __last1 && __first2 != __last2)
2761 if (*__first2 < *__first1)
2773 if (__first1 != __last1)
2778 template<
typename _InputIterator1,
typename _InputIterator2,
2779 typename _OutputIterator,
typename _Compare>
2781 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2782 _InputIterator2 __first2, _InputIterator2 __last2,
2783 _OutputIterator __result, _Compare __comp)
2785 while (__first1 != __last1 && __first2 != __last2)
2787 if (__comp(*__first2, *__first1))
2799 if (__first1 != __last1)
2804 template<
typename _BidirectionalIterator1,
typename _BidirectionalIterator2,
2805 typename _BidirectionalIterator3>
2807 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2808 _BidirectionalIterator1 __last1,
2809 _BidirectionalIterator2 __first2,
2810 _BidirectionalIterator2 __last2,
2811 _BidirectionalIterator3 __result)
2813 if (__first1 == __last1)
2818 else if (__first2 == __last2)
2825 if (*__last2 < *__last1)
2828 if (__first1 == __last1)
2838 if (__first2 == __last2)
2846 template<
typename _BidirectionalIterator1,
typename _BidirectionalIterator2,
2847 typename _BidirectionalIterator3,
typename _Compare>
2849 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2850 _BidirectionalIterator1 __last1,
2851 _BidirectionalIterator2 __first2,
2852 _BidirectionalIterator2 __last2,
2853 _BidirectionalIterator3 __result,
2856 if (__first1 == __last1)
2861 else if (__first2 == __last2)
2868 if (__comp(*__last2, *__last1))
2871 if (__first1 == __last1)
2881 if (__first2 == __last2)
2889 template<
typename _BidirectionalIterator1,
typename _BidirectionalIterator2,
2891 _BidirectionalIterator1
2892 __rotate_adaptive(_BidirectionalIterator1 __first,
2893 _BidirectionalIterator1 __middle,
2894 _BidirectionalIterator1 __last,
2895 _Distance __len1, _Distance __len2,
2896 _BidirectionalIterator2 __buffer,
2897 _Distance __buffer_size)
2899 _BidirectionalIterator2 __buffer_end;
2900 if (__len1 > __len2 && __len2 <= __buffer_size)
2911 else if (__len1 <= __buffer_size)
2924 std::rotate(__first, __middle, __last);
2925 std::advance(__first, std::distance(__middle, __last));
2931 template<
typename _BidirectionalIterator,
typename _Distance,
2934 __merge_adaptive(_BidirectionalIterator __first,
2935 _BidirectionalIterator __middle,
2936 _BidirectionalIterator __last,
2937 _Distance __len1, _Distance __len2,
2938 _Pointer __buffer, _Distance __buffer_size)
2940 if (__len1 <= __len2 && __len1 <= __buffer_size)
2942 _Pointer __buffer_end =
_GLIBCXX_MOVE3(__first, __middle, __buffer);
2943 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2946 else if (__len2 <= __buffer_size)
2948 _Pointer __buffer_end =
_GLIBCXX_MOVE3(__middle, __last, __buffer);
2949 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2950 __buffer_end, __last);
2954 _BidirectionalIterator __first_cut = __first;
2955 _BidirectionalIterator __second_cut = __middle;
2956 _Distance __len11 = 0;
2957 _Distance __len22 = 0;
2958 if (__len1 > __len2)
2960 __len11 = __len1 / 2;
2961 std::advance(__first_cut, __len11);
2962 __second_cut = std::lower_bound(__middle, __last,
2964 __len22 = std::distance(__middle, __second_cut);
2968 __len22 = __len2 / 2;
2969 std::advance(__second_cut, __len22);
2970 __first_cut = std::upper_bound(__first, __middle,
2972 __len11 = std::distance(__first, __first_cut);
2974 _BidirectionalIterator __new_middle =
2975 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2976 __len1 - __len11, __len22, __buffer,
2978 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2979 __len22, __buffer, __buffer_size);
2980 std::__merge_adaptive(__new_middle, __second_cut, __last,
2982 __len2 - __len22, __buffer, __buffer_size);
2987 template<
typename _BidirectionalIterator,
typename _Distance,
2988 typename _Pointer,
typename _Compare>
2990 __merge_adaptive(_BidirectionalIterator __first,
2991 _BidirectionalIterator __middle,
2992 _BidirectionalIterator __last,
2993 _Distance __len1, _Distance __len2,
2994 _Pointer __buffer, _Distance __buffer_size,
2997 if (__len1 <= __len2 && __len1 <= __buffer_size)
2999 _Pointer __buffer_end =
_GLIBCXX_MOVE3(__first, __middle, __buffer);
3000 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
3003 else if (__len2 <= __buffer_size)
3005 _Pointer __buffer_end =
_GLIBCXX_MOVE3(__middle, __last, __buffer);
3006 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
3007 __buffer_end, __last, __comp);
3011 _BidirectionalIterator __first_cut = __first;
3012 _BidirectionalIterator __second_cut = __middle;
3013 _Distance __len11 = 0;
3014 _Distance __len22 = 0;
3015 if (__len1 > __len2)
3017 __len11 = __len1 / 2;
3018 std::advance(__first_cut, __len11);
3019 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3021 __len22 = std::distance(__middle, __second_cut);
3025 __len22 = __len2 / 2;
3026 std::advance(__second_cut, __len22);
3027 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3029 __len11 = std::distance(__first, __first_cut);
3031 _BidirectionalIterator __new_middle =
3032 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3033 __len1 - __len11, __len22, __buffer,
3035 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3036 __len22, __buffer, __buffer_size, __comp);
3037 std::__merge_adaptive(__new_middle, __second_cut, __last,
3039 __len2 - __len22, __buffer,
3040 __buffer_size, __comp);
3045 template<
typename _B
idirectionalIterator,
typename _Distance>
3047 __merge_without_buffer(_BidirectionalIterator __first,
3048 _BidirectionalIterator __middle,
3049 _BidirectionalIterator __last,
3050 _Distance __len1, _Distance __len2)
3052 if (__len1 == 0 || __len2 == 0)
3054 if (__len1 + __len2 == 2)
3056 if (*__middle < *__first)
3057 std::iter_swap(__first, __middle);
3060 _BidirectionalIterator __first_cut = __first;
3061 _BidirectionalIterator __second_cut = __middle;
3062 _Distance __len11 = 0;
3063 _Distance __len22 = 0;
3064 if (__len1 > __len2)
3066 __len11 = __len1 / 2;
3067 std::advance(__first_cut, __len11);
3068 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3069 __len22 = std::distance(__middle, __second_cut);
3073 __len22 = __len2 / 2;
3074 std::advance(__second_cut, __len22);
3075 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3076 __len11 = std::distance(__first, __first_cut);
3078 std::rotate(__first_cut, __middle, __second_cut);
3079 _BidirectionalIterator __new_middle = __first_cut;
3080 std::advance(__new_middle, std::distance(__middle, __second_cut));
3081 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3083 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3084 __len1 - __len11, __len2 - __len22);
3088 template<
typename _BidirectionalIterator,
typename _Distance,
3091 __merge_without_buffer(_BidirectionalIterator __first,
3092 _BidirectionalIterator __middle,
3093 _BidirectionalIterator __last,
3094 _Distance __len1, _Distance __len2,
3097 if (__len1 == 0 || __len2 == 0)
3099 if (__len1 + __len2 == 2)
3101 if (__comp(*__middle, *__first))
3102 std::iter_swap(__first, __middle);
3105 _BidirectionalIterator __first_cut = __first;
3106 _BidirectionalIterator __second_cut = __middle;
3107 _Distance __len11 = 0;
3108 _Distance __len22 = 0;
3109 if (__len1 > __len2)
3111 __len11 = __len1 / 2;
3112 std::advance(__first_cut, __len11);
3113 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3115 __len22 = std::distance(__middle, __second_cut);
3119 __len22 = __len2 / 2;
3120 std::advance(__second_cut, __len22);
3121 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3123 __len11 = std::distance(__first, __first_cut);
3125 std::rotate(__first_cut, __middle, __second_cut);
3126 _BidirectionalIterator __new_middle = __first_cut;
3127 std::advance(__new_middle, std::distance(__middle, __second_cut));
3128 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3129 __len11, __len22, __comp);
3130 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3131 __len1 - __len11, __len2 - __len22, __comp);
3152 template<
typename _B
idirectionalIterator>
3154 inplace_merge(_BidirectionalIterator __first,
3155 _BidirectionalIterator __middle,
3156 _BidirectionalIterator __last)
3158 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3160 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3165 _BidirectionalIterator>)
3170 if (__first == __middle || __middle == __last)
3173 _DistanceType __len1 = std::distance(__first, __middle);
3174 _DistanceType __len2 = std::distance(__middle, __last);
3176 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3178 if (__buf.begin() == 0)
3179 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3181 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3182 __buf.begin(), _DistanceType(__buf.size()));
3207 template<typename _BidirectionalIterator, typename _Compare>
3209 inplace_merge(_BidirectionalIterator __first,
3210 _BidirectionalIterator __middle,
3211 _BidirectionalIterator __last,
3214 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3216 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3221 _BidirectionalIterator>)
3223 _ValueType, _ValueType>)
3227 if (__first == __middle || __middle == __last)
3230 const _DistanceType __len1 = std::distance(__first, __middle);
3231 const _DistanceType __len2 = std::distance(__middle, __last);
3233 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3235 if (__buf.begin() == 0)
3236 std::__merge_without_buffer(__first, __middle, __last, __len1,
3239 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3240 __buf.begin(), _DistanceType(__buf.size()),
3246 template<typename _InputIterator1, typename _InputIterator2,
3247 typename _OutputIterator>
3249 __move_merge(_InputIterator1 __first1, _InputIterator1 __last1,
3250 _InputIterator2 __first2, _InputIterator2 __last2,
3251 _OutputIterator __result)
3253 while (__first1 != __last1 && __first2 != __last2)
3255 if (*__first2 < *__first1)
3273 template<
typename _InputIterator1,
typename _InputIterator2,
3274 typename _OutputIterator,
typename _Compare>
3276 __move_merge(_InputIterator1 __first1, _InputIterator1 __last1,
3277 _InputIterator2 __first2, _InputIterator2 __last2,
3278 _OutputIterator __result, _Compare __comp)
3280 while (__first1 != __last1 && __first2 != __last2)
3282 if (__comp(*__first2, *__first1))
3299 template<
typename _RandomAccessIterator1,
typename _RandomAccessIterator2,
3302 __merge_sort_loop(_RandomAccessIterator1 __first,
3303 _RandomAccessIterator1 __last,
3304 _RandomAccessIterator2 __result,
3305 _Distance __step_size)
3307 const _Distance __two_step = 2 * __step_size;
3309 while (__last - __first >= __two_step)
3311 __result = std::__move_merge(__first, __first + __step_size,
3312 __first + __step_size,
3313 __first + __two_step, __result);
3314 __first += __two_step;
3317 __step_size =
std::min(_Distance(__last - __first), __step_size);
3318 std::__move_merge(__first, __first + __step_size,
3319 __first + __step_size, __last, __result);
3322 template<
typename _RandomAccessIterator1,
typename _RandomAccessIterator2,
3323 typename _Distance,
typename _Compare>
3325 __merge_sort_loop(_RandomAccessIterator1 __first,
3326 _RandomAccessIterator1 __last,
3327 _RandomAccessIterator2 __result, _Distance __step_size,
3330 const _Distance __two_step = 2 * __step_size;
3332 while (__last - __first >= __two_step)
3334 __result = std::__move_merge(__first, __first + __step_size,
3335 __first + __step_size,
3336 __first + __two_step,
3338 __first += __two_step;
3340 __step_size =
std::min(_Distance(__last - __first), __step_size);
3342 std::__move_merge(__first,__first + __step_size,
3343 __first + __step_size, __last, __result, __comp);
3346 template<
typename _RandomAccessIterator,
typename _Distance>
3348 __chunk_insertion_sort(_RandomAccessIterator __first,
3349 _RandomAccessIterator __last,
3350 _Distance __chunk_size)
3352 while (__last - __first >= __chunk_size)
3354 std::__insertion_sort(__first, __first + __chunk_size);
3355 __first += __chunk_size;
3357 std::__insertion_sort(__first, __last);
3360 template<
typename _RandomAccessIterator,
typename _Distance,
3363 __chunk_insertion_sort(_RandomAccessIterator __first,
3364 _RandomAccessIterator __last,
3365 _Distance __chunk_size, _Compare __comp)
3367 while (__last - __first >= __chunk_size)
3369 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3370 __first += __chunk_size;
3372 std::__insertion_sort(__first, __last, __comp);
3375 enum { _S_chunk_size = 7 };
3377 template<
typename _RandomAccessIterator,
typename _Po
inter>
3379 __merge_sort_with_buffer(_RandomAccessIterator __first,
3380 _RandomAccessIterator __last,
3383 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3386 const _Distance __len = __last - __first;
3387 const _Pointer __buffer_last = __buffer + __len;
3389 _Distance __step_size = _S_chunk_size;
3390 std::__chunk_insertion_sort(__first, __last, __step_size);
3392 while (__step_size < __len)
3394 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3396 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3401 template<
typename _RandomAccessIterator,
typename _Po
inter,
typename _Compare>
3403 __merge_sort_with_buffer(_RandomAccessIterator __first,
3404 _RandomAccessIterator __last,
3405 _Pointer __buffer, _Compare __comp)
3407 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3410 const _Distance __len = __last - __first;
3411 const _Pointer __buffer_last = __buffer + __len;
3413 _Distance __step_size = _S_chunk_size;
3414 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3416 while (__step_size < __len)
3418 std::__merge_sort_loop(__first, __last, __buffer,
3419 __step_size, __comp);
3421 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3422 __step_size, __comp);
3427 template<
typename _RandomAccessIterator,
typename _Pointer,
3430 __stable_sort_adaptive(_RandomAccessIterator __first,
3431 _RandomAccessIterator __last,
3432 _Pointer __buffer, _Distance __buffer_size)
3434 const _Distance __len = (__last - __first + 1) / 2;
3435 const _RandomAccessIterator __middle = __first + __len;
3436 if (__len > __buffer_size)
3438 std::__stable_sort_adaptive(__first, __middle,
3439 __buffer, __buffer_size);
3440 std::__stable_sort_adaptive(__middle, __last,
3441 __buffer, __buffer_size);
3445 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3446 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3448 std::__merge_adaptive(__first, __middle, __last,
3449 _Distance(__middle - __first),
3450 _Distance(__last - __middle),
3451 __buffer, __buffer_size);
3454 template<
typename _RandomAccessIterator,
typename _Pointer,
3455 typename _Distance,
typename _Compare>
3457 __stable_sort_adaptive(_RandomAccessIterator __first,
3458 _RandomAccessIterator __last,
3459 _Pointer __buffer, _Distance __buffer_size,
3462 const _Distance __len = (__last - __first + 1) / 2;
3463 const _RandomAccessIterator __middle = __first + __len;
3464 if (__len > __buffer_size)
3466 std::__stable_sort_adaptive(__first, __middle, __buffer,
3467 __buffer_size, __comp);
3468 std::__stable_sort_adaptive(__middle, __last, __buffer,
3469 __buffer_size, __comp);
3473 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3474 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3476 std::__merge_adaptive(__first, __middle, __last,
3477 _Distance(__middle - __first),
3478 _Distance(__last - __middle),
3479 __buffer, __buffer_size,
3484 template<
typename _RandomAccessIterator>
3486 __inplace_stable_sort(_RandomAccessIterator __first,
3487 _RandomAccessIterator __last)
3489 if (__last - __first < 15)
3491 std::__insertion_sort(__first, __last);
3494 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3495 std::__inplace_stable_sort(__first, __middle);
3496 std::__inplace_stable_sort(__middle, __last);
3497 std::__merge_without_buffer(__first, __middle, __last,
3503 template<
typename _RandomAccessIterator,
typename _Compare>
3505 __inplace_stable_sort(_RandomAccessIterator __first,
3506 _RandomAccessIterator __last, _Compare __comp)
3508 if (__last - __first < 15)
3510 std::__insertion_sort(__first, __last, __comp);
3513 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3514 std::__inplace_stable_sort(__first, __middle, __comp);
3515 std::__inplace_stable_sort(__middle, __last, __comp);
3516 std::__merge_without_buffer(__first, __middle, __last,
3547 template<
typename _InputIterator1,
typename _InputIterator2>
3549 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3550 _InputIterator2 __first2, _InputIterator2 __last2)
3552 typedef typename iterator_traits<_InputIterator1>::value_type
3554 typedef typename iterator_traits<_InputIterator2>::value_type
3565 while (__first1 != __last1 && __first2 != __last2)
3566 if (*__first2 < *__first1)
3568 else if(*__first1 < *__first2)
3571 ++__first1, ++__first2;
3573 return __first2 == __last2;
3597 template<typename _InputIterator1, typename _InputIterator2,
3600 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3601 _InputIterator2 __first2, _InputIterator2 __last2,
3604 typedef typename iterator_traits<_InputIterator1>::value_type
3606 typedef typename iterator_traits<_InputIterator2>::value_type
3613 _ValueType1, _ValueType2>)
3615 _ValueType2, _ValueType1>)
3619 while (__first1 != __last1 && __first2 != __last2)
3620 if (__comp(*__first2, *__first1))
3622 else if(__comp(*__first1, *__first2))
3625 ++__first1, ++__first2;
3627 return __first2 == __last2;
3652 template<typename _BidirectionalIterator>
3654 next_permutation(_BidirectionalIterator __first,
3655 _BidirectionalIterator __last)
3659 _BidirectionalIterator>)
3661 typename iterator_traits<_BidirectionalIterator>::value_type>)
3664 if (__first == __last)
3666 _BidirectionalIterator __i = __first;
3675 _BidirectionalIterator __ii = __i;
3679 _BidirectionalIterator __j = __last;
3680 while (!(*__i < *--__j))
3682 std::iter_swap(__i, __j);
3683 std::reverse(__ii, __last);
3688 std::reverse(__first, __last);
3709 template<
typename _B
idirectionalIterator,
typename _Compare>
3711 next_permutation(_BidirectionalIterator __first,
3712 _BidirectionalIterator __last, _Compare __comp)
3716 _BidirectionalIterator>)
3718 typename iterator_traits<_BidirectionalIterator>::value_type,
3719 typename iterator_traits<_BidirectionalIterator>::value_type>)
3722 if (__first == __last)
3724 _BidirectionalIterator __i = __first;
3733 _BidirectionalIterator __ii = __i;
3735 if (__comp(*__i, *__ii))
3737 _BidirectionalIterator __j = __last;
3738 while (!
bool(__comp(*__i, *--__j)))
3740 std::iter_swap(__i, __j);
3741 std::reverse(__ii, __last);
3746 std::reverse(__first, __last);
3765 template<
typename _B
idirectionalIterator>
3767 prev_permutation(_BidirectionalIterator __first,
3768 _BidirectionalIterator __last)
3772 _BidirectionalIterator>)
3774 typename iterator_traits<_BidirectionalIterator>::value_type>)
3777 if (__first == __last)
3779 _BidirectionalIterator __i = __first;
3788 _BidirectionalIterator __ii = __i;
3792 _BidirectionalIterator __j = __last;
3793 while (!(*--__j < *__i))
3795 std::iter_swap(__i, __j);
3796 std::reverse(__ii, __last);
3801 std::reverse(__first, __last);
3822 template<
typename _B
idirectionalIterator,
typename _Compare>
3824 prev_permutation(_BidirectionalIterator __first,
3825 _BidirectionalIterator __last, _Compare __comp)
3829 _BidirectionalIterator>)
3831 typename iterator_traits<_BidirectionalIterator>::value_type,
3832 typename iterator_traits<_BidirectionalIterator>::value_type>)
3835 if (__first == __last)
3837 _BidirectionalIterator __i = __first;
3846 _BidirectionalIterator __ii = __i;
3848 if (__comp(*__ii, *__i))
3850 _BidirectionalIterator __j = __last;
3851 while (!
bool(__comp(*--__j, *__i)))
3853 std::iter_swap(__i, __j);
3854 std::reverse(__ii, __last);
3859 std::reverse(__first, __last);
3882 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
3884 replace_copy(_InputIterator __first, _InputIterator __last,
3885 _OutputIterator __result,
3886 const _Tp& __old_value,
const _Tp& __new_value)
3891 typename iterator_traits<_InputIterator>::value_type>)
3893 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3896 for (; __first != __last; ++__first, ++__result)
3897 if (*__first == __old_value)
3898 *__result = __new_value;
3900 *__result = *__first;
3919 template<typename _InputIterator, typename _OutputIterator,
3920 typename _Predicate, typename _Tp>
3922 replace_copy_if(_InputIterator __first, _InputIterator __last,
3923 _OutputIterator __result,
3924 _Predicate __pred, const _Tp& __new_value)
3929 typename iterator_traits<_InputIterator>::value_type>)
3931 typename iterator_traits<_InputIterator>::value_type>)
3934 for (; __first != __last; ++__first, ++__result)
3935 if (__pred(*__first))
3936 *__result = __new_value;
3938 *__result = *__first;
3942 #if __cplusplus >= 201103L
3950 template<
typename _ForwardIterator>
3952 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3953 {
return std::is_sorted_until(__first, __last) == __last; }
3964 template<
typename _ForwardIterator,
typename _Compare>
3966 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3968 {
return std::is_sorted_until(__first, __last, __comp) == __last; }
3978 template<
typename _ForwardIterator>
3980 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3985 typename iterator_traits<_ForwardIterator>::value_type>)
3988 if (__first == __last)
3991 _ForwardIterator __next = __first;
3992 for (++__next; __next != __last; __first = __next, ++__next)
3993 if (*__next < *__first)
4007 template<typename _ForwardIterator, typename _Compare>
4009 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
4015 typename iterator_traits<_ForwardIterator>::value_type,
4016 typename iterator_traits<_ForwardIterator>::value_type>)
4019 if (__first == __last)
4022 _ForwardIterator __next = __first;
4023 for (++__next; __next != __last; __first = __next, ++__next)
4024 if (__comp(*__next, *__first))
4037 template<typename _Tp>
4038 inline pair<const _Tp&, const _Tp&>
4039 minmax(const _Tp& __a, const _Tp& __b)
4044 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
4045 : pair<const _Tp&, const _Tp&>(__a, __b);
4057 template<typename _Tp, typename _Compare>
4058 inline pair<const _Tp&, const _Tp&>
4059 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
4061 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
4062 : pair<const _Tp&, const _Tp&>(__a, __b);
4076 template<
typename _ForwardIterator>
4077 pair<_ForwardIterator, _ForwardIterator>
4078 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
4083 typename iterator_traits<_ForwardIterator>::value_type>)
4086 _ForwardIterator __next = __first;
4087 if (__first == __last
4088 || ++__next == __last)
4089 return std::make_pair(__first, __first);
4091 _ForwardIterator __min, __max;
4092 if (*__next < *__first)
4106 while (__first != __last)
4109 if (++__next == __last)
4111 if (*__first < *__min)
4113 else if (!(*__first < *__max))
4118 if (*__next < *__first)
4120 if (*__next < *__min)
4122 if (!(*__first < *__max))
4127 if (*__first < *__min)
4129 if (!(*__next < *__max))
4137 return std::make_pair(__min, __max);
4152 template<
typename _ForwardIterator,
typename _Compare>
4153 pair<_ForwardIterator, _ForwardIterator>
4154 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
4160 typename iterator_traits<_ForwardIterator>::value_type,
4161 typename iterator_traits<_ForwardIterator>::value_type>)
4164 _ForwardIterator __next = __first;
4165 if (__first == __last
4166 || ++__next == __last)
4167 return std::make_pair(__first, __first);
4169 _ForwardIterator __min, __max;
4170 if (__comp(*__next, *__first))
4184 while (__first != __last)
4187 if (++__next == __last)
4189 if (__comp(*__first, *__min))
4191 else if (!__comp(*__first, *__max))
4196 if (__comp(*__next, *__first))
4198 if (__comp(*__next, *__min))
4200 if (!__comp(*__first, *__max))
4205 if (__comp(*__first, *__min))
4207 if (!__comp(*__next, *__max))
4215 return std::make_pair(__min, __max);
4219 template<
typename _Tp>
4221 min(initializer_list<_Tp> __l)
4222 {
return *std::min_element(__l.begin(), __l.end()); }
4224 template<
typename _Tp,
typename _Compare>
4226 min(initializer_list<_Tp> __l, _Compare __comp)
4227 {
return *std::min_element(__l.begin(), __l.end(), __comp); }
4229 template<
typename _Tp>
4231 max(initializer_list<_Tp> __l)
4232 {
return *std::max_element(__l.begin(), __l.end()); }
4234 template<
typename _Tp,
typename _Compare>
4236 max(initializer_list<_Tp> __l, _Compare __comp)
4237 {
return *std::max_element(__l.begin(), __l.end(), __comp); }
4239 template<
typename _Tp>
4240 inline pair<_Tp, _Tp>
4241 minmax(initializer_list<_Tp> __l)
4243 pair<const _Tp*, const _Tp*> __p =
4244 std::minmax_element(__l.begin(), __l.end());
4245 return std::make_pair(*__p.first, *__p.second);
4248 template<
typename _Tp,
typename _Compare>
4249 inline pair<_Tp, _Tp>
4250 minmax(initializer_list<_Tp> __l, _Compare __comp)
4252 pair<const _Tp*, const _Tp*> __p =
4253 std::minmax_element(__l.begin(), __l.end(), __comp);
4254 return std::make_pair(*__p.first, *__p.second);
4269 template<
typename _ForwardIterator1,
typename _ForwardIterator2>
4271 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4272 _ForwardIterator2 __first2)
4276 for (; __first1 != __last1; ++__first1, ++__first2)
4277 if (!(*__first1 == *__first2))
4280 if (__first1 == __last1)
4285 _ForwardIterator2 __last2 = __first2;
4286 std::advance(__last2, std::distance(__first1, __last1));
4287 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
4289 if (__scan != _GLIBCXX_STD_A::find(__first1, __scan, *__scan))
4292 auto __matches = std::count(__first2, __last2, *__scan);
4294 || std::count(__scan, __last1, *__scan) != __matches)
4314 template<
typename _ForwardIterator1,
typename _ForwardIterator2,
4315 typename _BinaryPredicate>
4317 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4318 _ForwardIterator2 __first2, _BinaryPredicate __pred)
4322 for (; __first1 != __last1; ++__first1, ++__first2)
4323 if (!
bool(__pred(*__first1, *__first2)))
4326 if (__first1 == __last1)
4331 _ForwardIterator2 __last2 = __first2;
4332 std::advance(__last2, std::distance(__first1, __last1));
4333 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
4335 using std::placeholders::_1;
4337 if (__scan != _GLIBCXX_STD_A::find_if(__first1, __scan,
4338 std::bind(__pred, _1, *__scan)))
4341 auto __matches = std::count_if(__first2, __last2,
4342 std::bind(__pred, _1, *__scan));
4344 || std::count_if(__scan, __last1,
4345 std::bind(__pred, _1, *__scan)) != __matches)
4351 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4364 template<
typename _RandomAccessIterator,
4365 typename _UniformRandomNumberGenerator>
4367 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4368 _UniformRandomNumberGenerator&& __g)
4372 _RandomAccessIterator>)
4375 if (__first == __last)
4378 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4381 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
4382 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
4383 typedef typename __distr_type::param_type __p_type;
4386 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4387 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
4393 _GLIBCXX_END_NAMESPACE_VERSION
4395 _GLIBCXX_BEGIN_NAMESPACE_ALGO
4409 template<
typename _InputIterator,
typename _Function>
4411 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4416 for (; __first != __last; ++__first)
4430 template<typename _InputIterator, typename _Tp>
4431 inline _InputIterator
4432 find(_InputIterator __first, _InputIterator __last,
4438 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4440 return std::__find(__first, __last, __val,
4441 std::__iterator_category(__first));
4454 template<typename _InputIterator, typename _Predicate>
4455 inline _InputIterator
4456 find_if(_InputIterator __first, _InputIterator __last,
4462 typename iterator_traits<_InputIterator>::value_type>)
4464 return std::__find_if(__first, __last, __pred,
4465 std::__iterator_category(__first));
4484 template<typename _InputIterator, typename _ForwardIterator>
4486 find_first_of(_InputIterator __first1, _InputIterator __last1,
4487 _ForwardIterator __first2, _ForwardIterator __last2)
4493 typename iterator_traits<_InputIterator>::value_type,
4494 typename iterator_traits<_ForwardIterator>::value_type>)
4498 for (; __first1 != __last1; ++__first1)
4499 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4500 if (*__first1 == *__iter)
4524 template<typename _InputIterator, typename _ForwardIterator,
4525 typename _BinaryPredicate>
4527 find_first_of(_InputIterator __first1, _InputIterator __last1,
4528 _ForwardIterator __first2, _ForwardIterator __last2,
4529 _BinaryPredicate __comp)
4535 typename iterator_traits<_InputIterator>::value_type,
4536 typename iterator_traits<_ForwardIterator>::value_type>)
4540 for (; __first1 != __last1; ++__first1)
4541 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4542 if (__comp(*__first1, *__iter))
4556 template<typename _ForwardIterator>
4558 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4563 typename iterator_traits<_ForwardIterator>::value_type>)
4565 if (__first == __last)
4567 _ForwardIterator __next = __first;
4568 while(++__next != __last)
4570 if (*__first == *__next)
4588 template<
typename _ForwardIterator,
typename _BinaryPredicate>
4590 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4591 _BinaryPredicate __binary_pred)
4596 typename iterator_traits<_ForwardIterator>::value_type,
4597 typename iterator_traits<_ForwardIterator>::value_type>)
4599 if (__first == __last)
4601 _ForwardIterator __next = __first;
4602 while(++__next != __last)
4604 if (__binary_pred(*__first, *__next))
4620 template<
typename _InputIterator,
typename _Tp>
4621 typename iterator_traits<_InputIterator>::difference_type
4622 count(_InputIterator __first, _InputIterator __last,
const _Tp& __value)
4627 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4629 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4630 for (; __first != __last; ++__first)
4631 if (*__first == __value)
4645 template<typename _InputIterator, typename _Predicate>
4646 typename iterator_traits<_InputIterator>::difference_type
4647 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4652 typename iterator_traits<_InputIterator>::value_type>)
4654 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4655 for (; __first != __last; ++__first)
4656 if (__pred(*__first))
4687 template<typename _ForwardIterator1, typename _ForwardIterator2>
4689 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4690 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4696 typename iterator_traits<_ForwardIterator1>::value_type,
4697 typename iterator_traits<_ForwardIterator2>::value_type>)
4702 if (__first1 == __last1 || __first2 == __last2)
4706 _ForwardIterator2 __p1(__first2);
4707 if (++__p1 == __last2)
4708 return _GLIBCXX_STD_A::find(__first1, __last1, *__first2);
4711 _ForwardIterator2 __p;
4712 _ForwardIterator1 __current = __first1;
4716 __first1 = _GLIBCXX_STD_A::find(__first1, __last1, *__first2);
4717 if (__first1 == __last1)
4721 __current = __first1;
4722 if (++__current == __last1)
4725 while (*__current == *__p)
4727 if (++__p == __last2)
4729 if (++__current == __last1)
4758 template<
typename _ForwardIterator1,
typename _ForwardIterator2,
4759 typename _BinaryPredicate>
4761 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4762 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4763 _BinaryPredicate __predicate)
4769 typename iterator_traits<_ForwardIterator1>::value_type,
4770 typename iterator_traits<_ForwardIterator2>::value_type>)
4775 if (__first1 == __last1 || __first2 == __last2)
4779 _ForwardIterator2 __p1(__first2);
4780 if (++__p1 == __last2)
4782 while (__first1 != __last1
4783 && !
bool(__predicate(*__first1, *__first2)))
4789 _ForwardIterator2 __p;
4790 _ForwardIterator1 __current = __first1;
4794 while (__first1 != __last1
4795 && !
bool(__predicate(*__first1, *__first2)))
4797 if (__first1 == __last1)
4801 __current = __first1;
4802 if (++__current == __last1)
4805 while (__predicate(*__current, *__p))
4807 if (++__p == __last2)
4809 if (++__current == __last1)
4833 template<
typename _ForwardIterator,
typename _Integer,
typename _Tp>
4835 search_n(_ForwardIterator __first, _ForwardIterator __last,
4836 _Integer __count,
const _Tp& __val)
4841 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4847 return _GLIBCXX_STD_A::find(__first, __last, __val);
4848 return std::__search_n(__first, __last, __count, __val,
4849 std::__iterator_category(__first));
4870 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4871 typename _BinaryPredicate>
4873 search_n(_ForwardIterator __first, _ForwardIterator __last,
4874 _Integer __count, const _Tp& __val,
4875 _BinaryPredicate __binary_pred)
4880 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4887 while (__first != __last && !
bool(__binary_pred(*__first, __val)))
4891 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4892 std::__iterator_category(__first));
4912 template<
typename _InputIterator,
typename _OutputIterator,
4913 typename _UnaryOperation>
4915 transform(_InputIterator __first, _InputIterator __last,
4916 _OutputIterator __result, _UnaryOperation __unary_op)
4922 __typeof__(__unary_op(*__first))>)
4925 for (; __first != __last; ++__first, ++__result)
4926 *__result = __unary_op(*__first);
4949 template<typename _InputIterator1, typename _InputIterator2,
4950 typename _OutputIterator, typename _BinaryOperation>
4952 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4953 _InputIterator2 __first2, _OutputIterator __result,
4954 _BinaryOperation __binary_op)
4961 __typeof__(__binary_op(*__first1,*__first2))>)
4964 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4965 *__result = __binary_op(*__first1, *__first2);
4982 template<typename _ForwardIterator, typename _Tp>
4984 replace(_ForwardIterator __first, _ForwardIterator __last,
4985 const _Tp& __old_value, const _Tp& __new_value)
4991 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4993 typename iterator_traits<_ForwardIterator>::value_type>)
4996 for (; __first != __last; ++__first)
4997 if (*__first == __old_value)
4998 *__first = __new_value;
5014 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
5016 replace_if(_ForwardIterator __first, _ForwardIterator __last,
5017 _Predicate __pred, const _Tp& __new_value)
5023 typename iterator_traits<_ForwardIterator>::value_type>)
5025 typename iterator_traits<_ForwardIterator>::value_type>)
5028 for (; __first != __last; ++__first)
5029 if (__pred(*__first))
5030 *__first = __new_value;
5046 template<typename _ForwardIterator, typename _Generator>
5048 generate(_ForwardIterator __first, _ForwardIterator __last,
5054 typename iterator_traits<_ForwardIterator>::value_type>)
5057 for (; __first != __last; ++__first)
5077 template<typename _OutputIterator, typename _Size, typename _Generator>
5079 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
5084 __typeof__(__gen())>)
5086 for (__decltype(__n + 0) __niter = __n;
5087 __niter > 0; --__niter, ++__first)
5114 template<typename _InputIterator, typename _OutputIterator>
5115 inline _OutputIterator
5116 unique_copy(_InputIterator __first, _InputIterator __last,
5117 _OutputIterator __result)
5122 typename iterator_traits<_InputIterator>::value_type>)
5124 typename iterator_traits<_InputIterator>::value_type>)
5127 if (__first == __last)
5129 return std::__unique_copy(__first, __last, __result,
5130 std::__iterator_category(__first),
5131 std::__iterator_category(__result));
5153 template<typename _InputIterator, typename _OutputIterator,
5154 typename _BinaryPredicate>
5155 inline _OutputIterator
5156 unique_copy(_InputIterator __first, _InputIterator __last,
5157 _OutputIterator __result,
5158 _BinaryPredicate __binary_pred)
5163 typename iterator_traits<_InputIterator>::value_type>)
5166 if (__first == __last)
5168 return std::__unique_copy(__first, __last, __result, __binary_pred,
5169 std::__iterator_category(__first),
5170 std::__iterator_category(__result));
5185 template<typename _RandomAccessIterator>
5187 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
5191 _RandomAccessIterator>)
5194 if (__first != __last)
5195 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5197 _RandomAccessIterator __j = __first
5198 + std::rand() % ((__i - __first) + 1);
5200 std::iter_swap(__i, __j);
5218 template<
typename _RandomAccessIterator,
typename _RandomNumberGenerator>
5220 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
5221 #
if __cplusplus >= 201103L
5222 _RandomNumberGenerator&& __rand)
5224 _RandomNumberGenerator& __rand)
5229 _RandomAccessIterator>)
5232 if (__first == __last)
5234 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5236 _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
5238 std::iter_swap(__i, __j);
5258 template<
typename _ForwardIterator,
typename _Predicate>
5259 inline _ForwardIterator
5260 partition(_ForwardIterator __first, _ForwardIterator __last,
5267 typename iterator_traits<_ForwardIterator>::value_type>)
5270 return std::__partition(__first, __last, __pred,
5271 std::__iterator_category(__first));
5292 template<typename _RandomAccessIterator>
5294 partial_sort(_RandomAccessIterator __first,
5295 _RandomAccessIterator __middle,
5296 _RandomAccessIterator __last)
5298 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5303 _RandomAccessIterator>)
5308 std::__heap_select(__first, __middle, __last);
5309 std::sort_heap(__first, __middle);
5331 template<typename _RandomAccessIterator, typename _Compare>
5333 partial_sort(_RandomAccessIterator __first,
5334 _RandomAccessIterator __middle,
5335 _RandomAccessIterator __last,
5338 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5343 _RandomAccessIterator>)
5345 _ValueType, _ValueType>)
5349 std::__heap_select(__first, __middle, __last, __comp);
5350 std::sort_heap(__first, __middle, __comp);
5368 template<typename _RandomAccessIterator>
5370 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5371 _RandomAccessIterator __last)
5373 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5378 _RandomAccessIterator>)
5383 if (__first == __last || __nth == __last)
5386 std::__introselect(__first, __nth, __last,
5387 std::__lg(__last - __first) * 2);
5407 template<typename _RandomAccessIterator, typename _Compare>
5409 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5410 _RandomAccessIterator __last, _Compare __comp)
5412 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5417 _RandomAccessIterator>)
5419 _ValueType, _ValueType>)
5423 if (__first == __last || __nth == __last)
5426 std::__introselect(__first, __nth, __last,
5427 std::__lg(__last - __first) * 2, __comp);
5445 template<typename _RandomAccessIterator>
5447 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5449 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5454 _RandomAccessIterator>)
5458 if (__first != __last)
5460 std::__introsort_loop(__first, __last,
5461 std::__lg(__last - __first) * 2);
5462 std::__final_insertion_sort(__first, __last);
5481 template<
typename _RandomAccessIterator,
typename _Compare>
5483 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5486 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5491 _RandomAccessIterator>)
5496 if (__first != __last)
5498 std::__introsort_loop(__first, __last,
5499 std::__lg(__last - __first) * 2, __comp);
5500 std::__final_insertion_sort(__first, __last, __comp);
5523 template<
typename _InputIterator1,
typename _InputIterator2,
5524 typename _OutputIterator>
5526 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5527 _InputIterator2 __first2, _InputIterator2 __last2,
5528 _OutputIterator __result)
5530 typedef typename iterator_traits<_InputIterator1>::value_type
5532 typedef typename iterator_traits<_InputIterator2>::value_type
5546 while (__first1 != __last1 && __first2 != __last2)
5548 if (*__first2 < *__first1)
5550 *__result = *__first2;
5555 *__result = *__first1;
5560 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5587 template<
typename _InputIterator1,
typename _InputIterator2,
5588 typename _OutputIterator,
typename _Compare>
5590 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5591 _InputIterator2 __first2, _InputIterator2 __last2,
5592 _OutputIterator __result, _Compare __comp)
5594 typedef typename iterator_traits<_InputIterator1>::value_type
5596 typedef typename iterator_traits<_InputIterator2>::value_type
5607 _ValueType2, _ValueType1>)
5611 while (__first1 != __last1 && __first2 != __last2)
5613 if (__comp(*__first2, *__first1))
5615 *__result = *__first2;
5620 *__result = *__first1;
5625 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5647 template<
typename _RandomAccessIterator>
5649 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5651 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5653 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5658 _RandomAccessIterator>)
5662 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5664 if (__buf.begin() == 0)
5665 std::__inplace_stable_sort(__first, __last);
5667 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5668 _DistanceType(__buf.size()));
5689 template<typename _RandomAccessIterator, typename _Compare>
5691 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5694 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5696 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5701 _RandomAccessIterator>)
5707 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5709 if (__buf.begin() == 0)
5710 std::__inplace_stable_sort(__first, __last, __comp);
5712 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5713 _DistanceType(__buf.size()), __comp);
5735 template<typename _InputIterator1, typename _InputIterator2,
5736 typename _OutputIterator>
5738 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5739 _InputIterator2 __first2, _InputIterator2 __last2,
5740 _OutputIterator __result)
5742 typedef typename iterator_traits<_InputIterator1>::value_type
5744 typedef typename iterator_traits<_InputIterator2>::value_type
5759 while (__first1 != __last1 && __first2 != __last2)
5761 if (*__first1 < *__first2)
5763 *__result = *__first1;
5766 else if (*__first2 < *__first1)
5768 *__result = *__first2;
5773 *__result = *__first1;
5779 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5802 template<
typename _InputIterator1,
typename _InputIterator2,
5803 typename _OutputIterator,
typename _Compare>
5805 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5806 _InputIterator2 __first2, _InputIterator2 __last2,
5807 _OutputIterator __result, _Compare __comp)
5809 typedef typename iterator_traits<_InputIterator1>::value_type
5811 typedef typename iterator_traits<_InputIterator2>::value_type
5822 _ValueType1, _ValueType2>)
5824 _ValueType2, _ValueType1>)
5828 while (__first1 != __last1 && __first2 != __last2)
5830 if (__comp(*__first1, *__first2))
5832 *__result = *__first1;
5835 else if (__comp(*__first2, *__first1))
5837 *__result = *__first2;
5842 *__result = *__first1;
5848 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5869 template<
typename _InputIterator1,
typename _InputIterator2,
5870 typename _OutputIterator>
5872 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5873 _InputIterator2 __first2, _InputIterator2 __last2,
5874 _OutputIterator __result)
5876 typedef typename iterator_traits<_InputIterator1>::value_type
5878 typedef typename iterator_traits<_InputIterator2>::value_type
5891 while (__first1 != __last1 && __first2 != __last2)
5892 if (*__first1 < *__first2)
5894 else if (*__first2 < *__first1)
5898 *__result = *__first1;
5926 template<
typename _InputIterator1,
typename _InputIterator2,
5927 typename _OutputIterator,
typename _Compare>
5929 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5930 _InputIterator2 __first2, _InputIterator2 __last2,
5931 _OutputIterator __result, _Compare __comp)
5933 typedef typename iterator_traits<_InputIterator1>::value_type
5935 typedef typename iterator_traits<_InputIterator2>::value_type
5944 _ValueType1, _ValueType2>)
5946 _ValueType2, _ValueType1>)
5950 while (__first1 != __last1 && __first2 != __last2)
5951 if (__comp(*__first1, *__first2))
5953 else if (__comp(*__first2, *__first1))
5957 *__result = *__first1;
5984 template<
typename _InputIterator1,
typename _InputIterator2,
5985 typename _OutputIterator>
5987 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5988 _InputIterator2 __first2, _InputIterator2 __last2,
5989 _OutputIterator __result)
5991 typedef typename iterator_traits<_InputIterator1>::value_type
5993 typedef typename iterator_traits<_InputIterator2>::value_type
6006 while (__first1 != __last1 && __first2 != __last2)
6007 if (*__first1 < *__first2)
6009 *__result = *__first1;
6013 else if (*__first2 < *__first1)
6020 return std::copy(__first1, __last1, __result);
6045 template<
typename _InputIterator1,
typename _InputIterator2,
6046 typename _OutputIterator,
typename _Compare>
6048 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
6049 _InputIterator2 __first2, _InputIterator2 __last2,
6050 _OutputIterator __result, _Compare __comp)
6052 typedef typename iterator_traits<_InputIterator1>::value_type
6054 typedef typename iterator_traits<_InputIterator2>::value_type
6063 _ValueType1, _ValueType2>)
6065 _ValueType2, _ValueType1>)
6069 while (__first1 != __last1 && __first2 != __last2)
6070 if (__comp(*__first1, *__first2))
6072 *__result = *__first1;
6076 else if (__comp(*__first2, *__first1))
6083 return std::copy(__first1, __last1, __result);
6103 template<
typename _InputIterator1,
typename _InputIterator2,
6104 typename _OutputIterator>
6106 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
6107 _InputIterator2 __first2, _InputIterator2 __last2,
6108 _OutputIterator __result)
6110 typedef typename iterator_traits<_InputIterator1>::value_type
6112 typedef typename iterator_traits<_InputIterator2>::value_type
6127 while (__first1 != __last1 && __first2 != __last2)
6128 if (*__first1 < *__first2)
6130 *__result = *__first1;
6134 else if (*__first2 < *__first1)
6136 *__result = *__first2;
6145 return std::copy(__first2, __last2, std::copy(__first1,
6146 __last1, __result));
6169 template<
typename _InputIterator1,
typename _InputIterator2,
6170 typename _OutputIterator,
typename _Compare>
6172 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
6173 _InputIterator2 __first2, _InputIterator2 __last2,
6174 _OutputIterator __result,
6177 typedef typename iterator_traits<_InputIterator1>::value_type
6179 typedef typename iterator_traits<_InputIterator2>::value_type
6190 _ValueType1, _ValueType2>)
6192 _ValueType2, _ValueType1>)
6196 while (__first1 != __last1 && __first2 != __last2)
6197 if (__comp(*__first1, *__first2))
6199 *__result = *__first1;
6203 else if (__comp(*__first2, *__first1))
6205 *__result = *__first2;
6214 return std::copy(__first2, __last2,
6215 std::copy(__first1, __last1, __result));
6226 template<
typename _ForwardIterator>
6228 min_element(_ForwardIterator __first, _ForwardIterator __last)
6233 typename iterator_traits<_ForwardIterator>::value_type>)
6236 if (__first == __last)
6238 _ForwardIterator __result = __first;
6239 while (++__first != __last)
6240 if (*__first < *__result)
6254 template<typename _ForwardIterator, typename _Compare>
6256 min_element(_ForwardIterator __first, _ForwardIterator __last,
6262 typename iterator_traits<_ForwardIterator>::value_type,
6263 typename iterator_traits<_ForwardIterator>::value_type>)
6266 if (__first == __last)
6268 _ForwardIterator __result = __first;
6269 while (++__first != __last)
6270 if (__comp(*__first, *__result))
6282 template<typename _ForwardIterator>
6284 max_element(_ForwardIterator __first, _ForwardIterator __last)
6289 typename iterator_traits<_ForwardIterator>::value_type>)
6292 if (__first == __last)
6294 _ForwardIterator __result = __first;
6295 while (++__first != __last)
6296 if (*__result < *__first)
6310 template<typename _ForwardIterator, typename _Compare>
6312 max_element(_ForwardIterator __first, _ForwardIterator __last,
6318 typename iterator_traits<_ForwardIterator>::value_type,
6319 typename iterator_traits<_ForwardIterator>::value_type>)
6322 if (__first == __last) return __first;
6323 _ForwardIterator __result = __first;
6324 while (++__first != __last)
6325 if (__comp(*__result, *__first))
6330 _GLIBCXX_END_NAMESPACE_ALGO
#define __glibcxx_requires_partitioned_lower(_First, _Last, _Value)
Definition: debug.h:71
#define __glibcxx_requires_partitioned_upper_pred(_First, _Last, _Value, _Pred)
Definition: debug.h:74
#define __glibcxx_function_requires(...)
Definition: concept_check.h:47
#define _GLIBCXX_MOVE(__val)
Definition: move.h:145
#define __glibcxx_requires_partitioned_upper(_First, _Last, _Value)
Definition: debug.h:72
#define false
Definition: stdbool.h:35
#define __glibcxx_requires_sorted_pred(_First, _Last, _Pred)
Definition: debug.h:68
const _Tp & min(const _Tp &__a, const _Tp &__b)
Equivalent to std::min.
Definition: base.h:144
return(unsigned int) __res
#define __glibcxx_requires_partitioned_lower_pred(_First, _Last, _Value, _Pred)
Definition: debug.h:73
#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp)
#define __glibcxx_requires_sorted_set_pred(_First1, _Last1, _First2, _Pred)
Definition: debug.h:70
const _Tp & max(const _Tp &__a, const _Tp &__b)
Equivalent to std::max.
Definition: base.h:150
#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp)
#define __glibcxx_requires_sorted(_First, _Last)
Definition: debug.h:67
#define __glibcxx_requires_valid_range(_First, _Last)
Definition: debug.h:65
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160
#define __glibcxx_requires_sorted_set(_First1, _Last1, _First2)
Definition: debug.h:69