Compare commits

...
2 Commits
Author SHA1 Message Date
CHatingPython a264009629 fix(math): fix tensor and tensor_view 2026-11-01 11:17:44 +01:00
CHatingPython f3e2fb7c58 fix(math): fix with_storage trait 2026-11-01 10:51:49 +01:00
3 changed files with 24 additions and 8 deletions
+15 -3
View File
@@ -75,7 +75,7 @@ public:
tensor(const tensor& other) tensor(const tensor& other)
: m_shape(other.m_shape) { : m_shape(other.m_shape) {
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{}); 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) { tensor& operator=(const tensor& other) {
@@ -178,6 +178,16 @@ public:
explicit operator view_type() { return view(); } explicit operator view_type() { return view(); }
explicit operator constant_view_type() const { 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: public:
template <typename... RtDims, template <typename... RtDims,
typename = std::enable_if_t<( typename = std::enable_if_t<(
@@ -250,12 +260,14 @@ private:
private: private:
template <std::size_t... Is> template <std::size_t... Is>
void dyn_alloc(std::index_sequence<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> template <std::size_t... Is>
void dyn_copy(const value_type* src, std::index_sequence<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() { void release() {
+1 -1
View File
@@ -112,7 +112,7 @@ struct tensor_traits<tensor<T, Dims...>> {
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1); static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
template <typename ST> template <typename ST>
using with_storage_t = typename detail::tensor_with_storage<ST>::type; using with_storage_t = typename detail::tensor_with_storage<ST>::template type<T, Dims...>;
}; };
} // namespace math } // namespace math
+8 -4
View File
@@ -33,8 +33,10 @@ public:
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>> sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
decltype(auto) slice(Offs... offs) { decltype(auto) slice(Offs... offs) {
auto* ptr = m_view + calculate_offset(std::make_tuple(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 inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{}); return *ptr;
else
return inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
} }
template <typename... Offs, template <typename... Offs,
@@ -42,8 +44,10 @@ public:
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>> sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
decltype(auto) slice(Offs... offs) const { decltype(auto) slice(Offs... offs) const {
const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...)); 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 inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{}); return *ptr;
else
return inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
} }
public: public:
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>> template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>