diff --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h --- a/libcxx/include/__algorithm/sort.h +++ b/libcxx/include/__algorithm/sort.h @@ -180,10 +180,9 @@ _ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c) { using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type; - using _Ops = _IterOps<_AlgPolicy>; _WrappedComp __wrapped_comp(__c); - return std::__sort5<_WrappedComp>( - _Ops::__iter_move(__x1), _Ops::__iter_move(__x2), _Ops::__iter_move(__x3), _Ops::__iter_move(__x4), _Ops::__iter_move(__x5), __wrapped_comp); + return std::__sort5<_WrappedComp, _ForwardIterator>( + std::move(__x1), std::move(__x2), std::move(__x3), std::move(__x4), std::move(__x5), __wrapped_comp); } // The comparator being simple is a prerequisite for using the branchless optimization. @@ -273,7 +272,7 @@ std::__sort4<_AlgPolicy, _Compare>(__x1, __x2, __x3, __x4, __c); } -template +template inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<__use_branchless_sort<_Compare, _RandomAccessIterator>::value, void> __sort5_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2, _RandomAccessIterator __x3, _RandomAccessIterator __x4, _RandomAccessIterator __x5, _Compare __c) { @@ -376,7 +375,7 @@ return true; case 2: if (__comp(*--__last, *__first)) - _IterOps<_AlgPolicy>::iter_swap(__first, __last); + _Ops::iter_swap(__first, __last); return true; case 3: std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp); @@ -437,7 +436,7 @@ *__j2 = std::move(*__i2); *__j2 = _Ops::__iter_move(__first1); } else { - ::new ((void*)__j2) value_type(_Ops::_iter_move(__first1)); + ::new ((void*)__j2) value_type(_Ops::__iter_move(__first1)); __d.template __incr(); } } @@ -445,9 +444,10 @@ } } -template +template inline _LIBCPP_HIDE_FROM_ABI void __swap_bitmap_pos(_RandomAccessIterator __first, _RandomAccessIterator __last, uint64_t& __left_bitset, uint64_t& __right_bitset) { + using _Ops = _IterOps<_AlgPolicy>; typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type; // Swap one pair on each iteration as long as both bitsets have at least one // element for swapping. @@ -456,7 +456,7 @@ __left_bitset = __libcpp_blsr(__left_bitset); difference_type tz_right = __libcpp_ctz(__right_bitset); __right_bitset = __libcpp_blsr(__right_bitset); - std::iter_swap(__first + tz_left, __last - tz_right); + _Ops::iter_swap(__first + tz_left, __last - tz_right); } } @@ -490,7 +490,7 @@ } } -template ::value_type> inline _LIBCPP_HIDE_FROM_ABI void __bitset_partition_partial_blocks(_RandomAccessIterator& __first, _RandomAccessIterator& __lm1, _Compare __comp, @@ -529,14 +529,15 @@ --__iter; } } - __swap_bitmap_pos(__first, __lm1, __left_bitset, __right_bitset); + __swap_bitmap_pos<_AlgPolicy, _RandomAccessIterator>(__first, __lm1, __left_bitset, __right_bitset); __first += (__left_bitset == 0) ? __l_size : 0; __lm1 -= (__right_bitset == 0) ? __r_size : 0; } -template +template inline _LIBCPP_HIDE_FROM_ABI void __swap_bitmap_pos_within(_RandomAccessIterator& __first, _RandomAccessIterator& __lm1, uint64_t& __left_bitset, uint64_t& __right_bitset) { + using _Ops = _IterOps<_AlgPolicy>; typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type; if (__left_bitset) { // Swap within the left side. Need to find set positions in the reverse @@ -546,7 +547,7 @@ __left_bitset &= (static_cast(1) << __tz_left) - 1; _RandomAccessIterator it = __first + __tz_left; if (it != __lm1) { - std::iter_swap(it, __lm1); + _Ops::iter_swap(it, __lm1); } --__lm1; } @@ -559,7 +560,7 @@ __right_bitset &= (static_cast(1) << __tz_right) - 1; _RandomAccessIterator it = __lm1 - __tz_right; if (it != __first) { - std::iter_swap(it, __first); + _Ops::iter_swap(it, __first); } ++__first; } @@ -574,15 +575,16 @@ // // __bitset_partition uses bitsets for storing outcomes of the comparisons // between the pivot and other elements. -template +template _LIBCPP_HIDE_FROM_ABI std::pair<_RandomAccessIterator, bool> __bitset_partition(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { + using _Ops = _IterOps<_AlgPolicy>; typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type; typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type; _LIBCPP_ASSERT(__last - __first >= difference_type(3), ""); _RandomAccessIterator __begin = __first; - value_type __pivot(std::move(*__first)); + value_type __pivot(_Ops::__iter_move(__first)); // Find the first element greater than the pivot. if (__comp(__pivot, *(__last - difference_type(1)))) { // Not guarded since we know the last element is greater than the pivot. @@ -605,7 +607,7 @@ // partitioned. bool __already_partitioned = __first >= __last; if (!__already_partitioned) { - std::iter_swap(__first, __last); + _Ops::iter_swap(__first, __last); ++__first; } @@ -627,7 +629,7 @@ __populate_right_bitset<_Compare>(__lm1, __comp, __pivot, __right_bitset); // Swap the elements recorded to be the candidates for swapping in the // bitsets. - __swap_bitmap_pos(__first, __lm1, __left_bitset, __right_bitset); + __swap_bitmap_pos<_AlgPolicy, _RandomAccessIterator>(__first, __lm1, __left_bitset, __right_bitset); // Only advance the iterator if all the elements that need to be moved to // other side were moved. __first += (__left_bitset == 0) ? difference_type(__detail::__block_size) : difference_type(0); @@ -635,15 +637,15 @@ } // Now, we have a less-than a block worth of elements on at least one of the // sides. - __bitset_partition_partial_blocks<_Compare>(__first, __lm1, __comp, __pivot, __left_bitset, __right_bitset); + __bitset_partition_partial_blocks<_AlgPolicy, _Compare>(__first, __lm1, __comp, __pivot, __left_bitset, __right_bitset); // At least one the bitsets would be empty. For the non-empty one, we need to // properly partition the elements that appear within that bitset. - __swap_bitmap_pos_within(__first, __lm1, __left_bitset, __right_bitset); + __swap_bitmap_pos_within<_AlgPolicy>(__first, __lm1, __left_bitset, __right_bitset); // Move the pivot to its correct position. _RandomAccessIterator __pivot_pos = __first - difference_type(1); if (__begin != __pivot_pos) { - *__begin = std::move(*__pivot_pos); + *__begin = _Ops::__iter_move(__pivot_pos); } *__pivot_pos = std::move(__pivot); return std::make_pair(__pivot_pos, __already_partitioned); @@ -654,14 +656,15 @@ // pivot. Returns the iterator for the pivot and a bool value which is true if // the provided range is already sorted, false otherwise. We assume that the // length of the range is at least three elements. -template +template _LIBCPP_HIDE_FROM_ABI std::pair<_RandomAccessIterator, bool> __partition_with_equals_on_right(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { + using _Ops = _IterOps<_AlgPolicy>; typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type; _LIBCPP_ASSERT(__last - __first >= difference_type(3), ""); _RandomAccessIterator __begin = __first; - value_type __pivot(std::move(*__first)); + value_type __pivot(_Ops::__iter_move(__first)); // Find the first element greater or equal to the pivot. It will be always // guarded because __introsort will do the median-of-three before calling // this. @@ -686,7 +689,7 @@ // right of the pivot and the other to left of the pivot) that are not on the // correct side of the pivot. while (__first < __last) { - std::iter_swap(__first, __last); + _Ops::iter_swap(__first, __last); while (__comp(*++__first, __pivot)) ; while (!__comp(*--__last, __pivot)) @@ -695,7 +698,7 @@ // Move the pivot to its correct position. _RandomAccessIterator __pivot_pos = __first - difference_type(1); if (__begin != __pivot_pos) { - *__begin = std::move(*__pivot_pos); + *__begin = _Ops::__iter_move(__pivot_pos); } *__pivot_pos = std::move(__pivot); return std::make_pair(__pivot_pos, __already_partitioned); @@ -703,14 +706,15 @@ // Similar to the above function. Elements equivalent to the pivot are put to // the left of the pivot. Returns the iterator to the pivot element. -template +template _LIBCPP_HIDE_FROM_ABI _RandomAccessIterator __partition_with_equals_on_left(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { + using _Ops = _IterOps<_AlgPolicy>; typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type; _RandomAccessIterator __begin = __first; - value_type __pivot(std::move(*__first)); + value_type __pivot(_Ops::__iter_move(__first)); if (__comp(__pivot, *(__last - difference_type(1)))) { // Guarded. while (!__comp(__pivot, *++__first)) { @@ -727,7 +731,7 @@ } } while (__first < __last) { - std::iter_swap(__first, __last); + _Ops::iter_swap(__first, __last); while (!__comp(__pivot, *++__first)) ; while (__comp(__pivot, *--__last)) @@ -735,7 +739,7 @@ } _RandomAccessIterator __pivot_pos = __first - difference_type(1); if (__begin != __pivot_pos) { - *__begin = std::move(*__pivot_pos); + *__begin = _Ops::__iter_move(__pivot_pos); } *__pivot_pos = std::move(__pivot); return __first; @@ -766,7 +770,7 @@ return; case 2: if (__comp(*--__last, *__first)) - _IterOps<_AlgPolicy>::iter_swap(__first, __last); + _Ops::iter_swap(__first, __last); return; case 3: std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp); @@ -807,7 +811,7 @@ std::__sort3<_AlgPolicy, _Compare>(__first + difference_type(2), __first + (__half_len + 1), __last - difference_type(3), __comp); std::__sort3<_AlgPolicy, _Compare>(__first + (__half_len - 1), __first + __half_len, __first + (__half_len + 1), __comp); - std::iter_swap(__first, __first + __half_len); + _Ops::iter_swap(__first, __first + __half_len); } else { std::__sort3<_AlgPolicy, _Compare>(__first + __half_len, __first, __last - difference_type(1), __comp); } @@ -821,13 +825,13 @@ // partitioned. This also means that we do not need to sort the left // side of the partition. if (!__leftmost && !__comp(*(__first - difference_type(1)), *__first)) { - __first = __partition_with_equals_on_left<_RandomAccessIterator, _Comp_ref>(__first, __last, _Comp_ref(__comp)); + __first = __partition_with_equals_on_left<_AlgPolicy, _RandomAccessIterator, _Comp_ref>(__first, __last, _Comp_ref(__comp)); continue; } // Use bitset partition only if asked for. auto __ret = _UseBitSetPartition - ? __bitset_partition<_RandomAccessIterator, _Compare>(__first, __last, __comp) - : __partition_with_equals_on_right<_RandomAccessIterator, _Compare>(__first, __last, __comp); + ? __bitset_partition<_AlgPolicy, _RandomAccessIterator, _Compare>(__first, __last, __comp) + : __partition_with_equals_on_right<_AlgPolicy, _RandomAccessIterator, _Compare>(__first, __last, __comp); _RandomAccessIterator __i = __ret.first; // [__first, __i) < *__i and *__i <= [__i+1, __last) // If we were given a perfect partition, see if insertion sort is quick...