137#define List_Sort(List) \
145 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
146 List<T> sort(const List<T> & c, Cmp & cmp) \
148 List<T> ret_val = c; \
149 mergesort<List, T, Cmp>(ret_val, cmp); \
155 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
156 List<T> sort(const List<T> & c, Cmp && cmp = Cmp()) \
158 return sort<T, Cmp>(c, cmp); \
168 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
169 List<T> sort(List<T> && c, Cmp & cmp) \
171 mergesort<List, T, Cmp>(c, cmp); \
172 return std::move(c); \
177 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
178 List<T> sort(List<T> && c, Cmp && cmp = Cmp()) \
180 return sort<T, Cmp>(std::move(c), cmp); \
190 template <typename T, class Cmp = Aleph::less<T>> inline \
191 List<T> & in_place_sort(List<T> & c, Cmp & cmp) \
193 mergeinsertsort(c, cmp); \
199 template <typename T, class Cmp = Aleph::less<T>> inline \
200 List<T> & in_place_sort(List<T> & c, Cmp && cmp = Cmp()) \
202 return in_place_sort<T, Cmp>(c, cmp); \
225 template<
typename T,
class Cmp = Aleph::less<T>>
246 template<
typename T,
class Cmp = Aleph::less<T>>
257 template<
typename T,
class Cmp = Aleph::less<T>>
269 template<
typename T,
class Cmp = Aleph::less<T>>
298 class Cmp = std::less<typename Container::value_type>>
319 template<
typename T,
class Cmp = Aleph::less<T>>
330 template<
typename T,
class Cmp = Aleph::less<T>>
350 template<
typename T,
template <
typename>
class C>
353 using P = std::pair<T, size_t>;
359 const size_t n = c.
size();
362 for (
size_t i = 0; i < n; ++i)
366 return c(
i1) < c(
i2);
370 for (
size_t i = 0; i < n; ++i)
376 template<
template <
typename Type>
class List>
382 c.for_each([&items, &n, &
indexes](
auto k)
389 return items(
i1) < items(
i2);
393 for (
size_t i = 0; i < n; ++i)
401 const size_t n = c.size();
404 for (
size_t i = 0; i < n; ++i)
408 return c(
i1) < c(
i2);
412 for (
size_t i = 0; i < n; ++i)
415 ret(idx) =
P(c(idx), i);
421 template<
template <
typename Type>
class List>
427 c.for_each([&items, &n, &
indexes](
auto k)
434 return items(
i1) < items(
i2);
438 for (
size_t i = 0; i < n; ++i)
441 ret(idx) =
P(items(idx), i);
456 using P = std::pair<T, size_t>;
462 const size_t n = c.
size();
464 for (
size_t i = 0; i < n; ++i)
468 return c(
i1) < c(
i2);
472 for (
size_t i = 0; i < n; ++i)
480 const size_t n = c.
size();
482 for (
size_t i = 0; i < n; ++i)
486 return c(
i1) < c(
i2);
490 for (
size_t i = 0; i < n; ++i)
493 ret(idx) =
P(c(idx), i);
611 return func.list_pair_ranks(
l);
624 return func.list_pair_ranks(
l);
667 template<
typename C,
typename...
Args,
668 typename Cmp = std::less<typename C::value_type>>
672 const size_t n = first.size();
678 <<
"all arrays must have the same size";
680 std::vector<size_t>
indices(n);
681 std::iota(
indices.begin(),
indices.end(),
static_cast<size_t>(0));
685 return cmp(first[
i1], first[
i2]);
711 for (
size_t i = 0; i < n; ++i)
714 const size_t j =
perm[i];
715 swap(first[i], first[j]);
721 template<
typename C,
typename...
Args,
722 typename Cmp = std::less<typename C::value_type>>
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic singly linked list with functional programming support.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
auto pair_ranks(const Array< T > &c)
Computes (value, rank) pairs for each element in an Array.
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
DynList< std::pair< size_t, typename Container::Key_Type > > indexes(const Container &c)
Return pairs of (index, key).
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Container stdsort(const Container &c, Cmp cmp=Cmp())
Sorts an STL-compatible container using std::sort.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
void in_place_multisort_arrays(Cmp cmp, const bool stable, C &first, Args &... args)
Sorts multiple arrays in place, using the first array as the key.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
Dynamic doubly linked list implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.