Created
August 15, 2018 23:34
-
-
Save gatchamix/9b7184665edcb297cb19b0b3f8f3f07e to your computer and use it in GitHub Desktop.
fast insertion sort for non-type template parameter packs in C++2a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static auto constexpr less = [](auto&& l, auto&& r) { return l < r; }; | |
static auto constexpr less_equal = [](auto&& l, auto&& r) { return l <= r; }; | |
static auto constexpr greater = [](auto&& l, auto&& r) { return l > r; }; | |
static auto constexpr greater_equal = [](auto&& l, auto&& r) { return l <= r; }; | |
// | |
template <template <auto...> class C, auto... Vs, class Fn> | |
auto constexpr sort(C<Vs...> in, Fn) | |
{ | |
if constexpr (sizeof...(Vs) < 2) | |
{ return in; } | |
else | |
{ | |
auto constexpr impl = [] | |
<auto Head, auto... Tail, auto... SVs> | |
(C<Head, Tail...>, auto& f, C<SVs...> = {}) | |
{ | |
auto constexpr l = C<(Fn{}(Head, SVs) ? Head : SVs)..., Head>{}; | |
auto constexpr r = C<Head, (Fn{}(Head, SVs) ? SVs : Head)...>{}; | |
auto constexpr sorted = [] | |
<auto... LVs, auto... RVs> | |
(C<LVs...>, C<RVs...>) | |
{ return C<((LVs == Head) ? RVs : LVs)...>{}; } | |
(l, r); | |
if constexpr (sizeof...(Tail) == 0) | |
{ return sorted; } | |
else | |
{ return f(C<Tail...>{}, f, sorted); } | |
}; | |
return impl(in, impl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment