diff --git a/include/ck/utility/sequence.hpp b/include/ck/utility/sequence.hpp index 18bb36d112..1f392af070 100644 --- a/include/ck/utility/sequence.hpp +++ b/include/ck/utility/sequence.hpp @@ -576,31 +576,35 @@ struct is_valid_sequence_map : is_same +__host__ __device__ constexpr index_t find_source_index(Sequence) +{ + constexpr index_t values[] = {Is...}; + for(index_t i = 0; i < static_cast(sizeof...(Is)); ++i) + { + if(values[i] == Target) + return i; + } + return 0; // should not reach for valid permutation +} + +template +__host__ __device__ constexpr auto invert_permutation_impl(Sequence) +{ + return Sequence(SeqMap{})...>{}; +} +} // namespace detail + +// Invert a permutation sequence using O(1) template depth pack expansion +// For X2Y = {a, b, c, ...}, computes Y2X where Y2X[X2Y[i]] = i template struct sequence_map_inverse { - template - struct sequence_map_inverse_impl - { - static constexpr auto new_y2x = - WorkingY2X::Modify(X2Y::At(Number{}), Number{}); - - using type = - typename sequence_map_inverse_impl:: - type; - }; - - template - struct sequence_map_inverse_impl - { - using type = WorkingY2X; - }; - - using type = - typename sequence_map_inverse_impl::type, - 0, - SeqMap::Size()>::type; + using type = decltype(detail::invert_permutation_impl( + typename arithmetic_sequence_gen<0, SeqMap::Size(), 1>::type{})); }; template