Created
September 26, 2017 22:07
-
-
Save willkill07/04dbd8d84787a7a09005938ae35cbb80 to your computer and use it in GitHub Desktop.
Fib Fun
This file contains hidden or 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
#include <array> | |
#include <utility> | |
using Int = unsigned long long; | |
template<Int N, Int A = 0x0llu, Int B = 0x1llu> | |
constexpr Int Fib = []() constexpr { | |
if constexpr(N == 0) | |
return A; | |
else if constexpr(N == 1) | |
return B; | |
else if constexpr(N > 1) | |
return Fib<N - 1, B, A + B>; | |
}(); | |
// ============================================================================ | |
template <Int N, auto...> | |
struct FibWrapper { | |
static constexpr Int value = Fib<N>; | |
}; | |
// ============================================================================ | |
template <auto ... Vs> | |
struct seq; | |
template <typename> | |
struct seq_type; | |
template <auto V, auto ... Vs> | |
struct seq_type<seq<V,Vs...>> { | |
using type = decltype(V); | |
}; | |
template <typename T> | |
using seq_type_t = typename seq_type<T>::type; | |
template <typename> struct as_seq; | |
template <typename T, T ... Vs> | |
struct as_seq<std::integer_sequence<T,Vs...>> { | |
using type = seq<Vs...>; | |
}; | |
template <typename T> | |
using as_seq_t = typename as_seq<T>::type; | |
// ============================================================================ | |
template < | |
typename T, | |
typename List, | |
template<T, auto...> class Function, | |
auto ... Args> | |
struct apply; | |
template < | |
typename T, | |
auto ... Args, | |
template <T, auto...> class Fn, | |
template <auto...> class Seq, | |
T... Values> | |
struct apply<T, Seq<Values...>, Fn, Args...> { | |
using type = Seq<(Fn<Values, Args...>::value)...>; | |
}; | |
template < | |
typename List, | |
template <seq_type_t<List>, auto...> class Fn, | |
auto ... Args> | |
using apply_t = typename apply<seq_type_t<List>, List, Fn, Args...>::type; | |
// ============================================================================ | |
template <typename> | |
struct as_array; | |
template <template <auto...> class Seq, auto ... Vs> | |
struct as_array<Seq<Vs...>> { | |
using Type_ = seq_type_t<Seq<Vs...>>; | |
static constexpr std::array<Type_, sizeof...(Vs)> value {{ Vs... }}; | |
}; | |
template <typename T> | |
constexpr auto as_array_v = as_array<T>::value; | |
// ============================================================================ | |
using Seq = as_seq_t<std::make_integer_sequence<Int, 94>>; | |
auto fibs = as_array_v<apply_t<Seq, FibWrapper>>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment