#ifndef TULIP_TENSOR_TRAITS_HPP #define TULIP_TENSOR_TRAITS_HPP #include "./tensor_fwd.hpp" #include "./tensor_shape.hpp" #include "./type_traits.hpp" #include namespace tulip { template struct tensor_storage {}; template struct tensor_storage> { static_assert(std::is_arithmetic_v); using value_type = T; using storage_type = T*; }; template struct tensor_storage, Dims...>> { static_assert(std::is_arithmetic_v); using value_type = T; using storage_type = T[(Dims * ... * 1)]; }; template struct is_storage_inline : std::false_type {}; template struct is_storage_inline> : std::true_type {}; template static constexpr auto is_storage_inline_v = is_storage_inline::value; template struct detach_storage { using type = T; }; template struct detach_storage> { using type = T; }; template using detach_storage_t = typename detach_storage::type; template struct is_tensor : std::false_type {}; template struct is_tensor> : std::true_type {}; template static constexpr auto is_tensor_v = is_tensor::value; template struct is_tensor_dynamic : std::false_type {}; template struct is_tensor_dynamic> : std::false_type {}; template struct is_tensor_dynamic> : is_tensor_dynamic> {}; template struct is_tensor_dynamic> : std::true_type {}; template static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic::value; namespace detail { template struct tensor_with_storage {}; template <> struct tensor_with_storage { template using type = tensor, Dims...>; }; template <> struct tensor_with_storage { template using type = tensor>, Dims...>; }; template struct tensor_from_shape {}; template struct tensor_from_shape> { using type = tensor; }; template struct tensor_from_shape> { using type = tensor; }; } // namespace detail template struct tensor_traits {}; template struct tensor_traits> { using tensor_type = tensor; using value_type = typename tensor_storage::value_type; using storage_type = typename tensor_storage::storage_type; static constexpr auto rank = sizeof...(Dims); static constexpr auto dynamic = is_tensor_dynamic_v; static constexpr auto inline_storage = is_storage_inline_v; 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 using with_storage_t = typename detail::tensor_with_storage::template type; }; template constexpr decltype(auto) tensor_from_shape(const Shape& shape) { typename detail::tensor_from_shape>::type tensor; if constexpr (!detail::is_static_shape_v) { tensor.set_dimensions(shape); } return tensor; } } // namespace tulip #endif // TULIP_TENSOR_TRAITS_HPP