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
98 reference front() {return buffer_.front();}
100 const_reference front() const {return buffer_.front();}
101
103 reference back() {return buffer_.back();}
105 const_reference back() const {return buffer_.back();}
106
108 reference operator[](size_type index) noexcept {return buffer_[index];}
110 const_reference operator[](size_type index) const noexcept {return buffer_[index];}
111
118 reference at(difference_type index) {return buffer_[indexing::normalize_index(index, size())];}
125 const_reference at(difference_type index) const {return buffer_[indexing::normalize_index(index, size())];}
126
128 iterator begin() noexcept {return buffer_.begin();}
130 const_iterator begin() const noexcept {return buffer_.begin();}
132 const_iterator cbegin() const noexcept {return buffer_.cbegin();}
134 iterator end() noexcept {return buffer_.end();}
136 const_iterator end() const noexcept {return buffer_.end();}
138 const_iterator cend() const noexcept {return buffer_.cend();}
140 reverse_iterator rbegin() noexcept {return buffer_.rbegin();}
142 const_reverse_iterator rbegin() const noexcept {return buffer_.rbegin();}
144 const_reverse_iterator crbegin() const noexcept {return buffer_.crbegin();}
146 reverse_iterator rend() noexcept {return buffer_.rend();}
148 const_reverse_iterator rend() const noexcept {return buffer_.rend();}
150 const_reverse_iterator crend() const noexcept {return buffer_.crend();}
151
158 void fill(const_reference value) {buffer_.fill(value);}
159
169 void swap(ArrayBase& other) noexcept
170 {
171 buffer_.swap(other.buffer_);
172 shape_.swap(other.shape_);
173 strides_.swap(other.strides_);
174 }
175
176protected:
186 explicit ArrayBase(const Shape& shape)
187 : buffer_(shape.elements()),
188 shape_(shape),
189 strides_(shape.strides())
190 {}
191
203 : buffer_(shape.elements(), value),
204 shape_(shape),
205 strides_(shape.strides())
206 {}
207
224 : buffer_(std::move(buffer)),
225 shape_(shape),
226 strides_(shape.strides())
227 {
228 if (buffer_.size() != shape_.elements())
229 {
230 throw Exceptions::ShapeError(
231 "Buffer size must match shape element count.");
232 }
233 }
234
254 template<typename IndexContainer>
256 const IndexContainer& raw_indices,
257 const char* rank_mismatch_message = "Multi-index rank must match array rank.",
258 const char* component_oob_message = nullptr
259 ) const
260 {
261 if (raw_indices.size() != rank())
262 {
263 throw Exceptions::IndexError(rank_mismatch_message);
264 }
265
266 size_type offset = 0;
267
268 for (size_type i = 0; i < rank(); ++i)
269 {
270 try
271 {
272 const size_type index =
273 indexing::normalize_index(
274 raw_indices[i],
275 shape_[i]);
276
277 offset += index * strides_[i];
278 }
279 catch (const Exceptions::IndexError&)
280 {
281 if (component_oob_message != nullptr)
282 {
283 throw Exceptions::IndexError(component_oob_message);
284 }
285
286 throw;
287 }
288 }
289
290 return offset;
291 }
292
293private:
294 Buffer<value_type> buffer_;
295 Shape shape_;
296 Shape strides_;
297};
298
299}
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
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
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.
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
T & reference
Mutable element reference type.
Definition Buffer.hpp:64
T * pointer
Mutable pointer to an element.
Definition Buffer.hpp:68
std::ptrdiff_t difference_type
Signed type used for distances between iterators.
Definition Buffer.hpp:62
pointer iterator
Mutable contiguous random-access iterator type.
Definition Buffer.hpp:72
std::size_t size_type
Unsigned type used for element counts and indices.
Definition Buffer.hpp:60
const T * const_pointer
Read-only pointer to an element.
Definition Buffer.hpp:70
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
T value_type
Type of each stored element.
Definition Buffer.hpp:58
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
Provides compile-time metadata for a supported Stratax dtype.