Stratax 0.4.0rc1
Loading...
Searching...
No Matches
ArrayBase.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <cstddef>
7#include <utility>
8
15
16namespace stratax::core {
17
39template<DType T>
41{
42public:
65
67 [[nodiscard]] static constexpr std::string_view dtype() noexcept
68 {
70 }
71
73 [[nodiscard]] size_type size() const noexcept {return buffer_.size();}
74
76 [[nodiscard]] bool empty() const noexcept {return buffer_.empty();}
77
79 [[nodiscard]] size_type rank() const noexcept {return shape_.rank();}
80
82 [[nodiscard]] const Shape& shape() const noexcept {return shape_;}
83
85 [[nodiscard]] const Shape& strides() const noexcept {return strides_;}
86
92 [[nodiscard]] pointer data() noexcept {return buffer_.data();}
98 [[nodiscard]] const_pointer data() const noexcept {return buffer_.data();}
99
102 {
103 if (empty())
104 {
105 throw Exceptions::IndexError("Cannot access the front of an empty array.");
106 }
107
108 return buffer_.front();
109 }
112 {
113 if (empty())
114 {
115 throw Exceptions::IndexError("Cannot access the front of an empty array.");
116 }
117
118 return buffer_.front();
119 }
120
123 {
124 if (empty())
125 {
126 throw Exceptions::IndexError("Cannot access the back of an empty array.");
127 }
128
129 return buffer_.back();
130 }
133 {
134 if (empty())
135 {
136 throw Exceptions::IndexError("Cannot access the back of an empty array.");
137 }
138
139 return buffer_.back();
140 }
141
143 reference operator[](size_type index) noexcept {return buffer_[index];}
145 const_reference operator[](size_type index) const noexcept {return buffer_[index];}
146
153 reference at(difference_type index) {return buffer_[indexing::normalize_index(index, size())];}
160 const_reference at(difference_type index) const {return buffer_[indexing::normalize_index(index, size())];}
161
163 iterator begin() noexcept {return buffer_.begin();}
165 const_iterator begin() const noexcept {return buffer_.begin();}
167 const_iterator cbegin() const noexcept {return buffer_.cbegin();}
169 iterator end() noexcept {return buffer_.end();}
171 const_iterator end() const noexcept {return buffer_.end();}
173 const_iterator cend() const noexcept {return buffer_.cend();}
175 reverse_iterator rbegin() noexcept {return buffer_.rbegin();}
177 const_reverse_iterator rbegin() const noexcept {return buffer_.rbegin();}
179 const_reverse_iterator crbegin() const noexcept {return buffer_.crbegin();}
181 reverse_iterator rend() noexcept {return buffer_.rend();}
183 const_reverse_iterator rend() const noexcept {return buffer_.rend();}
185 const_reverse_iterator crend() const noexcept {return buffer_.crend();}
186
193 void fill(const_reference value) {buffer_.fill(value);}
194
206 void swap(ArrayBase& other) noexcept
207 {
208 buffer_.swap(other.buffer_);
209 shape_.swap(other.shape_);
210 strides_.swap(other.strides_);
211 }
212
213protected:
215 ArrayBase(const ArrayBase&) = default;
217 ArrayBase(ArrayBase&&) noexcept = default;
219 ArrayBase& operator=(ArrayBase&&) noexcept = default;
220
228 ArrayBase& operator=(const ArrayBase& other)
229 {
230 if (this != &other)
231 {
232 ArrayBase copy(other);
233 swap(copy);
234 }
235 return *this;
236 }
237
247 explicit ArrayBase(const Shape& shape)
248 : buffer_(shape.elements()),
249 shape_(shape),
250 strides_(shape.strides())
251 {}
252
264 : buffer_(shape.elements(), value),
265 shape_(shape),
266 strides_(shape.strides())
267 {}
268
285 : buffer_(std::move(buffer)),
286 shape_(shape),
287 strides_(shape.strides())
288 {
289 if (buffer_.size() != shape_.elements())
290 {
292 "Buffer size does not match the shape's element count.");
293 }
294 }
295
309 template<typename IndexContainer>
310 size_type normalized_flat_offset(const IndexContainer& raw_indices) const
311 {
312 if (raw_indices.size() != rank())
313 {
315 "The number of indices must match the array rank.");
316 }
317
318 if (empty())
319 {
320 throw Exceptions::IndexError("Cannot index an empty array.");
321 }
322
323 size_type offset = 0;
324
325 for (size_type i = 0; i < rank(); ++i)
326 {
327 const size_type index = indexing::normalize_index(
328 raw_indices[i], shape_[i]);
329 offset += index * strides_[i];
330 }
331
332 return offset;
333 }
334
335private:
336 Buffer<value_type> buffer_;
337 Shape shape_;
338 Shape strides_;
339};
340
341}
Fixed-size aligned storage with element lifetime management.
Dtype and array interface concepts.
Compile-time metadata for supported dtypes.
Stratax exception types derived from std::runtime_error.
Checked signed index normalization.
Immutable dimension metadata and checked row-major strides.
Index or slice cannot be used for the requested access.
Number of axes or supplied multidimensional indices does not match the required rank.
Shape incompatible with the requested operation or container.
Shared owning storage and layout base for Stratax array containers.
Definition ArrayBase.hpp:41
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:44
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:54
ArrayBase(const Shape &shape, Buffer< value_type > &&buffer)
Adopts an existing buffer for shape.
ArrayBase(ArrayBase &&) noexcept=default
Transfers storage and layout, leaving the source empty with rank zero.
typename Buffer< value_type >::const_reference const_reference
Read-only element reference type.
Definition ArrayBase.hpp:52
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:82
size_type size() const noexcept
Returns the number of stored elements.
Definition ArrayBase.hpp:73
typename Buffer< value_type >::reverse_iterator reverse_iterator
Mutable reverse iterator type.
Definition ArrayBase.hpp:62
bool empty() const noexcept
Reports whether no elements are stored.
Definition ArrayBase.hpp:76
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:64
const_pointer data() const noexcept
Returns a pointer to contiguous read-only element storage.
Definition ArrayBase.hpp:98
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:92
typename Buffer< value_type >::const_pointer const_pointer
Read-only element pointer type.
Definition ArrayBase.hpp:56
size_type rank() const noexcept
Returns the number of logical dimensions.
Definition ArrayBase.hpp:79
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:48
typename Buffer< value_type >::iterator iterator
Mutable contiguous random-access iterator type.
Definition ArrayBase.hpp:58
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:67
reference front()
Returns the first element.
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: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_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: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
Fixed-size owner of aligned, contiguous element storage.
Definition Buffer.hpp:54
const_pointer const_iterator
Read-only contiguous random-access iterator type.
Definition Buffer.hpp:77
std::reverse_iterator< iterator > reverse_iterator
Mutable iterator that traverses elements in reverse order.
Definition Buffer.hpp:79
reverse_iterator rend() noexcept
Returns the past-the-end iterator for mutable reverse traversal.
Definition Buffer.hpp:474
void swap(Buffer &other) noexcept
Swaps storage and size with another buffer.
Definition Buffer.hpp:534
T & reference
Mutable element reference type.
Definition Buffer.hpp:67
size_type size() const noexcept
Returns the number of stored elements.
Definition Buffer.hpp:497
T * pointer
Mutable pointer to an element.
Definition Buffer.hpp:71
const_reverse_iterator crend() const noexcept
Returns the past-the-end iterator for const reverse traversal.
Definition Buffer.hpp:486
std::ptrdiff_t difference_type
Signed type used for distances between iterators.
Definition Buffer.hpp:65
reference front()
Returns a reference to the first element.
Definition Buffer.hpp:345
const_iterator cend() const noexcept
Returns a const iterator one past the final element.
Definition Buffer.hpp:450
pointer iterator
Mutable contiguous random-access iterator type.
Definition Buffer.hpp:75
bool empty() const noexcept
Returns whether the buffer has no elements.
Definition Buffer.hpp:505
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final element.
Definition Buffer.hpp:468
std::size_t size_type
Unsigned type used for element counts and indices.
Definition Buffer.hpp:63
reverse_iterator rbegin() noexcept
Returns a mutable reverse iterator to the final element.
Definition Buffer.hpp:456
void fill(const_reference value)
Assigns a value to every element in the buffer.
Definition Buffer.hpp:518
pointer data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition Buffer.hpp:397
iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition Buffer.hpp:420
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition Buffer.hpp:432
const T * const_pointer
Read-only pointer to an element.
Definition Buffer.hpp:73
reference back()
Returns a reference to the last element.
Definition Buffer.hpp:369
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only iterator that traverses elements in reverse order.
Definition Buffer.hpp:81
const T & const_reference
Read-only element reference type.
Definition Buffer.hpp:69
iterator end() noexcept
Returns a mutable iterator one past the final element.
Definition Buffer.hpp:438
T value_type
Type of each stored element.
Definition Buffer.hpp:61
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
size_type elements() const
Computes the total number of elements described by the shape.
Definition Shape.hpp:94
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:124
void swap(Shape &other) noexcept
Exchanges dimension storage with another shape.
Definition Shape.hpp:272
Provides compile-time metadata for a supported Stratax dtype.