Loading...
Searching...
No Matches
compilesort.hpp
1
5#pragma once
6
7#ifdef __cplusplus
8
9#include <iterator>
10#include <array>
11
12namespace cstd {
13
14template <typename RAIt>
15constexpr RAIt next(RAIt it, typename std::iterator_traits<RAIt>::difference_type n = 1) {
16 return it + n;
17}
18
19template <typename RAIt>
20constexpr auto distance(RAIt first, RAIt last) {
21 return last - first;
22}
23
24template <class ForwardIt1, class ForwardIt2>
25constexpr void iter_swap(ForwardIt1 a, ForwardIt2 b) {
26 auto temp = std::move(*a);
27 *a = std::move(*b);
28 *b = std::move(temp);
29}
30
31template <class InputIt, class UnaryPredicate>
32constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) {
33 for(; first != last; ++first) {
34 if(!q(*first)) {
35 return first;
36 }
37 }
38 return last;
39}
40
41template <class ForwardIt, class UnaryPredicate>
42constexpr ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p) {
43 first = cstd::find_if_not(first, last, p);
44 if(first == last) return first;
45
46 for(ForwardIt i = cstd::next(first); i != last; ++i) {
47 if(p(*i)) {
48 cstd::iter_swap(i, first);
49 ++first;
50 }
51 }
52 return first;
53}
54
55}
56
57template <class RAIt, class Compare = std::less<> >
58constexpr void quick_sort(RAIt first, RAIt last, Compare cmp = Compare{}) {
59 auto const N = cstd::distance(first, last);
60 if(N <= 1) return;
61 auto const pivot = *cstd::next(first, N / 2);
62 auto const middle1 =
63 cstd::partition(first, last, [=](auto const& elem) { return cmp(elem, pivot); });
64 auto const middle2 =
65 cstd::partition(middle1, last, [=](auto const& elem) { return !cmp(pivot, elem); });
66 quick_sort(first, middle1, cmp); // assert(std::is_sorted(first, middle1, cmp));
67 quick_sort(middle2, last, cmp); // assert(std::is_sorted(middle2, last, cmp));
68}
69
70template <typename Range>
71constexpr auto sort(Range&& range) {
72 quick_sort(std::begin(range), std::end(range));
73 return range;
74}
75
76template <typename V, typename... T>
77constexpr auto array_of(T&&... t) -> std::array<V, sizeof...(T)> {
78 return {{std::forward<T>(t)...}};
79}
80
81template <typename T, typename... N>
82constexpr auto my_make_array(N&&... args) -> std::array<T, sizeof...(args)> {
83 return {std::forward<N>(args)...};
84}
85
86namespace traits {
87template <typename T, typename... Ts>
88struct array_type {
89 using type = T;
90};
91
92template <typename T, typename... Ts>
93static constexpr bool are_same_type() {
94 return std::conjunction_v<std::is_same<T, Ts>...>;
95}
96
97}
98
99template <typename... T>
100constexpr auto create_array(const T&&... values) {
101 using array_type = typename traits::array_type<T...>::type;
102 static_assert(sizeof...(T) > 0, "an array must have at least one element");
103 static_assert(traits::are_same_type<T...>(), "all elements must have same type");
104 return std::array<array_type, sizeof...(T)>{values...};
105}
106
107template <typename T, typename... Ts>
108constexpr auto create_array_t(const Ts&&... values) {
109 using array_type = T;
110 static_assert(sizeof...(Ts) > 0, "an array must have at least one element");
111 static_assert(traits::are_same_type<Ts...>(), "all elements must have same type");
112 return std::array<array_type, sizeof...(Ts)>{static_cast<T>(values)...};
113}
114
115#endif