[libcxx-commits] [libcxx] [libcxx][algorithm] Optimize std::stable_sort via radix sort algorithm (PR #104683)

Дмитрий Изволов via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 23 02:04:48 PDT 2024


https://github.com/izvolov updated https://github.com/llvm/llvm-project/pull/104683

>From 5717ad80b633d5a2005b98bc35001c3cd0a47887 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 5 May 2024 11:08:26 +0300
Subject: [PATCH 01/33] radix-sort

---
 libcxx/include/CMakeLists.txt            |   1 +
 libcxx/include/__algorithm/radix_sort.h  | 410 +++++++++++++++++++++++
 libcxx/include/__algorithm/stable_sort.h | 111 ++++--
 libcxx/include/module.modulemap          |   1 +
 4 files changed, 491 insertions(+), 32 deletions(-)
 create mode 100644 libcxx/include/__algorithm/radix_sort.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 32579272858a8e..95e4e3faf88671 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -74,6 +74,7 @@ set(files
   __algorithm/prev_permutation.h
   __algorithm/pstl.h
   __algorithm/push_heap.h
+  __algorithm/radix_sort.h
   __algorithm/ranges_adjacent_find.h
   __algorithm/ranges_all_of.h
   __algorithm/ranges_any_of.h
diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
new file mode 100644
index 00000000000000..5e14dec9df0918
--- /dev/null
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -0,0 +1,410 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_RADIX_SORT_H
+#define _LIBCPP___ALGORITHM_RADIX_SORT_H
+
+#include <__algorithm/copy.h>
+#include <__algorithm/for_each.h>
+#include <__config>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/move_iterator.h>
+#include <__iterator/next.h>
+#include <__numeric/partial_sum.h>
+#include <__type_traits/decay.h>
+#include <__type_traits/enable_if.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_assignable.h>
+#include <__type_traits/is_integral.h>
+#include <__type_traits/is_unsigned.h>
+#include <__type_traits/make_unsigned.h>
+#include <__utility/forward.h>
+#include <__utility/integer_sequence.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
+#include <climits>
+#include <cstdint>
+#include <initializer_list>
+#include <limits>
+#include <stdexcept>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 14
+
+inline void __variadic_expansion_dummy(initializer_list<int>) {}
+
+#  define EXPAND_VARIADIC(expression) __variadic_expansion_dummy({(expression, 0)...})
+
+template <typename _Iterator>
+constexpr auto __move_assign_please(_Iterator __i)
+    -> enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value,
+                   move_iterator<_Iterator> > {
+  return make_move_iterator(std::move(__i));
+}
+
+template <typename _Iterator>
+constexpr auto __move_assign_please(_Iterator __i)
+    -> enable_if_t<not is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, _Iterator> {
+  return __i;
+}
+
+template <typename _Integer>
+constexpr _Integer __intlog2_impl(_Integer __integer) {
+  auto __degree = _Integer{0};
+
+  while ((__integer >>= 1) > 0) {
+    ++__degree;
+  }
+
+  return __degree;
+}
+
+template <typename _Integer>
+constexpr _Integer __intlog2(_Integer __integer) {
+  static_assert(is_integral<_Integer>::value, "Must be an integral type");
+
+  return __integer > 0 ? __intlog2_impl(__integer)
+                       : throw domain_error("The binary logarithm is not defined on non-positive numbers");
+}
+
+template <typename _InputIterator, typename _OutputIterator>
+pair<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>
+__partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
+  if (__first == __last)
+    return {__result, 0};
+
+  auto __max                                                 = *__first;
+  typename iterator_traits<_InputIterator>::value_type __sum = *__first;
+  *__result                                                  = __sum;
+
+  while (++__first != __last) {
+    if (__max < *__first) {
+      __max = *__first;
+    }
+    __sum       = std::move(__sum) + *__first;
+    *++__result = __sum;
+  }
+  return {++__result, __max};
+}
+
+template <typename _Value, typename _Map, typename _Radix>
+struct __radix_sort_traits {
+  using image_type = decay_t<invoke_result_t<_Map, _Value> >;
+  static_assert(is_integral<image_type>::value, "");
+  static_assert(is_unsigned<image_type>::value, "");
+
+  using radix_type = decay_t<invoke_result_t<_Radix, image_type> >;
+  static_assert(is_integral<radix_type>::value, "");
+
+  constexpr static auto radix_value_range = numeric_limits<radix_type>::max() + 1;
+  constexpr static auto radix_size        = __intlog2<uint64_t>(radix_value_range);
+  constexpr static auto radix_count       = sizeof(image_type) * CHAR_BIT / radix_size;
+};
+
+template <typename _Value, typename _Map>
+struct __counting_sort_traits {
+  using image_type = decay_t<invoke_result_t<_Map, _Value> >;
+  static_assert(is_integral<image_type>::value, "");
+  static_assert(is_unsigned<image_type>::value, "");
+
+  constexpr static const auto value_range = numeric_limits<image_type>::max() + 1;
+  constexpr static auto radix_size        = __intlog2<uint64_t>(value_range);
+};
+
+template <typename _Radix>
+auto __nth_radix(size_t __radix_number, _Radix __radix) {
+  return [__radix_number, __radix = std::move(__radix)](auto __n) {
+    using value_type = decltype(__n);
+    static_assert(is_integral<value_type>::value, "");
+    static_assert(is_unsigned<value_type>::value, "");
+    using traits = __counting_sort_traits<value_type, _Radix>;
+
+    return __radix(static_cast<value_type>(__n >> traits::radix_size * __radix_number));
+  };
+}
+
+template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
+void __count(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
+  std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
+}
+
+template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
+void __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
+  using value_type = typename iterator_traits<_ForwardIterator>::value_type;
+  using traits     = __counting_sort_traits<value_type, _Map>;
+
+  __count(__first, __last, __map, __counters);
+
+  const auto __counters_end = __counters + traits::value_range;
+  partial_sum(__counters, __counters_end, __counters);
+}
+
+template <typename _ForwardIterator, typename _RandomAccessIterator1, typename _Map, typename _RandomAccessIterator2>
+void __dispose(_ForwardIterator __first,
+               _ForwardIterator __last,
+               _RandomAccessIterator1 __result,
+               _Map __map,
+               _RandomAccessIterator2 __counters) {
+  std::for_each(__first, __last, [&__result, &__counters, &__map](auto&& __preimage) {
+    auto __index      = __counters[__map(__preimage)]++;
+    __result[__index] = std::forward<decltype(__preimage)>(__preimage);
+  });
+}
+
+template <typename _BidirectionalIterator,
+          typename _RandomAccessIterator1,
+          typename _Map,
+          typename _RandomAccessIterator2>
+void dispose_backward(_BidirectionalIterator __first,
+                      _BidirectionalIterator __last,
+                      _RandomAccessIterator1 __result,
+                      _Map __map,
+                      _RandomAccessIterator2 __counters) {
+  std::for_each(make_reverse_iterator(__last),
+                make_reverse_iterator(__first),
+                [&__result, &__counters, &__map](auto&& __preimage) {
+                  auto __index      = --__counters[__map(__preimage)];
+                  __result[__index] = std::forward<decltype(__preimage)>(__preimage);
+                });
+}
+
+template <typename _ForwardIterator,
+          typename _Map,
+          typename _Radix,
+          typename _RandomAccessIterator1,
+          typename _RandomAccessIterator2,
+          size_t... _Radices>
+bool __collect_impl(
+    _ForwardIterator __first,
+    _ForwardIterator __last,
+    _Map __map,
+    _Radix __radix,
+    _RandomAccessIterator1 __counters,
+    _RandomAccessIterator2 __maximums,
+    index_sequence<_Radices...>) {
+  using value_type                   = typename iterator_traits<_ForwardIterator>::value_type;
+  constexpr auto __radix_value_range = __radix_sort_traits<value_type, _Map, _Radix>::radix_value_range;
+
+  auto __previous  = numeric_limits<invoke_result_t<_Map, value_type> >::min();
+  auto __is_sorted = true;
+  for_each(__first, __last, [&__counters, &__map, &__radix, &__previous, &__is_sorted](const auto& value) {
+    auto __current = __map(value);
+    __is_sorted &= (__current >= __previous);
+    __previous = __current;
+
+    EXPAND_VARIADIC(++__counters[_Radices][__nth_radix(_Radices, __radix)(__current)]);
+  });
+
+  EXPAND_VARIADIC(
+      __maximums[_Radices] =
+          __partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
+              .second);
+
+  return __is_sorted;
+}
+
+template <typename _ForwardIterator,
+          typename _Map,
+          typename _Radix,
+          typename _RandomAccessIterator1,
+          typename _RandomAccessIterator2>
+bool __collect(_ForwardIterator __first,
+               _ForwardIterator __last,
+               _Map __map,
+               _Radix __radix,
+               _RandomAccessIterator1 __counters,
+               _RandomAccessIterator2 __maximums) {
+  using value_type             = typename iterator_traits<_ForwardIterator>::value_type;
+  constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::radix_count;
+  return __collect_impl(__first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
+}
+
+template <typename _BidirectionalIterator,
+          typename _RandomAccessIterator1,
+          typename _Map,
+          typename _RandomAccessIterator2>
+void __dispose_backward(_BidirectionalIterator __first,
+                        _BidirectionalIterator __last,
+                        _RandomAccessIterator1 __result,
+                        _Map __map,
+                        _RandomAccessIterator2 __counters) {
+  for_each(
+      make_reverse_iterator(__last), make_reverse_iterator(__first), [&__result, &__counters, &__map](auto&& preimage) {
+        auto __index      = --__counters[__map(preimage)];
+        __result[__index] = std::forward<decltype(preimage)>(preimage);
+      });
+}
+
+template <typename _ForwardIterator, typename _RandomAccessIterator, typename _Map>
+_RandomAccessIterator
+__counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomAccessIterator __result, _Map __map) {
+  using value_type = typename iterator_traits<_ForwardIterator>::value_type;
+  using traits     = __counting_sort_traits<value_type, _Map>;
+
+  using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
+  difference_type __counters[traits::value_range + 1] = {0};
+
+  __collect(__first, __last, __map, next(std::begin(__counters)));
+  __dispose(__first, __last, __result, __map, std::begin(__counters));
+
+  return __result + __counters[traits::value_range];
+}
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+typename enable_if<
+    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count == 1,
+    void>::type
+__radix_sort_impl(_RandomAccessIterator1 __first,
+                  _RandomAccessIterator1 __last,
+                  _RandomAccessIterator2 buffer,
+                  _Map __map,
+                  _Radix __radix) {
+  auto __buffer_end = __counting_sort_impl(
+      __move_assign_please(__first), __move_assign_please(__last), buffer, [&__map, &__radix](const auto& value) {
+        return __radix(__map(value));
+      });
+
+  std::copy(__move_assign_please(buffer), __move_assign_please(__buffer_end), __first);
+}
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+typename enable_if<
+    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count % 2 ==
+        0,
+    void>::type
+__radix_sort_impl(_RandomAccessIterator1 __first,
+                  _RandomAccessIterator1 __last,
+                  _RandomAccessIterator2 __buffer_begin,
+                  _Map __map,
+                  _Radix __radix) {
+  using value_type = typename iterator_traits<_RandomAccessIterator1>::value_type;
+  using traits     = __radix_sort_traits<value_type, _Map, _Radix>;
+
+  using difference_type = typename iterator_traits<_RandomAccessIterator1>::difference_type;
+  difference_type __counters[traits::radix_count][traits::radix_value_range] = {{0}};
+  difference_type __maximums[traits::radix_count]                            = {0};
+  const auto __is_sorted = __collect(__first, __last, __map, __radix, __counters, __maximums);
+  if (not __is_sorted) {
+    const auto __range_size = distance(__first, __last);
+    auto __buffer_end       = __buffer_begin + __range_size;
+    for (size_t __radix_number = 0; __radix_number < traits::radix_count; __radix_number += 2) {
+      const auto __n0th_is_single = __maximums[__radix_number] == __range_size;
+      const auto __n1th_is_single = __maximums[__radix_number + 1] == __range_size;
+
+      if (__n0th_is_single && __n1th_is_single) {
+        continue;
+      }
+
+      if (__n0th_is_single) {
+        copy(__move_assign_please(__first), __move_assign_please(__last), __buffer_begin);
+      } else {
+        auto __n0th = [__radix_number, &__map, &__radix](const auto& __v) {
+          return __nth_radix(__radix_number, __radix)(__map(__v));
+        };
+        __dispose_backward(
+            __move_assign_please(__first),
+            __move_assign_please(__last),
+            __buffer_begin,
+            __n0th,
+            __counters[__radix_number]);
+      }
+
+      if (__n1th_is_single) {
+        copy(__move_assign_please(__buffer_begin), __move_assign_please(__buffer_end), __first);
+      } else {
+        auto __n1th = [__radix_number, &__map, &__radix](const auto& __v) {
+          return __nth_radix(__radix_number + 1, __radix)(__map(__v));
+        };
+        __dispose_backward(
+            __move_assign_please(__buffer_begin),
+            __move_assign_please(__buffer_end),
+            __first,
+            __n1th,
+            __counters[__radix_number + 1]);
+      }
+    }
+  }
+}
+
+constexpr auto __to_unsigned(bool __b) { return __b; }
+
+template <typename _Ip>
+constexpr auto __to_unsigned(_Ip __n) {
+  constexpr const auto __min_value = numeric_limits<_Ip>::min();
+  return static_cast<make_unsigned_t<_Ip> >(__n ^ __min_value);
+}
+
+struct __identity_fn {
+  template <typename _Tp>
+  constexpr decltype(auto) operator()(_Tp&& __value) const {
+    return std::forward<_Tp>(__value);
+  }
+};
+
+struct __low_byte_fn {
+  template <typename _Ip>
+  constexpr uint8_t operator()(_Ip __integer) const {
+    static_assert(is_integral<_Ip>::value, "");
+    static_assert(is_unsigned<_Ip>::value, "");
+
+    return static_cast<uint8_t>(__integer & 0xff);
+  }
+};
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+void __radix_sort(_RandomAccessIterator1 __first,
+                  _RandomAccessIterator1 __last,
+                  _RandomAccessIterator2 buffer,
+                  _Map __map,
+                  _Radix __radix) {
+  auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return __to_unsigned(__map(x)); };
+  __radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
+}
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+void __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
+  __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
+}
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+bool __radix_sort(
+    _RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer, _BoolConstant<true>) {
+  __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
+  return true;
+}
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<false>) {
+  return false;
+}
+
+#  undef EXPAND_VARIADIC
+
+#else // _LIBCPP_STD_VER > 14
+
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, bool _B>
+bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_B>) {
+  return false;
+}
+
+#endif // _LIBCPP_STD_VER > 14
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RADIX_SORT_H
diff --git a/libcxx/include/__algorithm/stable_sort.h b/libcxx/include/__algorithm/stable_sort.h
index 726e7e16b3564a..f8624726a4e323 100644
--- a/libcxx/include/__algorithm/stable_sort.h
+++ b/libcxx/include/__algorithm/stable_sort.h
@@ -13,6 +13,7 @@
 #include <__algorithm/comp_ref_type.h>
 #include <__algorithm/inplace_merge.h>
 #include <__algorithm/iterator_operations.h>
