Skip to content

Commit

Permalink
dispatch-locally
Browse files Browse the repository at this point in the history
  • Loading branch information
izvolov committed Sep 16, 2024
1 parent eaf9847 commit 6e973ad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 93 deletions.
24 changes: 0 additions & 24 deletions libcxx/include/__algorithm/radix_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,30 +323,6 @@ __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _Ran
std::__radix_sort(__first, __last, __buffer, __identity{}, __low_byte_fn{});
}

template <class _RandomAccessIterator1, class _RandomAccessIterator2>
_LIBCPP_HIDE_FROM_ABI bool
__radix_sort(_RandomAccessIterator1 __first,
_RandomAccessIterator1 __last,
_RandomAccessIterator2 __buffer,
_BoolConstant<true>) {
std::__radix_sort(__first, __last, __buffer, __identity{}, __low_byte_fn{});
return true;
}

template <class _RandomAccessIterator1, class _RandomAccessIterator2>
_LIBCPP_HIDE_FROM_ABI bool
__radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<false>) {
return false;
}

#else // _LIBCPP_STD_VER > 14

template <class _RandomAccessIterator1, class _RandomAccessIterator2, bool _EnableRadixSort>
_LIBCPP_HIDE_FROM_ABI bool
__radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_EnableRadixSort>) {
return false;
}

#endif // _LIBCPP_STD_VER > 14

_LIBCPP_END_NAMESPACE_STD
Expand Down
16 changes: 1 addition & 15 deletions libcxx/include/__algorithm/ranges_stable_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/dangling.h>
#include <__type_traits/desugars_to.h>
#include <__type_traits/is_integral.h>
#include <__utility/forward.h>
#include <__utility/move.h>

Expand All @@ -47,19 +45,7 @@ struct __stable_sort {
auto __last_iter = ranges::next(__first, __last);

auto&& __projected_comp = std::__make_projected(__comp, __proj);
constexpr auto __default_comp =
__desugars_to_v<__totally_ordered_less_tag, _Comp, iter_value_t<_Iter>, iter_value_t<_Iter> >;
constexpr auto __default_proj = __is_identity<_Proj>::value;
constexpr auto __integral_value = is_integral_v<iter_value_t<_Iter>>;
constexpr auto __integral_projection = __default_proj && __integral_value;
// constexpr auto __integral_projection = is_integral_v<remove_reference_t<invoke_result_t<_Proj&,
// iter_value_t<_Iter>>>>;
// TODO: Support projection in stable_sort
std::__stable_sort_impl<_RangeAlgPolicy>(
std::move(__first),
__last_iter,
__projected_comp,
_BoolConstant < __default_comp && __integral_projection > {});
std::__stable_sort_impl<_RangeAlgPolicy>(std::move(__first), __last_iter, __projected_comp);

return __last_iter;
}
Expand Down
96 changes: 42 additions & 54 deletions libcxx/include/__algorithm/stable_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <__memory/destruct_n.h>
#include <__memory/unique_ptr.h>
#include <__memory/unique_temporary_buffer.h>
#include <__type_traits/desugars_to.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_assignable.h>
#include <__type_traits/remove_cvref.h>
#include <__utility/move.h>
#include <__utility/pair.h>
#include <new>
Expand Down Expand Up @@ -138,24 +138,20 @@ _LIBCPP_HIDE_FROM_ABI void __merge_move_assign(
*__result = _Ops::__iter_move(__first2);
}

template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, bool _EnableRadixSort>
void __stable_sort(
_RandomAccessIterator __first,
_RandomAccessIterator __last,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
ptrdiff_t __buff_size,
_BoolConstant<_EnableRadixSort>);
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
void __stable_sort(_RandomAccessIterator __first,
_RandomAccessIterator __last,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
ptrdiff_t __buff_size);

template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, bool _EnableRadixSort>
void __stable_sort_move(
_RandomAccessIterator __first1,
_RandomAccessIterator __last1,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __first2,
_BoolConstant<_EnableRadixSort> __rs) {
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
void __stable_sort_move(_RandomAccessIterator __first1,
_RandomAccessIterator __last1,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __first2) {
using _Ops = _IterOps<_AlgPolicy>;

typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Expand Down Expand Up @@ -188,8 +184,8 @@ void __stable_sort_move(
}
typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
_RandomAccessIterator __m = __first1 + __l2;
std::__stable_sort<_AlgPolicy, _Compare>(__first1, __m, __comp, __l2, __first2, __l2, __rs);
std::__stable_sort<_AlgPolicy, _Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2, __rs);
std::__stable_sort<_AlgPolicy, _Compare>(__first1, __m, __comp, __l2, __first2, __l2);
std::__stable_sort<_AlgPolicy, _Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
std::__merge_move_construct<_AlgPolicy, _Compare>(__first1, __m, __m, __last1, __first2, __comp);
}

