dxx-rebirth/common/include/compiler-integer_sequence.h
Kp 4128ce2971 Use compiler-provided integer_sequence when it is depth-efficient
Early implementations of integer_sequence used a naive implementation
that required one level of template depth per additional integer in the
sequence.  Rebirth uses a private alternate implementation named
make_tree_index_sequence that requires only log(N) steps for an
N-element index_sequence.  Recent versions of gcc ship a log(N) version
of integer_sequence.  Probe for that version and, if found, use it
instead of the private implementation, on the theory that the compiler
writers did at least as good a job as I did, and possibly better if they
were able to leverage compiler implementation details.
2017-07-26 03:15:59 +00:00

48 lines
1.1 KiB
C++

#pragma once
#include <cstddef>
#include "dxxsconf.h"
#if defined(DXX_HAVE_CXX14_TREE_INTEGER_SEQUENCE) || defined(DXX_HAVE_CXX14_INTEGER_SEQUENCE)
#include <utility>
using std::index_sequence;
#else
template <std::size_t...>
struct index_sequence {};
#endif
#if defined(DXX_HAVE_CXX14_TREE_INTEGER_SEQUENCE)
template <std::size_t N>
using make_tree_index_sequence = std::make_index_sequence<N>;
#else
template <typename, std::size_t, typename>
struct cat_index_sequence;
template <std::size_t... N1, std::size_t A2, std::size_t... N2>
struct cat_index_sequence<index_sequence<N1...>, A2, index_sequence<N2...>>
{
typedef index_sequence<N1..., A2 + N2...> type;
};
/* Fan out to reduce template depth */
template <std::size_t N>
struct tree_index_sequence :
cat_index_sequence<typename tree_index_sequence<N / 2>::type, N / 2, typename tree_index_sequence<(N + 1) / 2>::type>
{
};
template <>
struct tree_index_sequence<0>
{
typedef index_sequence<> type;
};
template <>
struct tree_index_sequence<1>
{
typedef index_sequence<0> type;
};
template <std::size_t N>
using make_tree_index_sequence = typename tree_index_sequence<N>::type;
#endif