cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
sort.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_ALGORITHMS_SORT_H
4#define pRC_ALGORITHMS_SORT_H
5
9
10namespace pRC
11{
12 template<class C, IsSubscriptable T,
13 class S = ResultOf<Subscript, T &, Index>>
14 requires IsInvocable<C, S, S> && IsAssignable<S>
15 static inline constexpr void sort(C const &compare, T &a,
16 Size const k = T::size(), Size const d = 0)
17 {
18 auto const insertionSort =
19 [](auto &a, auto const &compare, auto const k, auto const d)
20 {
21 for(Index i = d + 1; i < k; ++i)
22 {
23 auto const x = move(a[i]);
24 auto j = i;
25 for(; j > d; --j)
26 {
27 if(!compare(x, a[j - 1]))
28 {
29 break;
30 }
31 a[j] = move(a[j - 1]);
32 }
33 a[j] = move(x);
34 }
35
36 return;
37 };
38
39 insertionSort(a, compare, k, d);
40
41 for(Index i = k; i < T::size(); ++i)
42 {
43 if(compare(a[i], a[k - 1]))
44 {
45 swap(a[i], a[k - 1]);
46
47 insertionSort(a, compare, k, d);
48 }
49 }
50 }
51
52 template<class C = Less, IsSubscriptable T,
53 class S = ResultOf<Subscript, T &, Index>>
54 requires IsInvocable<C, S, S> && IsAssignable<S>
55 static inline constexpr void sort(T &a, Size const k = T::size(),
56 Size const d = 0)
57 {
58 return sort(C(), a, k, d);
59 }
60}
61#endif // pRC_ALGORITHMS_SORT_H
Definition value.hpp:12
TN::Subscripts S
Definition externs_nonTT.hpp:9
pRC::Float<> T
Definition externs_nonTT.hpp:1
int i
Definition gmock-matchers-comparisons_test.cc:603
int x
Definition gmock-matchers-containers_test.cc:376
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static constexpr auto swap(XA &&a, XB &&b)
Definition swap.hpp:20
static constexpr void sort(C const &compare, T &a, Size const k=T::size(), Size const d=0)
Definition sort.hpp:15