Expand Down Expand Up @@ -218,15 +214,13 @@ struct __radix_sort_max_switch<_Int64, __enable_if_t<is_integral<_Int64>::value
static const unsigned value = (1 << 15);
};

template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, bool _EnableRadixSort>
void __stable_sort(
_RandomAccessIterator __first,
_RandomAccessIterator __last,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
ptrdiff_t __buff_size,
_BoolConstant<_EnableRadixSort> __rs) {
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
void __stable_sort(_RandomAccessIterator __first,
_RandomAccessIterator __last,
_Compare __comp,
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
ptrdiff_t __buff_size) {
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
switch (__len) {
Expand All @@ -242,9 +236,14 @@ void __stable_sort(
std::__insertion_sort<_AlgPolicy, _Compare>(__first, __last, __comp);
return;
}
if (__len <= __buff_size && __len >= static_cast<difference_type>(__radix_sort_min_switch<value_type>::value) &&
__len <= static_cast<difference_type>(__radix_sort_max_switch<value_type>::value)) {
if (std::__radix_sort(__first, __last, __buff, __rs)) {
constexpr auto __default_comp =
__desugars_to_v<__totally_ordered_less_tag, __remove_cvref_t<_Compare>, value_type, value_type >;
constexpr auto __integral_value = is_integral_v<value_type >;
constexpr auto __allowed_radix_sort = __default_comp && __integral_value;
if constexpr (__allowed_radix_sort) {
if (__len <= __buff_size && __len >= static_cast<difference_type>(__radix_sort_min_switch<value_type>::value) &&
__len <= static_cast<difference_type>(__radix_sort_max_switch<value_type>::value)) {
std::__radix_sort(__first, __last, __buff);
return;
}
}
Expand All @@ -253,9 +252,9 @@ void __stable_sort(
if (__len <= __buff_size) {
__destruct_n __d(0);
unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
std::__stable_sort_move<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff, __rs);
std::__stable_sort_move<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff);
__d.__set(__l2, (value_type*)nullptr);
std::__stable_sort_move<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff + __l2, __rs);
std::__stable_sort_move<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
__d.__set(__len, (value_type*)nullptr);
std::__merge_move_assign<_AlgPolicy, _Compare>(
__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
Expand All @@ -266,17 +265,14 @@ void __stable_sort(
// __first, __comp);
return;
}
std::__stable_sort<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff, __buff_size, __rs);
std::__stable_sort<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size, __rs);
std::__stable_sort<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
std::__stable_sort<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
std::__inplace_merge<_AlgPolicy>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
}

template <class _AlgPolicy, class _RandomAccessIterator, class _Compare, bool _EnableRadixSort = false>
inline _LIBCPP_HIDE_FROM_ABI void __stable_sort_impl(
_RandomAccessIterator __first,
_RandomAccessIterator __last,
_Compare& __comp,
_BoolConstant<_EnableRadixSort> __rs = _BoolConstant<false>()) {
template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
inline _LIBCPP_HIDE_FROM_ABI void
__stable_sort_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;

Expand All @@ -289,27 +285,19 @@ inline _LIBCPP_HIDE_FROM_ABI void __stable_sort_impl(
__buf.second = __unique_buf.get_deleter().__count_;
}

std::__stable_sort<_AlgPolicy, __comp_ref_type<_Compare> >(
__first, __last, __comp, __len, __buf.first, __buf.second, __rs);
std::__stable_sort<_AlgPolicy, __comp_ref_type<_Compare> >(__first, __last, __comp, __len, __buf.first, __buf.second);
std::__check_strict_weak_ordering_sorted(__first, __last, __comp);
}

template <class _RandomAccessIterator, class _Compare>
inline _LIBCPP_HIDE_FROM_ABI void
stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
std::__stable_sort_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, _BoolConstant<false>());
std::__stable_sort_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
}

template <class _RandomAccessIterator>
inline _LIBCPP_HIDE_FROM_ABI void stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) {
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
using reference_type = typename iterator_traits<_RandomAccessIterator>::reference;
auto __comp = __less<>();
std::__stable_sort_impl<_ClassicAlgPolicy>(
std::move(__first),
std::move(__last),
__comp,
_BoolConstant < is_integral<value_type>::value && is_same<value_type&, reference_type>::value > ());
std::stable_sort(__first, __last, __less<>());
}

_LIBCPP_END_NAMESPACE_STD
Expand Down

0 comments on commit 6e973ad

Please sign in to comment.