feat(math): add with_storage template to tensor

This commit is contained in:
2026-11-01 09:59:06 +01:00
parent 3dfcb754f8
commit 75243e9d08
3 changed files with 28 additions and 0 deletions
+3
View File
@@ -25,6 +25,9 @@ public:
using view_type = tensor_view<value_type, Dims...>;
using constant_view_type = tensor_view<constant_value_type, Dims...>;
using shape_type = std::conditional_t<traits::dynamic, dynamic_shape<Dims...>, static_shape<Dims...>>;
public:
template <typename ST>
using with_storage = typename traits::template with_storage_t<ST>;
public:
tensor() {
if constexpr (traits::dynamic) {
+3
View File
@@ -14,6 +14,9 @@ struct inline_storage {
using value_type = T;
};
struct heap_storage_tag {};
struct inline_storage_tag {};
template <typename T, std::size_t... Dims>
class tensor_view;
+22
View File
@@ -73,6 +73,25 @@ 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 {};
@@ -91,6 +110,9 @@ struct tensor_traits<tensor<T, Dims...>> {
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>::type;
};
} // namespace math