Files
cenet/include/cenet/math/tensor_traits.hpp
T

122 lines
3.3 KiB
C++

#ifndef CENET_MATH_TENSOR_TRAITS_HPP
#define CENET_MATH_TENSOR_TRAITS_HPP
#include "tensor_fwd.hpp"
#include <type_traits>
namespace cenet {
namespace math {
template <typename T>
struct tensor_storage {};
template <typename T, std::size_t... Dims>
struct tensor_storage<tensor<T, Dims...>> {
static_assert(std::is_arithmetic_v<T>);
using value_type = T;
using storage_type = T*;
};
template <typename T, std::size_t... Dims>
struct tensor_storage<tensor<inline_storage<T>, Dims...>> {
static_assert(std::is_arithmetic_v<T>);
using value_type = T;
using storage_type = T[(Dims * ... * 1)];
};
template <typename T>
struct is_storage_inline : std::false_type {};
template <typename T>
struct is_storage_inline<inline_storage<T>> : std::true_type {};
template <typename T>
static constexpr auto is_storage_inline_v = is_storage_inline<T>::value;
template <typename T>
struct detach_storage {
using type = T;
};
template <typename T>
struct detach_storage<inline_storage<T>> {
using type = T;
};
template <typename T>
using detach_storage_t = typename detach_storage<T>::type;
template <typename T>
struct is_tensor : std::false_type {};
template <typename T, std::size_t... Dims>
struct is_tensor<tensor<T, Dims...>> : std::true_type {};
template <typename T>
static constexpr auto is_tensor_v = is_tensor<T>::value;
template <typename T>
struct is_tensor_dynamic : std::false_type {};
template <typename T>
struct is_tensor_dynamic<tensor<T>> : std::false_type {};
template <typename T, std::size_t First, std::size_t... Rest>
struct is_tensor_dynamic<tensor<T, First, Rest...>> : is_tensor_dynamic<tensor<T, Rest...>> {};
template <typename T, std::size_t... Rest>
struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {};
template <typename T>
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
namespace detail {
template <typename>
struct tensor_with_storage {};
template <>
struct tensor_with_storage<heap_storage_tag> {
template <typename T, std::size_t... Dims>
using type = tensor<detach_storage_t<T>, Dims...>;
};
template <>
struct tensor_with_storage<inline_storage_tag> {
template <typename T, std::size_t... Dims>
using type = tensor<inline_storage<detach_storage_t<T>>, Dims...>;
};
} // namespace detail
template <typename T>
struct tensor_traits {};
template <typename T, std::size_t... Dims>
struct tensor_traits<tensor<T, Dims...>> {
using tensor_type = tensor<T, Dims...>;
using value_type = typename tensor_storage<tensor_type>::value_type;
using storage_type = typename tensor_storage<tensor_type>::storage_type;
static constexpr auto rank = sizeof...(Dims);
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
static constexpr auto inline_storage = is_storage_inline_v<T>;
static_assert(!inline_storage || !dynamic, "Tensor storage set to inline with dynamic extents!");
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
template <typename ST>
using with_storage_t = typename detail::tensor_with_storage<ST>::template type<T, Dims...>;
};
} // namespace math
} // namespace cenet
#endif // CENET_MATH_TENSOR_TRAITS_HPP