Stratax 0.2.0
Loading...
Searching...
No Matches
Vector.hpp
1#pragma once
2
3#include <cstddef>
4#include <initializer_list>
5#include <iterator>
6
7#include <stratax/core/containers/Buffer.hpp>
8#include <stratax/core/Concepts.hpp>
9#include <stratax/core/containers/Shape.hpp>
10#include <stratax/core/containers/Strides.hpp>
11#include <stratax/core/Exceptions.hpp>
13
14namespace stratax::container {
15
24template<typename T>
25requires Numeric<T>
26class Vector
27{
28private:
29 core::Shape shape_;
30 core::Strides strides_;
31 core::Buffer<T> buffer_;
32
33public:
35 using value_type = T;
36
39
42
45
48
50 template<typename U>
52
60 Vector() noexcept = default;
61
68 explicit Vector(std::size_t size)
69 : shape_({size}, core::Shape::allow_zero),
70 strides_(shape_),
71 buffer_(size)
72 {
73 }
74
82 explicit Vector(const core::Shape& shape)
83 : shape_(core::validation::require_rank(shape, 1, "Shape must be rank 1")),
84 strides_(shape_),
85 buffer_(shape_.elements())
86 {
87 }
88
96 Vector(std::size_t size, const T& value)
97 : shape_({size}, core::Shape::allow_zero),
98 strides_(shape_),
99 buffer_(size, value)
100 {
101 }
102
109 Vector(std::initializer_list<T> list)
110 : shape_({list.size()}, core::Shape::allow_zero),
111 strides_(shape_),
112 buffer_(list)
113 {
114 }
115
116 // Rule of Five
117
122 Vector(const Vector&) = default;
123
128 Vector(Vector&&) noexcept = default;
129
134 Vector& operator=(const Vector&) = default;
135
140 Vector& operator=(Vector&&) noexcept = default;
141
146 ~Vector() = default;
147
156 T& operator()(std::size_t index) noexcept
157 {
158 return buffer_[index];
159 }
160
169 const T& operator()(std::size_t index) const noexcept
170 {
171 return buffer_[index];
172 }
173
182 T& operator[](std::size_t index) noexcept
183 {
184 return buffer_[index];
185 }
186
195 const T& operator[](std::size_t index) const noexcept
196 {
197 return buffer_[index];
198 }
199
209 T& at(std::ptrdiff_t index)
210 {
211 const std::size_t normalized =
212 core::validation::normalize_index(index, size(), "Vector index out of bounds.");
213 return buffer_[normalized];
214 }
215
225 const T& at(std::ptrdiff_t index) const
226 {
227 const std::size_t normalized =
228 core::validation::normalize_index(index, size(), "Vector index out of bounds.");
229 return buffer_[normalized];
230 }
231
237 T& front()
238 {
239 return buffer_.front();
240 }
241
247 const T& front() const
248 {
249 return buffer_.front();
250 }
251
257 T& back()
258 {
259 return buffer_.back();
260 }
261
267 const T& back() const
268 {
269 return buffer_.back();
270 }
271
277 [[nodiscard]] std::size_t size() const noexcept
278 {
279 return shape_.elements();
280 }
281
287 [[nodiscard]] std::size_t rank() const noexcept
288 {
289 return shape_.rank();
290 }
291
297 [[nodiscard]] bool empty() const noexcept
298 {
299 return buffer_.empty();
300 }
301
307 const stratax::core::Shape& shape() const noexcept
308 {
309 return shape_;
310 }
311
317 const stratax::core::Strides& strides() const noexcept
318 {
319 return strides_;
320 }
321
327 [[nodiscard]] T* data() noexcept
328 {
329 return buffer_.data();
330 }
331
337 [[nodiscard]] const T* data() const noexcept
338 {
339 return buffer_.data();
340 }
341
347 [[nodiscard]] iterator begin() noexcept
348 {
349 return buffer_.begin();
350 }
351
357 [[nodiscard]] const_iterator begin() const noexcept
358 {
359 return buffer_.begin();
360 }
361
367 [[nodiscard]] const_iterator cbegin() const noexcept
368 {
369 return buffer_.cbegin();
370 }
371
377 [[nodiscard]] iterator end() noexcept
378 {
379 return buffer_.end();
380 }
381
387 [[nodiscard]] const_iterator end() const noexcept
388 {
389 return buffer_.end();
390 }
391
397 [[nodiscard]] const_iterator cend() const noexcept
398 {
399 return buffer_.cend();
400 }
401
407 [[nodiscard]] reverse_iterator rbegin() noexcept
408 {
409 return buffer_.rbegin();
410 }
411
417 [[nodiscard]] const_reverse_iterator rbegin() const noexcept
418 {
419 return buffer_.rbegin();
420 }
421
427 [[nodiscard]] const_reverse_iterator crbegin() const noexcept
428 {
429 return buffer_.crbegin();
430 }
431
437 [[nodiscard]] reverse_iterator rend() noexcept
438 {
439 return buffer_.rend();
440 }
441
447 [[nodiscard]] const_reverse_iterator rend() const noexcept
448 {
449 return buffer_.rend();
450 }
451
457 [[nodiscard]] const_reverse_iterator crend() const noexcept
458 {
459 return buffer_.crend();
460 }
461
467 void fill(const T& value)
468 {
469 buffer_.fill(value);
470 }
471
477 void swap(Vector& other) noexcept
478 {
479 using std::swap;
480
481 swap(shape_, other.shape_);
482 swap(strides_, other.strides_);
483 swap(buffer_, other.buffer_);
484 }
485
486};
487
488}
489
Shared runtime validation helpers.
iterator end() noexcept
Returns an iterator one past the last element.
Definition Vector.hpp:377
const_iterator end() const noexcept
Returns a const iterator one past the last element.
Definition Vector.hpp:387
typename core::Buffer< T >::const_iterator const_iterator
Const iterator over vector elements.
Definition Vector.hpp:41
const_iterator cend() const noexcept
Returns a const iterator one past the last element.
Definition Vector.hpp:397
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition Vector.hpp:427
iterator begin() noexcept
Returns an iterator to the first element.
Definition Vector.hpp:347
T & back()
Returns the last element.
Definition Vector.hpp:257
T * data() noexcept
Returns the raw data pointer.
Definition Vector.hpp:327
typename core::Buffer< T >::const_reverse_iterator const_reverse_iterator
Const reverse iterator over vector elements.
Definition Vector.hpp:47
Vector(Vector &&) noexcept=default
Transfers ownership from another vector.
const T * data() const noexcept
Returns the raw data pointer as a const pointer.
Definition Vector.hpp:337
typename core::Buffer< T >::reverse_iterator reverse_iterator
Mutable reverse iterator over vector elements.
Definition Vector.hpp:44
T & at(std::ptrdiff_t index)
Returns a flat element with bounds checking.
Definition Vector.hpp:209
reverse_iterator rend() noexcept
Returns a reverse iterator before the first element.
Definition Vector.hpp:437
T & front()
Returns the first element.
Definition Vector.hpp:237
void fill(const T &value)
Fills every element with the same value.
Definition Vector.hpp:467
const T & front() const
Returns the first element as a const reference.
Definition Vector.hpp:247
bool empty() const noexcept
Returns whether the vector has no elements.
Definition Vector.hpp:297
const stratax::core::Shape & shape() const noexcept
Definition Vector.hpp:307
std::size_t rank() const noexcept
Returns the vector rank.
Definition Vector.hpp:287
std::size_t size() const noexcept
Definition Vector.hpp:277
const T & operator()(std::size_t index) const noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:169
void swap(Vector &other) noexcept
Swaps the contents of two vectors.
Definition Vector.hpp:477
typename core::Buffer< T >::iterator iterator
Mutable iterator over vector elements.
Definition Vector.hpp:38
const_reverse_iterator crend() const noexcept
Returns a const reverse iterator before the first element.
Definition Vector.hpp:457
const stratax::core::Strides & strides() const noexcept
Returns the vector strides.
Definition Vector.hpp:317
T & operator[](std::size_t index) noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:182
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition Vector.hpp:417
T value_type
Element type stored by the vector.
Definition Vector.hpp:35
const_reverse_iterator rend() const noexcept
Returns a const reverse iterator before the first element.
Definition Vector.hpp:447
Vector< U > rebind
Rebinds the vector container to another element type.
Definition Vector.hpp:51
Vector(const core::Shape &shape)
Creates a vector from a validated rank-1 shape.
Definition Vector.hpp:82
Vector() noexcept=default
Creates a default vector with no logical dimensions.
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition Vector.hpp:367
Vector(std::initializer_list< T > list)
Creates a vector from an initializer list.
Definition Vector.hpp:109
const T & operator[](std::size_t index) const noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:195
Vector(std::size_t size, const T &value)
Creates a vector and fills it with a value.
Definition Vector.hpp:96
const_iterator begin() const noexcept
Returns a const iterator to the first element.
Definition Vector.hpp:357
const T & at(std::ptrdiff_t index) const
Returns a flat element with bounds checking.
Definition Vector.hpp:225
const T & back() const
Returns the last element as a const reference.
Definition Vector.hpp:267
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the last element.
Definition Vector.hpp:407
Vector(const Vector &)=default
Creates a copy of another vector.
Owns contiguous dynamically allocated storage.
Definition Buffer.hpp:38
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse iterator over contiguous buffer elements.
Definition Buffer.hpp:53
const T * const_iterator
Const iterator over contiguous buffer elements.
Definition Buffer.hpp:50
T * iterator
Mutable iterator over contiguous buffer elements.
Definition Buffer.hpp:47
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse iterator over contiguous buffer elements.
Definition Buffer.hpp:56
Stores a list of dimension lengths for an array shape.
Definition Shape.hpp:22
static constexpr allow_zero_t allow_zero
Tag value documenting that zero-valued dimensions are intentional.
Definition Shape.hpp:41
Stores strides for a shape in contiguous memory.
Definition Strides.hpp:23