Is there a way to generate compile-time switch statements, for matching indices? For example if I have a sequence 1,5,8 and I want to match it to 0,1,2, is there a way the compiler can generate a function at compile time, which when given 1,5,8 returns respectively 0,1,2. To better illustrate what I want, I have prepared this sparse array structure: https://godbolt.org/z/QpWVST
#include <tuple>
#include <limits>
template<size_t depth, size_t Idx, size_t I, size_t...Is>
size_t get_idx()
{
static_assert(Idx==I || sizeof...(Is)>0, "Index not found.");
if constexpr(Idx==I) return depth;
else return get_idx<depth+1, Idx,Is...>();
}
template<typename T, size_t... Is>
struct sparse_array
{
constexpr static size_t N = sizeof...(Is);
constexpr static size_t size() { return N; }
T e[N];
constexpr sparse_array() : e{} {}
template<typename...type_pack>
constexpr sparse_array(const type_pack&... pack) : e{ pack... }
{
static_assert(sizeof...(type_pack) == size(), "Argument count must mach array size.");
}
template<size_t I>
constexpr size_t idx()
{
return get_idx<0, I, Is...>();
}
size_t idx(const size_t i)
{
// how to achieve somethig like this?
return idx<i>();
}
constexpr T& at(size_t idx)
{
return e[idx];
}
};
template<typename T, size_t...Is>
auto make_dense_array(std::index_sequence<Is...>&& seq)
{
return sparse_array<T, Is...>{};
}
template<typename T, size_t N>
using dense_array = decltype(make_dense_array<T>(std::declval<std::make_index_sequence<N>>()));
size_t test()
{
dense_array<int, 3> a;
sparse_array<int,1,5,8> b;
return b.idx<8>();
}
I want to be able to also pass in runtime variables, which would go through a switch with the indices and return the appropriate corresponding index. The only idea I have for solving this, involves generating an array of the Is... sequence, and then having a for loop with if statements in order to return the correct index. The other option being, using a map (but this is also not compile-time). The sparse_array would in general be very small, and I would have liked to be able to do most things compile time.
Something like this?
It might be a bit tricky to read, but should do what you want, if I understood correctly. After instantiation this is basically equivalent to a series of
if elsegoing left-to-right through the indices inIs.You can make it more readable by separating the body of the fold expression into a lambda. You should also replace the
throwexpression with whatever is sensible for you.With the
constexprqualifier this can be reused for the template version as well:(Putting the result in the template default argument guarantees compile time evaluation.)
This will not be the most well-performing code, sharing the same problems a manually written
switchstatement has. Depending on the predictability of the inputs, the many branches may be often mispredicted, in which case a (mostly) branchless version would be preferable. This can be achieved by modifying the fold expression appropriately.If the number of indices is not small, a loop through a properly constructed static array would be preferable for instruction cache locality:
Again, it might be preferable to replace the early exit in the loop.
If the array is even larger, it might be useful to not only use a declared array with loop, but implement binary search on that array properly.
Standard algorithms like
std::any_of,std::findandstd::binary_searchcould be used instead of the manual search implementations. However these algorithms will beconstexpronly in C++20 and so this would limit the use here.