Stratax 0.3.1
Loading...
Searching...
No Matches
Tensor.hpp
1// TODO: make normalize flat offset more explicit
2
3#pragma once
4
5#include <array>
6#include <cstddef>
7#include <type_traits>
8#include <vector>
9
10#include <stratax/core/dtypes/Concepts.hpp>
11#include <stratax/core/ArrayBase.hpp>
12#include <stratax/core/Shape.hpp>
13#include <stratax/indexing/Indexing.hpp>
14
15namespace stratax::container {
16
47template<typename T>
48requires DType<T>
49class Tensor : public core::ArrayBase<T>
50{
51public:
74
75protected:
78
79public:
81 using core::ArrayBase<T>::at;
82
87 Tensor() : Tensor(core::Shape{0}) {}
88
97 explicit Tensor(const core::Shape& shape)
98 : core::ArrayBase<T>(shape)
99 {}
100
111 : core::ArrayBase<T>(shape, value)
112 {}
113
124 template<typename... Rest>
125 requires ((std::is_integral_v<Rest>) && ...)
126 reference operator()(size_type first, Rest... rest)
127 {
128 std::array<size_type, sizeof...(Rest) + 1> indices{
129 first,
130 static_cast<size_type>(rest)...
131 };
132
133 return (*this)[indexing::offset(this->strides(), indices)];
134 }
135
146 template<typename... Rest>
147 requires ((std::is_integral_v<Rest>) && ...)
148 const_reference operator()(size_type first, Rest... rest) const
149 {
150 std::array<size_type, sizeof...(Rest) + 1> indices{
151 first,
152 static_cast<size_type>(rest)...
153 };
154
155 return (*this)[indexing::offset(this->strides(), indices)];
156 }
157
165 reference operator()(const std::vector<size_type>& indices) {return (*this)[indexing::offset(this->strides(), indices)];}
166
174 const_reference operator()(const std::vector<size_type>& indices) const {return (*this)[indexing::offset(this->strides(), indices)];}
175
189 template<typename... Rest>
190 requires ((std::is_integral_v<Rest>) && ...)
191 reference at(difference_type first, Rest... rest)
192 {
193 std::array<difference_type, sizeof...(Rest) + 1> raw_indices{
194 first,
195 static_cast<difference_type>(rest)...
196 };
197
198 return (*this)[normalized_flat_offset(raw_indices)];
199 }
200
214 template<typename... Rest>
215 requires ((std::is_integral_v<Rest>) && ...)
216 const_reference at(difference_type first, Rest... rest) const
217 {
218 std::array<difference_type, sizeof...(Rest) + 1> raw_indices{
219 first,
220 static_cast<difference_type>(rest)...
221 };
222
223 return (*this)[normalized_flat_offset(raw_indices)];
224 }
225
233 reference at(const std::vector<difference_type>& raw_indices)
234 {
235 return (*this)[normalized_flat_offset(raw_indices)];
236 }
237
245 const_reference at(const std::vector<difference_type>& raw_indices) const
246 {
247 return (*this)[normalized_flat_offset(raw_indices)];
248 }
249
255 void swap(Tensor& other) noexcept {core::ArrayBase<T>::swap(other);}
256
263 friend void swap(Tensor& lhs, Tensor& rhs) noexcept {lhs.swap(rhs);}
264};
265
266} // namespace stratax::container
Arbitrary-rank owning array of numeric values.
Definition Tensor.hpp:50
typename core::ArrayBase< T >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition Tensor.hpp:57
reference at(difference_type first, Rest... rest)
Returns an element using checked variadic indices.
Definition Tensor.hpp:191
void swap(Tensor &other) noexcept
Exchanges storage and layout metadata with other.
Definition Tensor.hpp:255
typename core::ArrayBase< T >::pointer pointer
Mutable element pointer type.
Definition Tensor.hpp:63
Tensor(const core::Shape &shape, const_reference value)
Constructs a tensor filled with copies of value.
Definition Tensor.hpp:110
typename core::ArrayBase< T >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition Tensor.hpp:71
typename core::ArrayBase< T >::reference reference
Mutable element reference type.
Definition Tensor.hpp:59
typename core::ArrayBase< T >::const_pointer const_pointer
Read-only element pointer type.
Definition Tensor.hpp:65
reference at(const std::vector< difference_type > &raw_indices)
Returns an element using checked vector-based indices.
Definition Tensor.hpp:233
Tensor()
Constructs an empty rank-one tensor with shape {0}.
Definition Tensor.hpp:87
typename core::ArrayBase< T >::value_type value_type
Stored element type inherited from ArrayBase.
Definition Tensor.hpp:53
const_reference operator()(size_type first, Rest... rest) const
Returns an element using unchecked variadic indices.
Definition Tensor.hpp:148
typename core::ArrayBase< T >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition Tensor.hpp:73
friend void swap(Tensor &lhs, Tensor &rhs) noexcept
Exchanges two tensors using argument-dependent lookup.
Definition Tensor.hpp:263
const_reference operator()(const std::vector< size_type > &indices) const
Returns an element using unchecked vector-based indices.
Definition Tensor.hpp:174
typename core::ArrayBase< T >::size_type size_type
Unsigned type used for element counts and normalized indices.
Definition Tensor.hpp:55
reference operator()(size_type first, Rest... rest)
Returns an element using unchecked variadic indices.
Definition Tensor.hpp:126
typename core::ArrayBase< T >::iterator iterator
Mutable contiguous random-access iterator type.
Definition Tensor.hpp:67
typename core::ArrayBase< T >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition Tensor.hpp:69
typename core::ArrayBase< T >::const_reference const_reference
Read-only element reference type.
Definition Tensor.hpp:61
const_reference at(difference_type first, Rest... rest) const
Returns an element using checked variadic indices.
Definition Tensor.hpp:216
reference operator()(const std::vector< size_type > &indices)
Returns an element using unchecked vector-based indices.
Definition Tensor.hpp:165
Tensor(const core::Shape &shape)
Constructs value-initialized storage for an arbitrary shape.
Definition Tensor.hpp:97
const_reference at(const std::vector< difference_type > &raw_indices) const
Returns an element using checked vector-based indices.
Definition Tensor.hpp:245
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:38
typename Buffer< T >::value_type value_type
Stored element type.
Definition ArrayBase.hpp:41
typename Buffer< value_type >::pointer pointer
Mutable element pointer type.
Definition ArrayBase.hpp:51
typename Buffer< value_type >::const_reference const_reference
Read-only element reference type.
Definition ArrayBase.hpp:49
ArrayBase(const Shape &shape)
Constructs value-initialized storage for shape.
const Shape & shape() const noexcept
Returns the logical shape metadata.
Definition ArrayBase.hpp:79
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:59
typename Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition ArrayBase.hpp:61
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:53
size_type normalized_flat_offset(const IndexContainer &raw_indices) const
Converts checked signed multidimensional indices to a flat offset.
typename Buffer< value_type >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition ArrayBase.hpp:45
typename Buffer< value_type >::iterator iterator
Mutable contiguous random-access iterator type.
Definition ArrayBase.hpp:55
typename Buffer< value_type >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition ArrayBase.hpp:57
typename Buffer< value_type >::reference reference
Mutable element reference type.
Definition ArrayBase.hpp:47
const Shape & strides() const noexcept
Returns the row-major stride metadata.
Definition ArrayBase.hpp:82
void swap(ArrayBase &other) noexcept
Exchanges storage and layout metadata with other.
typename Buffer< value_type >::size_type size_type
Unsigned type used for element counts and indices.
Definition ArrayBase.hpp:43
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33