Created
December 12, 2013 19:24
-
-
Save jdeng/7933852 to your computer and use it in GitHub Desktop.
integer sequence c++ 11
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
//copied from http://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence | |
template<int...> struct seq { using type = seq; }; | |
template<typename T1, typename T2> struct concat; | |
template<int... I1, int... I2> struct concat<seq<I1...>, seq<I2...>>: seq<I1..., (sizeof...(I1) + I2)...> {}; | |
template<int N> struct gen_seq; | |
template<int N> struct gen_seq: concat<typename gen_seq<N/2>::type, typename gen_seq<N-N/2>::type>::type {}; | |
template <> struct gen_seq<0>: seq<>{}; | |
template <> struct gen_seq<1>: seq<0>{}; | |
//sample | |
template<int N> struct fab { const static long n = fab<N-1>::n + fab<N-2>::n; }; | |
template <> struct fab<0> { const static long n = 0; }; | |
template <> struct fab<1> { const static long n = 1; }; | |
template <> struct fab<2> { const static long n = 1; }; | |
#include <vector> | |
template<int... Is> std::vector<long> vseq(const seq<Is...>&) { return std::vector<long>{fab<Is>::n...}; } | |
template<int N> std::vector<long> vseq() { return vseq(gen_seq<N>()); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment