144#define List_Sort(List) \
152 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
153 List<T> sort(const List<T> & c, Cmp & cmp) \
155 List<T> ret_val = c; \
156 mergesort<List, T, Cmp>(ret_val, cmp); \
162 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
163 List<T> sort(const List<T> & c, Cmp && cmp = Cmp()) \
165 return sort<T, Cmp>(c, cmp); \
175 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
176 List<T> sort(List<T> && c, Cmp & cmp) \
178 mergesort<List, T, Cmp>(c, cmp); \
179 return std::move(c); \
184 template <typename T, class Cmp = Aleph::less<T>> [[nodiscard]] inline \
185 List<T> sort(List<T> && c, Cmp && cmp = Cmp()) \
187 return sort<T, Cmp>(std::move(c), cmp); \
197 template <typename T, class Cmp = Aleph::less<T>> inline \
198 List<T> & in_place_sort(List<T> & c, Cmp & cmp) \
200 mergeinsertsort(c, cmp); \
206 template <typename T, class Cmp = Aleph::less<T>> inline \
207 List<T> & in_place_sort(List<T> & c, Cmp && cmp = Cmp()) \
209 return in_place_sort<T, Cmp>(c, cmp); \
232 template<
typename T,
class Cmp = Aleph::less<T>>
253 template<
typename T,
class Cmp = Aleph::less<T>>
264 template<
typename T,
class Cmp = Aleph::less<T>>
276 template<
typename T,
class Cmp = Aleph::less<T>>
305 class Cmp = std::less<typename Container::value_type>>
310 std::sort(ret.begin(), ret.end(),
cmp);
326 template<
typename T,
class Cmp = Aleph::less<T>>
337 template<
typename T,
class Cmp = Aleph::less<T>>
386 const Compare &
cmp = Compare())
434 const Compare &
cmp = Compare())
451 template<
typename T,
template <
typename>
class C>
454 using P = std::pair<T, size_t>;
469 C<size_t> compute_ranks(
const C<T> & c)
471 const size_t n = c.size();
474 for (
size_t i = 0; i < n; ++i)
478 return c(i1) < c(i2);
482 for (
size_t i = 0; i < n; ++i)
501 template<
template <
typename Type>
class List>
502 C<size_t> list_compute_ranks(
const List<T> & c)
507 c.for_each([&items, &n, &
indexes](
const auto &
k)
514 return items(i1) < items(i2);
518 for (
size_t i = 0; i < n; ++i)
536 C<P> compute_pair_ranks(
const C<T> & c)
538 const size_t n = c.size();
541 for (
size_t i = 0; i < n; ++i)
545 return c(i1) < c(i2);
549 for (
size_t i = 0; i < n; ++i)
552 ret(idx) =
P(c(idx), i);
571 template<
template <
typename Type>
class List>
572 C<P> list_pair_ranks(
const List<T> & c)
577 c.for_each([&items, &n, &
indexes](
const auto &
k)
584 return items(i1) < items(i2);
588 for (
size_t i = 0; i < n; ++i)
591 ret(idx) =
P(std::move(items(idx)), i);
604 class Compute_Ranks<
T,
Array>
606 using P = std::pair<T, size_t>;
624 const size_t n = c.
size();
626 for (
size_t i = 0; i < n; ++i)
630 return c(i1) < c(i2);
633 for (
size_t i = 0; i < n; ++i)
653 const size_t n = c.
size();
655 for (
size_t i = 0; i < n; ++i)
659 return c(i1) < c(i2);
662 for (
size_t i = 0; i < n; ++i)
665 ret(idx) =
P(c(idx), i);
698 return Compute_Ranks<T, Array>().compute_ranks(array);
707 return Compute_Ranks<T, DynArray>().compute_ranks(array);
721 return Compute_Ranks<T, DynArray>().list_compute_ranks(
l);
735 return Compute_Ranks<T, DynArray>().list_compute_ranks(
l);
761 return Compute_Ranks<T, Array>().compute_pair_ranks(c);
770 return Compute_Ranks<T, DynArray>().compute_pair_ranks(c);
782 Compute_Ranks<T, DynArray> func;
783 return func.list_pair_ranks(
l);
795 Compute_Ranks<T, DynArray> func;
796 return func.list_pair_ranks(
l);
839 template<
typename C,
typename... Args,
840 typename Cmp = std::less<typename C::value_type>>
844 const size_t n = first.size();
848 const bool all_same_size = ((args.size() == n) && ...);
850 <<
"all arrays must have the same size";
852 std::vector<size_t> indices(n);
853 std::iota(indices.begin(), indices.end(),
static_cast<size_t>(0));
855 const auto index_cmp_unstable = [&first, &
cmp](
const size_t i1,
const size_t i2)
857 return cmp(first[i1], first[i2]);
860 const auto index_cmp_stable = [&first, &
cmp](
const size_t i1,
const size_t i2)
862 if (
cmp(first[i1], first[i2]))
864 if (
cmp(first[i2], first[i1]))
871 static_cast<long>(indices.size() - 1), index_cmp_stable);
879 for (
size_t new_pos = 0; new_pos < n; ++new_pos)
880 perm[indices[new_pos]] = new_pos;
883 for (
size_t i = 0; i < n; ++i)
886 const size_t j = perm[i];
887 swap(first[i], first[j]);
888 (
swap(args[i], args[j]), ...);
889 swap(perm[i], perm[j]);
893 template<
typename C,
typename... Args,
894 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_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.
static Array create(const size_t n)
Create an array with n logical elements.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
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.
Array< size_t > stable_build_index(const C &a, const Compare &cmp=Compare())
Build a stable index array for indirect sorting.
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.
Array< size_t > stable_argsort(const C &a, const Compare &cmp=Compare())
Return stable sorted-order indices for an array-like container.
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.
Array< size_t > build_index(const C &a, const Compare &cmp=Compare())
Build an index array for indirect sorting.
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).
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.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Array< size_t > argsort(const C &a, const Compare &cmp=Compare())
Return indices that visit an array-like container in sorted order.
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.