From a47302d532cceb402fd83a123f6f96b3b7a28c09 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Wed, 28 Oct 2026 18:42:54 +0100 Subject: [PATCH] fix(math): fix tensor.hpp --- include/cenet/math/tensor.hpp | 50 +++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 1ff5a05..e43e987 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -36,7 +36,7 @@ public: tensor() { if constexpr (traits::dynamic) { m_items = nullptr; - } else if constexpr (traits::inplace_storage) { + } else if constexpr (traits::inline_storage) { std::memset(m_items, 0, sizeof(m_items)); } else { m_items = new value_type[traits::size]; @@ -52,24 +52,31 @@ public: ~tensor() { release(); } - tensor(tensor&& other) noexcept { *this = std::move(other); } + tensor(tensor&& other) noexcept { + if constexpr (!traits::inline_storage) m_items = nullptr; + *this = std::move(other); + } + tensor& operator=(tensor&& other) noexcept { if (this == &other) return *this; release(); m_items = other.m_items; - std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions)); - std::memcpy(m_strides, other.m_strides, sizeof(m_strides)); - - other.m_items = nullptr; + std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions)); + std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides)); + if constexpr (!traits::inline_storage) other.m_items = nullptr; return *this; } - tensor(const tensor& other) { *this = other; } + tensor(const tensor& other) { + if constexpr (!traits::inline_storage) m_items = nullptr; + *this = other; + } + tensor& operator=(const tensor& other) { if (this == &other) return *this; release(); - std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions)); - std::memcpy(m_strides, other.m_strides, sizeof(m_strides)); + std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions)); + std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides)); dyn_alloc(std::make_index_sequence{}); dyn_copy(std::make_index_sequence{}); @@ -80,7 +87,7 @@ public: typename = std::enable_if_t<( traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v && ...))>> void set_dimensions(RtDims... dimensions) { - if constexpr (!valid_dims(std::make_index_sequence{}, dimensions...)) + if (!valid_dims(std::make_index_sequence{}, dimensions...)) throw std::runtime_error("invalid dynamic tensor dimensions"); store_dims(std::make_index_sequence{}, dimensions...); @@ -100,30 +107,39 @@ private: } private: template - constexpr void store_dims(std::index_sequence, RtDims... dimensions) const { - return ((m_dimensions[Is] = dimensions) && ...); + constexpr void store_dims(std::index_sequence, RtDims... dimensions) { + ((m_shape.dimensions[Is] = static_cast(dimensions)), ...); } private: template void dyn_alloc(std::index_sequence) { - m_items = new value_type[(m_dimensions[Is] * ...)]; + m_items = new value_type[(m_shape.dimensions[Is] * ...)]; } template void dyn_copy(const value_type* src, std::index_sequence) { - std::memcpy(m_items, src, sizeof(*m_items) * (m_dimensions[Is] * ... * 1)); + std::memcpy(m_items, src, sizeof(*m_items) * (m_shape.dimensions[Is] * ... * 1)); } void release() { if constexpr (!traits::inline_storage) { delete[] m_items; + m_items = nullptr; } } private: - traits::storage_type m_items; + struct empty_shape {}; - std::enable_if_t m_dimensions; - std::enable_if_t m_strides; + template + struct dynamic_shape { + std::size_t dimensions[Rank]; + std::size_t strides[Rank]; + }; + + using shape_type = std::conditional_t, empty_shape>; +private: + traits::storage_type m_items; + shape_type m_shape; }; template