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(
199 raw_indices,
200 "Tensor multi-index rank must match tensor rank.",
201 "Tensor multi-index component is out of bounds.")];
202 }
203
217 template<typename... Rest>
218 requires ((std::is_integral_v<Rest>) && ...)
219 const_reference at(difference_type first, Rest... rest) const
220 {
221 std::array<difference_type, sizeof...(Rest) + 1> raw_indices{
222 first,
223 static_cast<difference_type>(rest)...
224 };
225
226 return (*this)[normalized_flat_offset(
227 raw_indices,
228 "Tensor multi-index rank must match tensor rank.",
229 "Tensor multi-index component is out of bounds.")];
230 }
231
239 reference at(const std::vector<difference_type>& raw_indices)
240 {
241 return (*this)[normalized_flat_offset(
242 raw_indices,
243 "Tensor multi-index rank must match tensor rank.",
244 "Tensor multi-index component is out of bounds.")];
245 }
246
254 const_reference at(const std::vector<difference_type>& raw_indices) const
255 {
256 return (*this)[normalized_flat_offset(
257 raw_indices,
258 "Tensor multi-index rank must match tensor rank.",
259 "Tensor multi-index component is out of bounds.")];
260 }
261
267 void swap(Tensor& other) noexcept {core::ArrayBase<T>::swap(other);}
268
275 friend void swap(Tensor& lhs, Tensor& rhs) noexcept {lhs.swap(rhs);}
276};
277
278} // namespace stratax::container
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:267
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:239
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:275
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:219
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:254
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
size_type normalized_flat_offset(const IndexContainer &raw_indices, const char *rank_mismatch_message="Multi-index rank must match array rank.", const char *component_oob_message=nullptr) const
Converts checked signed multidimensional indices to a flat offset.
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:53
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