Skip to content

Commit

Permalink
Merge pull request #23 from maartenarnst/type_list_index_v
Browse files Browse the repository at this point in the history
Add `type_list_index_v`
  • Loading branch information
maartenarnst authored Oct 28, 2024
2 parents 961c6ff + 4315eb7 commit 30bc07f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/kokkos-utils/impl/type_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ template <typename T>
using type_list_to_tuple_t = typename TypeListToTuple<T>::type;
///@}

//! @name Get the index of a type in a @c Kokkos::Impl::type_list.
///@{
template <typename, typename>
struct TypeListIndex;

template <typename T, typename... Ts>
struct TypeListIndex<T, Kokkos::Impl::type_list<T, Ts...>>
: std::integral_constant<size_t, 0> {};

template <typename T, typename Head, typename... Ts>
struct TypeListIndex<T, Kokkos::Impl::type_list<Head, Ts...>>
: std::integral_constant<size_t, 1 + TypeListIndex<T, Kokkos::Impl::type_list<Ts...>>::value> {};

template <typename T, typename S>
inline constexpr size_t type_list_index_v = TypeListIndex<T, S>::value;
///@}

} // namespace Kokkos::utils::impl

#endif // KOKKOS_UTILS_IMPL_TYPE_LIST_HPP
10 changes: 10 additions & 0 deletions tests/impl/test_type_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ TEST(impl, type_list_to_tuple)
static_assert(std::same_as<type_list_to_tuple_t<type_list_t>, expt_tuple_t>);
}

//! @test Check @ref Kokkos::utils::impl::type_list_index_v.
TEST(impl, type_list_index_v)
{
using Kokkos::utils::impl::type_list_index_v;

static_assert(type_list_index_v<char, type_list_t> == 0);
static_assert(type_list_index_v<short, type_list_t> == 1);
static_assert(type_list_index_v<int, type_list_t> == 2);
}

} // namespace Kokkos::utils::tests::impl

0 comments on commit 30bc07f

Please sign in to comment.