cMHN 1.1
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, class T, If<IsSubscriptable<T>> = 0,
13 class S = ResultOf<Subscript, T &, Index>, If<IsInvocable<C, S, S>> = 0,
14 If<IsAssignable<S>> = 0>
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
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
48 }
49 }
50 }
51
52 template<class C = Less, class T, If<IsSubscriptable<T>> = 0,
53 class S = ResultOf<Subscript, T &, Index>, If<IsInvocable<C, S, S>> = 0,
54 If<IsAssignable<S>> = 0>
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 cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::size_t Size
Definition type_traits.hpp:20
static constexpr void sort(C const &compare, T &a, Size const k=T::size(), Size const d=0)
Definition sort.hpp:15
static constexpr auto swap(XA &&a, XB &&b)
Definition swap.hpp:21