Add integer sequence

This commit is contained in:
BlackMark 2020-05-11 14:32:11 +02:00
parent a2bb2c2900
commit 37c4f5543a

View File

@ -1,5 +1,7 @@
#pragma once
#include <stdint.h>
#include "../type/type.hpp"
namespace util {
@ -23,4 +25,80 @@ inline constexpr T &&forward(type::remove_reference_t<T> &&t) noexcept
return static_cast<T &&>(t);
}
template <typename T, T... Ints>
struct integer_sequence {
using __type = integer_sequence;
using value_type = T;
static constexpr size_t size() noexcept
{
return sizeof...(Ints);
}
};
template <size_t... Ints>
using index_sequence = integer_sequence<size_t, Ints...>;
namespace detail {
template <typename T, typename Seq1, typename Seq2>
struct concat;
template <typename T, T... Ints1, T... Ints2>
struct concat<T, integer_sequence<T, Ints1...>, integer_sequence<T, Ints2...>>
: integer_sequence<T, Ints1..., (sizeof...(Ints1) + Ints2)...> {
};
// uint64_t because this must be able to hold all possible sizes and cannot be T, because then the specialization for 0
// and 1 are not possible
template <typename T, uint64_t N>
struct gen_integer_sequence : detail::concat<T, typename gen_integer_sequence<T, N / 2>::__type,
typename gen_integer_sequence<T, N - N / 2>::__type> {
};
template <typename T>
struct gen_integer_sequence<T, 0> : integer_sequence<T> {
};
template <typename T>
struct gen_integer_sequence<T, 1> : integer_sequence<T, 0> {
};
} // namespace detail
template <typename T, T N>
struct make_integer_sequence : detail::gen_integer_sequence<T, N> {
static_assert(N >= 0, "Integer sequence cannot be negative");
};
template <size_t N>
using make_index_sequence = make_integer_sequence<size_t, N>;
template <typename... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;
// Not part of <utility>, but very useful
namespace detail {
template <typename T, T Offset, T... Ints>
inline constexpr integer_sequence<T, (Offset + Ints)...> add_offset(integer_sequence<T, Ints...>)
{
return {};
}
} // namespace detail
template <typename T, T Offset, T N>
inline constexpr auto make_offset_integer_sequence()
{
return detail::add_offset<T, Offset>(make_integer_sequence<T, N>{});
}
template <size_t Offset, size_t N>
inline constexpr auto make_offset_index_sequence()
{
return make_offset_integer_sequence<size_t, Offset, N>();
}
} // namespace util