Sprout/sprout/index_tuple.hpp

166 lines
4.3 KiB
C++
Raw Normal View History

2011-09-01 02:48:32 +00:00
#ifndef SPROUT_INDEX_TUPLE_HPP
#define SPROUT_INDEX_TUPLE_HPP
#include <cstddef>
#include <type_traits>
2011-09-01 02:48:32 +00:00
#include <sprout/config.hpp>
namespace sprout {
2012-02-28 01:46:39 +00:00
//
// index_t
//
typedef std::ptrdiff_t index_t;
2011-09-01 02:48:32 +00:00
//
// index_tuple
//
2012-02-28 01:46:39 +00:00
template<sprout::index_t... Indexes>
2011-09-01 02:48:32 +00:00
struct index_tuple {};
//
// index_range
//
namespace detail {
template<typename IndexTuple, sprout::index_t Next>
struct index_range_next;
template<sprout::index_t... Indexes, sprout::index_t Next>
struct index_range_next<sprout::index_tuple<Indexes...>, Next> {
public:
typedef sprout::index_tuple<Indexes..., (Indexes + Next)...> type;
};
template<typename IndexTuple, sprout::index_t Next, sprout::index_t Tail>
struct index_range_next2;
template<sprout::index_t... Indexes, sprout::index_t Next, sprout::index_t Tail>
struct index_range_next2<sprout::index_tuple<Indexes...>, Next, Tail> {
public:
typedef sprout::index_tuple<Indexes..., (Indexes + Next)..., Tail> type;
};
template<sprout::index_t First, sprout::index_t Step, std::size_t N, typename Enable = void>
struct index_range_impl;
template<sprout::index_t First, sprout::index_t Step, std::size_t N>
struct index_range_impl<
First,
Step,
N,
typename std::enable_if<(N == 0)>::type
> {
public:
typedef sprout::index_tuple<> type;
};
template<sprout::index_t First, sprout::index_t Step, std::size_t N>
struct index_range_impl<
First,
Step,
N,
typename std::enable_if<(N == 1)>::type
> {
public:
typedef sprout::index_tuple<First> type;
};
template<sprout::index_t First, sprout::index_t Step, std::size_t N>
struct index_range_impl<
First,
Step,
N,
typename std::enable_if<(N > 1 && N % 2 == 0)>::type
>
: public sprout::detail::index_range_next<
typename sprout::detail::index_range_impl<First, Step, N / 2>::type,
First + N / 2 * Step
>
{};
template<sprout::index_t First, sprout::index_t Step, std::size_t N>
struct index_range_impl<
First,
Step,
N,
typename std::enable_if<(N > 1 && N % 2 == 1)>::type
>
: public sprout::detail::index_range_next2<
typename sprout::detail::index_range_impl<First, Step, N / 2>::type,
First + N / 2 * Step,
First + (N - 1) * Step
>
{};
} // namespace detail
template<sprout::index_t First, sprout::index_t Last, sprout::index_t Step = 1>
struct index_range
: public sprout::detail::index_range_impl<
First,
Step,
((Last - First) + (Step - 1)) / Step
>
2011-09-01 02:48:32 +00:00
{};
//
// index_n
//
namespace detail {
2012-02-28 06:02:56 +00:00
template<typename IndexTuple>
struct index_n_next;
template<sprout::index_t... Indexes>
struct index_n_next<sprout::index_tuple<Indexes...>> {
public:
typedef sprout::index_tuple<Indexes..., Indexes...> type;
};
template<typename IndexTuple, sprout::index_t Tail>
struct index_n_next2;
template<sprout::index_t... Indexes, sprout::index_t Tail>
struct index_n_next2<sprout::index_tuple<Indexes...>, Tail> {
public:
typedef sprout::index_tuple<Indexes..., Indexes..., Tail> type;
};
template<sprout::index_t I, std::size_t N, typename Enable = void>
struct index_n_impl;
template<sprout::index_t I, std::size_t N>
struct index_n_impl<
I,
N,
typename std::enable_if<(N == 0)>::type
> {
public:
typedef sprout::index_tuple<> type;
};
template<sprout::index_t I, std::size_t N>
struct index_n_impl<
I,
N,
typename std::enable_if<(N == 1)>::type
> {
public:
2012-02-28 06:02:56 +00:00
typedef sprout::index_tuple<I> type;
};
2012-02-28 06:02:56 +00:00
template<sprout::index_t I, std::size_t N>
struct index_n_impl<
I,
N,
typename std::enable_if<(N > 1 && N % 2 == 0)>::type
>
2012-02-28 06:02:56 +00:00
: public sprout::detail::index_n_next<
typename sprout::detail::index_n_impl<I, N / 2>::type
>
{};
template<sprout::index_t I, std::size_t N>
struct index_n_impl<
I,
N,
typename std::enable_if<(N > 1 && N % 2 == 1)>::type
>
: public sprout::detail::index_n_next2<
typename sprout::detail::index_n_impl<I, N / 2>::type,
I
>
{};
} // namespace detail
2012-02-28 09:54:39 +00:00
template<sprout::index_t I, std::size_t N>
struct index_n
2012-02-28 06:02:56 +00:00
: public sprout::detail::index_n_impl<I, N>
2011-09-01 02:48:32 +00:00
{};
} // namespace sprout
#endif // #ifndef SPROUT_INDEX_TUPLE_HPP