diff --git a/include/cenet/math/tensor_shape.hpp b/include/cenet/math/tensor_shape.hpp index 0b0a6d6..8fce8c9 100644 --- a/include/cenet/math/tensor_shape.hpp +++ b/include/cenet/math/tensor_shape.hpp @@ -76,10 +76,12 @@ public: template std::size_t calculate_offset(const std::tuple& offs) const { - static_assert(sizeof...(Offs) > 0); - static_assert(sizeof...(Offs) <= sizeof...(Dims)); + constexpr std::size_t off_count = sizeof...(Offs); - return calculate_offset_impl(offs); + static_assert(off_count > 0); + static_assert(Start + off_count <= sizeof...(Dims)); + + return calculate_offset_impl(offs) * trailing_product(); } private: template @@ -90,6 +92,15 @@ private: return (calculate_offset_impl(offs) * get()) + std::get(offs); } } + + template + std::size_t trailing_product() const { + if constexpr (I == sizeof...(Dims)) { + return 1; + } else { + return get() * trailing_product(); + } + } }; template @@ -171,10 +182,12 @@ public: template std::size_t calculate_offset(const std::tuple& offs) const { - static_assert(sizeof...(Offs) > 0); - static_assert(sizeof...(Offs) <= sizeof...(Dims)); + constexpr std::size_t off_count = sizeof...(Offs); - return calculate_offset_impl(offs); + static_assert(off_count > 0); + static_assert(Start + off_count <= sizeof...(Dims)); + + return calculate_offset_impl(offs) * trailing_product(); } private: template @@ -185,6 +198,15 @@ private: return (calculate_offset_impl(offs) * get()) + std::get(offs); } } + + template + std::size_t trailing_product() const { + if constexpr (I == sizeof...(Dims)) { + return 1; + } else { + return get() * trailing_product(); + } + } }; namespace detail {