mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
index_range のオーダーを N(logN) に再実装
This commit is contained in:
parent
d5dcbaf4b2
commit
8ad87d104d
1 changed files with 97 additions and 31 deletions
|
@ -2,6 +2,7 @@
|
||||||
#define SPROUT_INDEX_TUPLE_HPP
|
#define SPROUT_INDEX_TUPLE_HPP
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <type_traits>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
|
@ -19,45 +20,110 @@ namespace sprout {
|
||||||
//
|
//
|
||||||
// index_range
|
// index_range
|
||||||
//
|
//
|
||||||
template<
|
namespace detail {
|
||||||
sprout::index_t First,
|
template<typename IndexTuple, sprout::index_t Next>
|
||||||
sprout::index_t Last,
|
struct index_range_next;
|
||||||
sprout::index_t Step = 1,
|
template<sprout::index_t... Indexes, sprout::index_t Next>
|
||||||
typename Acc = sprout::index_tuple<>,
|
struct index_range_next<sprout::index_tuple<Indexes...>, Next> {
|
||||||
bool Break = (First >= Last)
|
public:
|
||||||
>
|
typedef sprout::index_tuple<Indexes..., (Indexes + Next)...> type;
|
||||||
struct index_range {
|
};
|
||||||
typedef Acc type;
|
|
||||||
};
|
template<typename IndexTuple, sprout::index_t Next, sprout::index_t Tail>
|
||||||
template<
|
struct index_range_next2;
|
||||||
sprout::index_t First,
|
template<sprout::index_t... Indexes, sprout::index_t Next, sprout::index_t Tail>
|
||||||
sprout::index_t Last,
|
struct index_range_next2<sprout::index_tuple<Indexes...>, Next, Tail> {
|
||||||
sprout::index_t Step,
|
public:
|
||||||
sprout::index_t... Indexes
|
typedef sprout::index_tuple<Indexes..., (Indexes + Next)..., Tail> type;
|
||||||
>
|
};
|
||||||
struct index_range<First, Last, Step, sprout::index_tuple<Indexes...>, false>
|
|
||||||
: public sprout::index_range<First + Step, Last, Step, sprout::index_tuple<Indexes..., First> >
|
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
|
||||||
|
>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
//
|
//
|
||||||
// index_n
|
// index_n
|
||||||
//
|
//
|
||||||
|
namespace detail {
|
||||||
|
template<
|
||||||
|
sprout::index_t I,
|
||||||
|
std::size_t N,
|
||||||
|
typename Acc,
|
||||||
|
bool Break = (N == 0)
|
||||||
|
>
|
||||||
|
struct index_n_impl {
|
||||||
|
public:
|
||||||
|
typedef Acc type;
|
||||||
|
};
|
||||||
|
template<
|
||||||
|
sprout::index_t I,
|
||||||
|
std::size_t N,
|
||||||
|
sprout::index_t... Indexes
|
||||||
|
>
|
||||||
|
struct index_n_impl<I, N, sprout::index_tuple<Indexes...>, false>
|
||||||
|
: public sprout::detail::index_n_impl<I, N - 1, sprout::index_tuple<Indexes..., I> >
|
||||||
|
{};
|
||||||
|
} // namespace detail
|
||||||
template<
|
template<
|
||||||
sprout::index_t I,
|
sprout::index_t I,
|
||||||
sprout::index_t N,
|
std::size_t N,
|
||||||
typename Acc = sprout::index_tuple<>,
|
typename Acc = sprout::index_tuple<>
|
||||||
bool Break = (N == 0)
|
|
||||||
>
|
>
|
||||||
struct index_n {
|
struct index_n
|
||||||
typedef Acc type;
|
: public sprout::detail::index_n_impl<I, N, Acc>
|
||||||
};
|
|
||||||
template<
|
|
||||||
sprout::index_t I,
|
|
||||||
sprout::index_t N,
|
|
||||||
sprout::index_t... Indexes
|
|
||||||
>
|
|
||||||
struct index_n<I, N, sprout::index_tuple<Indexes...>, false>
|
|
||||||
: public sprout::index_n<I, N - 1, sprout::index_tuple<Indexes..., I> >
|
|
||||||
{};
|
{};
|
||||||
} // namespace sprout
|
} // namespace sprout
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue