fix(math): fix tensor and tensor_view

This commit is contained in:
2026-11-01 11:17:44 +01:00
parent f3e2fb7c58
commit a264009629
2 changed files with 23 additions and 7 deletions
+15 -3
View File
@@ -75,7 +75,7 @@ public:
tensor(const tensor& other)
: m_shape(other.m_shape) {
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
dyn_copy(other.m_items, std::make_index_sequence<sizeof...(Dims)>{});
}
tensor& operator=(const tensor& other) {
@@ -178,6 +178,16 @@ public:
explicit operator view_type() { return view(); }
explicit operator constant_view_type() const { return view(); }
public:
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
decltype(auto) operator[](Off off) {
return view()[off];
}
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
decltype(auto) operator[](Off off) const {
return view()[off];
}
public:
template <typename... RtDims,
typename = std::enable_if_t<(
@@ -250,12 +260,14 @@ private:
private:
template <std::size_t... Is>
void dyn_alloc(std::index_sequence<Is...>) {
m_items = new value_type[(m_shape.dimensions[Is] * ...)];
if constexpr (traits::dynamic) {
m_items = new value_type[size()];
}
}
template <std::size_t... Is>
void dyn_copy(const value_type* src, std::index_sequence<Is...>) {
std::memcpy(m_items, src, sizeof(*m_items) * (m_shape.dimensions[Is] * ... * 1));
std::memcpy(m_items, src, sizeof(*m_items) * size());
}
void release() {
+6 -2
View File
@@ -33,7 +33,9 @@ public:
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
decltype(auto) slice(Offs... offs) {
auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
if constexpr (sizeof...(Offs) == sizeof...(Dims))
return *ptr;
else
return inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
@@ -42,7 +44,9 @@ public:
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
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;
if constexpr (sizeof...(Offs) == sizeof...(Dims))
return *ptr;
else
return inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
public: