2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2013-03-31 04:39:26 +00:00
|
|
|
#ifndef SPROUT_INDEX_TUPLE_INTEGER_RANGE_HPP
|
|
|
|
#define SPROUT_INDEX_TUPLE_INTEGER_RANGE_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-05-10 10:52:39 +00:00
|
|
|
#include <sprout/index_tuple/integer_sequence.hpp>
|
2013-04-06 04:06:51 +00:00
|
|
|
#include <sprout/index_tuple/enable_make_indexes.hpp>
|
2013-11-22 12:11:08 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
2013-03-31 04:39:26 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2013-04-05 14:37:24 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T, T First, T Last>
|
|
|
|
struct integer_range_default_step
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<typename std::make_signed<T>::type, (First <= Last ? 1 : -1)>
|
2013-04-05 14:37:24 +00:00
|
|
|
{};
|
|
|
|
} // namespace detail
|
|
|
|
|
2013-03-31 04:39:26 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename Seq, T Next>
|
|
|
|
struct integer_range_next_even;
|
|
|
|
template<typename T, T... Is, T Next>
|
2013-05-10 10:52:39 +00:00
|
|
|
struct integer_range_next_even<T, sprout::integer_sequence<T, Is...>, Next>
|
|
|
|
: public sprout::integer_sequence<T, Is..., (Is + Next)...>
|
2013-03-31 04:39:26 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T, typename Seq, T Next, T Tail>
|
|
|
|
struct integer_range_next_odd;
|
|
|
|
template<typename T, T... Is, T Next, T Tail>
|
2013-05-10 10:52:39 +00:00
|
|
|
struct integer_range_next_odd<T, sprout::integer_sequence<T, Is...>, Next, Tail>
|
|
|
|
: public sprout::integer_sequence<T, Is..., (Is + Next)..., Tail>
|
2013-03-31 04:39:26 +00:00
|
|
|
{};
|
|
|
|
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, typename std::make_signed<T>::type Step, typename std::make_unsigned<T>::type N, typename Enable = void>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_dispatch;
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, typename std::make_signed<T>::type Step, typename std::make_unsigned<T>::type N>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_dispatch<
|
2013-03-31 04:39:26 +00:00
|
|
|
T, First, Step, N,
|
|
|
|
typename std::enable_if<(N == 0)>::type
|
|
|
|
>
|
2013-05-10 10:52:39 +00:00
|
|
|
: public sprout::integer_sequence<T>
|
2013-03-31 04:39:26 +00:00
|
|
|
{};
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, typename std::make_signed<T>::type Step, typename std::make_unsigned<T>::type N>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_dispatch<
|
2013-03-31 04:39:26 +00:00
|
|
|
T, First, Step, N,
|
|
|
|
typename std::enable_if<(N == 1)>::type
|
|
|
|
>
|
2013-05-10 10:52:39 +00:00
|
|
|
: public sprout::integer_sequence<T, First>
|
2013-03-31 04:39:26 +00:00
|
|
|
{};
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, typename std::make_signed<T>::type Step, typename std::make_unsigned<T>::type N>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_dispatch<
|
2013-03-31 04:39:26 +00:00
|
|
|
T, First, Step, N,
|
|
|
|
typename std::enable_if<(N > 1 && N % 2 == 0)>::type
|
|
|
|
>
|
|
|
|
: public sprout::detail::integer_range_next_even<
|
2013-05-12 02:05:31 +00:00
|
|
|
T, typename sprout::detail::integer_range_dispatch<T, First, Step, N / 2>::type,
|
2013-04-05 09:11:56 +00:00
|
|
|
N / 2 * Step
|
2013-03-31 04:39:26 +00:00
|
|
|
>
|
|
|
|
{};
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, typename std::make_signed<T>::type Step, typename std::make_unsigned<T>::type N>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_dispatch<
|
2013-03-31 04:39:26 +00:00
|
|
|
T, First, Step, N,
|
|
|
|
typename std::enable_if<(N > 1 && N % 2 == 1)>::type
|
|
|
|
>
|
|
|
|
: public sprout::detail::integer_range_next_odd<
|
2013-05-12 02:05:31 +00:00
|
|
|
T, typename sprout::detail::integer_range_dispatch<T, First, Step, N / 2>::type,
|
2013-04-05 09:11:56 +00:00
|
|
|
N / 2 * Step, First + (N - 1) * Step
|
2013-03-31 04:39:26 +00:00
|
|
|
>
|
|
|
|
{};
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, T Last, typename std::make_signed<T>::type Step, typename Enable = void>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_impl {};
|
2013-04-05 14:37:24 +00:00
|
|
|
template<typename T, T First, T Last, typename std::make_signed<T>::type Step>
|
2013-05-12 02:05:31 +00:00
|
|
|
struct integer_range_impl<
|
2013-04-05 14:37:24 +00:00
|
|
|
T, First, Last, Step,
|
|
|
|
typename std::enable_if<((First < Last && Step > 0) || (First > Last && Step < 0) || (First == Last))>::type
|
|
|
|
>
|
2013-05-12 02:05:31 +00:00
|
|
|
: public sprout::detail::integer_range_dispatch<
|
2013-04-05 14:37:24 +00:00
|
|
|
T, First, Step,
|
|
|
|
(static_cast<typename std::make_signed<T>::type>(Last - First) + (Step > 0 ? Step - 1 : Step + 1)) / Step
|
|
|
|
>
|
|
|
|
{};
|
2013-05-12 02:05:31 +00:00
|
|
|
|
|
|
|
template<
|
|
|
|
typename T, T First, T Last,
|
|
|
|
typename std::make_signed<T>::type Step
|
|
|
|
>
|
|
|
|
struct integer_range
|
|
|
|
: public sprout::enable_make_indexes<
|
|
|
|
sprout::detail::integer_range_impl<T, First, Last, Step>
|
|
|
|
>
|
|
|
|
{
|
|
|
|
static_assert(
|
|
|
|
(First < Last && Step > 0) || (First > Last && Step < 0) || (First == Last),
|
|
|
|
"(First < Last && Step > 0) || (First > Last && Step < 0) || (First == Last)"
|
|
|
|
);
|
|
|
|
};
|
2013-03-31 04:39:26 +00:00
|
|
|
} // namespace detail
|
2013-04-05 14:37:24 +00:00
|
|
|
//
|
|
|
|
// integer_range
|
|
|
|
//
|
2013-05-12 02:05:31 +00:00
|
|
|
#if SPROUT_USE_TEMPLATE_ALIASES
|
|
|
|
template<
|
|
|
|
typename T, T First, T Last,
|
|
|
|
typename std::make_signed<T>::type Step = sprout::detail::integer_range_default_step<T, First, Last>::value
|
|
|
|
>
|
|
|
|
using integer_range = typename sprout::detail::integer_range<T, First, Last, Step>::type;
|
|
|
|
#else // #if SPROUT_USE_TEMPLATE_ALIASES
|
2013-04-05 14:37:24 +00:00
|
|
|
template<
|
|
|
|
typename T, T First, T Last,
|
|
|
|
typename std::make_signed<T>::type Step = sprout::detail::integer_range_default_step<T, First, Last>::value
|
|
|
|
>
|
2013-03-31 04:39:26 +00:00
|
|
|
struct integer_range
|
2013-05-12 02:05:31 +00:00
|
|
|
: public sprout::detail::integer_range<T, First, Last, Step>
|
|
|
|
{};
|
|
|
|
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
|
2013-03-31 04:39:26 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_INDEX_TUPLE_INTEGER_RANGE_HPP
|