Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <initializer_list>
5#include <iterator>
6
13
14namespace stratax::container {
15
16template<typename T>
17requires Numeric<T>
26class Vector
27{
28private:
29 core::Shape shape_;
30 core::Strides strides_;
31 core::Buffer<T> buffer_;
32
33public:
34 using value_type = T;
35
36 template<typename U>
38
46 Vector() noexcept = default;
47
54 explicit Vector(std::size_t size)
55 : shape_({size}, core::Shape::allow_zero),
56 strides_(shape_),
57 buffer_(size)
58 {
59 }
60
68 explicit Vector(const core::Shape& shape)
69 : shape_(core::validation::require_rank(shape, 1, "Shape must be rank 1")),
70 strides_(shape_),
71 buffer_(shape_.elements())
72 {
73 }
74
82 Vector(std::size_t size, const T& value)
83 : shape_({size}, core::Shape::allow_zero),
84 strides_(shape_),
85 buffer_(size, value)
86 {
87 }
88
95 Vector(std::initializer_list<T> list)
96 : shape_({list.size()}, core::Shape::allow_zero),
97 strides_(shape_),
98 buffer_(list)
99 {
100 }
101
102 // Rule of Five
103
108 Vector(const Vector&) = default;
109
114 Vector(Vector&&) noexcept = default;
115
120 Vector& operator=(const Vector&) = default;
121
126 Vector& operator=(Vector&&) noexcept = default;
127
132 ~Vector() = default;
133
142 T& operator()(std::size_t index) noexcept
143 {
144 return buffer_[index];
145 }
146
155 const T& operator()(std::size_t index) const noexcept
156 {
157 return buffer_[index];
158 }
159
168 T& operator[](std::size_t index) noexcept
169 {
170 return buffer_[index];
171 }
172
181 const T& operator[](std::size_t index) const noexcept
182 {
183 return buffer_[index];
184 }
185
195 T& at(std::size_t index)
196 {
197 core::validation::require_index(index, size(), "Vector index out of bounds.");
198 return buffer_[index];
199 }
200
210 const T& at(std::size_t index) const
211 {
212 core::validation::require_index(index, size(), "Vector index out of bounds.");
213 return buffer_[index];
214 }
215
221 T& front()
222 {
223 return buffer_.front();
224 }
225
231 const T& front() const
232 {
233 return buffer_.front();
234 }
235
241 T& back()
242 {
243 return buffer_.back();
244 }
245
251 const T& back() const
252 {
253 return buffer_.back();
254 }
255
261 [[nodiscard]] std::size_t size() const noexcept
262 {
263 return shape_.elements();
264 }
265
271 [[nodiscard]] std::size_t rank() const noexcept
272 {
273 return shape_.rank();
274 }
275
281 [[nodiscard]] bool empty() const noexcept
282 {
283 return buffer_.empty();
284 }
285
291 const stratax::core::Shape& shape() const noexcept
292 {
293 return shape_;
294 }
295
301 const stratax::core::Strides& strides() const noexcept
302 {
303 return strides_;
304 }
305
311 [[nodiscard]] T* data() noexcept
312 {
313 return buffer_.data();
314 }
315
321 [[nodiscard]] const T* data() const noexcept
322 {
323 return buffer_.data();
324 }
325
331 [[nodiscard]] T* begin() noexcept
332 {
333 return buffer_.begin();
334 }
335
341 [[nodiscard]] const T* begin() const noexcept
342 {
343 return buffer_.begin();
344 }
345
351 [[nodiscard]] const T* cbegin() const noexcept
352 {
353 return buffer_.cbegin();
354 }
355
361 [[nodiscard]] T* end() noexcept
362 {
363 return buffer_.end();
364 }
365
371 [[nodiscard]] const T* end() const noexcept
372 {
373 return buffer_.end();
374 }
375
381 [[nodiscard]] const T* cend() const noexcept
382 {
383 return buffer_.cend();
384 }
385
391 [[nodiscard]] std::reverse_iterator<T*> rbegin() noexcept
392 {
393 return buffer_.rbegin();
394 }
395
401 [[nodiscard]] std::reverse_iterator<const T*> rbegin() const noexcept
402 {
403 return buffer_.rbegin();
404 }
405
411 [[nodiscard]] std::reverse_iterator<const T*> crbegin() const noexcept
412 {
413 return buffer_.crbegin();
414 }
415
421 [[nodiscard]] std::reverse_iterator<T*> rend() noexcept
422 {
423 return buffer_.rend();
424 }
425
431 [[nodiscard]] std::reverse_iterator<const T*> rend() const noexcept
432 {
433 return buffer_.rend();
434 }
435
441 [[nodiscard]] std::reverse_iterator<const T*> crend() const noexcept
442 {
443 return buffer_.crend();
444 }
445
451 void fill(const T& value)
452 {
453 buffer_.fill(value);
454 }
455
461 void swap(Vector& other) noexcept
462 {
463 using std::swap;
464
465 swap(shape_, other.shape_);
466 swap(strides_, other.strides_);
467 swap(buffer_, other.buffer_);
468 }
469
470};
471
472}
Shared runtime validation helpers.
std::reverse_iterator< const T * > crbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition Vector.hpp:411
std::reverse_iterator< T * > rbegin() noexcept
Returns a reverse iterator to the last element.
Definition Vector.hpp:391
const T & at(std::size_t index) const
Returns a flat element with bounds checking.
Definition Vector.hpp:210
T & at(std::size_t index)
Returns a flat element with bounds checking.
Definition Vector.hpp:195
T & back()
Returns the last element.
Definition Vector.hpp:241
std::reverse_iterator< const T * > rbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition Vector.hpp:401
T * data() noexcept
Returns the raw data pointer.
Definition Vector.hpp:311
Vector(Vector &&) noexcept=default
Transfers ownership from another vector.
const T * end() const noexcept
Returns a const iterator one past the last element.
Definition Vector.hpp:371
const T * data() const noexcept
Returns the raw data pointer as a const pointer.
Definition Vector.hpp:321
T & front()
Returns the first element.
Definition Vector.hpp:221
void fill(const T &value)
Fills every element with the same value.
Definition Vector.hpp:451
const T & front() const
Returns the first element as a const reference.
Definition Vector.hpp:231
bool empty() const noexcept
Returns whether the vector has no elements.
Definition Vector.hpp:281
const T * cend() const noexcept
Returns a const iterator one past the last element.
Definition Vector.hpp:381
const stratax::core::Shape & shape() const noexcept
Definition Vector.hpp:291
std::size_t rank() const noexcept
Returns the vector rank.
Definition Vector.hpp:271
T * end() noexcept
Returns an iterator one past the last element.
Definition Vector.hpp:361
std::size_t size() const noexcept
Definition Vector.hpp:261
const T & operator()(std::size_t index) const noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:155
void swap(Vector &other) noexcept
Swaps the contents of two vectors.
Definition Vector.hpp:461
const stratax::core::Strides & strides() const noexcept
Returns the vector strides.
Definition Vector.hpp:301
T & operator[](std::size_t index) noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:168
const T * begin() const noexcept
Returns a const iterator to the first element.
Definition Vector.hpp:341
Vector(const core::Shape &shape)
Creates a vector from a validated rank-1 shape.
Definition Vector.hpp:68
Vector() noexcept=default
Creates a default vector with no logical dimensions.
std::reverse_iterator< const T * > crend() const noexcept
Returns a const reverse iterator before the first element.
Definition Vector.hpp:441
Vector(std::initializer_list< T > list)
Creates a vector from an initializer list.
Definition Vector.hpp:95
const T * cbegin() const noexcept
Returns a const iterator to the first element.
Definition Vector.hpp:351
const T & operator[](std::size_t index) const noexcept
Returns a flat element without bounds checking.
Definition Vector.hpp:181
Vector(std::size_t size, const T &value)
Creates a vector and fills it with a value.
Definition Vector.hpp:82
T * begin() noexcept
Returns an iterator to the first element.
Definition Vector.hpp:331
const T & back() const
Returns the last element as a const reference.
Definition Vector.hpp:251
std::reverse_iterator< T * > rend() noexcept
Returns a reverse iterator before the first element.
Definition Vector.hpp:421
std::reverse_iterator< const T * > rend() const noexcept
Returns a const reverse iterator before the first element.
Definition Vector.hpp:431
Vector(const Vector &)=default
Creates a copy of another vector.
Owns contiguous dynamically allocated storage.
Definition Buffer.hpp:38
Stores a list of dimension lengths for an array shape.
Definition Shape.hpp:20
static constexpr allow_zero_t allow_zero
Definition Shape.hpp:36
Stores strides for a shape in contiguous memory.
Definition Strides.hpp:23
void require_index(std::size_t index, std::size_t size, const char *message)
Requires an index to be less than a size.