Skip to content

Commit

Permalink
add utilities that extend Kokkos::Impl::type_list
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenarnst committed Sep 2, 2024
1 parent f00438d commit 6d40405
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ target_sources(
include/kokkos-utils/concepts/Space.hpp
include/kokkos-utils/concepts/View.hpp

include/kokkos-utils/impl/type_list.hpp
include/kokkos-utils/impl/type_traits.hpp

include/kokkos-utils/view/extents.hpp
Expand Down
78 changes: 78 additions & 0 deletions include/kokkos-utils/impl/type_list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef KOKKOS_UTILS_IMPL_TYPE_LIST_HPP
#define KOKKOS_UTILS_IMPL_TYPE_LIST_HPP

#include "impl/Kokkos_Utilities.hpp"

/**
* @file
*
* This file provides extensions to the @c Kokkos::Impl::type_list struct in @c Kokkos_Utilities.hpp.
*/

namespace Kokkos::utils::impl
{

//! @name Get the number of types in a @c Kokkos::Impl::type_list.
///@{
template <typename>
struct TypeListSize;

template <typename... Ts>
struct TypeListSize<Kokkos::Impl::type_list<Ts...>> : std::integral_constant<size_t, sizeof...(Ts)> {};

template <typename T>
inline constexpr size_t type_list_size_v = TypeListSize<T>::value;
///@}

//! @name Get the @p I th type in a @c Kokkos::Impl::type_list.
///@{
template <size_t, typename>
struct TypeListAt;

template <size_t I, typename Head, typename... Tail>
struct TypeListAt<I, Kokkos::Impl::type_list<Head, Tail...>>
: TypeListAt<I - 1, Kokkos::Impl::type_list<Tail...>> {};

template <class Head, class... Tail>
struct TypeListAt<0, Kokkos::Impl::type_list<Head, Tail...>>
{
using type = Head;
};

template <size_t I, typename T>
using type_list_at_t = typename TypeListAt<I, T>::type;
///@}

//! @name Transform a @c Kokkos::Impl::type_list.
///@{
template <template <typename> typename, typename>
struct TransformTypeList;

template <template <typename> typename TransformerType, typename... Ts>
struct TransformTypeList<TransformerType, Kokkos::Impl::type_list<Ts...>>
{
using type = Kokkos::Impl::concat_type_list_t<Kokkos::Impl::type_list<typename TransformerType<Ts>::type...>>;
};

template <template <typename> typename TransformerType, typename T>
using transform_type_list_t = typename TransformTypeList<TransformerType, T>::type;
///@}

//! @name Convert a @c Kokkos::Impl::type_list into a @c std::tuple.
///@{
template <typename>
struct TypeListToTuple;

template <typename... Ts>
struct TypeListToTuple<Kokkos::Impl::type_list<Ts...>>
{
using type = std::tuple<Ts...>;
};

template <typename T>
using type_list_to_tuple_t = typename TypeListToTuple<T>::type;
///@}

} // namespace Kokkos::utils::impl

#endif // KOKKOS_UTILS_IMPL_TYPE_LIST_HPP
5 changes: 4 additions & 1 deletion include/kokkos-utils/impl/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <type_traits>

/**
* @brief This namespace provides extensions to the @c type_traits standard header.
* @file
*
* This file provides extensions to the @c type_traits standard header.
*
* References:
* * https://en.cppreference.com/w/cpp/header/type_traits
*/

namespace Kokkos::utils::impl
{

Expand Down
5 changes: 5 additions & 0 deletions tests/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### TEST : type_list ###
add_one_test(
TEST_NAME type_list
)

### TEST : type_traits ###
add_one_test(
TEST_NAME type_traits
Expand Down
70 changes: 70 additions & 0 deletions tests/impl/test_type_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "gtest/gtest.h"

#include "kokkos-utils/impl/type_list.hpp"

/**
* @file
*
* @addtogroup unittests
*
* **Type list**
*
* This group of tests check the behavior of our utilities extending @c Kokkos::Impl::type_list.
*/

namespace Kokkos::utils::tests::impl
{

using type_list_t = Kokkos::Impl::type_list<char, short, int>;

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

using type_list_empty_t = Kokkos::Impl::type_list<>;
static_assert(type_list_size_v<type_list_empty_t> == 0);

static_assert(type_list_size_v<type_list_t> == 3);
}

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

static_assert(std::same_as<type_list_at_t<0, type_list_t>, char>);
static_assert(std::same_as<type_list_at_t<1, type_list_t>, short>);
static_assert(std::same_as<type_list_at_t<2, type_list_t>, int>);
}

//! Helper struct for the test of @ref Kokkos::utils::impl::transform_type_list_t.
template <typename T>
struct VectorOfTransformer
{
using type = std::vector<T>;
};

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

using transformed_type_list_t = transform_type_list_t<VectorOfTransformer, type_list_t>;

using expt_transformed_type_list_t = Kokkos::Impl::type_list<std::vector<char>, std::vector<short>, std::vector<int>>;

static_assert(std::same_as<transformed_type_list_t, expt_transformed_type_list_t>);
}

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

using expt_tuple_t = std::tuple<char, short, int>;

static_assert(std::same_as<type_list_to_tuple_t<type_list_t>, expt_tuple_t>);
}

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

0 comments on commit 6d40405

Please sign in to comment.