Stratax 0.3.1
Loading...
Searching...
No Matches
ArrayBase.hpp
1#pragma once
2
3#include <cstddef>
4#include <utility>
5
6#include <stratax/core/Buffer.hpp>
7#include <stratax/core/Shape.hpp>
8#include <stratax/exceptions/Exceptions.hpp>
9#include <stratax/indexing/Normalize.hpp>
10#include <stratax/core/dtypes/DTypeTraits.hpp>
11#include <stratax/core/dtypes/Concepts.hpp>
12
13namespace stratax::core {
14
36template<DType T>
38{
39public:
62
64 [[nodiscard]] static constexpr std::string_view dtype() noexcept
65 {
67 }
68
70 [[nodiscard]] size_type size() const noexcept {return buffer_.size();}
71
73 [[nodiscard]] bool empty() const noexcept {return buffer_.empty();}
74
76 [[nodiscard]] size_type rank() const noexcept {return shape_.rank();}
77
79 [[nodiscard]] const Shape& shape() const noexcept {return shape_;}
80
82 [[nodiscard]] const Shape& strides() const noexcept {return strides_;}
83
89 [[nodiscard]] pointer data() noexcept {return buffer_.data();}
95 [[nodiscard]] const_pointer data() const noexcept {return buffer_.data();}
96
99 {
100 if (empty())
101 {
102 throw Exceptions::IndexError("Cannot access the front of an empty array.");
103 }
104
105 return buffer_.front();
106 }
109 {
110 if (empty())
111 {
112 throw Exceptions::IndexError("Cannot access the front of an empty array.");
113 }
114
115 return buffer_.front();
116 }
117
120 {
121 if (empty())
122 {
123 throw Exceptions::IndexError("Cannot access the back of an empty array.");
124 }
125
126 return buffer_.back();
127 }
130 {
131 if (empty())
132 {
133 throw Exceptions::IndexError("Cannot access the back of an empty array.");
134 }
135
136 return buffer_.back();
137 }
138
140 reference operator[](size_type index) noexcept {return buffer_[index];}
142 const_reference operator[](size_type index) const noexcept {return buffer_[index];}
143
150 reference at(difference_type index) {return buffer_[indexing::normalize_index(index, size())];}
157 const_reference at(difference_type index) const {return buffer_[indexing::normalize_index(index, size())];}
158
160 iterator begin() noexcept {return buffer_.begin();}
162 const_iterator begin() const noexcept {return buffer_.begin();}
164 const_iterator cbegin() const noexcept {return buffer_.cbegin();}
166 iterator end() noexcept {return buffer_.end();}
168 const_iterator end() const noexcept {return buffer_.end();}
170 const_iterator cend() const noexcept {return buffer_.cend();}
172 reverse_iterator rbegin() noexcept {return buffer_.rbegin();}
174 const_reverse_iterator rbegin() const noexcept {return buffer_.rbegin();}
176 const_reverse_iterator crbegin() const noexcept {return buffer_.crbegin();}
178 reverse_iterator rend() noexcept {return buffer_.rend();}
180 const_reverse_iterator rend() const noexcept {return buffer_.rend();}
182 const_reverse_iterator crend() const noexcept {return buffer_.crend();}
183
190 void fill(const_reference value) {buffer_.fill(value);}
191
201 void swap(ArrayBase& other) noexcept
202 {
203 buffer_.swap(other.buffer_);
204 shape_.swap(other.shape_);
205 strides_.swap(other.strides_);
206 }
207
208protected:
218 explicit ArrayBase(const Shape& shape)
219 : buffer_(shape.elements()),
220 shape_(shape),
221 strides_(shape.strides())
222 {}
223
235 : buffer_(shape.elements(), value),
236 shape_(shape),
237 strides_(shape.strides())
238 {}
239
256 : buffer_(std::move(buffer)),
257 shape_(shape),
258 strides_(shape.strides())
259 {
260 if (buffer_.size() != shape_.elements())
261 {
263 "Buffer size does not match the shape's element count.");
264 }
265 }
266
282 template<typename IndexContainer>
283 size_type normalized_flat_offset(const IndexContainer& raw_indices) const
284 {
285 if (raw_indices.size() != rank())
286 {
288 "The number of indices must match the array rank.");
289 }
290
291 size_type offset = 0;
292
293 for (size_type i = 0; i < rank(); ++i)
294 {
295 const size_type index = indexing::normalize_index(
296 raw_indices[i], shape_[i]);
297 offset += index * strides_[i];
298 }
299
300 return offset;
301 }
302
303private:
304 Buffer<value_type> buffer_;
305 Shape shape_;
306 Shape strides_;
307};
308
309}
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:38
reference back()
Returns the final element.
ArrayBase(const Shape &shape, const_reference value)
Constructs storage filled with value for shape.
reverse_iterator rend() noexcept
Returns the past-the-end mutable reverse iterator.
typename Buffer< T >::value_type value_type
Stored element type.
Definition ArrayBase.hpp:41
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final element.
const_iterator end() const noexcept
Returns a const iterator past the final element.
const_iterator begin() const noexcept
Returns a const iterator to the first element.
typename Buffer< value_type >::pointer pointer
Mutable element pointer type.
Definition ArrayBase.hpp:51
ArrayBase(const Shape &shape, Buffer< value_type > &&buffer)
Adopts an existing buffer for shape.
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.
reference operator[](size_type index) noexcept
Returns an element without bounds checking.
const Shape & shape() const noexcept
Returns the logical shape metadata.
Definition ArrayBase.hpp:79
size_type size() const noexcept
Returns the number of stored elements.
Definition ArrayBase.hpp:70
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:59
bool empty() const noexcept
Reports whether no elements are stored.
Definition ArrayBase.hpp:73
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
void fill(const_reference value)
Assigns value to every stored element.
typename Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only reverse iterator type.
Definition ArrayBase.hpp:61
const_pointer data() const noexcept
Returns a pointer to contiguous read-only element storage.
Definition ArrayBase.hpp:95
const_iterator cend() const noexcept
Returns a const iterator past the final element.
pointer data() noexcept
Returns a pointer to contiguous mutable element storage.
Definition ArrayBase.hpp:89
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:53
size_type rank() const noexcept
Returns the number of logical dimensions.
Definition ArrayBase.hpp:76
const_reference operator[](size_type index) const noexcept
Returns an element without bounds checking.
iterator begin() noexcept
Returns a mutable iterator to the first element.
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
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the final element.
static constexpr std::string_view dtype() noexcept
Returns the canonical name of the stored element dtype.
Definition ArrayBase.hpp:64
reference front()
Returns the first element.
Definition ArrayBase.hpp:98
reference at(difference_type index)
Returns an element using checked, Python-style flat indexing.
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_reverse_iterator crend() const noexcept
Returns the past-the-end const reverse iterator.
const_reference front() const
Returns the first element.
iterator end() noexcept
Returns a mutable iterator past the final element.
reverse_iterator rbegin() noexcept
Returns a mutable reverse iterator to the final element.
const_reference back() const
Returns the final element.
const_reverse_iterator rend() const noexcept
Returns the past-the-end const reverse iterator.
const_reference at(difference_type index) const
Returns an element using checked, Python-style flat indexing.
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
Fixed-size owner of aligned, contiguous element storage.
Definition Buffer.hpp:51
const_pointer const_iterator
Read-only contiguous random-access iterator type.
Definition Buffer.hpp:74
std::reverse_iterator< iterator > reverse_iterator
Mutable iterator that traverses elements in reverse order.
Definition Buffer.hpp:76
reverse_iterator rend() noexcept
Returns the past-the-end iterator for mutable reverse traversal.
Definition Buffer.hpp:463
void swap(Buffer &other) noexcept
Swaps storage and size with another buffer.
Definition Buffer.hpp:523
T & reference
Mutable element reference type.
Definition Buffer.hpp:64
size_type size() const noexcept
Returns the number of stored elements.
Definition Buffer.hpp:486
T * pointer
Mutable pointer to an element.
Definition Buffer.hpp:68
const_reverse_iterator crend() const noexcept
Returns the past-the-end iterator for const reverse traversal.
Definition Buffer.hpp:475
std::ptrdiff_t difference_type
Signed type used for distances between iterators.
Definition Buffer.hpp:62
reference front()
Returns a reference to the first element.
Definition Buffer.hpp:334
const_iterator cend() const noexcept
Returns a const iterator one past the final element.
Definition Buffer.hpp:439
pointer iterator
Mutable contiguous random-access iterator type.
Definition Buffer.hpp:72
bool empty() const noexcept
Returns whether the buffer has no elements.
Definition Buffer.hpp:494
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final element.
Definition Buffer.hpp:457
std::size_t size_type
Unsigned type used for element counts and indices.
Definition Buffer.hpp:60
reverse_iterator rbegin() noexcept
Returns a mutable reverse iterator to the final element.
Definition Buffer.hpp:445
void fill(const_reference value)
Assigns a value to every element in the buffer.
Definition Buffer.hpp:507
pointer data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition Buffer.hpp:386
iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition Buffer.hpp:409
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition Buffer.hpp:421
const T * const_pointer
Read-only pointer to an element.
Definition Buffer.hpp:70
reference back()
Returns a reference to the last element.
Definition Buffer.hpp:358
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only iterator that traverses elements in reverse order.
Definition Buffer.hpp:78
const T & const_reference
Read-only element reference type.
Definition Buffer.hpp:66
iterator end() noexcept
Returns a mutable iterator one past the final element.
Definition Buffer.hpp:427
T value_type
Type of each stored element.
Definition Buffer.hpp:58
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
size_type elements() const
Computes the total number of elements described by the shape.
Definition Shape.hpp:91
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:121
void swap(Shape &other) noexcept
Exchanges dimension storage with another shape.
Definition Shape.hpp:268
Provides compile-time metadata for a supported Stratax dtype.