+#include <__algorithm/radix_sort.h>
 #include <__algorithm/sort.h>
 #include <__config>
 #include <__debug_utils/strict_weak_ordering_check.h>
@@ -20,6 +21,9 @@
 #include <__memory/destruct_n.h>
 #include <__memory/temporary_buffer.h>
 #include <__memory/unique_ptr.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 <__utility/move.h>
 #include <__utility/pair.h>
@@ -133,20 +137,24 @@ _LIBCPP_HIDE_FROM_ABI void __merge_move_assign(
     *__result = _Ops::__iter_move(__first2);
 }
 
-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(
+    _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_move(_RandomAccessIterator __first1,
-                        _RandomAccessIterator __last1,
-                        _Compare __comp,
-                        typename iterator_traits<_RandomAccessIterator>::difference_type __len,
-                        typename iterator_traits<_RandomAccessIterator>::value_type* __first2) {
+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) {
   using _Ops = _IterOps<_AlgPolicy>;
 
   typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
@@ -179,8 +187,8 @@ void __stable_sort_move(_RandomAccessIterator __first1,
   }
   typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
   _RandomAccessIterator __m                                             = __first1 + __l2;
-  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::__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::__merge_move_construct<_AlgPolicy, _Compare>(__first1, __m, __m, __last1, __first2, __comp);
 }
 
@@ -189,13 +197,35 @@ struct __stable_sort_switch {
   static const unsigned value = 128 * is_trivially_copy_assignable<_Tp>::value;
 };
 
-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 _Tp, class = void>
+struct __radix_sort_min_switch {
+  static const unsigned value = (1 << 10);
+};
+
+template <class _Int8>
+struct __radix_sort_min_switch<_Int8, enable_if_t<is_integral_v<_Int8> && sizeof(_Int8) == 1>> {
+  static const unsigned value = (1 << 8);
+};
+
+template <class _Tp, class = void>
+struct __radix_sort_max_switch {
+  static const unsigned value = (1 << 16);
+};
+
+template <class _Int64>
+struct __radix_sort_max_switch<_Int64, enable_if_t<is_integral_v<_Int64> && sizeof(_Int64) == 8>> {
+  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) {
   typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
   typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
   switch (__len) {
@@ -211,14 +241,20 @@ void __stable_sort(_RandomAccessIterator __first,
     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)) {
+      return;
+    }
+  }
   typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
   _RandomAccessIterator __m                                             = __first + __l2;
   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);
+    std::__stable_sort_move<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff, __rs);
     __d.__set(__l2, (value_type*)nullptr);
-    std::__stable_sort_move<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
+    std::__stable_sort_move<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff + __l2, __rs);
     __d.__set(__len, (value_type*)nullptr);
     std::__merge_move_assign<_AlgPolicy, _Compare>(
         __buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
@@ -229,14 +265,17 @@ void __stable_sort(_RandomAccessIterator __first,
     //                                  __first, __comp);
     return;
   }
-  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::__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::__inplace_merge<_AlgPolicy>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
 }
 
-template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
-inline _LIBCPP_HIDE_FROM_ABI void
-__stable_sort_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
+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>{}) {
   using value_type      = typename iterator_traits<_RandomAccessIterator>::value_type;
   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
 
@@ -251,19 +290,27 @@ __stable_sort_impl(_RandomAccessIterator __first, _RandomAccessIterator __last,
     __h.reset(__buf.first);
   }
 
-  std::__stable_sort<_AlgPolicy, __comp_ref_type<_Compare> >(__first, __last, __comp, __len, __buf.first, __buf.second);
+  std::__stable_sort<_AlgPolicy, __comp_ref_type<_Compare> >(
+      __first, __last, __comp, __len, __buf.first, __buf.second, __rs);
   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);
+  std::__stable_sort_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, _BoolConstant<false>());
 }
 
 template <class _RandomAccessIterator>
 inline _LIBCPP_HIDE_FROM_ABI void stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) {
-  std::stable_sort(__first, __last, __less<>());
+  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 > ());
 }
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 13d0dce34d97e3..b6125add8acf2d 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -720,6 +720,7 @@ module std_private_algorithm_pstl                                        [system
   export *
 }
 module std_private_algorithm_push_heap                                   [system] { header "__algorithm/push_heap.h" }
+module std_private_algorithm_radix_sort                                  [system] { header "__algorithm/radix_sort.h" }
 module std_private_algorithm_ranges_adjacent_find                        [system] { header "__algorithm/ranges_adjacent_find.h" }
 module std_private_algorithm_ranges_all_of                               [system] { header "__algorithm/ranges_all_of.h" }
 module std_private_algorithm_ranges_any_of                               [system] { header "__algorithm/ranges_any_of.h" }

>From edaab46ed6286d4170daaad78af6bfad8791b6c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sat, 17 Aug 2024 17:21:06 +0300
Subject: [PATCH 02/33] ranged

---
 libcxx/include/__algorithm/ranges_stable_sort.h | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/ranges_stable_sort.h b/libcxx/include/__algorithm/ranges_stable_sort.h
index 9c7df80ae98722..96d84b208687fc 100644
--- a/libcxx/include/__algorithm/ranges_stable_sort.h
+++ b/libcxx/include/__algorithm/ranges_stable_sort.h
@@ -24,6 +24,8 @@
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
 #include <__ranges/dangling.h>
+#include <__type_traits/is_integral.h>
+#include <__type_traits/is_same.h>
 #include <__utility/forward.h>
 #include <__utility/move.h>
 
@@ -45,7 +47,18 @@ struct __stable_sort {
     auto __last_iter = ranges::next(__first, __last);
 
     auto&& __projected_comp = std::__make_projected(__comp, __proj);
-    std::__stable_sort_impl<_RangeAlgPolicy>(std::move(__first), __last_iter, __projected_comp);
+    constexpr auto __default_comp        = is_same_v<_Comp, ranges::less>;
+    constexpr auto __default_proj        = is_same_v<_Proj, identity>;
+    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 > {});
 
     return __last_iter;
   }

>From ec4f083b90171e86d091f30bd67140efe6dd8927 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 01:17:10 +0300
Subject: [PATCH 03/33] distance

---
 libcxx/include/__algorithm/radix_sort.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 5e14dec9df0918..8b3dce525fbcf2 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -13,6 +13,7 @@
 #include <__algorithm/copy.h>
 #include <__algorithm/for_each.h>
 #include <__config>
+#include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/move_iterator.h>
 #include <__iterator/next.h>
