From 961067c28d04c823811602b9b0307af66c302fb8 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 20:44:00 +0100 Subject: [PATCH] refactor(math): move calculate_offset to shapes --- include/cenet/math/tensor_shape.hpp | 44 +++++++++++++++++++++++++++++ include/cenet/math/tensor_view.hpp | 17 ++--------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/include/cenet/math/tensor_shape.hpp b/include/cenet/math/tensor_shape.hpp index 06fb34d..0b0a6d6 100644 --- a/include/cenet/math/tensor_shape.hpp +++ b/include/cenet/math/tensor_shape.hpp @@ -63,6 +63,33 @@ private: struct get_impl<0, First, Rest...> { static constexpr auto value = First; }; +private: + template + struct size_impl { + static constexpr auto value = S; + }; + + template + struct size_impl : size_impl {}; +public: + static constexpr auto size = size_impl<1, Dims...>::value; + + template + std::size_t calculate_offset(const std::tuple& offs) const { + static_assert(sizeof...(Offs) > 0); + static_assert(sizeof...(Offs) <= sizeof...(Dims)); + + return calculate_offset_impl(offs); + } +private: + template + std::size_t calculate_offset_impl(const std::tuple& offs) const { + if constexpr (I == 0) { + return std::get<0>(offs); + } else { + return (calculate_offset_impl(offs) * get()) + std::get(offs); + } + } }; template @@ -141,6 +168,23 @@ private: public: static constexpr auto rank = sizeof...(Dims); static constexpr auto score = score_impl<0, Dims...>::value; + + template + std::size_t calculate_offset(const std::tuple& offs) const { + static_assert(sizeof...(Offs) > 0); + static_assert(sizeof...(Offs) <= sizeof...(Dims)); + + return calculate_offset_impl(offs); + } +private: + template + std::size_t calculate_offset_impl(const std::tuple& offs) const { + if constexpr (I == 0) { + return std::get<0>(offs); + } else { + return (calculate_offset_impl(offs) * get()) + std::get(offs); + } + } }; namespace detail { diff --git a/include/cenet/math/tensor_view.hpp b/include/cenet/math/tensor_view.hpp index 4ad57d3..bbd3887 100644 --- a/include/cenet/math/tensor_view.hpp +++ b/include/cenet/math/tensor_view.hpp @@ -33,7 +33,7 @@ public: typename = std::enable_if_t<( sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> decltype(auto) slice(Offs... offs) { - auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); + auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...)); if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; else @@ -44,7 +44,7 @@ public: typename = std::enable_if_t<( sizeof...(Offs) > 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...)); + const auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...)); if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; else @@ -60,19 +60,6 @@ public: 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{}); }