94#ifndef TPL_SORT_UTILS_H
95#define TPL_SORT_UTILS_H
108#include <type_traits>
171 const T *prev = &first;
176 if (
cmp(curr, *prev))
207 const Compare &
cmp = Compare())
210 return std::make_pair(
true, 0);
214 const T *prev = &first;
220 if (
cmp(curr, *prev))
221 return std::make_pair(
false, i);
225 return std::make_pair(
true, i);
255 const T *prev = &first;
260 if (
cmp(*prev, curr))
291 const Compare &
cmp = Compare())
298 const T *prev = &first;
304 if (
cmp(curr, *prev))
344template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
346 noexcept(static_cast<
bool>(
cmp(a[0], a[0])))
and std::is_nothrow_swappable_v<
T>)
351 for (
size_t i = 0,
min, j; i < n - 1; ++i)
353 for (
min = i, j = i + 1; j < n; ++j)
358 std::swap(a[
min], a[i]);
384template <
class Link,
class Compare>
389 typename Link::Iterator it(
const_cast<Link &
>(list));
390 Link *extreme = it.get_curr();
392 for (it.next(); it.has_curr(); it.next_ne())
394 Link *curr = it.get_curr();
395 if (
cmp(curr, extreme))
402template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
415 if (
cmp(curr, *extreme))
422template <
class Compare>
425 return search_extreme<Dlink, Compare>(list,
cmp);
428template <
class Compare>
431 return search_extreme<Slinknc, Compare>(list,
cmp);
443template <
class Compare>
449 Dlink *extreme = search_extreme<Dlink, Compare>(list,
cmp);
466template <
typename Tlink,
template <
class>
class Tnode,
typename T,
class Compare>
473 Compare_Tnode(Compare cmp_fct = Compare()) noexcept(
std::is_nothrow_copy_constructible_v<Compare>)
480 noexcept(
noexcept(
static_cast<bool>(std::declval<const Compare &>()(
481 std::declval<Tnode<T> *>()->get_data(), std::declval<Tnode<T> *>()->get_data()))))
483 auto *n1 =
static_cast<Tnode<T> *
>(
l1);
484 auto *n2 =
static_cast<Tnode<T> *
>(
l2);
488 return static_cast<bool>(
cmp(n1->get_data(), n2->get_data()));
493 noexcept(
noexcept(
static_cast<bool>(std::declval<const Compare &>()(
494 std::declval<Tnode<T> *>()->get_data(), x))))
496 auto *n =
static_cast<Tnode<T> *
>(
l);
500 return static_cast<bool>(
cmp(n->get_data(), x));
507template <
typename T,
class Compare>
519template <
typename T,
class Compare>
529template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
556template <
typename T,
class Equal = Aleph::equal_to<T>>
560 for (
long i =
l; i <=
r; ++i)
584template <
typename T,
class Equal = Aleph::equal_to<T>>
588 for (
long i =
l; i <=
r; ++i)
608template <
class Link,
typename T,
class Equal>
611 for (
typename Link::Iterator it(
const_cast<Link &
>(list)); it.has_curr(); it.next_ne())
612 if (Link *curr = it.get_curr();
eq(curr, x))
624template <
typename T,
class Equal = Aleph::equal_to<T>>
629 return sequential_search<Dlink, T, Compare_Dnode<T, Equal>>(list, x,
cmp);
638template <
typename T,
class Equal = Aleph::equal_to<T>>
643 return sequential_search<Slinknc, T, Compare_Snodenc<T, Equal>>(list, x,
cmp);
652template <
typename T,
class Equal = Aleph::equal_to<T>>
657 Dlink *ret = sequential_search<Dlink, T, Compare_Dnode<T, Equal>>(list, x,
cmp);
659 return ret ==
nullptr ? nullptr :
static_cast<Dnode<T> *
>(ret);
663template <
typename T,
class Equal = Aleph::equal_to<T>>
666 return sequential_search<T, Equal>(list, x,
eq);
675template <
typename T,
class Equal = Aleph::equal_to<T>>
680 const auto &base =
static_cast<const Dlink &
>(list);
681 const Dlink *ret = sequential_search<Dlink, T, Compare_Dnode<T, Equal>>(base, x,
cmp);
683 return ret ==
nullptr ? nullptr :
static_cast<const Dnode<T> *
>(ret);
687template <
typename T,
class Equal = Aleph::equal_to<T>>
690 return sequential_search<T, Equal>(list, x,
eq);
699template <
typename T,
class Equal = Aleph::equal_to<T>>
703 return ret !=
nullptr ? &ret->
get_data() :
nullptr;
712template <
typename T,
class Equal = Aleph::equal_to<T>>
715 const Dnode<T> *ret = sequential_search<T, Equal>(
static_cast<const Dnode<T> &
>(list), x,
eq);
716 return ret !=
nullptr ? &ret->
get_data() :
nullptr;
725template <
typename T,
class Equal = Aleph::equal_to<T>>
730 T &curr = it.get_curr_ne();
743template <
typename T,
class Equal = Aleph::equal_to<T>>
748 const T &curr = it.get_curr_ne();
769template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
772 long extreme_index =
l;
773 for (
long i =
l + 1; i <=
r; ++i)
774 if (
cmp(a[i], a[extreme_index]))
777 return extreme_index;
784template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
787 return search_extreme<T, Compare>(a,
l,
r,
cmp);
794template <
typename T, StrictWeakOrder<T> Compare = Aleph::greater<T>>
797 return search_extreme<T, Compare>(a,
l,
r,
cmp);
803template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
807 Dlink *ret = search_extreme<Dlink, Compare_Dnode<T, Compare>>(
808 const_cast<Dlink &
>(
static_cast<const Dlink &
>(list)), cmp_dnode);
810 return static_cast<Dnode<T> *
>(ret);
823template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
826 const auto &base =
static_cast<const Dnode<T> &
>(list);
829 return ret !=
nullptr ? &(ret->
get_data()) :
nullptr;
833template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
836 const auto &base =
static_cast<const Dnode<T> &
>(list);
839 return ret !=
nullptr ? &(ret->
get_data()) :
nullptr;
847template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
859 if (
cmp(curr, *extreme))
871template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
874 return search_extreme<T, Compare>(list,
cmp);
878template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
881 return search_extreme<T, Compare>(list,
cmp);
889template <
typename T, StrictWeakOrder<T> Compare = Aleph::greater<T>>
892 return search_extreme<T, Compare>(list,
cmp);
896template <
typename T, StrictWeakOrder<T> Compare = Aleph::greater<T>>
899 return search_extreme<T, Compare>(list,
cmp);
937template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
939 noexcept(static_cast<
bool>(
cmp(
std::declval<
T &>(),
std::declval<
T &>())))
and
940 std::is_nothrow_move_constructible_v<
T>
and std::is_nothrow_move_assignable_v<
T>)
944 for (
long i =
l + 1, j; i <=
r; ++i)
946 T tmp = std::move(a[i]);
947 for (j = i; j >
l and static_cast<bool>(
cmp(tmp, a[j - 1])); --j)
948 a[j] = std::move(a[j - 1]);
950 a[j] = std::move(tmp);
967template <
class Compare>
973 while (q != &list
and static_cast<bool>(
cmp(p, q)))
978template <
class Compare>
989 if (not
static_cast<bool>(
cmp(p, list.
get_last())))
1007 ah_logic_error() <<
"insert_sorted(): reached end of list traversal without inserting";
1017template <
class ListType,
class Compare>
1020 if (list.is_empty())
1024 aux.append(list.remove_first());
1028 if constexpr (
noexcept(
static_cast<bool>(
cmp(aux.get_first(), aux.get_first()))))
1029 while (not list.is_empty())
1030 insert_sorted<Compare>(aux, list.remove_first(),
cmp);
1033 decltype(list.remove_first()) p =
nullptr;
1036 while (not list.is_empty())
1038 p = list.remove_first();
1039 insert_sorted<Compare>(aux, p,
cmp);
1045 aux.concat_list(list);
1061template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1066 list_insertion_sort<HTList, Cmp>(
l, c);
1080template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1085 list_insertion_sort<HTList, Cmp>(
l, c);
1086 return std::move(
l);
1096template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1101 list_insertion_sort<Dlink, Cmp>(list, c);
1113template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1116 insertion_sort<T, Compare>(
static_cast<Dnode<T> &
>(list),
cmp);
1149template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1151 const Compare &
cmp = Compare())
1153 const long s =
m -
l + 1;
1162 if (buf.
size() <
static_cast<size_t>(s))
1163 buf.
putn(
static_cast<size_t>(s) - buf.
size());
1167 T *
const b = &buf.
base();
1168 for (
long i = 0; i < s; ++i)
1169 b[i] = std::move(a[
l + i]);
1177 if constexpr (
noexcept(
static_cast<bool>(
cmp(a[
l], a[
l]))))
1178 while (i < s
and j <=
r)
1179 if (
cmp(a[j], b[i]))
1180 a[
k++] = std::move(a[j++]);
1182 a[
k++] = std::move(b[i++]);
1186 while (i < s
and j <=
r)
1187 if (
cmp(a[j], b[i]))
1188 a[
k++] = std::move(a[j++]);
1190 a[
k++] = std::move(b[i++]);
1196 a[
k++] = std::move(b[i++]);
1201 a[
k++] = std::move(b[i++]);
1218template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1219inline void merge(
T *a,
const long l,
const long m,
const long r,
const Compare &
cmp = Compare())
1232template <
typename T,
class Compare>
1238 const long m =
l + (
r -
l) / 2;
1240 mergesort<T, Compare>(a,
l,
m, buf,
cmp);
1241 mergesort<T, Compare>(a,
m + 1,
r, buf,
cmp);
1243 if (not
cmp(a[
m + 1], a[
m]))
1246 merge<T, Compare>(a,
l,
m,
r, buf,
cmp);
1283template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1320template <
typename Tlist,
class Compare>
1323 assert(result.is_empty());
1333 result.concat_list(
l2);
1335 result.concat_list(
l1);
1349template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1387template <
typename Tlist,
class Compare>
1390 if (list.is_unitarian_or_empty())
1394 list.split_list_ne(
l,
r);
1399 if constexpr (
noexcept(
static_cast<bool>(
cmp(
l.
get_first_ne(),
r.get_first_ne()))))
1401 mergesort<Tlist, Compare>(
l,
cmp);
1402 mergesort<Tlist, Compare>(
r,
cmp);
1404 merge_lists<Tlist, Compare>(
l,
r, list,
cmp);
1409 mergesort<Tlist, Compare>(
l,
cmp);
1410 mergesort<Tlist, Compare>(
r,
cmp);
1412 merge_lists<Tlist, Compare>(
l,
r, list,
cmp);
1416 list.concat_list(
l);
1417 list.concat_list(
r);
1426template <
class Tlist,
class Tlink,
class Compare>
1434 list_insertion_sort<Tlink, Compare>(list,
cmp);
1439 list.split_list_ne(
l,
r);
1440 const size_t nl = n - n / 2;
1442 if constexpr (
noexcept(
static_cast<bool>(
cmp(
l.
get_first_ne(),
r.get_first_ne()))))
1444 __link_mergeinsertsort<Tlist, Tlink, Compare>(
l, nl,
cmp, lsz);
1445 __link_mergeinsertsort<Tlist, Tlink, Compare>(
r, n - nl,
cmp, lsz);
1446 merge_lists<Tlist, Compare>(
l,
r, list,
cmp);
1451 __link_mergeinsertsort<Tlist, Tlink, Compare>(
l, nl,
cmp, lsz);
1452 __link_mergeinsertsort<Tlist, Tlink, Compare>(
r, n - nl,
cmp, lsz);
1453 merge_lists<Tlist, Compare>(
l,
r, list,
cmp);
1457 list.concat_list(
l);
1458 list.concat_list(
r);
1481template <
template <
typename>
class Tlist,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
1485 if (list.is_unitarian_or_empty())
1488 if constexpr (std::is_same_v<Tlist<T>,
DynList<T>>)
1491 __link_mergeinsertsort<HTList, HTList, Cmp>(list, list.size(),
Cmp(
cmp), lsz);
1493 else if constexpr (std::is_same_v<Tlist<T>,
DynDlist<T>>)
1496 __link_mergeinsertsort<Dnode<T>,
Dlink,
Cmp>(list, list.size(),
Cmp(
cmp), lsz);
1500 if (list.size() <= lsz)
1502 insertion_sort<T, Compare>(list,
cmp);
1507 list.split_list(
l,
r);
1511 mergeinsertsort<Tlist, T, Compare>(
l,
cmp, lsz);
1512 mergeinsertsort<Tlist, T, Compare>(
r,
cmp, lsz);
1514 merge_lists<Tlist<T>, Compare>(
l,
r, list,
cmp);
1518 list.concat_list(
l);
1519 list.concat_list(
r);
1533template <
template <
typename>
class Tlist,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
1537 mergesort<T, Compare>(list,
cmp);
1539 mergesort<Tlist<T>, Compare>(list,
cmp);
1549template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1562template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1580template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1586 mergesort<HTList, Cmp>(list,
Cmp(
cmp));
1599template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1602 mergesort<T, Compare>(list,
cmp);
1603 return std::move(list);
1613template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1614inline long select_pivot(
T *a,
long l,
long r,
const Compare &
cmp = Compare()) noexcept(
1615 noexcept(static_cast<
bool>(
cmp(a[0], a[0])))
and std::is_nothrow_swappable_v<
T>);
1634template <typename
T, StrictWeakOrder<
T> Compare =
Aleph::
less<
T>>
1635inline
long partition(
T *a, const
long l, const
long r, const Compare &
cmp = Compare()) noexcept(
1636 noexcept(static_cast<
bool>(
cmp(a[0], a[0])))
and std::is_nothrow_swappable_v<
T>)
1641 const long p = select_pivot<T, Compare>(a,
l,
r,
cmp);
1643 std::swap(a[p], a[
r]);
1652 while (
cmp(a[++i], a[
r]))
1655 while (
cmp(a[
r], a[--j]))
1663 std::swap(a[i], a[j]);
1667 std::swap(a[i], a[
r]);
1690template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1696 const long pivot = partition<T, Compare>(a,
l,
r,
cmp);
1698 quicksort_rec<T, Compare>(a,
l, pivot - 1,
cmp);
1699 quicksort_rec<T, Compare>(a, pivot + 1,
r,
cmp);
1715template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1720 const long pivot = partition<T, Compare>(a,
l,
r,
cmp);
1721 const long left_size = pivot -
l;
1722 const long right_size =
r - pivot;
1725 if (left_size < right_size)
1727 quicksort_no_tail<T, Compare>(a,
l, pivot - 1,
cmp);
1732 quicksort_no_tail<T, Compare>(a, pivot + 1,
r,
cmp);
1754template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1760 if (
const long pivot = partition<T, Compare>(a,
l,
r,
cmp); pivot -
l <
r - pivot)
1763 quicksort_rec_min<T, Compare>(a,
l, pivot - 1,
cmp);
1764 quicksort_rec_min<T, Compare>(a, pivot + 1,
r,
cmp);
1768 quicksort_rec_min<T, Compare>(a, pivot + 1,
r,
cmp);
1769 quicksort_rec_min<T, Compare>(a,
l, pivot - 1,
cmp);
1773template <
typename T, StrictWeakOrder<T> Compare>
1775 noexcept(
static_cast<bool>(
cmp(a[0], a[0])))
and std::is_nothrow_swappable_v<T>)
1777 const long m =
l + (
r -
l) / 2;
1781 if (
cmp(a[
r], a[
l]))
1782 std::swap(a[
l], a[
r]);
1784 if (
cmp(a[
m], a[
l]))
1785 std::swap(a[
m], a[
l]);
1787 if (
cmp(a[
r], a[
m]))
1788 std::swap(a[
r], a[
m]);
1833template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
1842 const auto n =
static_cast<size_t>(
r -
l + 1);
1844 typedef std::pair<long, long> Partition;
1847 stack.
push(Partition(
l,
r));
1849 while (stack.
size() > 0)
1851 auto [fst, snd] = stack.
pop();
1852 const long diff = snd - fst;
1863 if (
const long pivot = partition<T, Compare>(a, fst, snd,
cmp); pivot - fst < snd - pivot)
1866 stack.
push(Partition(pivot + 1, snd));
1867 stack.
push(Partition(fst, pivot - 1));
1871 stack.
push(Partition(fst, pivot - 1));
1872 stack.
push(Partition(pivot + 1, snd));
1901template <
typename T,
class Compare>
1906 if (depth_limit == 0) [[unlikely]]
1917 if (
const long pivot = partition<T, Compare>(a,
l,
r,
cmp); pivot -
l <
r - pivot)
1919 introsort_loop<T, Compare>(a,
l, pivot - 1, depth_limit,
cmp);
1924 introsort_loop<T, Compare>(a, pivot + 1,
r, depth_limit,
cmp);
1938namespace pdq_detail {
1963template <
typename T>
1964using Pivot_Ref = std::conditional_t<std::is_trivially_copyable_v<T>,
const T,
const T &>;
1969template <
typename T>
1978template <
typename T,
class Compare>
1982 std::iter_swap(a, b);
1986template <
typename T,
class Compare>
2002template <
bool Guarded,
typename T,
class Compare>
2005 T item = std::move(*cur);
2011 *hole = std::move(*(hole - 1));
2014 while ((not Guarded or hole != begin)
and cmp(item, *(hole - 1)));
2018 *hole = std::move(item);
2021 *hole = std::move(item);
2030template <
bool Guarded,
typename T,
class Compare>
2035 for (
T *cur = begin + 1; cur != end; ++cur)
2036 if (
cmp(*cur, *(cur - 1)))
2037 pdq_detail::insert_back<Guarded>(begin, cur,
cmp);
2046template <
typename T,
class Compare>
2052 for (
T *cur = begin + 1; cur != end; ++cur)
2054 if (
cmp(*cur, *(cur - 1)))
2055 moves += cur - pdq_detail::insert_back<true>(begin, cur,
cmp);
2068template <
typename T>
2070 const unsigned char *offsets_r,
const long num,
const bool use_swaps)
2074 for (
long i = 0; i < num; ++i)
2075 std::iter_swap(first + offsets_l[i], last - offsets_r[i]);
2080 T *
l = first + offsets_l[0];
2081 T *
r = last - offsets_r[0];
2082 T tmp = std::move(*
l);
2084 for (
long i = 1; i < num; ++i)
2086 l = first + offsets_l[i];
2088 r = last - offsets_r[i];
2091 *
r = std::move(tmp);
2104template <
typename T,
class Compare>
2110 while (
cmp(*++first, pivot))
2114 if (first - 1 == begin)
2115 while (first < last
and not
cmp(*--last, pivot))
2118 while (not
cmp(*--last, pivot))
2121 const bool already_partitioned = first >= last;
2123 if (not already_partitioned)
2125 std::iter_swap(first, last);
2131 alignas(64)
unsigned char offsets_l[
Block_Size];
2132 alignas(64)
unsigned char offsets_r[
Block_Size];
2135 long num_l = 0, num_r = 0, start_l = 0, start_r = 0;
2136 while (first < last)
2140 const long unknown = last - first;
2141 const long left = num_l == 0 ? (num_r == 0 ? unknown / 2 : unknown) : 0;
2142 const long right = num_r == 0 ? unknown - left : 0;
2143 for (
long i = 0, n = std::min(left,
Block_Size); i < n; ++i)
2145 offsets_l[num_l] =
static_cast<unsigned char>(i);
2146 num_l +=
static_cast<long>(not
cmp(*first++, pivot));
2148 for (
long i = 1, n = std::min(right,
Block_Size); i <= n; ++i)
2150 offsets_r[num_r] =
static_cast<unsigned char>(i);
2151 num_r +=
static_cast<long>(
cmp(*--last, pivot));
2154 const long num = std::min(num_l, num_r);
2156 num, num_l == num_r);
2176 swapped += num_l + num_r;
2180 std::iter_swap(base_l + offsets_l[start_l + num_l], --last);
2185 std::iter_swap(base_r - offsets_r[start_r + num_r], first++);
2188 T *
const pivot_pos = first - 1;
2189 std::iter_swap(begin, pivot_pos);
2190 return {pivot_pos, already_partitioned, swapped};
2199template <
typename T,
class Compare>
2205 while (
cmp(*++first, pivot))
2207 if (first - 1 == begin)
2208 while (first < last
and not
cmp(*--last, pivot))
2211 while (not
cmp(*--last, pivot))
2214 const bool already_partitioned = first >= last;
2216 while (first < last)
2218 std::iter_swap(first, last);
2220 while (
cmp(*++first, pivot))
2222 while (not
cmp(*--last, pivot))
2226 T *
const pivot_pos = first - 1;
2227 std::iter_swap(begin, pivot_pos);
2228 return {pivot_pos, already_partitioned, swapped};
2238template <
typename T,
class Compare>
2244 while (
cmp(pivot, *--last))
2246 if (last + 1 == end)
2247 while (first < last
and not
cmp(pivot, *++first))
2250 while (not
cmp(pivot, *++first))
2252 while (first < last)
2254 std::iter_swap(first, last);
2255 while (
cmp(pivot, *--last))
2257 while (not
cmp(pivot, *++first))
2260 std::iter_swap(begin, last);
2279template <
typename T,
class Compare>
2285 const long size = end - begin;
2289 pdq_detail::insertion_sort_range<true>(begin, end,
cmp);
2291 pdq_detail::insertion_sort_range<false>(begin, end,
cmp);
2302 std::iter_swap(begin, begin + s2);
2309 if (not leftmost
and not
cmp(*(begin - 1), *begin))
2315 const auto [pivot_pos, already_partitioned, swapped] =
2319 const long l_size = pivot_pos - begin;
2320 const long r_size = end - (pivot_pos + 1);
2321 if (l_size <
size / 8 or r_size <
size / 8)
2326 if (--bad_allowed == 0)
2333 std::iter_swap(begin, begin + l_size / 4);
2334 std::iter_swap(pivot_pos - 1, pivot_pos - l_size / 4);
2337 std::iter_swap(begin + 1, begin + (l_size / 4 + 1));
2338 std::iter_swap(begin + 2, begin + (l_size / 4 + 2));
2339 std::iter_swap(pivot_pos - 2, pivot_pos - (l_size / 4 + 1));
2340 std::iter_swap(pivot_pos - 3, pivot_pos - (l_size / 4 + 2));
2345 std::iter_swap(pivot_pos + 1, pivot_pos + (1 + r_size / 4));
2346 std::iter_swap(end - 1, end - r_size / 4);
2349 std::iter_swap(pivot_pos + 2, pivot_pos + (2 + r_size / 4));
2350 std::iter_swap(pivot_pos + 3, pivot_pos + (3 + r_size / 4));
2351 std::iter_swap(end - 2, end - (1 + r_size / 4));
2352 std::iter_swap(end - 3, end - (2 + r_size / 4));
2356 else if (already_partitioned
2363 begin = pivot_pos + 1;
2426template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2432 for (
auto n =
static_cast<size_t>(
r -
l + 1); n > 1; n >>= 1)
2448template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2483template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2488 const auto n =
static_cast<size_t>(end - begin);
2499template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2502 const size_t n = arr.
size();
2522template <
class Compare>
2529 Dlink smaller, bigger;
2537 quicksort<Compare>(bigger,
cmp);
2538 quicksort<Compare>(smaller,
cmp);
2566template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2569 quicksort<Compare_Dnode<T, Compare>>(list,
cmp);
2576template <
typename T,
class Compare>
2587 cmp(p->get_data(), pivot->get_data()))
2592 quicksort<T, Compare>(bigger,
cmp);
2593 quicksort<T, Compare>(smaller,
cmp);
2604template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2607 quicksort<T, Compare>(
static_cast<HTList &
>(list),
cmp);
2648template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2654 const long pivot = partition<T, Compare>(a,
l,
r,
cmp);
2656 const long l_size = pivot -
l;
2657 const long r_size =
r - pivot;
2658 bool left_done =
false;
2659 bool right_done =
false;
2663 insertion_sort<T, Compare>(a,
l, pivot - 1,
cmp);
2669 insertion_sort<T, Compare>(a, pivot + 1,
r,
cmp);
2673 if (left_done
and right_done)
2678 quicksort_insertion<T, Compare>(a, pivot + 1,
r,
cmp);
2684 quicksort_insertion<T, Compare>(a,
l, pivot - 1,
cmp);
2689 if (l_size < r_size)
2691 quicksort_insertion<T, Compare>(a,
l, pivot - 1,
cmp);
2692 quicksort_insertion<T, Compare>(a, pivot + 1,
r,
cmp);
2696 quicksort_insertion<T, Compare>(a, pivot + 1,
r,
cmp);
2697 quicksort_insertion<T, Compare>(a,
l, pivot - 1,
cmp);
2734template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2736 const Compare &
cmp = Compare())
2741 const long pivot = partition<T, Compare>(a,
l,
r,
cmp);
2743 if (
cmp(x, a[pivot]))
2744 return random_search<T, Compare>(a, x,
l, pivot - 1,
cmp);
2745 if (
cmp(a[pivot], x))
2746 return random_search<T, Compare>(a, x, pivot + 1,
r,
cmp);
2759template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2761 const Compare &
cmp = Compare())
2768 if (
cmp(x, a(pivot)))
2769 return random_search<T, Compare>(a, x,
l, pivot - 1,
cmp);
2770 if (
cmp(a(pivot), x))
2771 return random_search<T, Compare>(a, x, pivot + 1,
r,
cmp);
2802template <
typename T,
class Compare>
2825 if (
cmp(item_ptr, pivot))
2826 ret_val = dlink_random_search<T, Compare>(smaller, x,
cmp);
2827 else if (
cmp(pivot, item_ptr))
2828 ret_val = dlink_random_search<T, Compare>(bigger, x,
cmp);
2834 list.
swap(&smaller);
2870template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2873 return dlink_random_search<T, Compare_Dnode<T, Compare>>(list, x,
cmp);
2905template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
2908 Dnode<T> *p = dlink_random_search<T, Compare_Dnode<T, Compare>>(list, x,
cmp);
2910 return p ==
nullptr ? nullptr : &(p->
get_data());
2913template <
typename T,
class Compare>
2916 const long pivot_idx = select_pivot<T, Compare>(a,
l,
r,
cmp);
2918 std::swap(a[pivot_idx], a[
r]);
2924 while (current <= gt)
2925 if (
cmp(a[current], a[
r]))
2927 std::swap(a[lt], a[current]);
2931 else if (
cmp(a[
r], a[current]))
2933 std::swap(a[current], a[gt]);
2940 std::swap(a[current], a[
r]);
2941 return {lt, current};
2944template <
class Container,
typename T,
class Compare>
2948 const long pivot_idx = select_pivot_op<T, Compare>(a,
l,
r,
cmp);
2950 std::swap(a(pivot_idx), a(
r));
2956 while (current <= gt)
2957 if (
cmp(a(current), a(
r)))
2959 std::swap(a(lt), a(current));
2963 else if (
cmp(a(
r), a(current)))
2965 std::swap(a(current), a(gt));
2972 std::swap(a(current), a(
r));
2973 return {lt, current};
2976template <
typename T,
class Compare>
2983 insertion_sort<T, Compare>(a,
l,
r,
cmp);
2987 const auto [lt, current] = partition_three_way<T, Compare>(a,
l,
r,
cmp);
2989 if (i >= lt
and i <= current)
3009template <
typename T,
class Compare,
typename Container>
3017 const long m =
l + (
r -
l) / 2;
3024 const T *med_ptr = Aleph::median<T, Compare>(la, ma, ra,
cmp);
3032 assert(med_ptr == &ra);
3042template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3044 const Compare &
cmp = Compare())
3046 return select_pivot_op_impl<T, Compare>(a,
l,
r,
cmp);
3054template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3056 const Compare &
cmp = Compare())
3058 return select_pivot_op_impl<T, Compare>(a,
l,
r,
cmp);
3061namespace sort_utils_detail {
3084template <
template <
typename>
class C,
typename T,
class Compare>
3089 const long m =
l + (
r -
l) / 2;
3090 if (
cmp(a(
m), a(
l)))
3091 std::swap(a(
l), a(
m));
3092 if (
cmp(a(
r), a(
m)))
3093 std::swap(a(
m), a(
r));
3094 if (
cmp(a(
m), a(
l)))
3095 std::swap(a(
l), a(
m));
3096 std::swap(a(
m), a(
r));
3108template <
typename T,
class Compare,
typename Container>
3123 while (
cmp(a(++i), a(
r)))
3126 while (
cmp(a(
r), a(--j)))
3133 std::swap(a(i), a(j));
3137 std::swap(a(i), a(
r));
3149template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3152 return partition_op_impl<T, Compare>(a,
l,
r,
cmp);
3162template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3165 return partition_op_impl<T, Compare>(a,
l,
r,
cmp);
3168template <
typename Container,
class Compare>
3171 using T =
typename Container::Item_Type;
3172 for (
long p =
l + 1; p <=
r; ++p)
3174 T key = std::move(a(p));
3176 while (j >=
l and cmp(key, a(j)))
3178 a(j + 1) = std::move(a(j));
3181 a(j + 1) = std::move(key);
3185template <
typename T,
class Compare>
3187 const Compare &
cmp = Compare())
3189 assert(i >=
l and i <=
r);
3199 const auto [lt, current] = partition_three_way_op<DynArray<T>,
T, Compare>(a,
l,
r,
cmp);
3201 if (i >= lt
and i <= current)
3213template <
typename T,
class Compare>
3215 const Compare &
cmp = Compare())
3217 assert(i >=
l and i <=
r);
3227 const auto [lt, current] = partition_three_way_op<Array<T>,
T, Compare>(a,
l,
r,
cmp);
3229 if (i >= lt
and i <= current)
3248template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3251 const auto n_sz = a.
size();
3254 const long n =
static_cast<long>(n_sz) - 1;
3257 return __random_select<T, Compare>(a, i, 0, n,
cmp);
3267template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3270 const auto n_sz = a.
size();
3273 const long n =
static_cast<long>(n_sz) - 1;
3276 return __random_select<T, Compare>(a, i, 0, n,
cmp);
3319template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3326 return __random_select<T, Compare>(a, i, 0, n - 1,
cmp);
3336template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3339 return random_select<T, Compare>(a, i, n,
cmp);
3364template <
class Compare>
3373 size_t smaller_count = 0,
3390 if (i >= smaller_count + bigger_count + 1)
3398 Dlink *ret_val =
nullptr;
3399 if (i == smaller_count)
3401 else if (i < smaller_count)
3402 ret_val = dlink_random_select<Compare>(smaller, i,
cmp);
3404 ret_val = dlink_random_select<Compare>(bigger, i - (smaller_count + 1),
cmp);
3438template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3441 return static_cast<Dnode<T> *
>(dlink_random_select<Compare_Dnode<T, Compare>>(list, i,
cmp));
3469template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3472 Dlink *link = dlink_random_select<Compare_Dnode<T, Compare>>(list, i,
cmp);
3474 auto *p =
static_cast<Dnode<T> *
>(link);
3476 return p !=
nullptr ? &(p->get_data()) :
nullptr;
3498template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3501 const long n =
static_cast<long>(a.
size());
3505 for (
long i = 0; i < n - 1; ++i)
3509 for (
long j = i + 1; j < n; ++j)
3514 std::swap(a(
min), a(i));
3548template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3551 const long n =
static_cast<long>(a.
size());
3555 for (
long i = 0; i < n - 1; ++i)
3556 for (
long j = n - 1; j > i; --j)
3557 if (
cmp(a(j), a(j - 1)))
3559 std::swap(a(j - 1), a(j));
3588template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
3591 for (
long i =
l + 1; i <=
r; i++)
3593 T tmp = std::move(a(i));
3595 for (; j >
l and cmp(tmp, a(j - 1)); --j)
3596 a(j) = std::move(a(j - 1));
3598 a(j) = std::move(tmp);
3606template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
3609 const auto n = a.size();
3649template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3652 const long n = a.
size();
3658 static constexpr long ciura_gaps[] = {1, 4, 10, 23, 57, 132,
3659 301, 701, 1750, 4024, 9233, 21223,
3660 48805, 112217, 258100, 593630, 1365513, 3140680};
3661 static constexpr int num_gaps = std::size(ciura_gaps);
3664 int gap_idx = num_gaps - 1;
3665 while (gap_idx >= 0 && ciura_gaps[gap_idx] >= n)
3669 for (; gap_idx >= 0; --gap_idx)
3671 const long h = ciura_gaps[gap_idx];
3672 for (
long i =
h; i < n; i++)
3674 T tmp = std::move(a(i));
3677 while (j >=
h and cmp(tmp, a(j -
h)))
3679 a(j) = std::move(a(j -
h));
3683 a(j) = std::move(tmp);
3700template <
typename T,
class Compare>
3704 for (
long i = n; i > 1; i = p)
3722template <
typename T,
class Compare>
3748template <
typename T,
class Compare>
3751 sift_down<T, Compare>(table, 1, n,
cmp);
3766template <
typename T,
class Compare>
3780 noexcept(
noexcept(
static_cast<bool>(std::declval<const Compare &>()(e2, e1))))
3782 return static_cast<bool>(
cmp(e2, e1));
3826template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3829 const long n = a.
size();
3837 for (
long i = n / 2; i >= 1; --i)
3841 for (
long i = n; i >= 2; --i)
3843 std::swap(a(0), a(i - 1));
3844 sift_down<T, Negate_Compare<T, Compare>>(a, 1, i - 1, nc);
3856template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
3857inline long partition(C<T> &a,
const long l,
const long r,
const Compare &
cmp = Compare())
3864 const T &pivot = a(
r);
3868 while (
cmp(a(++i), pivot))
3871 while (
cmp(pivot, a(--j)))
3878 std::swap(a(i), a(j));
3881 std::swap(a(i), a(
r));
3896template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3905 quicksort_rec<T, Compare>(a,
l, i - 1,
cmp);
3906 quicksort_rec<T, Compare>(a, i + 1,
r,
cmp);
3910 quicksort_rec<T, Compare>(a, i + 1,
r,
cmp);
3911 quicksort_rec<T, Compare>(a,
l, i - 1,
cmp);
3926template <
class Stack,
class A,
class B>
3927inline void push2(Stack &stack,
const A &a,
const B &b)
3964template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
3967 long l = 0,
r = a.
size() - 1;
3969 const size_t n = a.
size();
4006template <
typename T,
class Compare>
4012 long c = start << 1;
4045template <
typename T,
class Compare>
4048 const long n =
r -
l + 1;
4055 for (
long i = n / 2; i >= 1; --i)
4059 for (
long i = n; i >= 2; --i)
4061 std::swap(a(
l), a(
l + i - 1));
4062 sift_down_subrange<T, Negate_Compare<T, Compare>>(a, 1, i - 1,
l, nc);
4070template <
typename T,
class Compare>
4075 if (depth_limit == 0) [[unlikely]]
4078 heapsort_subrange<T, Compare>(a,
l,
r,
cmp);
4091 introsort_loop<T, Compare>(a,
l, pivot - 1, depth_limit,
cmp);
4096 introsort_loop<T, Compare>(a, pivot + 1,
r, depth_limit,
cmp);
4135template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
4138 const long n = a.
size();
4169template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
4171 const Compare &
cmp = Compare())
4173 long extreme_index =
l;
4175 for (
long i =
l + 1; i <=
r; i++)
4176 if (
cmp(a(i), a(extreme_index)))
4179 return extreme_index;
4186template <
typename T, StrictWeakOrder<T> Compare = Aleph::greater<T>>
4188 const Compare &
cmp = Compare())
4190 return search_extreme<T, Compare>(a,
l,
r,
cmp);
4238template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4245 if (
long m =
l + (
r -
l) / 2;
cmp(x, a(
m)))
4247 else if (
cmp(a(
m), x))
4272template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4279 if (
long m =
l + (
r -
l) / 2;
cmp(x, *a(
m)))
4281 else if (
cmp(*a(
m), x))
4300template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4303 const auto n = a.size();
4336template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4339 const auto n = a.size();
4359template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4363 const auto n = a.
size();
4375 for (
long i = idx - 1; i >= 0; --i)
4382 for (
long i = idx + 1; i < static_cast<long>(n); ++i)
4412template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4415 const size_t n = a.
size();
4417 for (
size_t i = 0; i < n; ++i)
4422 return cmp(*ptr1, *ptr2);
4442template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4450 const T *found_ptr = &a(idx);
4454 for (
long i = idx - 1; i >= 0; --i)
4456 const T *ptr = &a(i);
4464 for (
long i = idx + 1, n = a.size(); i < n; ++i)
4466 const T *ptr = &a(i);
4486template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4497template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4498inline const T *
bsearch(
const C<T> &a,
const T &x,
const Compare &
cmp = Compare())
4503 const T *ptr = &a(i);
4518template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4519inline T *
bsearch(
const C<T *> &a,
const T &x,
const Compare &
cmp = Compare())
4539template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4547 T *found_ptr = &a(idx);
4551 for (
long i = idx - 1; i >= 0; --i)
4561 for (
long i = idx + 1, n = a.size(); i < n; ++i)
4583template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4587 const auto n = a.
size();
4595 T *found_ptr = a(idx);
4599 for (
long i = idx - 1; i >= 0; --i)
4609 for (
long i = idx + 1, n = a.size(); i < n; ++i)
4653template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4654inline long binindex(
const C<T> &a,
const T &x,
const Compare &
cmp = Compare())
4688template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4699 const long mid = idx;
4701 for (
long i = idx - 1; i >= 0; --i)
4710 for (
long i = idx + 1, n = a.size(); i < n; ++i)
4731template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4743 for (
long i = idx - 1; i >= 0; --i)
4753 for (
long i = idx + 1, n = a.size(); i < n; ++i)
4772namespace sort_utils_detail {
4786decltype(
auto) indexed_value(
const C &c,
const size_t i)
4788 if constexpr (
requires { c(i); })
4803using Indexed_Value_Type =
4804 std::remove_cvref_t<decltype(indexed_value(std::declval<const C &>(),
size_t{}))>;
4821template <
class C,
class Compare>
4822bool stable_index_less(
const C &c,
const size_t i,
const size_t j,
const Compare &
cmp)
4824 auto &&lhs = indexed_value(c, i);
4825 auto &&rhs = indexed_value(c, j);
4883 StrictWeakOrder<sort_utils_detail::Indexed_Value_Type<C>> Compare =
4887 const size_t n = a.
size();
4889 for (
size_t i = 0; i < n; ++i)
4894 return cmp(sort_utils_detail::indexed_value(a, i), sort_utils_detail::indexed_value(a, j));
4940 StrictWeakOrder<sort_utils_detail::Indexed_Value_Type<C>> Compare =
4944 const size_t n = a.
size();
4946 for (
size_t i = 0; i < n; ++i)
4951 return sort_utils_detail::stable_index_less(a, i, j,
cmp);
4989template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
4992 const size_t n = a.
size();
4994 for (
size_t i = 0; i < n; ++i)
4999 return cmp(*ptr1, *ptr2);
5033template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
5036 const size_t n = a.
size();
5038 for (
size_t i = 0; i < n; ++i)
5043 return sort_utils_detail::stable_index_less(a, i, j,
cmp);
5047 for (
size_t i = 0; i < n; ++i)
5048 ret(i) = &a(idx(i));
5071template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
5074 const size_t n = a.
size();
5076 for (
size_t i = 0; i < n; ++i)
5081 return sort_utils_detail::stable_index_less(a, i, j,
cmp);
5085 for (
size_t i = 0; i < n; ++i)
5086 ret(i) = &a(idx(i));
5118template <
template <
typename>
class C,
typename T, StrictWeakOrder<T> Compare =
Aleph::less<T>>
5122 const size_t n = a.size();
5126 size_t l = 0,
r = n - 1;
5137 const size_t partition_size =
r -
l + 1;
5138 if (partition_size <= 1)
5141 if (partition_size <= threshold)
5147 const auto i =
static_cast<size_t>(
5148 partition_op<T, Compare>(a,
static_cast<long>(
l),
static_cast<long>(
r),
cmp));
5150 const size_t left_size = i >
l ? i -
l : 0;
5151 const size_t right_size =
r > i ?
r - i : 0;
5153 if (left_size > right_size)
5178template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
5180 const Compare &
cmp = Compare())
5185 const long m =
l + (
r -
l) / 2;
5188 return binary_search_rec<T, Compare>(a, x,
l,
m - 1,
cmp);
5190 return binary_search_rec<T, Compare>(a, x,
m + 1,
r,
cmp);
5203template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
5208 long m =
l + (
r -
l) / 2;
5211 else if (
cmp(a[
m], x))
5245template <
typename KeyFn>
5246 requires std::invocable<const KeyFn &, size_t>
and
5247 std::convertible_to<std::invoke_result_t<const KeyFn &, size_t>,
int>
5249 const int max_key,
const KeyFn &key_of)
5251 ah_domain_error_if(max_key < min_key) <<
"counting_sort_indices(): max_key < min_key";
5257 <<
"counting_sort_indices(): sa/tmp size is smaller than n";
5259 const auto range_minus_one =
static_cast<unsigned long long>(
static_cast<long long>(max_key) -
5260 static_cast<long long>(min_key));
5262 constexpr auto max_count =
5263 static_cast<unsigned long long>(std::numeric_limits<size_t>::max() - size_t(1));
5265 <<
"counting_sort_indices(): key range is too large";
5267 const auto k =
static_cast<size_t>(range_minus_one) + 1;
5269 for (
size_t i = 0; i <
k; ++i)
5272 for (
size_t i = 0; i < n; ++i)
5274 const int key = key_of(sa[i]);
5276 <<
"counting_sort_indices(): key out of expected range";
5277 const auto bucket =
static_cast<size_t>(
static_cast<unsigned long long>(
5278 static_cast<long long>(key) -
static_cast<long long>(min_key)));
5282 for (
size_t j = 1; j <
k; ++j)
5285 for (
size_t i = n; i > 0; --i)
5287 const int key = key_of(sa[i - 1]);
5288 const auto idx =
static_cast<size_t>(
static_cast<unsigned long long>(
5289 static_cast<long long>(key) -
static_cast<long long>(min_key)));
5290 tmp[--
count[idx]] = sa[i - 1];
5293 for (
size_t i = 0; i < n; ++i)
5297namespace sort_utils_detail {
5306template <
typename IntT>
5317template <
typename IntT>
5329template <
typename IntT>
5335 const U range_minus_one =
static_cast<U
>(max_key) -
static_cast<U
>(min_key);
5337 if constexpr (
sizeof(U) >=
sizeof(
size_t))
5339 constexpr size_t max_count = std::numeric_limits<size_t>::max() -
static_cast<size_t>(1);
5341 <<
"counting_sort(): key range is too large";
5344 return static_cast<size_t>(range_minus_one) + 1;
5354template <
typename IntT>
5368template <
typename IntT>
5372 return static_cast<IntT
>(
static_cast<U
>(min_key) +
static_cast<U
>(bucket));
5381template <
template <
typename>
class C,
typename IntT>
5385 const size_t n = a.size();
5389 IntT min_key = a(0), max_key = a(0);
5390 for (
size_t i = 1; i < n; ++i)
5400 for (
size_t i = 0; i <
k; ++i)
5403 for (
size_t i = 0; i < n; ++i)
5406 for (
size_t i = 1; i <
k; ++i)
5410 for (
size_t i = n; i > 0; --i)
5413 tmp[--
count[bucket]] = a(i - 1);
5416 for (
size_t i = 0; i < n; ++i)
5429template <
typename IntT>
5436 IntT min_key = a[0], max_key = a[0];
5437 for (
size_t i = 1; i < n; ++i)
5447 for (
size_t i = 0; i <
k; ++i)
5450 for (
size_t i = 0; i < n; ++i)
5453 for (
size_t i = 1; i <
k; ++i)
5457 for (
size_t i = n; i > 0; --i)
5460 tmp[--
count[bucket]] = a[i - 1];
5463 for (
size_t i = 0; i < n; ++i)
5475template <
typename IntT>
5479 const size_t n = list.
size();
5485 IntT max_key = min_key;
5489 if (
value < min_key)
5491 if (
value > max_key)
5497 for (
size_t i = 0; i <
k; ++i)
5504 for (
size_t bucket = 0; bucket <
k; ++bucket)
5506 const size_t times =
count[bucket];
5511 for (
size_t c = 0; c < times; ++c)
5527template <
typename IntT>
5531 const size_t n = list.
size();
5537 IntT max_key = min_key;
5541 if (
value < min_key)
5543 if (
value > max_key)
5549 for (
size_t i = 0; i <
k; ++i)
5556 for (
size_t bucket = 0; bucket <
k; ++bucket)
5558 const size_t times =
count[bucket];
5563 for (
size_t c = 0; c < times; ++c)
5581template <
typename IntT>
5585 if constexpr (std::is_signed_v<std::remove_cv_t<IntT>>)
5586 return static_cast<U
>(
value) ^ (U{1} << (std::numeric_limits<U>::digits - 1));
5588 return static_cast<U
>(
value);
5600template <
template <
typename>
class C,
typename IntT>
5606 const size_t n = a.size();
5613 for (
size_t pass = 0; pass <
sizeof(U); ++pass)
5615 for (
size_t i = 0; i < 256; ++i)
5618 const size_t shift = pass * 8;
5619 for (
size_t i = 0; i < n; ++i)
5621 const U key = radix_key<IntT>(a(i));
5622 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5626 for (
size_t i = 1; i < 256; ++i)
5629 for (
size_t i = n; i > 0; --i)
5631 const U key = radix_key<IntT>(a(i - 1));
5632 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5633 tmp[--
count[bucket]] = a(i - 1);
5636 for (
size_t i = 0; i < n; ++i)
5649template <
typename IntT>
5661 for (
size_t pass = 0; pass <
sizeof(U); ++pass)
5663 for (
size_t i = 0; i < 256; ++i)
5666 const size_t shift = pass * 8;
5667 for (
size_t i = 0; i < n; ++i)
5669 const U key = radix_key<IntT>(a[i]);
5670 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5674 for (
size_t i = 1; i < 256; ++i)
5677 for (
size_t i = n; i > 0; --i)
5679 const U key = radix_key<IntT>(a[i - 1]);
5680 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5681 tmp[--
count[bucket]] = a[i - 1];
5684 for (
size_t i = 0; i < n; ++i)
5697template <
typename IntT>
5703 if (
const size_t n = list.
size(); n < 2)
5708 for (
size_t i = 0; i < 256; ++i)
5711 for (
size_t pass = 0; pass <
sizeof(U); ++pass)
5713 const size_t shift = pass * 8;
5717 Slinknc *link = list.HTList::remove_first_ne();
5719 const U key = radix_key<IntT>(node->get_data());
5720 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5721 buckets[bucket].
append(link);
5724 for (
auto &bucket : buckets)
5725 list.HTList::append(bucket);
5736template <
typename IntT>
5742 if (
const size_t n = list.
size(); n < 2)
5747 for (
size_t i = 0; i < 256; ++i)
5750 for (
size_t pass = 0; pass <
sizeof(U); ++pass)
5752 const size_t shift = pass * 8;
5757 const U key = radix_key<IntT>(node->
get_data());
5758 const auto bucket =
static_cast<size_t>((key >> shift) & U{0xFF});
5759 buckets[bucket].
append(node);
5762 for (
auto &bucket : buckets)
5772template <
typename T>
5779template <
typename T>
5810template <
typename T>
5833template <
typename T>
5834 requires CountingSortable<T>
5860template <
typename T>
5861 requires CountingSortable<T>
5887template <
typename T>
5888 requires CountingSortable<T>
5909template <
typename T>
5910 requires CountingSortable<T>
5914 <<
"counting_sort(): null pointer with non-zero length";
5928template <
typename T,
size_t N>
5929 requires CountingSortable<T>
5965template <
typename T>
5966 requires RadixSortable<T>
5987template <
typename T>
5988 requires RadixSortable<T>
6005template <
typename T>
6006 requires RadixSortable<T>
6023template <
typename T>
6024 requires RadixSortable<T>
6043template <
typename T>
6044 requires RadixSortable<T>
6048 <<
"radix_sort(): null pointer with non-zero length";
6062template <
typename T,
size_t N>
6063 requires RadixSortable<T>
6073namespace bucket_sort_detail {
6077template <
typename T,
class Compare>
6079 std::is_nothrow_move_constructible_v<T>
and std::is_nothrow_move_assignable_v<T>
and
6080 noexcept(
static_cast<bool>(std::declval<const Compare &>()(std::declval<T &>(),
6081 std::declval<T &>()))))
6083 for (
size_t i = lo + 1; i < hi; ++i)
6085 T tmp = std::move(a[i]);
6087 while (j > lo
and static_cast<bool>(
cmp(tmp, a[j - 1])))
6089 a[j] = std::move(a[j - 1]);
6092 a[j] = std::move(tmp);
6106template <
typename T,
class Compare,
class BucketKey>
6117 for (
size_t i = 0; i < num_buckets; ++i)
6120 for (
size_t i = 0; i < n; ++i)
6122 const size_t b = bucket_key(a[i]);
6124 <<
" which is >= num_buckets (" << num_buckets <<
")";
6131 for (
size_t i = 1; i < num_buckets; ++i)
6132 offsets[i] = offsets[i - 1] + counts[i - 1];
6137 for (
size_t i = 0; i < num_buckets; ++i)
6140 for (
size_t i = 0; i < n; ++i)
6142 const size_t b = bucket_key(a[i]);
6143 tmp[offsets[b] + counts[b]] = std::move(a[i]);
6148 for (
size_t b = 0; b < num_buckets; ++b)
6153 for (
size_t i = 0; i < n; ++i)
6154 a[i] = std::move(tmp[i]);
6165template <
typename T,
class Container,
class SortFn,
class Compare>
6168 const size_t n = a.size();
6174 for (
auto it = a.get_it(); it.has_curr(); it.next_ne())
6175 tmp[i++] = std::move(it.get_curr_ne());
6177 sort_fn(&tmp[0], n,
cmp);
6180 for (
auto it = a.get_it(); it.has_curr(); it.next_ne())
6181 it.get_curr_ne() = std::move(tmp[i++]);
6185template <auto SortFn>
6188 template <
class Compare>
6191 return [](
auto *p,
size_t n,
const Compare &c)
6199template <
class BucketKey>
6205 template <
class Compare>
6208 return [
this](
auto *p,
size_t n,
const Compare &c)
6264template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>,
class BucketKey>
6265void bucket_sort(
T *a,
const size_t n,
const size_t num_buckets,
const BucketKey &bucket_key,
6266 const Compare &
cmp = Compare())
6269 <<
"bucket_sort(): null pointer with non-zero length";
6307template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
6308 requires std::floating_point<T>
6312 <<
"bucket_sort(): null pointer with non-zero length";
6317 T min_val = a[0], max_val = a[0];
6318 for (
size_t i = 0; i < n; ++i)
6320 if (not std::isfinite(a[i])) [[unlikely]]
6331 const T range = max_val - min_val;
6339 if constexpr (std::is_same_v<Compare, Aleph::less<T>> or std::is_same_v<Compare, Aleph::greater<T>>)
6341 const bool descending =
cmp(max_val, min_val)
and not
cmp(min_val, max_val);
6342 const size_t num_buckets = n;
6343 auto key = [min_val,
range, num_buckets, descending](
const T &val) ->
size_t
6345 auto b =
static_cast<size_t>((val - min_val) /
range *
static_cast<T>(num_buckets - 1));
6346 if (b >= num_buckets)
6347 b = num_buckets - 1;
6350 b = (num_buckets - 1) - b;
6377template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
6378 requires std::floating_point<T>
6381 bucket_sort_detail::apply_sort_with_temp<T>(
6397template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
6398 requires std::floating_point<T>
6416template <
typename T,
size_t N, StrictWeakOrder<T> Compare = Aleph::less<T>>
6417 requires std::floating_point<T>
6435template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
6436 requires std::floating_point<T>
6439 bucket_sort_detail::apply_sort_with_temp<T>(
6458template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>,
class BucketKey>
6460 const Compare &
cmp = Compare())
6462 bucket_sort_detail::apply_sort_with_temp<T>(
6478template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>,
class BucketKey>
6480 const Compare &
cmp = Compare())
6491namespace timsort_detail {
6512template <
typename T>
6513void reverse_range(
T *a,
size_t lo,
size_t hi)
noexcept(std::is_nothrow_swappable_v<T>)
6517 std::swap(a[lo], a[hi - 1]);
6531template <
typename T,
class Compare>
6537 size_t run_hi = lo + 1;
6538 if (
cmp(a[run_hi], a[lo]))
6540 while (run_hi < hi
and cmp(a[run_hi], a[run_hi - 1]))
6545 while (run_hi < hi
and not
cmp(a[run_hi], a[run_hi - 1]))
6558template <
typename T,
class Compare>
6560 T *a,
const size_t lo,
const size_t hi,
const size_t start,
6561 const Compare &
cmp)
noexcept(std::is_nothrow_move_constructible_v<T>
and
6562 std::is_nothrow_move_assignable_v<T>
and
6563 noexcept(
static_cast<bool>(std::declval<const Compare &>()(
6564 std::declval<T &>(), std::declval<T &>()))))
6567 size_t i = (start <= lo) ? lo + 1 : start;
6570 T pivot = std::move(a[i]);
6575 while (left < right)
6577 const size_t mid = left + (right - left) / 2;
6578 if (
cmp(pivot, a[mid]))
6585 for (
size_t p = i; p > left; --p)
6586 a[p] = std::move(a[p - 1]);
6587 a[left] = std::move(pivot);
6608template <
typename T,
class Compare>
6610 const T &key,
const T *a,
const size_t len,
const size_t hint,
6611 const Compare &
cmp)
noexcept(
noexcept(
static_cast<bool>(std::declval<const Compare &>()(
6612 std::declval<const T &>(), std::declval<const T &>()))))
6614 size_t last_ofs = 0;
6617 if (
cmp(a[hint], key))
6619 const size_t max_ofs = len - hint;
6620 while (ofs < max_ofs
and static_cast<bool>(
cmp(a[hint + ofs], key)))
6623 ofs = (ofs << 1) + 1;
6632 const size_t max_ofs = hint + 1;
6633 while (ofs < max_ofs
and not
static_cast<bool>(
cmp(a[hint - ofs], key)))
6636 ofs = (ofs << 1) + 1;
6640 const size_t tmp = last_ofs;
6644 last_ofs = hint - ofs;
6650 while (last_ofs < ofs)
6652 const size_t mid = last_ofs + (ofs - last_ofs) / 2;
6653 if (
cmp(a[mid], key))
6677template <
typename T,
class Compare>
6679 const T &key,
const T *a,
const size_t len,
const size_t hint,
6680 const Compare &
cmp)
noexcept(
noexcept(
static_cast<bool>(std::declval<const Compare &>()(
6681 std::declval<const T &>(), std::declval<const T &>()))))
6683 size_t last_ofs = 0;
6686 if (
cmp(key, a[hint]))
6688 const size_t max_ofs = hint + 1;
6689 while (ofs < max_ofs
and static_cast<bool>(
cmp(key, a[hint - ofs])))
6692 ofs = (ofs << 1) + 1;
6696 const size_t tmp = last_ofs;
6700 last_ofs = hint - ofs;
6705 const size_t max_ofs = len - hint;
6706 while (ofs < max_ofs
and not
static_cast<bool>(
cmp(key, a[hint + ofs])))
6709 ofs = (ofs << 1) + 1;
6718 while (last_ofs < ofs)
6720 const size_t mid = last_ofs + (ofs - last_ofs) / 2;
6721 if (
cmp(key, a[mid]))
6735template <
typename T,
class Compare>
6736void merge_lo(
T *a,
const size_t base1,
const size_t len1,
const size_t base2,
const size_t len2,
6737 T *tmp,
const Compare &
cmp)
6740 for (
size_t i = 0; i < len1; ++i)
6741 tmp[i] = std::move(a[base1 + i]);
6745 size_t dest = base1;
6746 const size_t end1 = len1;
6747 const size_t end2 = base2 + len2;
6749 while (c1 < end1
and c2 < end2)
6750 if (
cmp(a[c2], tmp[c1]))
6751 a[dest++] = std::move(a[c2++]);
6753 a[dest++] = std::move(tmp[c1++]);
6757 a[dest++] = std::move(tmp[c1++]);
6765template <
typename T,
class Compare>
6766void merge_hi(
T *a,
const size_t base1,
const size_t len1,
const size_t base2,
const size_t len2,
6767 T *tmp,
const Compare &
cmp)
6770 for (
size_t i = 0; i < len2; ++i)
6771 tmp[i] = std::move(a[base2 + i]);
6774 size_t dest = base2 + len2;
6775 size_t c1 = base1 + len1;
6778 while (c1 > base1
and c2 > 0)
6779 if (
cmp(tmp[c2 - 1], a[c1 - 1]))
6780 a[--dest] = std::move(a[--c1]);
6782 a[--dest] = std::move(tmp[--c2]);
6786 a[--dest] = std::move(tmp[--c2]);
6795template <
typename T,
class Compare>
6796void merge_at(
T *a,
size_t *run_base,
size_t *run_len,
size_t &stack_size,
const size_t at,
T *tmp,
6799 size_t base1 = run_base[at];
6800 size_t len1 = run_len[at];
6801 size_t base2 = run_base[at + 1];
6802 size_t len2 = run_len[at + 1];
6805 run_len[at] = len1 + len2;
6806 if (at == stack_size - 3)
6808 run_base[at + 1] = run_base[at + 2];
6809 run_len[at + 1] = run_len[at + 2];
6821 len2 =
gallop_left(a[base1 + len1 - 1], a + base2, len2, len2 - 1,
cmp);
6838template <
typename T,
class Compare>
6842 while (stack_size > 1)
6844 long n =
static_cast<long>(stack_size) - 2;
6845 if ((n > 0
and run_len[n - 1] <= run_len[n] + run_len[n + 1]) or
6846 (n > 1
and run_len[n - 2] <= run_len[n - 1] + run_len[n]))
6848 if (run_len[n - 1] < run_len[n + 1])
6850 merge_at(a, run_base, run_len, stack_size, n, tmp,
cmp);
6852 else if (run_len[n] <= run_len[n + 1])
6853 merge_at(a, run_base, run_len, stack_size, n, tmp,
cmp);
6860template <
typename T,
class Compare>
6864 while (stack_size > 1)
6866 long n =
static_cast<long>(stack_size) - 2;
6867 if (n > 0
and run_len[n - 1] < run_len[n + 1])
6869 merge_at(a, run_base, run_len, stack_size, n, tmp,
cmp);
6887template <
typename T,
class Compare>
6905 constexpr size_t MAX_STACK = 85;
6906 size_t run_base_arr[MAX_STACK];
6907 size_t run_len_arr[MAX_STACK];
6908 size_t stack_size = 0;
6913 T *tmp = &tmp_buf[0];
6916 size_t remaining = n;
6918 while (remaining > 0)
6924 if (run_len < minrun)
6926 const size_t force = remaining < minrun ? remaining : minrun;
6933 <<
"timsort: run stack overflow (stack_size=" << stack_size <<
" >= MAX_STACK=" << MAX_STACK
6934 <<
"); this should never happen";
6935 run_base_arr[stack_size] = lo;
6936 run_len_arr[stack_size] = run_len;
6943 remaining -= run_len;
6978template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7005template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7009 <<
"timsort(): negative bounds [" <<
l <<
", " <<
r <<
"] are not allowed";
7015 <<
"timsort(): null pointer contract violation for range [" <<
l <<
", " <<
r
7016 <<
"]; cannot call timsort_detail::timsort_impl()";
7031template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7034 bucket_sort_detail::apply_sort_with_temp<T>(
7050template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7068template <
typename T,
size_t N, StrictWeakOrder<T> Compare = Aleph::less<T>>
7069 requires std::is_invocable_r_v<bool, Compare, const T &, const T &>
7087template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7090 bucket_sort_detail::apply_sort_with_temp<T>(
7109template <
typename T, StrictWeakOrder<T> Compare = Aleph::less<T>>
7112 bucket_sort_detail::apply_sort_with_temp<T>(
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_logic_error()
Throws std::logic_error unconditionally.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
#define ah_out_of_range_error()
Throws std::out_of_range unconditionally.
STL-compatible iterator adapters for Aleph containers.
Functional programming utilities for Aleph-w containers.
General utility functions and helpers.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & base()
Return a reference to the first element of array.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Helper class to compare nodes of a linked list.
bool operator()(Tlink *l1, Tlink *l2) const noexcept(noexcept(static_cast< bool >(std::declval< const Compare & >()(std::declval< Tnode< T > * >() ->get_data(), std::declval< Tnode< T > * >() ->get_data()))))
Compares two nodes based on their data.
Compare_Tnode(Compare cmp_fct=Compare()) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Construct from a comparison functor.
Read-only view of an Aleph iterator: the iterator of a constant container.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
auto get_curr() const noexcept(noexcept(std::declval< I & >().get_curr())) -> const_iterator_detail::const_item_t< decltype(std::declval< I & >().get_curr())>
The current item, read-only.
auto next_ne() noexcept(noexcept(std::declval< I & >().next_ne())) -> decltype(std::declval< I & >().next_ne())
next() without checks.
bool has_curr() const noexcept
Return true if the iterator has current item.
Doubly linked circular list node.
constexpr bool is_unitarian_or_empty() const noexcept
Return true if this (as header node) has zero or one element.
constexpr Dlink *& get_last() const noexcept
If this is a header node, it return the last node of this
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
void append(Dlink *node) noexcept
Insert node before this.
void swap(Dlink *link) noexcept
Swap this with list whose header is link.
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
void insert(Dlink *node) noexcept
Insert node after this.
Dlink *& get_prev() const noexcept
Return the link that is before this
void concat_list(Dlink *head) noexcept
Concatenate list head to list this
Dlink * remove_next() noexcept
Remove the item that is after this
Node belonging to a double circular linked list with header node.
Dnode< T > * remove_first_ne() noexcept
Remove the first node and return its address.
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
size_t size() const noexcept
Return the current dimension of array.
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
T & get_curr_ne() const noexcept
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Iterator on the items of list.
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T & get_first_ne() noexcept
Return the first item of the list without exception.
T & insert(const T &item)
T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
See remove_ne().
size_t size() const noexcept
Return the number of items of the list, in constant time.
size_t size() const noexcept
Return the number of elements stored in the stack.
bool is_empty() const noexcept
Return true if stack is empty.
T pop() noexcept
Pop by moving the top of stack.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Slinknc * get_curr() const
bool has_curr() const noexcept
Single linked list of nodes.
Slinknc * get_first() const noexcept
constexpr bool is_empty() const noexcept
void concat_list(HTList &l) noexcept
See concat(HTList & l).
void append(Slinknc *link) noexcept
Slinknc * remove_first_ne() noexcept
Remove the header item from the list.
void insert(Slinknc *link) noexcept
Slinknc * get_last() const noexcept
Return the last item of the list (nullptr if the list is empty)
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Number Theoretic Transform over Z / MOD Z.
Comparator wrapper that inverts the comparison order.
bool operator()(const T &e1, const T &e2) const noexcept(noexcept(static_cast< bool >(std::declval< const Compare & >()(e2, e1))))
Compare in reverse order: returns cmp(e2, e1).
Negate_Compare(Compare __cmp=Compare()) noexcept(std::is_nothrow_copy_assignable_v< Compare >)
Construct from a comparator.
Link of a single linked list non-circular and without header node.
void insert(Slinknc *p) noexcept
insert(p) inserts the node pointed by p after this.
Value concept accepted by counting sort overloads.
Integral value accepted by linear-time integer sorting algorithms.
Value concept accepted by radix sort overloads.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
void insertion_sort_range(T *a, const size_t lo, const size_t hi, const Compare &cmp) noexcept(std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_assignable_v< T > and noexcept(static_cast< bool >(std::declval< const Compare & >()(std::declval< T & >(), std::declval< T & >()))))
Insertion sort a sub-array [lo, hi).
void apply_sort_with_temp(Container &a, SortFn sort_fn, const Compare &cmp)
Helper to sort any container by copying to a temporary array.
void bucket_sort_impl(T *a, const size_t n, const size_t num_buckets, const BucketKey &bucket_key, const Compare &cmp)
Core bucket sort implementation on a raw pointer range.
void insertion_sort_range(T *begin, T *end, const Compare &cmp)
Sort [begin, end) by insertion.
Partitioned< T > partition_by_blocks(T *begin, T *end, const Compare &cmp)
Partition [begin, end) around the pivot *begin by blocks: the items less than the pivot go before it,...
void sort_loop(T *begin, T *end, const Compare &cmp, int bad_allowed, bool leftmost, bool by_blocks)
Sort [begin, end): the loop of introsort().
T * partition_left(T *begin, T *end, const Compare &cmp)
Partition [begin, end) around the pivot *begin when it equals the item before begin,...
constexpr long Ninther_Threshold
Ranges longer than this one take their pivot from nine items.
constexpr long Few_Swaps_Ratio
A partition that swaps fewer than one item in this many of its range leaves its subranges to the part...
std::conditional_t< std::is_trivially_copyable_v< T >, const T, const T & > Pivot_Ref
The pivot as a partition reads it: a copy of a trivially copyable item, which the compiler can keep i...
void sort2(T *a, T *b, const Compare &cmp)
Swap *a and *b if *b is less than *a.
bool partial_insertion_sort_range(T *begin, T *end, const Compare &cmp)
Sort [begin, end) by insertion, unless that takes more than Partial_Insertion_Limit moves.
void swap_offsets(T *first, T *last, const unsigned char *offsets_l, const unsigned char *offsets_r, const long num, const bool use_swaps)
Move the num pairs of items on the wrong sides, at first + offsets_l[i] and at last - offsets_r[i].
constexpr long Block_Size
Items that a partition by blocks compares at a time.
constexpr long Insertion_Threshold
Ranges shorter than this one are sorted by insertion.
void sort3(T *a, T *b, T *c, const Compare &cmp)
Sort *a, *b and *c.
constexpr long Partial_Insertion_Limit
Moves after which the insertion sort of a range that a partition found in order gives up.
T * insert_back(T *begin, T *cur, const Compare &cmp)
Move *cur, which is less than *(cur - 1), back into the sorted range [begin, cur),...
Partitioned< T > partition_by_branches(T *begin, T *end, const Compare &cmp)
Partition [begin, end) around the pivot *begin with branches, as partition_by_blocks() does by blocks...
void radix_sort_impl(C< IntT > &a)
Internal radix-sort implementation for array-like containers.
void median_of_three_to_last(C< T > &a, const long l, const long r, const Compare &cmp)
Put in order a(l), a(m) and a(r), with m the middle of [l, r], and move their median to a(r),...
constexpr radix_unsigned_t< IntT > radix_key(const IntT value) noexcept
Map an integer value to an unsigned radix key.
IntT counting_value_from_bucket(const size_t bucket, const IntT min_key) noexcept
Retrieve the original value from a bucket index in counting sort.
std::make_unsigned_t< std::remove_cv_t< IntT > > counting_unsigned_t
Unsigned counterpart used to measure counting-sort key ranges.
constexpr long Median_Of_Three_Min_Size
Ranges with fewer items than this one keep their last item as the pivot of partition(): there the med...
std::make_unsigned_t< std::remove_cv_t< IntT > > radix_unsigned_t
Unsigned key type used by radix sort passes.
size_t counting_bucket(const IntT value, const IntT min_key) noexcept
Map a value to its bucket index in counting sort.
size_t counting_bucket_count(const IntT min_key, const IntT max_key)
Compute the number of buckets required for a counting sort range.
void counting_sort_impl(C< IntT > &a)
Internal implementation of counting sort for containers.
void merge_force_collapse(T *a, size_t *run_base, size_t *run_len, size_t &stack_size, T *tmp, const Compare &cmp)
Force-merge all remaining runs at the end.
void merge_at(T *a, size_t *run_base, size_t *run_len, size_t &stack_size, const size_t at, T *tmp, const Compare &cmp)
Merge runs[at] with runs[at+1].
void binary_insertion_sort(T *a, const size_t lo, const size_t hi, const size_t start, const Compare &cmp) noexcept(std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_assignable_v< T > and noexcept(static_cast< bool >(std::declval< const Compare & >()(std::declval< T & >(), std::declval< T & >()))))
Binary insertion sort on [lo, hi) starting from start.
size_t gallop_left(const T &key, const T *a, const size_t len, const size_t hint, const Compare &cmp) noexcept(noexcept(static_cast< bool >(std::declval< const Compare & >()(std::declval< const T & >(), std::declval< const T & >()))))
Gallop left: find the insertion point for a key in a sorted range.
void timsort_impl(T *a, const size_t n, const Compare &cmp)
Core Timsort implementation.
size_t count_run_and_make_ascending(T *a, const size_t lo, const size_t hi, const Compare &cmp)
Find the length of the natural run starting at lo.
void merge_hi(T *a, const size_t base1, const size_t len1, const size_t base2, const size_t len2, T *tmp, const Compare &cmp)
Merge two adjacent sorted runs where len1 >= len2.
void merge_collapse(T *a, size_t *run_base, size_t *run_len, size_t &stack_size, T *tmp, const Compare &cmp)
Maintain the timsort run stack invariants.
void merge_lo(T *a, const size_t base1, const size_t len1, const size_t base2, const size_t len2, T *tmp, const Compare &cmp)
Merge two adjacent sorted runs a[base1..base1+len1) and a[base2..base2+len2) using temporary buffer.
constexpr size_t compute_minrun(size_t n) noexcept
Compute the minimum run length for timsort.
void reverse_range(T *a, size_t lo, size_t hi) noexcept(std::is_nothrow_swappable_v< T >)
Reverse elements in [lo, hi).
size_t gallop_right(const T &key, const T *a, const size_t len, const size_t hint, const Compare &cmp) noexcept(noexcept(static_cast< bool >(std::declval< const Compare & >()(std::declval< const T & >(), std::declval< const T & >()))))
Gallop right: find the insertion point for a key in a sorted range.
Main namespace for Aleph-w library functions.
Array< const T * > build_index_ptr(const C< T > &a, const Compare &cmp=Compare())
Build an index array of pointers for indirect sorting (const version).
DynList< size_t > binary_search_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
Binary search for all occurrences of a value.
and std::convertible_to< std::invoke_result_t< const KeyFn &, size_t >, int > void counting_sort_indices(Array< size_t > &sa, Array< size_t > &tmp, const size_t n, const int min_key, const int max_key, const KeyFn &key_of)
Stable counting sort on an index array by integer keys.
void insertion_sort(T *a, const long l, const long r, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(std::declval< T & >(), std::declval< T & >()))) and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_assignable_v< T >)
Sort an array using insertion sort.
long search_min(T *a, const long l, const long r, const Compare &cmp=Compare())
Returns the smallest element of the array a between l and r.
bool is_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Check if a container is sorted in ascending order.
static std::pair< long, long > partition_three_way(T *a, long l, long r, const Compare &cmp)
Array< const T * > stable_build_index_ptr(const C< T > &a, const Compare &cmp=Compare())
Build a stable index array of pointers for indirect sorting (const version).
static void insertion_sort_range(Container &a, long l, long r, const Compare &cmp)
long select_pivot_op_impl(const Container &a, const long l, const long r, const Compare &cmp)
Implementation of pivot selection using operator().
std::pair< bool, size_t > search_inversion(const Container< T > &cont, const Compare &cmp=Compare())
Find the first inversion in a container.
void heapsort_subrange(DynArray< T > &a, const long l, const long r, const Compare &cmp)
Heapsort implementation for a subrange of a DynArray.
size_t introsort_depth_limit(size_t n) noexcept
Forward declaration.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
long select_pivot_op(const DynArray< T > &a, const long l, const long r, const Compare &cmp=Compare())
Selects a pivot element as the median between the ends and the center.
Dnode< T > * dlink_random_search(Dlink &list, const T &x, const Compare &cmp=Compare())
Random search for an element in a dlink list.
Array< size_t > stable_build_index(const C &a, const Compare &cmp=Compare())
Build a stable index array for indirect sorting.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
void sift_down_subrange(DynArray< T > &a, long start, const long n, const long offset, const Compare &cmp)
Sift down operation for heapsort on a DynArray subrange.
void quicksort_no_tail(T *a, long l, long r, const Compare &cmp=Compare())
Quicksort implementation with tail-recursion optimization.
long random_search(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Random search for an element in an array.
size_t Quicksort_Threshold
Threshold for using quicksort vs simpler algorithms.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
const int Not_Found
Return value for search functions when element is not found.
void counting_sort(DynArray< T > &a)
Stable counting sort for integral DynArray values.
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > partition(const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
Partition a container into two based on a predicate.
std::pair< bool, size_t > test_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Test if a container is sorted, returning the inversion position.
void introsort_loop(T *a, long l, long r, size_t depth_limit, const Compare &cmp)
Quicksort loop with a depth limit, after which heapsort sorts the range: the loop of introsort() befo...
long search_max(T *a, const long l, const long r, const Compare &cmp=Compare())
Returns the maximum element of the array a between l and r.
void timsort(T *a, const size_t n, const Compare &cmp=Compare())
Timsort — adaptive, stable, natural merge sort.
void mergeinsertsort(Tlist< T > &list, const Compare &cmp=Compare(), const size_t lsz=Aleph::Insertion_Threshold)
Sort a list by mergesort combined with the insert method.
void insert_sorted(Dlink &list, Dlink *p, const Compare &cmp)
Inserts a node orderly into a doubly linked list.
Dlink * dlink_random_select(Dlink &list, const size_t i, const Compare &cmp=Compare())
Random selection of the ith element from a list based on Dlink.
std::decay_t< typename HeadC::Item_Type > T
T * bsearch(C< T > &a, const T &x, const Compare &cmp=Compare())
Search for a value in a sorted container returning a pointer.
Array< size_t > build_index(const C &a, const Compare &cmp=Compare())
Build an index array for indirect sorting.
void list_insertion_sort(ListType &list, const Compare &cmp)
Generic insertion sort for linked lists.
const T & random_select(DynArray< T > &a, const long i, const Compare &cmp=Compare())
Select the i-th smallest element in a DynArray.
void bucket_sort(T *a, const size_t n, const size_t num_buckets, const BucketKey &bucket_key, const Compare &cmp=Compare())
Bucket sort with user-supplied bucket mapping.
and
Check uniqueness with explicit hash + equality functors.
static long back_index(const long i) noexcept
Convert a 1-based heap index to a 0-based array index.
void bubble_sort(DynArray< T > &a, const Compare &cmp=Compare())
Sort a dynamic array using bubble sort.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
void radix_sort(DynArray< T > &a)
LSD radix sort for integral DynArray values.
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
void shellsort(DynArray< T > &a, const Compare &cmp=Compare())
Sort a dynamic array using Shell sort.
void selection_sort(T *a, const size_t n, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(a[0], a[0]))) and std::is_nothrow_swappable_v< T >)
Sort an array using the selection sort algorithm.
bool are_equals(const T &op1, const T &op2, Compare &&cmp=Compare())
Determines if operands are equal using a comparison operator.
void __link_mergeinsertsort(Tlist &list, const size_t n, const Compare &cmp, const size_t lsz)
static std::pair< long, long > partition_three_way_op(Container &a, long l, long r, const Compare &cmp)
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
void quicksort_rec_min(T *a, const long l, const long r, const Compare &cmp=Compare())
Sorts an array according to the quicksort method with minimum space consumption.
long sequential_search(T *a, const T &x, const long l, const long r, Equal eq=Equal())
Linear search for an element in an array.
Link * search_extreme(const Link &list, const Compare &cmp)
Find the extreme (minimum or maximum) element in a linked list.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
static const T & __random_select(T *a, const long i, long l, long r, const Compare &cmp)
void quicksort_rec(T *a, const long l, const long r, const Compare &cmp=Compare())
Recursively sort an array using quicksort.
DynList< long > binindex_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
Returns the indices of all occurrences of a value in a sorted container.
bool is_inversely_sorted(const Container< T > &cont, const Compare &cmp=Compare())
Check if a container is sorted in descending order.
DynList< const T * > bsearch_dup(const C< T > &a, const T &x, const Compare &cmp=Compare())
Search for all occurrences of a value returning pointers (const version).
long partition_op_impl(Container &a, const long l, const long r, const Compare &cmp)
Implementation of partitioning using operator().
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
long select_pivot(T *a, long l, long r, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(a[0], a[0]))) and std::is_nothrow_swappable_v< T >)
Select a pivot for partitioning using median-of-three.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
long binindex(const C< T > &a, const T &x, const Compare &cmp=Compare())
Returns the index where a value appears (or should be inserted) in a sorted container.
void quicksort_insertion(T *a, const long l, const long r, const Compare &cmp=Compare())
Sorts an array by the improved quicksort method.
size_t Insertion_Threshold
Threshold for switching to insertion sort in hybrid algorithms.
long binary_search_rec(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Recursive binary search on an ordered array.
long partition_op(DynArray< T > &a, const long l, const long r, const Compare &cmp=Compare())
Partition a DynArray using operator().
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
void merge_lists(Tlist &l1, Tlist &l2, Tlist &result, const Compare &cmp=Compare())
Merge two sorted lists into a single sorted list.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void push2(Stack &stack, const A &a, const B &b)
Push two values onto a stack.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
Comparator specialization for Dnode objects.
Compare_Dnode(const Compare &cmp=Compare()) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Factory for bucket_sort with extra parameters.
const BucketKey & bucket_key
auto operator()(const Compare &) const
Factory for lambdas that forward to a sort function.
auto operator()(const Compare &) const
What a partition did: the place of the pivot, whether the range was already partitioned,...
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Fixed-capacity binary heap and heapsort algorithms.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.