@@ -299,7 +300,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   difference_type __maximums[traits::radix_count]                            = {0};
   const auto __is_sorted = __collect(__first, __last, __map, __radix, __counters, __maximums);
   if (not __is_sorted) {
-    const auto __range_size = distance(__first, __last);
+    const auto __range_size = std::distance(__first, __last);
     auto __buffer_end       = __buffer_begin + __range_size;
     for (size_t __radix_number = 0; __radix_number < traits::radix_count; __radix_number += 2) {
       const auto __n0th_is_single = __maximums[__radix_number] == __range_size;

>From 23fa74b05abba227e3f6737fbf7278071830f063 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 01:17:24 +0300
Subject: [PATCH 04/33] c++03

---
 libcxx/include/__algorithm/stable_sort.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__algorithm/stable_sort.h b/libcxx/include/__algorithm/stable_sort.h
index f8624726a4e323..8e6c02b55ea630 100644
--- a/libcxx/include/__algorithm/stable_sort.h
+++ b/libcxx/include/__algorithm/stable_sort.h
@@ -203,7 +203,7 @@ struct __radix_sort_min_switch {
 };
 
 template <class _Int8>
-struct __radix_sort_min_switch<_Int8, enable_if_t<is_integral_v<_Int8> && sizeof(_Int8) == 1>> {
+struct __radix_sort_min_switch<_Int8, enable_if_t<is_integral<_Int8>::value && sizeof(_Int8) == 1> > {
   static const unsigned value = (1 << 8);
 };
 
@@ -213,7 +213,7 @@ struct __radix_sort_max_switch {
 };
 
 template <class _Int64>
-struct __radix_sort_max_switch<_Int64, enable_if_t<is_integral_v<_Int64> && sizeof(_Int64) == 8>> {
+struct __radix_sort_max_switch<_Int64, enable_if_t<is_integral<_Int64>::value && sizeof(_Int64) == 8> > {
   static const unsigned value = (1 << 15);
 };
 
@@ -275,7 +275,7 @@ inline _LIBCPP_HIDE_FROM_ABI void __stable_sort_impl(
     _RandomAccessIterator __first,
     _RandomAccessIterator __last,
     _Compare& __comp,
-    _BoolConstant<_EnableRadixSort> __rs = _BoolConstant<false>{}) {
+    _BoolConstant<_EnableRadixSort> __rs = _BoolConstant<false>()) {
   using value_type      = typename iterator_traits<_RandomAccessIterator>::value_type;
   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
 

>From 9eb272bf836605a5b0c8baca2fa185b9f3e73691 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 08:24:41 +0300
Subject: [PATCH 05/33] more-c++03

---
 libcxx/include/__algorithm/stable_sort.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/stable_sort.h b/libcxx/include/__algorithm/stable_sort.h
index 8e6c02b55ea630..ea817abd5058dd 100644
--- a/libcxx/include/__algorithm/stable_sort.h
+++ b/libcxx/include/__algorithm/stable_sort.h
@@ -21,6 +21,7 @@
 #include <__memory/destruct_n.h>
 #include <__memory/temporary_buffer.h>
 #include <__memory/unique_ptr.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>
@@ -203,7 +204,7 @@ struct __radix_sort_min_switch {
 };
 
 template <class _Int8>
-struct __radix_sort_min_switch<_Int8, enable_if_t<is_integral<_Int8>::value && sizeof(_Int8) == 1> > {
+struct __radix_sort_min_switch<_Int8, __enable_if_t<is_integral<_Int8>::value && sizeof(_Int8) == 1> > {
   static const unsigned value = (1 << 8);
 };
 
@@ -213,7 +214,7 @@ struct __radix_sort_max_switch {
 };
 
 template <class _Int64>
-struct __radix_sort_max_switch<_Int64, enable_if_t<is_integral<_Int64>::value && sizeof(_Int64) == 8> > {
+struct __radix_sort_max_switch<_Int64, __enable_if_t<is_integral<_Int64>::value && sizeof(_Int64) == 8> > {
   static const unsigned value = (1 << 15);
 };
 

>From 671ac8f21639a197b6040b87db5ad7db4e856ff8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 08:24:51 +0300
Subject: [PATCH 06/33] support -fno-exceptions

---
 libcxx/include/__algorithm/radix_sort.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 8b3dce525fbcf2..3eb602d75a09a2 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -78,8 +78,9 @@ template <typename _Integer>
 constexpr _Integer __intlog2(_Integer __integer) {
   static_assert(is_integral<_Integer>::value, "Must be an integral type");
 
-  return __integer > 0 ? __intlog2_impl(__integer)
-                       : throw domain_error("The binary logarithm is not defined on non-positive numbers");
+  return __integer > 0
+           ? __intlog2_impl(__integer)
+           : (__throw_domain_error("The binary logarithm is not defined on non-positive numbers"), _Integer{0});
 }
 
 template <typename _InputIterator, typename _OutputIterator>

>From a39038e8e8e34dfbfa4667ff72421dd503e15616 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 16:01:47 +0300
Subject: [PATCH 07/33] naming

---
 libcxx/include/__algorithm/radix_sort.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 3eb602d75a09a2..c4112c6edf02ad 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -398,8 +398,9 @@ bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessI
 
 #else // _LIBCPP_STD_VER > 14
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, bool _B>
-bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_B>) {
+template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, bool _EnableRadixSort>
+bool __radix_sort(
+    _RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_EnableRadixSort>) {
   return false;
 }
 

>From 7a97d0dc8e832e6f37920ae6302ed632dc1e47a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Sun, 18 Aug 2024 22:48:44 +0300
Subject: [PATCH 08/33] no-exceptions

---
 libcxx/include/__algorithm/radix_sort.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index c4112c6edf02ad..c4f90e6a6888cd 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -33,7 +33,6 @@
 #include <cstdint>
 #include <initializer_list>
 #include <limits>
-#include <stdexcept>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -78,9 +77,11 @@ template <typename _Integer>
 constexpr _Integer __intlog2(_Integer __integer) {
   static_assert(is_integral<_Integer>::value, "Must be an integral type");
 
-  return __integer > 0
-           ? __intlog2_impl(__integer)
-           : (__throw_domain_error("The binary logarithm is not defined on non-positive numbers"), _Integer{0});
+  if (__integer > 0) {
+    return __intlog2_impl(__integer);
+  }
+
+  return 0;
 }
 
 template <typename _InputIterator, typename _OutputIterator>

>From bfd0c16ea6ded0769d873bc3ee61058a3f83fc83 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 12:38:42 +0300
Subject: [PATCH 09/33] libcppver

---
 libcxx/include/__algorithm/radix_sort.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index c4f90e6a6888cd..a67a32a6805aaf 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -43,7 +43,7 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-#if _LIBCPP_STD_VER > 14
+#if _LIBCPP_STD_VER >= 14
 
 inline void __variadic_expansion_dummy(initializer_list<int>) {}
 

>From 49e28834f4abf901392a242927a368ca03c920cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 12:43:28 +0300
Subject: [PATCH 10/33] hide-from-abi

---
 libcxx/include/__algorithm/radix_sort.h | 120 +++++++++++++-----------
 1 file changed, 65 insertions(+), 55 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index a67a32a6805aaf..26c49f232c05d8 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -45,25 +45,25 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 14
 
-inline void __variadic_expansion_dummy(initializer_list<int>) {}
+inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<int>) {}
 
 #  define EXPAND_VARIADIC(expression) __variadic_expansion_dummy({(expression, 0)...})
 
 template <typename _Iterator>
-constexpr auto __move_assign_please(_Iterator __i)
+_LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
     -> enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value,
                    move_iterator<_Iterator> > {
   return make_move_iterator(std::move(__i));
 }
 
 template <typename _Iterator>
-constexpr auto __move_assign_please(_Iterator __i)
+_LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
     -> enable_if_t<not is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, _Iterator> {
   return __i;
 }
 
 template <typename _Integer>
-constexpr _Integer __intlog2_impl(_Integer __integer) {
+_LIBCPP_HIDE_FROM_ABI constexpr _Integer __intlog2_impl(_Integer __integer) {
   auto __degree = _Integer{0};
 
   while ((__integer >>= 1) > 0) {
@@ -74,7 +74,7 @@ constexpr _Integer __intlog2_impl(_Integer __integer) {
 }
 
 template <typename _Integer>
-constexpr _Integer __intlog2(_Integer __integer) {
+_LIBCPP_HIDE_FROM_ABI constexpr _Integer __intlog2(_Integer __integer) {
   static_assert(is_integral<_Integer>::value, "Must be an integral type");
 
   if (__integer > 0) {
@@ -85,7 +85,7 @@ constexpr _Integer __intlog2(_Integer __integer) {
 }
 
 template <typename _InputIterator, typename _OutputIterator>
-pair<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>
+_LIBCPP_HIDE_FROM_ABI pair<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>
 __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
   if (__first == __last)
     return {__result, 0};
@@ -129,7 +129,7 @@ struct __counting_sort_traits {
 };
 
 template <typename _Radix>
-auto __nth_radix(size_t __radix_number, _Radix __radix) {
+_LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
   return [__radix_number, __radix = std::move(__radix)](auto __n) {
     using value_type = decltype(__n);
     static_assert(is_integral<value_type>::value, "");
@@ -141,12 +141,14 @@ auto __nth_radix(size_t __radix_number, _Radix __radix) {
 }
 
 template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
-void __count(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
+_LIBCPP_HIDE_FROM_ABI void
+__count(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
   std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
 }
 
 template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
-void __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
+_LIBCPP_HIDE_FROM_ABI void
+__collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
@@ -157,11 +159,12 @@ void __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _R
 }
 
 template <typename _ForwardIterator, typename _RandomAccessIterator1, typename _Map, typename _RandomAccessIterator2>
-void __dispose(_ForwardIterator __first,
-               _ForwardIterator __last,
-               _RandomAccessIterator1 __result,
-               _Map __map,
-               _RandomAccessIterator2 __counters) {
+_LIBCPP_HIDE_FROM_ABI void
+__dispose(_ForwardIterator __first,
+          _ForwardIterator __last,
+          _RandomAccessIterator1 __result,
+          _Map __map,
+          _RandomAccessIterator2 __counters) {
   std::for_each(__first, __last, [&__result, &__counters, &__map](auto&& __preimage) {
     auto __index      = __counters[__map(__preimage)]++;
     __result[__index] = std::forward<decltype(__preimage)>(__preimage);
@@ -172,13 +175,14 @@ template <typename _BidirectionalIterator,
           typename _RandomAccessIterator1,
           typename _Map,
           typename _RandomAccessIterator2>
-void dispose_backward(_BidirectionalIterator __first,
-                      _BidirectionalIterator __last,
-                      _RandomAccessIterator1 __result,
-                      _Map __map,
-                      _RandomAccessIterator2 __counters) {
-  std::for_each(make_reverse_iterator(__last),
-                make_reverse_iterator(__first),
+_LIBCPP_HIDE_FROM_ABI void dispose_backward(
+    _BidirectionalIterator __first,
+    _BidirectionalIterator __last,
+    _RandomAccessIterator1 __result,
+    _Map __map,
+    _RandomAccessIterator2 __counters) {
+  std::for_each(std::make_reverse_iterator(__last),
+                std::make_reverse_iterator(__first),
                 [&__result, &__counters, &__map](auto&& __preimage) {
                   auto __index      = --__counters[__map(__preimage)];
                   __result[__index] = std::forward<decltype(__preimage)>(__preimage);
@@ -191,7 +195,7 @@ template <typename _ForwardIterator,
           typename _RandomAccessIterator1,
           typename _RandomAccessIterator2,
           size_t... _Radices>
-bool __collect_impl(
+_LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     _ForwardIterator __first,
     _ForwardIterator __last,
     _Map __map,
@@ -225,12 +229,13 @@ template <typename _ForwardIterator,
           typename _Radix,
           typename _RandomAccessIterator1,
           typename _RandomAccessIterator2>
-bool __collect(_ForwardIterator __first,
-               _ForwardIterator __last,
-               _Map __map,
-               _Radix __radix,
-               _RandomAccessIterator1 __counters,
-               _RandomAccessIterator2 __maximums) {
+_LIBCPP_HIDE_FROM_ABI bool
+__collect(_ForwardIterator __first,
+          _ForwardIterator __last,
+          _Map __map,
+          _Radix __radix,
+          _RandomAccessIterator1 __counters,
+          _RandomAccessIterator2 __maximums) {
   using value_type             = typename iterator_traits<_ForwardIterator>::value_type;
   constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::radix_count;
   return __collect_impl(__first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
@@ -240,20 +245,22 @@ template <typename _BidirectionalIterator,
           typename _RandomAccessIterator1,
           typename _Map,
           typename _RandomAccessIterator2>
-void __dispose_backward(_BidirectionalIterator __first,
-                        _BidirectionalIterator __last,
-                        _RandomAccessIterator1 __result,
-                        _Map __map,
-                        _RandomAccessIterator2 __counters) {
-  for_each(
-      make_reverse_iterator(__last), make_reverse_iterator(__first), [&__result, &__counters, &__map](auto&& preimage) {
-        auto __index      = --__counters[__map(preimage)];
-        __result[__index] = std::forward<decltype(preimage)>(preimage);
-      });
+_LIBCPP_HIDE_FROM_ABI void __dispose_backward(
+    _BidirectionalIterator __first,
+    _BidirectionalIterator __last,
+    _RandomAccessIterator1 __result,
+    _Map __map,
+    _RandomAccessIterator2 __counters) {
+  std::for_each(std::make_reverse_iterator(__last),
+                std::make_reverse_iterator(__first),
+                [&__result, &__counters, &__map](auto&& preimage) {
+                  auto __index      = --__counters[__map(preimage)];
+                  __result[__index] = std::forward<decltype(preimage)>(preimage);
+                });
 }
 
 template <typename _ForwardIterator, typename _RandomAccessIterator, typename _Map>
-_RandomAccessIterator
+_LIBCPP_HIDE_FROM_ABI _RandomAccessIterator
 __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomAccessIterator __result, _Map __map) {
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
   using traits     = __counting_sort_traits<value_type, _Map>;
@@ -268,7 +275,7 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
-typename enable_if<
+_LIBCPP_HIDE_FROM_ABI typename enable_if<
     __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count == 1,
     void>::type
 __radix_sort_impl(_RandomAccessIterator1 __first,
@@ -285,7 +292,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
-typename enable_if<
+_LIBCPP_HIDE_FROM_ABI typename enable_if<
     __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count % 2 ==
         0,
     void>::type
@@ -343,24 +350,24 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   }
 }
 
-constexpr auto __to_unsigned(bool __b) { return __b; }
+_LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(bool __b) { return __b; }
 
 template <typename _Ip>
-constexpr auto __to_unsigned(_Ip __n) {
+_LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(_Ip __n) {
   constexpr const auto __min_value = numeric_limits<_Ip>::min();
   return static_cast<make_unsigned_t<_Ip> >(__n ^ __min_value);
 }
 
 struct __identity_fn {
   template <typename _Tp>
-  constexpr decltype(auto) operator()(_Tp&& __value) const {
+  _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Tp&& __value) const {
     return std::forward<_Tp>(__value);
   }
 };
 
 struct __low_byte_fn {
   template <typename _Ip>
-  constexpr uint8_t operator()(_Ip __integer) const {
+  _LIBCPP_HIDE_FROM_ABI constexpr uint8_t operator()(_Ip __integer) const {
     static_assert(is_integral<_Ip>::value, "");
     static_assert(is_unsigned<_Ip>::value, "");
 
@@ -369,29 +376,32 @@ struct __low_byte_fn {
 };
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
-void __radix_sort(_RandomAccessIterator1 __first,
-                  _RandomAccessIterator1 __last,
-                  _RandomAccessIterator2 buffer,
-                  _Map __map,
-                  _Radix __radix) {
+_LIBCPP_HIDE_FROM_ABI void
+__radix_sort(_RandomAccessIterator1 __first,
+             _RandomAccessIterator1 __last,
+             _RandomAccessIterator2 buffer,
+             _Map __map,
+             _Radix __radix) {
   auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return __to_unsigned(__map(x)); };
   __radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
-void __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
+_LIBCPP_HIDE_FROM_ABI void
+__radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
   __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
-bool __radix_sort(
+_LIBCPP_HIDE_FROM_ABI bool __radix_sort(
     _RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer, _BoolConstant<true>) {
   __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
   return true;
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
-bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<false>) {
+_LIBCPP_HIDE_FROM_ABI bool
+__radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<false>) {
   return false;
 }
 
@@ -400,8 +410,8 @@ bool __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessI
 #else // _LIBCPP_STD_VER > 14
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, bool _EnableRadixSort>
-bool __radix_sort(
-    _RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_EnableRadixSort>) {
+_LIBCPP_HIDE_FROM_ABI bool
+__radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_EnableRadixSort>) {
   return false;
 }
 

>From 390cf70a4f39fc8d608471655e3279752f79dcd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 12:44:22 +0300
Subject: [PATCH 11/33] adl

---
 libcxx/include/__algorithm/radix_sort.h | 68 +++++++++++++------------
 1 file changed, 35 insertions(+), 33 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 26c49f232c05d8..4f41a302d226ee 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -47,13 +47,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<int>) {}
 
-#  define EXPAND_VARIADIC(expression) __variadic_expansion_dummy({(expression, 0)...})
+#  define EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
 
 template <typename _Iterator>
 _LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
     -> enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value,
                    move_iterator<_Iterator> > {
-  return make_move_iterator(std::move(__i));
+  return std::make_move_iterator(std::move(__i));
 }
 
 template <typename _Iterator>
@@ -78,7 +78,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Integer __intlog2(_Integer __integer) {
   static_assert(is_integral<_Integer>::value, "Must be an integral type");
 
   if (__integer > 0) {
-    return __intlog2_impl(__integer);
+    return std::__intlog2_impl(__integer);
   }
 
   return 0;
@@ -114,7 +114,7 @@ struct __radix_sort_traits {
   static_assert(is_integral<radix_type>::value, "");
 
   constexpr static auto radix_value_range = numeric_limits<radix_type>::max() + 1;
-  constexpr static auto radix_size        = __intlog2<uint64_t>(radix_value_range);
+  constexpr static auto radix_size        = std::__intlog2<uint64_t>(radix_value_range);
   constexpr static auto radix_count       = sizeof(image_type) * CHAR_BIT / radix_size;
 };
 
@@ -125,7 +125,7 @@ struct __counting_sort_traits {
   static_assert(is_unsigned<image_type>::value, "");
 
   constexpr static const auto value_range = numeric_limits<image_type>::max() + 1;
-  constexpr static auto radix_size        = __intlog2<uint64_t>(value_range);
+  constexpr static auto radix_size        = std::__intlog2<uint64_t>(value_range);
 };
 
 template <typename _Radix>
@@ -152,10 +152,10 @@ __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _Random
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
-  __count(__first, __last, __map, __counters);
+  std::__count(__first, __last, __map, __counters);
 
   const auto __counters_end = __counters + traits::value_range;
-  partial_sum(__counters, __counters_end, __counters);
+  std::partial_sum(__counters, __counters_end, __counters);
 }
 
 template <typename _ForwardIterator, typename _RandomAccessIterator1, typename _Map, typename _RandomAccessIterator2>
@@ -208,17 +208,17 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
 
   auto __previous  = numeric_limits<invoke_result_t<_Map, value_type> >::min();
   auto __is_sorted = true;
-  for_each(__first, __last, [&__counters, &__map, &__radix, &__previous, &__is_sorted](const auto& value) {
+  std::for_each(__first, __last, [&__counters, &__map, &__radix, &__previous, &__is_sorted](const auto& value) {
     auto __current = __map(value);
     __is_sorted &= (__current >= __previous);
     __previous = __current;
 
-    EXPAND_VARIADIC(++__counters[_Radices][__nth_radix(_Radices, __radix)(__current)]);
+    EXPAND_VARIADIC(++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)]);
   });
 
   EXPAND_VARIADIC(
       __maximums[_Radices] =
-          __partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
+          std::__partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
               .second);
 
   return __is_sorted;
@@ -238,7 +238,8 @@ __collect(_ForwardIterator __first,
           _RandomAccessIterator2 __maximums) {
   using value_type             = typename iterator_traits<_ForwardIterator>::value_type;
   constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::radix_count;
-  return __collect_impl(__first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
+  return std::__collect_impl(
+      __first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
 }
 
 template <typename _BidirectionalIterator,
@@ -268,8 +269,8 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
   difference_type __counters[traits::value_range + 1] = {0};
 
-  __collect(__first, __last, __map, next(std::begin(__counters)));
-  __dispose(__first, __last, __result, __map, std::begin(__counters));
+  std::__collect(__first, __last, __map, std::next(std::begin(__counters)));
+  std::__dispose(__first, __last, __result, __map, std::begin(__counters));
 
   return __result + __counters[traits::value_range];
 }
@@ -283,12 +284,13 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
                   _RandomAccessIterator2 buffer,
                   _Map __map,
                   _Radix __radix) {
-  auto __buffer_end = __counting_sort_impl(
-      __move_assign_please(__first), __move_assign_please(__last), buffer, [&__map, &__radix](const auto& value) {
-        return __radix(__map(value));
-      });
+  auto __buffer_end = std::__counting_sort_impl(
+      std::__move_assign_please(__first),
+      std::__move_assign_please(__last),
+      buffer,
+      [&__map, &__radix](const auto& value) { return __radix(__map(value)); });
 
-  std::copy(__move_assign_please(buffer), __move_assign_please(__buffer_end), __first);
+  std::copy(std::__move_assign_please(buffer), std::__move_assign_please(__buffer_end), __first);
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
@@ -307,7 +309,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   using difference_type = typename iterator_traits<_RandomAccessIterator1>::difference_type;
   difference_type __counters[traits::radix_count][traits::radix_value_range] = {{0}};
   difference_type __maximums[traits::radix_count]                            = {0};
-  const auto __is_sorted = __collect(__first, __last, __map, __radix, __counters, __maximums);
+  const auto __is_sorted = std::__collect(__first, __last, __map, __radix, __counters, __maximums);
   if (not __is_sorted) {
     const auto __range_size = std::distance(__first, __last);
     auto __buffer_end       = __buffer_begin + __range_size;
@@ -320,28 +322,28 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
       }
 
       if (__n0th_is_single) {
-        copy(__move_assign_please(__first), __move_assign_please(__last), __buffer_begin);
+        std::copy(std::__move_assign_please(__first), std::__move_assign_please(__last), __buffer_begin);
       } else {
         auto __n0th = [__radix_number, &__map, &__radix](const auto& __v) {
-          return __nth_radix(__radix_number, __radix)(__map(__v));
+          return std::__nth_radix(__radix_number, __radix)(__map(__v));
         };
-        __dispose_backward(
-            __move_assign_please(__first),
-            __move_assign_please(__last),
+        std::__dispose_backward(
+            std::__move_assign_please(__first),
+            std::__move_assign_please(__last),
             __buffer_begin,
             __n0th,
             __counters[__radix_number]);
       }
 
       if (__n1th_is_single) {
-        copy(__move_assign_please(__buffer_begin), __move_assign_please(__buffer_end), __first);
+        std::copy(std::__move_assign_please(__buffer_begin), std::__move_assign_please(__buffer_end), __first);
       } else {
         auto __n1th = [__radix_number, &__map, &__radix](const auto& __v) {
-          return __nth_radix(__radix_number + 1, __radix)(__map(__v));
+          return std::__nth_radix(__radix_number + 1, __radix)(__map(__v));
         };
-        __dispose_backward(
-            __move_assign_please(__buffer_begin),
-            __move_assign_please(__buffer_end),
+        std::__dispose_backward(
+            std::__move_assign_please(__buffer_begin),
+            std::__move_assign_please(__buffer_end),
             __first,
             __n1th,
             __counters[__radix_number + 1]);
@@ -382,20 +384,20 @@ __radix_sort(_RandomAccessIterator1 __first,
              _RandomAccessIterator2 buffer,
              _Map __map,
              _Radix __radix) {
-  auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return __to_unsigned(__map(x)); };
-  __radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
+  auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return std::__to_unsigned(__map(x)); };
+  std::__radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void
 __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
-  __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
+  std::__radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI bool __radix_sort(
     _RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer, _BoolConstant<true>) {
-  __radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
+  std::__radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
   return true;
 }
 

>From 478f668c3bb121112e503586fc0664b8b1c9ff43 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 13:27:25 +0300
Subject: [PATCH 12/33] rev-iter

---
 libcxx/include/__algorithm/radix_sort.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 4f41a302d226ee..73d03c9d43bdda 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -17,6 +17,7 @@
 #include <__iterator/iterator_traits.h>
 #include <__iterator/move_iterator.h>
 #include <__iterator/next.h>
+#include <__iterator/reverse_iterator.h>
 #include <__numeric/partial_sum.h>
 #include <__type_traits/decay.h>
 #include <__type_traits/enable_if.h>

>From b847e3779f866848d1c9635a1b53d396b9d83c14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 22:36:18 +0300
Subject: [PATCH 13/33] use-countl-zero-for-log

---
 libcxx/include/__algorithm/radix_sort.h | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 73d03c9d43bdda..f6b4e775da7c31 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -12,6 +12,7 @@
 
 #include <__algorithm/copy.h>
 #include <__algorithm/for_each.h>
+#include <__bit/countl.h>
 #include <__config>
 #include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
@@ -63,26 +64,12 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
   return __i;
 }
 
-template <typename _Integer>
-_LIBCPP_HIDE_FROM_ABI constexpr _Integer __intlog2_impl(_Integer __integer) {
-  auto __degree = _Integer{0};
+template <typename _UnsignedInteger>
+_LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n) {
+  static_assert(is_integral<_UnsignedInteger>::value, "Must be an integral type");
+  static_assert(is_unsigned<_UnsignedInteger>::value, "Must be unsigned");
 
-  while ((__integer >>= 1) > 0) {
-    ++__degree;
-  }
-
-  return __degree;
-}
-
-template <typename _Integer>
-_LIBCPP_HIDE_FROM_ABI constexpr _Integer __intlog2(_Integer __integer) {
-  static_assert(is_integral<_Integer>::value, "Must be an integral type");
-
-  if (__integer > 0) {
-    return std::__intlog2_impl(__integer);
-  }
-
-  return 0;
+  return numeric_limits<_UnsignedInteger>::digits - 1 - std::__countl_zero(__n);
 }
 
 template <typename _InputIterator, typename _OutputIterator>

>From 1131d26366c8f4ee4f0ef72eb250e9d753e26b38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 22:37:15 +0300
Subject: [PATCH 14/33] uglify-macro

---
 libcxx/include/__algorithm/radix_sort.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index f6b4e775da7c31..51f29a6fedace0 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -49,7 +49,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<int>) {}
 
-#  define EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
+#  define _EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
 
 template <typename _Iterator>
 _LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
@@ -201,10 +201,10 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     __is_sorted &= (__current >= __previous);
     __previous = __current;
 
-    EXPAND_VARIADIC(++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)]);
+    _EXPAND_VARIADIC(++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)]);
   });
 
-  EXPAND_VARIADIC(
+  _EXPAND_VARIADIC(
       __maximums[_Radices] =
           std::__partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
               .second);
@@ -395,7 +395,7 @@ __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterat
   return false;
 }
 
-#  undef EXPAND_VARIADIC
+#  undef _EXPAND_VARIADIC
 
 #else // _LIBCPP_STD_VER > 14
 

>From c07ec8f3bc7ebbb94167800241b673002f26e5ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Tue, 20 Aug 2024 22:45:13 +0300
Subject: [PATCH 15/33] uglify-static-members

---
 libcxx/include/__algorithm/radix_sort.h | 33 +++++++++++++------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 51f29a6fedace0..e3de71a417ceab 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -101,9 +101,9 @@ struct __radix_sort_traits {
   using radix_type = decay_t<invoke_result_t<_Radix, image_type> >;
   static_assert(is_integral<radix_type>::value, "");
 
-  constexpr static auto radix_value_range = numeric_limits<radix_type>::max() + 1;
-  constexpr static auto radix_size        = std::__intlog2<uint64_t>(radix_value_range);
-  constexpr static auto radix_count       = sizeof(image_type) * CHAR_BIT / radix_size;
+  constexpr static auto __radix_value_range = numeric_limits<radix_type>::max() + 1;
+  constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__radix_value_range);
+  constexpr static auto __radix_count       = sizeof(image_type) * CHAR_BIT / __radix_size;
 };
 
 template <typename _Value, typename _Map>
@@ -112,8 +112,8 @@ struct __counting_sort_traits {
   static_assert(is_integral<image_type>::value, "");
   static_assert(is_unsigned<image_type>::value, "");
 
-  constexpr static const auto value_range = numeric_limits<image_type>::max() + 1;
-  constexpr static auto radix_size        = std::__intlog2<uint64_t>(value_range);
+  constexpr static const auto __value_range = numeric_limits<image_type>::max() + 1;
+  constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__value_range);
 };
 
 template <typename _Radix>
@@ -124,7 +124,7 @@ _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
     static_assert(is_unsigned<value_type>::value, "");
     using traits = __counting_sort_traits<value_type, _Radix>;
 
-    return __radix(static_cast<value_type>(__n >> traits::radix_size * __radix_number));
+    return __radix(static_cast<value_type>(__n >> traits::__radix_size * __radix_number));
   };
 }
 
@@ -142,7 +142,7 @@ __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _Random
 
   std::__count(__first, __last, __map, __counters);
 
-  const auto __counters_end = __counters + traits::value_range;
+  const auto __counters_end = __counters + traits::__value_range;
   std::partial_sum(__counters, __counters_end, __counters);
 }
 
@@ -192,7 +192,7 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     _RandomAccessIterator2 __maximums,
     index_sequence<_Radices...>) {
   using value_type                   = typename iterator_traits<_ForwardIterator>::value_type;
-  constexpr auto __radix_value_range = __radix_sort_traits<value_type, _Map, _Radix>::radix_value_range;
+  constexpr auto __radix_value_range = __radix_sort_traits<value_type, _Map, _Radix>::__radix_value_range;
 
   auto __previous  = numeric_limits<invoke_result_t<_Map, value_type> >::min();
   auto __is_sorted = true;
@@ -225,7 +225,7 @@ __collect(_ForwardIterator __first,
           _RandomAccessIterator1 __counters,
           _RandomAccessIterator2 __maximums) {
   using value_type             = typename iterator_traits<_ForwardIterator>::value_type;
-  constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::radix_count;
+  constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::__radix_count;
   return std::__collect_impl(
       __first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
 }
@@ -255,17 +255,17 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
   using traits     = __counting_sort_traits<value_type, _Map>;
 
   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
-  difference_type __counters[traits::value_range + 1] = {0};
+  difference_type __counters[traits::__value_range + 1] = {0};
 
   std::__collect(__first, __last, __map, std::next(std::begin(__counters)));
   std::__dispose(__first, __last, __result, __map, std::begin(__counters));
 
-  return __result + __counters[traits::value_range];
+  return __result + __counters[traits::__value_range];
 }
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
 _LIBCPP_HIDE_FROM_ABI typename enable_if<
-    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count == 1,
+    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count == 1,
     void>::type
 __radix_sort_impl(_RandomAccessIterator1 __first,
                   _RandomAccessIterator1 __last,
@@ -283,7 +283,8 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
 
 template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
 _LIBCPP_HIDE_FROM_ABI typename enable_if<
-    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::radix_count % 2 ==
+    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count %
+            2 ==
         0,
     void>::type
 __radix_sort_impl(_RandomAccessIterator1 __first,
@@ -295,13 +296,13 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   using traits     = __radix_sort_traits<value_type, _Map, _Radix>;
 
   using difference_type = typename iterator_traits<_RandomAccessIterator1>::difference_type;
-  difference_type __counters[traits::radix_count][traits::radix_value_range] = {{0}};
-  difference_type __maximums[traits::radix_count]                            = {0};
+  difference_type __counters[traits::__radix_count][traits::__radix_value_range] = {{0}};
+  difference_type __maximums[traits::__radix_count]                              = {0};
   const auto __is_sorted = std::__collect(__first, __last, __map, __radix, __counters, __maximums);
   if (not __is_sorted) {
     const auto __range_size = std::distance(__first, __last);
     auto __buffer_end       = __buffer_begin + __range_size;
-    for (size_t __radix_number = 0; __radix_number < traits::radix_count; __radix_number += 2) {
+    for (size_t __radix_number = 0; __radix_number < traits::__radix_count; __radix_number += 2) {
       const auto __n0th_is_single = __maximums[__radix_number] == __range_size;
       const auto __n1th_is_single = __maximums[__radix_number + 1] == __range_size;
 

>From 9f6129c8dae1f05848a4d68eab7dde9b70d12dae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 00:37:52 +0300
Subject: [PATCH 16/33] class-not-typename

---
 libcxx/include/__algorithm/radix_sort.h | 68 +++++++++++--------------
 1 file changed, 29 insertions(+), 39 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index e3de71a417ceab..9827e1aa4a53b9 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -51,20 +51,20 @@ inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<in
 
 #  define _EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
 
-template <typename _Iterator>
+template <class _Iterator>
 _LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
     -> enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value,
                    move_iterator<_Iterator> > {
   return std::make_move_iterator(std::move(__i));
 }
 
-template <typename _Iterator>
+template <class _Iterator>
 _LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
     -> enable_if_t<not is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, _Iterator> {
   return __i;
 }
 
-template <typename _UnsignedInteger>
+template <class _UnsignedInteger>
 _LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n) {
   static_assert(is_integral<_UnsignedInteger>::value, "Must be an integral type");
   static_assert(is_unsigned<_UnsignedInteger>::value, "Must be unsigned");
@@ -72,7 +72,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n)
   return numeric_limits<_UnsignedInteger>::digits - 1 - std::__countl_zero(__n);
 }
 
-template <typename _InputIterator, typename _OutputIterator>
+template <class _InputIterator, class _OutputIterator>
 _LIBCPP_HIDE_FROM_ABI pair<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>
 __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
   if (__first == __last)
@@ -92,7 +92,7 @@ __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator
   return {++__result, __max};
 }
 
-template <typename _Value, typename _Map, typename _Radix>
+template <class _Value, class _Map, class _Radix>
 struct __radix_sort_traits {
   using image_type = decay_t<invoke_result_t<_Map, _Value> >;
   static_assert(is_integral<image_type>::value, "");
@@ -106,7 +106,7 @@ struct __radix_sort_traits {
   constexpr static auto __radix_count       = sizeof(image_type) * CHAR_BIT / __radix_size;
 };
 
-template <typename _Value, typename _Map>
+template <class _Value, class _Map>
 struct __counting_sort_traits {
   using image_type = decay_t<invoke_result_t<_Map, _Value> >;
   static_assert(is_integral<image_type>::value, "");
@@ -116,7 +116,7 @@ struct __counting_sort_traits {
   constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__value_range);
 };
 
-template <typename _Radix>
+template <class _Radix>
 _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
   return [__radix_number, __radix = std::move(__radix)](auto __n) {
     using value_type = decltype(__n);
@@ -128,13 +128,13 @@ _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
   };
 }
 
-template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
+template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
 _LIBCPP_HIDE_FROM_ABI void
 __count(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
   std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
 }
 
-template <typename _ForwardIterator, typename _Map, typename _RandomAccessIterator>
+template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
 _LIBCPP_HIDE_FROM_ABI void
 __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
@@ -146,7 +146,7 @@ __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _Random
   std::partial_sum(__counters, __counters_end, __counters);
 }
 
-template <typename _ForwardIterator, typename _RandomAccessIterator1, typename _Map, typename _RandomAccessIterator2>
+template <class _ForwardIterator, class _RandomAccessIterator1, class _Map, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void
 __dispose(_ForwardIterator __first,
           _ForwardIterator __last,
@@ -159,10 +159,7 @@ __dispose(_ForwardIterator __first,
   });
 }
 
-template <typename _BidirectionalIterator,
-          typename _RandomAccessIterator1,
-          typename _Map,
-          typename _RandomAccessIterator2>
+template <class _BidirectionalIterator, class _RandomAccessIterator1, class _Map, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void dispose_backward(
     _BidirectionalIterator __first,
     _BidirectionalIterator __last,
@@ -177,11 +174,11 @@ _LIBCPP_HIDE_FROM_ABI void dispose_backward(
                 });
 }
 
-template <typename _ForwardIterator,
-          typename _Map,
-          typename _Radix,
-          typename _RandomAccessIterator1,
-          typename _RandomAccessIterator2,
+template <class _ForwardIterator,
+          class _Map,
+          class _Radix,
+          class _RandomAccessIterator1,
+          class _RandomAccessIterator2,
           size_t... _Radices>
 _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     _ForwardIterator __first,
@@ -212,11 +209,7 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
   return __is_sorted;
 }
 
-template <typename _ForwardIterator,
-          typename _Map,
-          typename _Radix,
-          typename _RandomAccessIterator1,
-          typename _RandomAccessIterator2>
+template <class _ForwardIterator, class _Map, class _Radix, class _RandomAccessIterator1, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI bool
 __collect(_ForwardIterator __first,
           _ForwardIterator __last,
@@ -230,10 +223,7 @@ __collect(_ForwardIterator __first,
       __first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
 }
 
-template <typename _BidirectionalIterator,
-          typename _RandomAccessIterator1,
-          typename _Map,
-          typename _RandomAccessIterator2>
+template <class _BidirectionalIterator, class _RandomAccessIterator1, class _Map, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void __dispose_backward(
     _BidirectionalIterator __first,
     _BidirectionalIterator __last,
@@ -248,7 +238,7 @@ _LIBCPP_HIDE_FROM_ABI void __dispose_backward(
                 });
 }
 
-template <typename _ForwardIterator, typename _RandomAccessIterator, typename _Map>
+template <class _ForwardIterator, class _RandomAccessIterator, class _Map>
 _LIBCPP_HIDE_FROM_ABI _RandomAccessIterator
 __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomAccessIterator __result, _Map __map) {
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
@@ -263,7 +253,7 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
   return __result + __counters[traits::__value_range];
 }
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
 _LIBCPP_HIDE_FROM_ABI typename enable_if<
     __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count == 1,
     void>::type
@@ -281,7 +271,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   std::copy(std::__move_assign_please(buffer), std::__move_assign_please(__buffer_end), __first);
 }
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
 _LIBCPP_HIDE_FROM_ABI typename enable_if<
     __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count %
             2 ==
@@ -343,21 +333,21 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
 
 _LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(bool __b) { return __b; }
 
-template <typename _Ip>
+template <class _Ip>
 _LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(_Ip __n) {
   constexpr const auto __min_value = numeric_limits<_Ip>::min();
   return static_cast<make_unsigned_t<_Ip> >(__n ^ __min_value);
 }
 
 struct __identity_fn {
-  template <typename _Tp>
+  template <class _Tp>
   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Tp&& __value) const {
     return std::forward<_Tp>(__value);
   }
 };
 
 struct __low_byte_fn {
-  template <typename _Ip>
+  template <class _Ip>
   _LIBCPP_HIDE_FROM_ABI constexpr uint8_t operator()(_Ip __integer) const {
     static_assert(is_integral<_Ip>::value, "");
     static_assert(is_unsigned<_Ip>::value, "");
@@ -366,7 +356,7 @@ struct __low_byte_fn {
   }
 };
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Map, typename _Radix>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
 _LIBCPP_HIDE_FROM_ABI void
 __radix_sort(_RandomAccessIterator1 __first,
              _RandomAccessIterator1 __last,
@@ -377,20 +367,20 @@ __radix_sort(_RandomAccessIterator1 __first,
   std::__radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
 }
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void
 __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
   std::__radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
 }
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+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_fn{}, __low_byte_fn{});
   return true;
 }
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI bool
 __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<false>) {
   return false;
@@ -400,7 +390,7 @@ __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterat
 
 #else // _LIBCPP_STD_VER > 14
 
-template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, bool _EnableRadixSort>
+template <class _RandomAccessIterator1, class _RandomAccessIterator2, bool _EnableRadixSort>
 _LIBCPP_HIDE_FROM_ABI bool
 __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BoolConstant<_EnableRadixSort>) {
   return false;

>From 50e83db2424a30b8ba9035c94645af6a1267327c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 00:43:15 +0300
Subject: [PATCH 17/33] no-trailing-return

---
 libcxx/include/__algorithm/radix_sort.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 9827e1aa4a53b9..98ca9e7a5a6431 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -51,16 +51,15 @@ inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<in
 
 #  define _EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
 
-template <class _Iterator>
-_LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
-    -> enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value,
-                   move_iterator<_Iterator> > {
+template <class _Iterator,
+          enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI constexpr move_iterator<_Iterator> __move_assign_please(_Iterator __i) {
   return std::make_move_iterator(std::move(__i));
 }
 
-template <class _Iterator>
-_LIBCPP_HIDE_FROM_ABI constexpr auto __move_assign_please(_Iterator __i)
-    -> enable_if_t<not is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, _Iterator> {
+template <class _Iterator,
+          enable_if_t<!is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI constexpr _Iterator __move_assign_please(_Iterator __i) {
   return __i;
 }
 

>From 50001db80dd98cb6de50ebc18a93a889f463e66a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 01:18:46 +0300
Subject: [PATCH 18/33] redundancy

---
 libcxx/include/__algorithm/radix_sort.h | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 98ca9e7a5a6431..2dfc61c811d1bd 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -65,8 +65,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Iterator __move_assign_please(_Iterator __i) {
 
 template <class _UnsignedInteger>
 _LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n) {
-  static_assert(is_integral<_UnsignedInteger>::value, "Must be an integral type");
-  static_assert(is_unsigned<_UnsignedInteger>::value, "Must be unsigned");
+  static_assert(is_unsigned<_UnsignedInteger>::value, "Must be unsigned integral");
 
   return numeric_limits<_UnsignedInteger>::digits - 1 - std::__countl_zero(__n);
 }
@@ -94,7 +93,6 @@ __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator
 template <class _Value, class _Map, class _Radix>
 struct __radix_sort_traits {
   using image_type = decay_t<invoke_result_t<_Map, _Value> >;
-  static_assert(is_integral<image_type>::value, "");
   static_assert(is_unsigned<image_type>::value, "");
 
   using radix_type = decay_t<invoke_result_t<_Radix, image_type> >;
@@ -108,7 +106,6 @@ struct __radix_sort_traits {
 template <class _Value, class _Map>
 struct __counting_sort_traits {
   using image_type = decay_t<invoke_result_t<_Map, _Value> >;
-  static_assert(is_integral<image_type>::value, "");
   static_assert(is_unsigned<image_type>::value, "");
 
   constexpr static const auto __value_range = numeric_limits<image_type>::max() + 1;
@@ -119,7 +116,6 @@ template <class _Radix>
 _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
   return [__radix_number, __radix = std::move(__radix)](auto __n) {
     using value_type = decltype(__n);
-    static_assert(is_integral<value_type>::value, "");
     static_assert(is_unsigned<value_type>::value, "");
     using traits = __counting_sort_traits<value_type, _Radix>;
 
@@ -348,7 +344,6 @@ struct __identity_fn {
 struct __low_byte_fn {
   template <class _Ip>
   _LIBCPP_HIDE_FROM_ABI constexpr uint8_t operator()(_Ip __integer) const {
-    static_assert(is_integral<_Ip>::value, "");
     static_assert(is_unsigned<_Ip>::value, "");
 
     return static_cast<uint8_t>(__integer & 0xff);

>From 8493c850f76069262d50142145eaa8b27c5d0c19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 01:30:11 +0300
Subject: [PATCH 19/33] rm-count

---
 libcxx/include/__algorithm/radix_sort.h | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 2dfc61c811d1bd..2ba2456c5a0489 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -123,19 +123,13 @@ _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
   };
 }
 
-template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
-_LIBCPP_HIDE_FROM_ABI void
-__count(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
-  std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
-}
-
 template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
 _LIBCPP_HIDE_FROM_ABI void
 __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
   using value_type = typename iterator_traits<_ForwardIterator>::value_type;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
-  std::__count(__first, __last, __map, __counters);
+  std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
 
   const auto __counters_end = __counters + traits::__value_range;
   std::partial_sum(__counters, __counters_end, __counters);

>From 24a8d3d128eaf4742e6b4fe8f94b9f19b0005e93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 01:30:26 +0300
Subject: [PATCH 20/33] rm-duplicate

---
 libcxx/include/__algorithm/radix_sort.h | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 2ba2456c5a0489..6ac41d7d3f25ca 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -148,21 +148,6 @@ __dispose(_ForwardIterator __first,
   });
 }
 
-template <class _BidirectionalIterator, class _RandomAccessIterator1, class _Map, class _RandomAccessIterator2>
-_LIBCPP_HIDE_FROM_ABI void dispose_backward(
-    _BidirectionalIterator __first,
-    _BidirectionalIterator __last,
-    _RandomAccessIterator1 __result,
-    _Map __map,
-    _RandomAccessIterator2 __counters) {
-  std::for_each(std::make_reverse_iterator(__last),
-                std::make_reverse_iterator(__first),
-                [&__result, &__counters, &__map](auto&& __preimage) {
-                  auto __index      = --__counters[__map(__preimage)];
-                  __result[__index] = std::forward<decltype(__preimage)>(__preimage);
-                });
-}
-
 template <class _ForwardIterator,
           class _Map,
           class _Radix,
@@ -221,9 +206,9 @@ _LIBCPP_HIDE_FROM_ABI void __dispose_backward(
     _RandomAccessIterator2 __counters) {
   std::for_each(std::make_reverse_iterator(__last),
                 std::make_reverse_iterator(__first),
-                [&__result, &__counters, &__map](auto&& preimage) {
-                  auto __index      = --__counters[__map(preimage)];
-                  __result[__index] = std::forward<decltype(preimage)>(preimage);
+                [&__result, &__counters, &__map](auto&& __preimage) {
+                  auto __index      = --__counters[__map(__preimage)];
+                  __result[__index] = std::forward<decltype(__preimage)>(__preimage);
                 });
 }
 

>From 24c418cfd4e528d984c82934e9f969b522cca925 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 01:33:40 +0300
Subject: [PATCH 21/33] identity

---
 libcxx/include/__algorithm/radix_sort.h | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 6ac41d7d3f25ca..43288bee69de5a 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -14,6 +14,7 @@
 #include <__algorithm/for_each.h>
 #include <__bit/countl.h>
 #include <__config>
+#include <__functional/identity.h>
 #include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/move_iterator.h>
@@ -313,13 +314,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(_Ip __n) {
   return static_cast<make_unsigned_t<_Ip> >(__n ^ __min_value);
 }
 
-struct __identity_fn {
-  template <class _Tp>
-  _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Tp&& __value) const {
-    return std::forward<_Tp>(__value);
-  }
-};
-
 struct __low_byte_fn {
   template <class _Ip>
   _LIBCPP_HIDE_FROM_ABI constexpr uint8_t operator()(_Ip __integer) const {
@@ -343,13 +337,13 @@ __radix_sort(_RandomAccessIterator1 __first,
 template <class _RandomAccessIterator1, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void
 __radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
-  std::__radix_sort(__first, __last, buffer, __identity_fn{}, __low_byte_fn{});
+  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_fn{}, __low_byte_fn{});
+  std::__radix_sort(__first, __last, buffer, __identity{}, __low_byte_fn{});
   return true;
 }
 

>From eae1d2a844dd293973740116415c187cdfbf614b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 09:12:30 +0300
Subject: [PATCH 22/33] rm-expand-variadic-macro

---
 libcxx/include/__algorithm/radix_sort.h | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 43288bee69de5a..01b81fffbd4290 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -48,10 +48,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 14
 
-inline _LIBCPP_HIDE_FROM_ABI void __variadic_expansion_dummy(initializer_list<int>) {}
-
-#  define _EXPAND_VARIADIC(expression) std::__variadic_expansion_dummy({(expression, 0)...})
-
 template <class _Iterator,
           enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI constexpr move_iterator<_Iterator> __move_assign_please(_Iterator __i) {
@@ -173,13 +169,13 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     __is_sorted &= (__current >= __previous);
     __previous = __current;
 
-    _EXPAND_VARIADIC(++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)]);
+    (++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)], ...);
   });
 
-  _EXPAND_VARIADIC(
-      __maximums[_Radices] =
-          std::__partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
-              .second);
+  ((__maximums[_Radices] =
+        std::__partial_sum_max(__counters[_Radices], __counters[_Radices] + __radix_value_range, __counters[_Radices])
+            .second),
+   ...);
 
   return __is_sorted;
 }
@@ -353,8 +349,6 @@ __radix_sort(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterat
   return false;
 }
 
-#  undef _EXPAND_VARIADIC
-
 #else // _LIBCPP_STD_VER > 14
 
 template <class _RandomAccessIterator1, class _RandomAccessIterator2, bool _EnableRadixSort>

>From 920d24273be9cefff1368fda7fecdb9305980519 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:12:29 +0300
Subject: [PATCH 23/33] iter-value

---
 libcxx/include/__algorithm/radix_sort.h | 34 +++++++++++--------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 01b81fffbd4290..a68f023e29e036 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -48,14 +48,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 14
 
-template <class _Iterator,
-          enable_if_t<is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, int> = 0>
+template <class _Iterator, enable_if_t<is_move_assignable<__iter_value_type<_Iterator>>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI constexpr move_iterator<_Iterator> __move_assign_please(_Iterator __i) {
   return std::make_move_iterator(std::move(__i));
 }
 
-template <class _Iterator,
-          enable_if_t<!is_move_assignable<typename iterator_traits<_Iterator>::value_type>::value, int> = 0>
+template <class _Iterator, enable_if_t<!is_move_assignable<__iter_value_type<_Iterator>>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI constexpr _Iterator __move_assign_please(_Iterator __i) {
   return __i;
 }
@@ -68,14 +66,14 @@ _LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n)
 }
 
 template <class _InputIterator, class _OutputIterator>
-_LIBCPP_HIDE_FROM_ABI pair<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>
+_LIBCPP_HIDE_FROM_ABI pair<_OutputIterator, __iter_value_type<_InputIterator>>
 __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
   if (__first == __last)
     return {__result, 0};
 
-  auto __max                                                 = *__first;
-  typename iterator_traits<_InputIterator>::value_type __sum = *__first;
-  *__result                                                  = __sum;
+  auto __max                              = *__first;
+  __iter_value_type<_InputIterator> __sum = *__first;
+  *__result                               = __sum;
 
   while (++__first != __last) {
     if (__max < *__first) {
@@ -123,7 +121,7 @@ _LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
 template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
 _LIBCPP_HIDE_FROM_ABI void
 __collect(_ForwardIterator __first, _ForwardIterator __last, _Map __map, _RandomAccessIterator __counters) {
-  using value_type = typename iterator_traits<_ForwardIterator>::value_type;
+  using value_type = __iter_value_type<_ForwardIterator>;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
   std::for_each(__first, __last, [&__counters, &__map](const auto& __preimage) { ++__counters[__map(__preimage)]; });
@@ -159,7 +157,7 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     _RandomAccessIterator1 __counters,
     _RandomAccessIterator2 __maximums,
     index_sequence<_Radices...>) {
-  using value_type                   = typename iterator_traits<_ForwardIterator>::value_type;
+  using value_type                   = __iter_value_type<_ForwardIterator>;
   constexpr auto __radix_value_range = __radix_sort_traits<value_type, _Map, _Radix>::__radix_value_range;
 
   auto __previous  = numeric_limits<invoke_result_t<_Map, value_type> >::min();
@@ -188,7 +186,7 @@ __collect(_ForwardIterator __first,
           _Radix __radix,
           _RandomAccessIterator1 __counters,
           _RandomAccessIterator2 __maximums) {
-  using value_type             = typename iterator_traits<_ForwardIterator>::value_type;
+  using value_type             = __iter_value_type<_ForwardIterator>;
   constexpr auto __radix_count = __radix_sort_traits<value_type, _Map, _Radix>::__radix_count;
   return std::__collect_impl(
       __first, __last, __map, __radix, __counters, __maximums, make_index_sequence<__radix_count>());
@@ -212,7 +210,7 @@ _LIBCPP_HIDE_FROM_ABI void __dispose_backward(
 template <class _ForwardIterator, class _RandomAccessIterator, class _Map>
 _LIBCPP_HIDE_FROM_ABI _RandomAccessIterator
 __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomAccessIterator __result, _Map __map) {
-  using value_type = typename iterator_traits<_ForwardIterator>::value_type;
+  using value_type = __iter_value_type<_ForwardIterator>;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
@@ -225,9 +223,9 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
 }
 
 template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
-_LIBCPP_HIDE_FROM_ABI typename enable_if<
-    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count == 1,
-    void>::type
+_LIBCPP_HIDE_FROM_ABI
+typename enable_if< __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count == 1,
+                    void>::type
 __radix_sort_impl(_RandomAccessIterator1 __first,
                   _RandomAccessIterator1 __last,
                   _RandomAccessIterator2 buffer,
@@ -244,16 +242,14 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
 
 template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
 _LIBCPP_HIDE_FROM_ABI typename enable_if<
-    __radix_sort_traits<typename iterator_traits<_RandomAccessIterator1>::value_type, _Map, _Radix>::__radix_count %
-            2 ==
-        0,
+    __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count % 2 == 0,
     void>::type
 __radix_sort_impl(_RandomAccessIterator1 __first,
                   _RandomAccessIterator1 __last,
                   _RandomAccessIterator2 __buffer_begin,
                   _Map __map,
                   _Radix __radix) {
-  using value_type = typename iterator_traits<_RandomAccessIterator1>::value_type;
+  using value_type = __iter_value_type<_RandomAccessIterator1>;
   using traits     = __radix_sort_traits<value_type, _Map, _Radix>;
 
   using difference_type = typename iterator_traits<_RandomAccessIterator1>::difference_type;

>From 96ff510b534a000ba53bc24047f5a56f016a1d29 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:17:31 +0300
Subject: [PATCH 24/33] iter-diff-t

---
 libcxx/include/__algorithm/radix_sort.h | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index a68f023e29e036..cba56e75814a78 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -213,8 +213,7 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
   using value_type = __iter_value_type<_ForwardIterator>;
   using traits     = __counting_sort_traits<value_type, _Map>;
 
-  using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
-  difference_type __counters[traits::__value_range + 1] = {0};
+  __iter_diff_t<_RandomAccessIterator> __counters[traits::__value_range + 1] = {0};
 
   std::__collect(__first, __last, __map, std::next(std::begin(__counters)));
   std::__dispose(__first, __last, __result, __map, std::begin(__counters));
@@ -252,9 +251,8 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   using value_type = __iter_value_type<_RandomAccessIterator1>;
   using traits     = __radix_sort_traits<value_type, _Map, _Radix>;
 
-  using difference_type = typename iterator_traits<_RandomAccessIterator1>::difference_type;
-  difference_type __counters[traits::__radix_count][traits::__radix_value_range] = {{0}};
-  difference_type __maximums[traits::__radix_count]                              = {0};
+  __iter_diff_t<_RandomAccessIterator1> __counters[traits::__radix_count][traits::__radix_value_range] = {{0}};
+  __iter_diff_t<_RandomAccessIterator1> __maximums[traits::__radix_count]                              = {0};
   const auto __is_sorted = std::__collect(__first, __last, __map, __radix, __counters, __maximums);
   if (not __is_sorted) {
     const auto __range_size = std::distance(__first, __last);

>From 6e82ad0d6f73670bfa271f74f8a8037486842f8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:22:43 +0300
Subject: [PATCH 25/33] uglify-more

---
 libcxx/include/__algorithm/radix_sort.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index cba56e75814a78..9626c00fd9a1c4 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -87,23 +87,23 @@ __partial_sum_max(_InputIterator __first, _InputIterator __last, _OutputIterator
 
 template <class _Value, class _Map, class _Radix>
 struct __radix_sort_traits {
-  using image_type = decay_t<invoke_result_t<_Map, _Value> >;
-  static_assert(is_unsigned<image_type>::value, "");
+  using __image_type = decay_t<invoke_result_t<_Map, _Value> >;
+  static_assert(is_unsigned<__image_type>::value, "");
 
-  using radix_type = decay_t<invoke_result_t<_Radix, image_type> >;
-  static_assert(is_integral<radix_type>::value, "");
+  using __radix_type = decay_t<invoke_result_t<_Radix, __image_type> >;
+  static_assert(is_integral<__radix_type>::value, "");
 
-  constexpr static auto __radix_value_range = numeric_limits<radix_type>::max() + 1;
+  constexpr static auto __radix_value_range = numeric_limits<__radix_type>::max() + 1;
   constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__radix_value_range);
-  constexpr static auto __radix_count       = sizeof(image_type) * CHAR_BIT / __radix_size;
+  constexpr static auto __radix_count       = sizeof(__image_type) * CHAR_BIT / __radix_size;
 };
 
 template <class _Value, class _Map>
 struct __counting_sort_traits {
-  using image_type = decay_t<invoke_result_t<_Map, _Value> >;
-  static_assert(is_unsigned<image_type>::value, "");
+  using __image_type = decay_t<invoke_result_t<_Map, _Value> >;
+  static_assert(is_unsigned<__image_type>::value, "");
 
-  constexpr static const auto __value_range = numeric_limits<image_type>::max() + 1;
+  constexpr static const auto __value_range = numeric_limits<__image_type>::max() + 1;
   constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__value_range);
 };
 

>From c8a1369ae6a9161f3afb7b5153c1e8c216b7ab0f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:33:21 +0300
Subject: [PATCH 26/33] nth-radix

---
 libcxx/include/__algorithm/radix_sort.h | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 9626c00fd9a1c4..a5a1321a917666 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -107,15 +107,12 @@ struct __counting_sort_traits {
   constexpr static auto __radix_size        = std::__intlog2<uint64_t>(__value_range);
 };
 
-template <class _Radix>
-_LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix) {
-  return [__radix_number, __radix = std::move(__radix)](auto __n) {
-    using value_type = decltype(__n);
-    static_assert(is_unsigned<value_type>::value, "");
-    using traits = __counting_sort_traits<value_type, _Radix>;
-
-    return __radix(static_cast<value_type>(__n >> traits::__radix_size * __radix_number));
-  };
+template <class _Radix, class _Integer>
+_LIBCPP_HIDE_FROM_ABI auto __nth_radix(size_t __radix_number, _Radix __radix, _Integer __n) {
+  static_assert(is_unsigned<_Integer>::value, "");
+  using traits = __counting_sort_traits<_Integer, _Radix>;
+
+  return __radix(static_cast<_Integer>(__n >> traits::__radix_size * __radix_number));
 }
 
 template <class _ForwardIterator, class _Map, class _RandomAccessIterator>
@@ -167,7 +164,7 @@ _LIBCPP_HIDE_FROM_ABI bool __collect_impl(
     __is_sorted &= (__current >= __previous);
     __previous = __current;
 
-    (++__counters[_Radices][std::__nth_radix(_Radices, __radix)(__current)], ...);
+    (++__counters[_Radices][std::__nth_radix(_Radices, __radix, __current)], ...);
   });
 
   ((__maximums[_Radices] =
@@ -269,7 +266,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
         std::copy(std::__move_assign_please(__first), std::__move_assign_please(__last), __buffer_begin);
       } else {
         auto __n0th = [__radix_number, &__map, &__radix](const auto& __v) {
-          return std::__nth_radix(__radix_number, __radix)(__map(__v));
+          return std::__nth_radix(__radix_number, __radix, __map(__v));
         };
         std::__dispose_backward(
             std::__move_assign_please(__first),
@@ -283,7 +280,7 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
         std::copy(std::__move_assign_please(__buffer_begin), std::__move_assign_please(__buffer_end), __first);
       } else {
         auto __n1th = [__radix_number, &__map, &__radix](const auto& __v) {
-          return std::__nth_radix(__radix_number + 1, __radix)(__map(__v));
+          return std::__nth_radix(__radix_number + 1, __radix, __map(__v));
         };
         std::__dispose_backward(
             std::__move_assign_please(__buffer_begin),

>From c6aed5b09dfd00eedf22adbcbcd7a01e7737427c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:43:52 +0300
Subject: [PATCH 27/33] is-identity

---
 libcxx/include/__algorithm/ranges_stable_sort.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/ranges_stable_sort.h b/libcxx/include/__algorithm/ranges_stable_sort.h
index 96d84b208687fc..029551fefa692c 100644
--- a/libcxx/include/__algorithm/ranges_stable_sort.h
+++ b/libcxx/include/__algorithm/ranges_stable_sort.h
@@ -48,7 +48,7 @@ struct __stable_sort {
 
     auto&& __projected_comp = std::__make_projected(__comp, __proj);
     constexpr auto __default_comp        = is_same_v<_Comp, ranges::less>;
-    constexpr auto __default_proj        = is_same_v<_Proj, identity>;
+    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&,

>From 5a5ead88d0aad815714a1339fdf0ee4b4a9e344f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 12:59:43 +0300
Subject: [PATCH 28/33] desugars

---
 libcxx/include/__algorithm/ranges_stable_sort.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/ranges_stable_sort.h b/libcxx/include/__algorithm/ranges_stable_sort.h
index 029551fefa692c..329ff1b7ba8da3 100644
--- a/libcxx/include/__algorithm/ranges_stable_sort.h
+++ b/libcxx/include/__algorithm/ranges_stable_sort.h
@@ -24,8 +24,8 @@
 #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 <__type_traits/is_same.h>
 #include <__utility/forward.h>
 #include <__utility/move.h>
 
@@ -47,7 +47,8 @@ struct __stable_sort {
     auto __last_iter = ranges::next(__first, __last);
 
     auto&& __projected_comp = std::__make_projected(__comp, __proj);
-    constexpr auto __default_comp        = is_same_v<_Comp, ranges::less>;
+    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;

>From 4daa0282afb4789be6a464e67b2d336c14d302a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 23:07:47 +0300
Subject: [PATCH 29/33] enable-if

---
 libcxx/include/__algorithm/radix_sort.h | 43 ++++++++++++++-----------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index a5a1321a917666..874fc6584bee38 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -218,15 +218,18 @@ __counting_sort_impl(_ForwardIterator __first, _ForwardIterator __last, _RandomA
   return __result + __counters[traits::__value_range];
 }
 
-template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
-_LIBCPP_HIDE_FROM_ABI
-typename enable_if< __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count == 1,
-                    void>::type
-__radix_sort_impl(_RandomAccessIterator1 __first,
-                  _RandomAccessIterator1 __last,
-                  _RandomAccessIterator2 buffer,
-                  _Map __map,
-                  _Radix __radix) {
+template <class _RandomAccessIterator1,
+          class _RandomAccessIterator2,
+          class _Map,
+          class _Radix,
+          enable_if_t< __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count == 1,
+                       int> = 0>
+_LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
+    _RandomAccessIterator1 __first,
+    _RandomAccessIterator1 __last,
+    _RandomAccessIterator2 buffer,
+    _Map __map,
+    _Radix __radix) {
   auto __buffer_end = std::__counting_sort_impl(
       std::__move_assign_please(__first),
       std::__move_assign_please(__last),
@@ -236,15 +239,19 @@ __radix_sort_impl(_RandomAccessIterator1 __first,
   std::copy(std::__move_assign_please(buffer), std::__move_assign_please(__buffer_end), __first);
 }
 
-template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map, class _Radix>
-_LIBCPP_HIDE_FROM_ABI typename enable_if<
-    __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count % 2 == 0,
-    void>::type
-__radix_sort_impl(_RandomAccessIterator1 __first,
-                  _RandomAccessIterator1 __last,
-                  _RandomAccessIterator2 __buffer_begin,
-                  _Map __map,
-                  _Radix __radix) {
+template <
+    class _RandomAccessIterator1,
+    class _RandomAccessIterator2,
+    class _Map,
+    class _Radix,
+    enable_if_t< __radix_sort_traits<__iter_value_type<_RandomAccessIterator1>, _Map, _Radix>::__radix_count % 2 == 0,
+                 int> = 0 >
+_LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
+    _RandomAccessIterator1 __first,
+    _RandomAccessIterator1 __last,
+    _RandomAccessIterator2 __buffer_begin,
+    _Map __map,
+    _Radix __radix) {
   using value_type = __iter_value_type<_RandomAccessIterator1>;
   using traits     = __radix_sort_traits<value_type, _Map, _Radix>;
 

>From b70ef6b74e30de31cb2bd11671ffaa0427abe88c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Wed, 21 Aug 2024 23:32:41 +0300
Subject: [PATCH 30/33] naming

---
 libcxx/include/__algorithm/radix_sort.h | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 874fc6584bee38..d720a5f0e06aa9 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -227,16 +227,16 @@ template <class _RandomAccessIterator1,
 _LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
     _RandomAccessIterator1 __first,
     _RandomAccessIterator1 __last,
-    _RandomAccessIterator2 buffer,
+    _RandomAccessIterator2 __buffer,
     _Map __map,
     _Radix __radix) {
   auto __buffer_end = std::__counting_sort_impl(
       std::__move_assign_please(__first),
       std::__move_assign_please(__last),
-      buffer,
+      __buffer,
       [&__map, &__radix](const auto& value) { return __radix(__map(value)); });
 
-  std::copy(std::__move_assign_please(buffer), std::__move_assign_please(__buffer_end), __first);
+  std::copy(std::__move_assign_please(__buffer), std::__move_assign_please(__buffer_end), __first);
 }
 
 template <
@@ -321,23 +321,26 @@ template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Map
 _LIBCPP_HIDE_FROM_ABI void
 __radix_sort(_RandomAccessIterator1 __first,
              _RandomAccessIterator1 __last,
-             _RandomAccessIterator2 buffer,
+             _RandomAccessIterator2 __buffer,
              _Map __map,
              _Radix __radix) {
   auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return std::__to_unsigned(__map(x)); };
-  std::__radix_sort_impl(__first, __last, buffer, __map_to_unsigned, __radix);
+  std::__radix_sort_impl(__first, __last, __buffer, __map_to_unsigned, __radix);
 }
 
 template <class _RandomAccessIterator1, class _RandomAccessIterator2>
 _LIBCPP_HIDE_FROM_ABI void
-__radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 buffer) {
-  std::__radix_sort(__first, __last, buffer, __identity{}, __low_byte_fn{});
+__radix_sort(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __buffer) {
+  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{});
+_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;
 }
 

>From 420607caab47626f30f9f28eb41b1acc42aff1ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Thu, 22 Aug 2024 16:45:50 +0300
Subject: [PATCH 31/33] description

---
 libcxx/include/__algorithm/radix_sort.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index d720a5f0e06aa9..e60092c0184303 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -10,6 +10,23 @@
 #ifndef _LIBCPP___ALGORITHM_RADIX_SORT_H
 #define _LIBCPP___ALGORITHM_RADIX_SORT_H
 
+// This is an implementation of classic LSD radix sort algorithm, running in linear time and using `O(max(N, M))`
+// additional memory, where `N` is size of an input range, `M` — maximum value of
+// a radix of the sorted integer type. Type of the radix and its maximum value are determined at compile time
+// based on type returned by function `__radix`. The default radix is uint8.
+
+// The algorithm is equivalent to several consecutive calls of counting sort for each
+// radix of the sorted numbers from low to high byte.
+// The algorithm uses a temporary buffer of size equal to size of the input range. Each `i`-th pass
+// of the algorithm sorts values by `i`-th radix and moves values to the temporary buffer (for each even `i`, counted
+// from zero), or moves them back to the initial range (for each odd `i`). It there is only one radix in sorted integers
+// (e.g. int8), than sorted values are placed to the buffer, and then moved back to the initial range.
+
+// The implementation also has several optimizations:
+// - the counters for the counting sort are calculated in one pass for all radices;
+// - if all values of a radix are the same, we do not sort that radix, and just move items to the buffer;
+// - if two consecutive radices satisfies condition above, we do nothing for these two radices.
+
 #include <__algorithm/copy.h>
 #include <__algorithm/for_each.h>
 #include <__bit/countl.h>

>From 432a179fc988045ba97b2af1507d9577fd86df61 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Thu, 22 Aug 2024 18:28:09 +0300
Subject: [PATCH 32/33] move-explicitly

---
 libcxx/include/__algorithm/radix_sort.h | 44 +++++++------------------
 1 file changed, 11 insertions(+), 33 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index e60092c0184303..4f0023f7dc3a55 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -27,8 +27,8 @@
 // - if all values of a radix are the same, we do not sort that radix, and just move items to the buffer;
 // - if two consecutive radices satisfies condition above, we do nothing for these two radices.
 
-#include <__algorithm/copy.h>
 #include <__algorithm/for_each.h>
+#include <__algorithm/move.h>
 #include <__bit/countl.h>
 #include <__config>
 #include <__functional/identity.h>
@@ -65,16 +65,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 14
 
-template <class _Iterator, enable_if_t<is_move_assignable<__iter_value_type<_Iterator>>::value, int> = 0>
-_LIBCPP_HIDE_FROM_ABI constexpr move_iterator<_Iterator> __move_assign_please(_Iterator __i) {
-  return std::make_move_iterator(std::move(__i));
-}
-
-template <class _Iterator, enable_if_t<!is_move_assignable<__iter_value_type<_Iterator>>::value, int> = 0>
-_LIBCPP_HIDE_FROM_ABI constexpr _Iterator __move_assign_please(_Iterator __i) {
-  return __i;
-}
-
 template <class _UnsignedInteger>
 _LIBCPP_HIDE_FROM_ABI constexpr _UnsignedInteger __intlog2(_UnsignedInteger __n) {
   static_assert(is_unsigned<_UnsignedInteger>::value, "Must be unsigned integral");
@@ -153,7 +143,7 @@ __dispose(_ForwardIterator __first,
           _RandomAccessIterator2 __counters) {
   std::for_each(__first, __last, [&__result, &__counters, &__map](auto&& __preimage) {
     auto __index      = __counters[__map(__preimage)]++;
-    __result[__index] = std::forward<decltype(__preimage)>(__preimage);
+    __result[__index] = std::move(__preimage);
   });
 }
 
@@ -217,7 +207,7 @@ _LIBCPP_HIDE_FROM_ABI void __dispose_backward(
                 std::make_reverse_iterator(__first),
                 [&__result, &__counters, &__map](auto&& __preimage) {
                   auto __index      = --__counters[__map(__preimage)];
-                  __result[__index] = std::forward<decltype(__preimage)>(__preimage);
+                  __result[__index] = std::move(__preimage);
                 });
 }
 
@@ -247,13 +237,11 @@ _LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
     _RandomAccessIterator2 __buffer,
     _Map __map,
     _Radix __radix) {
-  auto __buffer_end = std::__counting_sort_impl(
-      std::__move_assign_please(__first),
-      std::__move_assign_please(__last),
-      __buffer,
-      [&__map, &__radix](const auto& value) { return __radix(__map(value)); });
+  auto __buffer_end = std::__counting_sort_impl(__first, __last, __buffer, [&__map, &__radix](const auto& value) {
+    return __radix(__map(value));
+  });
 
-  std::copy(std::__move_assign_please(__buffer), std::__move_assign_please(__buffer_end), __first);
+  std::move(__buffer, __buffer_end, __first);
 }
 
 template <
@@ -287,31 +275,21 @@ _LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
       }
 
       if (__n0th_is_single) {
-        std::copy(std::__move_assign_please(__first), std::__move_assign_please(__last), __buffer_begin);
+        std::move(__first, __last, __buffer_begin);
       } else {
         auto __n0th = [__radix_number, &__map, &__radix](const auto& __v) {
           return std::__nth_radix(__radix_number, __radix, __map(__v));
         };
-        std::__dispose_backward(
-            std::__move_assign_please(__first),
-            std::__move_assign_please(__last),
-            __buffer_begin,
-            __n0th,
-            __counters[__radix_number]);
+        std::__dispose_backward(__first, __last, __buffer_begin, __n0th, __counters[__radix_number]);
       }
 
       if (__n1th_is_single) {
-        std::copy(std::__move_assign_please(__buffer_begin), std::__move_assign_please(__buffer_end), __first);
+        std::move(__buffer_begin, __buffer_end, __first);
       } else {
         auto __n1th = [__radix_number, &__map, &__radix](const auto& __v) {
           return std::__nth_radix(__radix_number + 1, __radix, __map(__v));
         };
-        std::__dispose_backward(
-            std::__move_assign_please(__buffer_begin),
-            std::__move_assign_please(__buffer_end),
-            __first,
-            __n1th,
-            __counters[__radix_number + 1]);
+        std::__dispose_backward(__buffer_begin, __buffer_end, __first, __n1th, __counters[__radix_number + 1]);
       }
     }
   }

>From 1b2dedc969864f9529a841a3ffd527960de8c6c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=98=D0=B7?=
 =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D0=BE=D0=B2?= <dmitriy at izvolov.ru>
Date: Fri, 23 Aug 2024 12:04:19 +0300
Subject: [PATCH 33/33] shift-to-unsigned

---
 libcxx/include/__algorithm/radix_sort.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__algorithm/radix_sort.h b/libcxx/include/__algorithm/radix_sort.h
index 4f0023f7dc3a55..5b494e5c352d7f 100644
--- a/libcxx/include/__algorithm/radix_sort.h
+++ b/libcxx/include/__algorithm/radix_sort.h
@@ -295,10 +295,10 @@ _LIBCPP_HIDE_FROM_ABI void __radix_sort_impl(
   }
 }
 
-_LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(bool __b) { return __b; }
+_LIBCPP_HIDE_FROM_ABI constexpr auto __shift_to_unsigned(bool __b) { return __b; }
 
 template <class _Ip>
-_LIBCPP_HIDE_FROM_ABI constexpr auto __to_unsigned(_Ip __n) {
+_LIBCPP_HIDE_FROM_ABI constexpr auto __shift_to_unsigned(_Ip __n) {
   constexpr const auto __min_value = numeric_limits<_Ip>::min();
   return static_cast<make_unsigned_t<_Ip> >(__n ^ __min_value);
 }
@@ -319,7 +319,7 @@ __radix_sort(_RandomAccessIterator1 __first,
              _RandomAccessIterator2 __buffer,
              _Map __map,
              _Radix __radix) {
-  auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return std::__to_unsigned(__map(x)); };
+  auto __map_to_unsigned = [__map = std::move(__map)](const auto& x) { return std::__shift_to_unsigned(__map(x)); };
   std::__radix_sort_impl(__first, __last, __buffer, __map_to_unsigned, __radix);
 }
 



More information about the libcxx-commits mailing list