14template <
typename RAIt>
15constexpr RAIt next(RAIt it,
typename std::iterator_traits<RAIt>::difference_type n = 1) {
19template <
typename RAIt>
20constexpr auto distance(RAIt first, RAIt last) {
24template <
class ForwardIt1,
class ForwardIt2>
25constexpr void iter_swap(ForwardIt1 a, ForwardIt2 b) {
26 auto temp = std::move(*a);
31template <
class InputIt,
class UnaryPredicate>
32constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) {
33 for(; first != last; ++first) {
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;
46 for(ForwardIt i = cstd::next(first); i != last; ++i) {
48 cstd::iter_swap(i, first);
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);
61 auto const pivot = *cstd::next(first, N / 2);
63 cstd::partition(first, last, [=](
auto const& elem) {
return cmp(elem, pivot); });
65 cstd::partition(middle1, last, [=](
auto const& elem) {
return !cmp(pivot, elem); });
66 quick_sort(first, middle1, cmp);
67 quick_sort(middle2, last, cmp);
70template <
typename Range>
71constexpr auto sort(Range&& range) {
72 quick_sort(std::begin(range), std::end(range));
76template <
typename V,
typename... T>
77constexpr auto array_of(T&&... t) -> std::array<V,
sizeof...(T)> {
78 return {{std::forward<T>(t)...}};
81template <
typename T,
typename... N>
82constexpr auto my_make_array(N&&... args) -> std::array<T,
sizeof...(args)> {
83 return {std::forward<N>(args)...};
87template <
typename T,
typename... Ts>
92template <
typename T,
typename... Ts>
93static constexpr bool are_same_type() {
94 return std::conjunction_v<std::is_same<T, Ts>...>;
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...};
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)...};