From bdb019864023473579ea0f3904cf418a2604ff06 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 30 Oct 2026 16:04:10 +0100 Subject: [PATCH] refactor(math): move tensor_view to new file Move tensor_view to its own new file and add fill() functions. --- include/cenet/math/tensor.hpp | 104 +---------------------- include/cenet/math/tensor_view.hpp | 130 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 103 deletions(-) create mode 100644 include/cenet/math/tensor_view.hpp diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index c63e901..0579961 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -5,6 +5,7 @@ #include "tensor_fwd.hpp" #include "tensor_shape.hpp" #include "tensor_traits.hpp" +#include "tensor_view.hpp" #include #include @@ -15,109 +16,6 @@ namespace cenet { namespace math { -template -class tensor_view { - static_assert(std::is_arithmetic_v); - - template - friend class tensor; - - template - friend class tensor_view; -public: - using traits = tensor_traits>; - using shape_type = std::conditional_t, static_shape>; - using value_type = T; -public: - template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> - decltype(auto) slice(Offs... offs) { - auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); - if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; - return inner_view(ptr, std::index_sequence{}); - } - - template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> - decltype(auto) slice(Offs... offs) const { - const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); - if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; - return inner_view(ptr, std::index_sequence{}); - } -public: - template >> - decltype(auto) operator[](Off off) { - return slice(off); - } - - template >> - decltype(auto) operator[](Off off) const { - return slice(off); - } -private: - template - std::size_t calculate_offset(const std::tuple& offs) const { - return calculate_offset_impl(offs); - } - - template - std::size_t calculate_offset_impl(const std::tuple& offs) const { - if constexpr (I == 0) - return std::get(offs) * dimension(); - else - return (calculate_offset_impl(offs) + std::get(offs)) * dimension(); - } -public: - constexpr std::size_t size() const { return size_impl(std::make_index_sequence{}); } - - auto dimensions() const { return dimensions_impl(std::make_index_sequence{}); } -private: - template - constexpr std::size_t size_impl(std::index_sequence) const { - return (dimension() * ... * 1); - } - - template - constexpr auto dimensions_impl(std::index_sequence) const { - return std::make_tuple(dimension()...); - } -public: - value_type& coeff(std::size_t index) { return m_view[index]; } - const value_type& coeff(std::size_t index) const { return m_view[index]; } - - value_type* data() { return m_view; } - const value_type* data() const { return m_view; } - - const shape_type& shape() const { return m_shape; } -private: - template - auto inner_view(value_type* view, std::index_sequence) { - if constexpr (Off == 0) - return tensor_view{ view, m_shape }; - else - return inner_view(view, std::index_sequence{}); - } -private: - template - constexpr std::size_t dimension() const { - if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) { - return m_shape.dimensions[I]; - } else { - return traits::dimensions[I]; - } - } -private: - template - tensor_view(value_type* view, Fwd&& shape) - : m_view(view), m_shape(std::forward(shape)) {} -private: - value_type* m_view; - - shape_type m_shape; -}; - template class tensor { public: diff --git a/include/cenet/math/tensor_view.hpp b/include/cenet/math/tensor_view.hpp new file mode 100644 index 0000000..22dcde5 --- /dev/null +++ b/include/cenet/math/tensor_view.hpp @@ -0,0 +1,130 @@ +#ifndef CENET_MATH_TENSOR_VIEW_HPP +#define CENET_MATH_TENSOR_VIEW_HPP + +#include "expr.hpp" +#include "tensor_fwd.hpp" +#include "tensor_shape.hpp" +#include "tensor_traits.hpp" + +#include +#include +#include +#include + +namespace cenet { +namespace math { + +template +class tensor_view { + static_assert(std::is_arithmetic_v); + + template + friend class tensor; + + template + friend class tensor_view; +public: + using traits = tensor_traits>; + using shape_type = std::conditional_t, static_shape>; + using value_type = T; +public: + template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> + decltype(auto) slice(Offs... offs) { + auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); + if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; + return inner_view(ptr, std::index_sequence{}); + } + + template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> + decltype(auto) slice(Offs... offs) const { + const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); + if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; + return inner_view(ptr, std::index_sequence{}); + } +public: + template >> + decltype(auto) operator[](Off off) { + return slice(off); + } + + template >> + decltype(auto) operator[](Off off) const { + return slice(off); + } +private: + template + std::size_t calculate_offset(const std::tuple& offs) const { + return calculate_offset_impl(offs); + } + + template + std::size_t calculate_offset_impl(const std::tuple& offs) const { + if constexpr (I == 0) + return std::get(offs) * dimension(); + else + return (calculate_offset_impl(offs) + std::get(offs)) * dimension(); + } +public: + constexpr std::size_t size() const { return size_impl(std::make_index_sequence{}); } + + auto dimensions() const { return dimensions_impl(std::make_index_sequence{}); } +private: + template + constexpr std::size_t size_impl(std::index_sequence) const { + return (dimension() * ... * 1); + } + + template + constexpr auto dimensions_impl(std::index_sequence) const { + return std::make_tuple(dimension()...); + } +public: + void fill(const value_type* source, std::size_t offset = 0, std::size_t length = dynamic_extent) { + offset = std::min(offset, size()); + std::memcpy(m_view + offset, source, std::min(length, size() - offset)); + } + + void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size()); } +public: + value_type& coeff(std::size_t index) { return m_view[index]; } + const value_type& coeff(std::size_t index) const { return m_view[index]; } + + value_type* data() { return m_view; } + const value_type* data() const { return m_view; } + + const shape_type& shape() const { return m_shape; } +private: + template + auto inner_view(value_type* view, std::index_sequence) { + if constexpr (Off == 0) + return tensor_view{ view, m_shape }; + else + return inner_view(view, std::index_sequence{}); + } +private: + template + constexpr std::size_t dimension() const { + if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) { + return m_shape.dimensions[I]; + } else { + return traits::dimensions[I]; + } + } +private: + template + tensor_view(value_type* view, Fwd&& shape) + : m_view(view), m_shape(std::forward(shape)) {} +private: + value_type* m_view; + + shape_type m_shape; +}; + +} // namespace math +} // namespace cenet + +#endif // CENET_MATH_TENSOR_VIEW_HPP