Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Tensor.hpp
Go to the documentation of this file.
1
4// TODO: make normalize flat offset more explicit
5
6#pragma once
7
8#include <array>
9#include <cstddef>
10#include <type_traits>
11#include <vector>
12
17
18namespace stratax::container {
19
51template<typename T>
52requires DType<T>
53class Tensor : public core::ArrayBase<T>
54{
55public:
78
79protected:
82
83public:
85 using core::ArrayBase<T>::at;
86
91 Tensor() : Tensor(core::Shape{0}) {}
92
101 explicit Tensor(const core::Shape& shape)
102 : core::ArrayBase<T>(shape)
103 {}
104
115 : core::ArrayBase<T>(shape, value)
116 {}
117
128 template<typename... Rest>
129 requires ((std::is_integral_v<Rest>) && ...)
130 reference operator()(size_type first, Rest... rest)
131 {
132 std::array<size_type, sizeof...(Rest) + 1> indices{
133 first,
134 static_cast<size_type>(rest)...
135 };
136
137 return (*this)[indexing::offset(this->strides(), indices)];
138 }
139
150 template<typename... Rest>
151 requires ((std::is_integral_v<Rest>) && ...)
152 const_reference operator()(size_type first, Rest... rest) const
153 {
154 std::array<size_type, sizeof...(Rest) + 1> indices{
155 first,
156 static_cast<size_type>(rest)...
157 };
158
159 return (*this)[indexing::offset(this->strides(), indices)];
160 }
161
169 reference operator()(const std::vector<size_type>& indices) {return (*this)[indexing::offset(this->strides(), indices)];}
170
178 const_reference operator()(const std::vector<size_type>& indices) const {return (*this)[indexing::offset(this->strides(), indices)];}
179
193 template<typename... Rest>
194 requires ((std::is_integral_v<Rest>) && ...)
195 reference at(difference_type first, Rest... rest)
196 {
197 std::array<difference_type, sizeof...(Rest) + 1> raw_indices{
198 first,
199 static_cast<difference_type>(rest)...
200 };
201
202 return (*this)[normalized_flat_offset(raw_indices)];
203 }
204
218 template<typename... Rest>
219 requires ((std::is_integral_v<Rest>) && ...)
220 const_reference at(difference_type first, Rest... rest) const
221 {
222 std::array<difference_type, sizeof...(Rest) + 1> raw_indices{
223 first,
224 static_cast<difference_type>(rest)...
225 };
226
227 return (*this)[normalized_flat_offset(raw_indices)];
228 }
229
237 reference at(const std::vector<difference_type>& raw_indices)
238 {
239 return (*this)[normalized_flat_offset(raw_indices)];
240 }
241
249 const_reference at(const std::vector<difference_type>& raw_indices) const
250 {
251 return (*this)[normalized_flat_offset(raw_indices)];
252 }
253
259 void swap(Tensor& other) noexcept {core::ArrayBase<T>::swap(other);}
260
267 friend void swap(Tensor& lhs, Tensor& rhs) noexcept {lhs.swap(rhs);}
268};
269
270} // namespace stratax::container
Shared owning storage and row-major metadata for containers.
Dtype and array interface concepts.
Unchecked offsets from element strides.
Immutable dimension metadata and checked row-major strides.
Arbitrary-rank owning array of supported dtype values.
Definition Tensor.hpp:54
typename core::ArrayBase< T >::difference_type difference_type
Signed type used for checked indices and iterator distances.
Definition Tensor.hpp:61
reference at(difference_type first, Rest... rest)
Returns an element using checked variadic indices.
Definition Tensor.hpp:195
void swap(Tensor &other) noexcept
Exchanges storage and layout metadata with other.
Definition Tensor.hpp:259
typename core::ArrayBase< T >::pointer pointer
Mutable element pointer type.
Definition Tensor.hpp:67
Tensor(const core::Shape &shape, const_reference value)
Constructs a tensor filled with copies of value.
Definition Tensor.hpp:114
typename core::ArrayBase< T >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition Tensor.hpp:75
typename core::ArrayBase< T >::reference reference
Mutable element reference type.
Definition Tensor.hpp:63
typename core::ArrayBase< T >::const_pointer const_pointer
Read-only element pointer type.
Definition Tensor.hpp:69
reference at(const std::vector< difference_type > &raw_indices)
Returns an element using checked vector-based indices.
Definition Tensor.hpp:237
Tensor()
Constructs an empty rank-one tensor with shape {0}.
Definition Tensor.hpp:91
typename core::ArrayBase< T >::value_type value_type
Stored element type inherited from ArrayBase.
Definition Tensor.hpp:57
const_reference operator()(size_type first, Rest... rest) const
Returns an element using unchecked variadic indices.
Definition Tensor.hpp:152
typename core::ArrayBase< T >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition Tensor.hpp:77
friend void swap(Tensor &lhs, Tensor &rhs) noexcept
Exchanges two tensors using argument-dependent lookup.
Definition Tensor.hpp:267
const_reference operator()(const std::vector< size_type > &indices) const
Returns an element using unchecked vector-based indices.
Definition Tensor.hpp:178
typename core::ArrayBase< T >::size_type size_type
Unsigned type used for element counts and normalized indices.
Definition Tensor.hpp:59
reference operator()(size_type first, Rest... rest)
Returns an element using unchecked variadic indices.
Definition Tensor.hpp:130
typename core::ArrayBase< T >::iterator iterator
Mutable contiguous random-access iterator type.
Definition Tensor.hpp:71
typename core::ArrayBase< T >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition Tensor.hpp:73
typename core::ArrayBase< T >::const_reference const_reference
Read-only element reference type.
Definition Tensor.hpp:65
const_reference at(difference_type first, Rest... rest) const
Returns an element using checked variadic indices.
Definition Tensor.hpp:220
reference operator()(const std::vector< size_type > &indices)
Returns an element using unchecked vector-based indices.
Definition Tensor.hpp:169
Tensor(const core::Shape &shape)
Constructs value-initialized storage for an arbitrary shape.
Definition Tensor.hpp:101
const_reference at(const std::vector< difference_type > &raw_indices) const
Returns an element using checked vector-based indices.
Definition Tensor.hpp:249
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:41
typename Buffer< T >::value_type value_type
Stored element type.
Definition ArrayBase.hpp:44
typename Buffer< value_type >::pointer pointer
Mutable element pointer type.
Definition ArrayBase.hpp:54
typename Buffer< value_type >::const_reference const_reference
Read-only element reference type.
Definition ArrayBase.hpp:52
const Shape & shape() const noexcept
Returns the logical shape metadata.
Definition ArrayBase.hpp:82
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:62
typename Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition ArrayBase.hpp:64
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:56
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:48
typename Buffer< value_type >::iterator iterator
Mutable contiguous random-access iterator type.
Definition ArrayBase.hpp:58
typename Buffer< value_type >::const_iterator const_iterator
Read-only contiguous random-access iterator type.
Definition ArrayBase.hpp:60
typename Buffer< value_type >::reference reference
Mutable element reference type.
Definition ArrayBase.hpp:50
ArrayBase(const ArrayBase &)=default
Copies storage and layout into independent buffers.
const Shape & strides() const noexcept
Returns the row-major stride metadata.
Definition ArrayBase.hpp:85
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:46
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
Supported numeric or boolean element dtype.
Definition Concepts.hpp:74