tenno 0.1.0
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <cstddef> /* for std::ptrdiff_t */
9#include <initializer_list> /* brace-enclosed initializer list must use std */
10#include <iterator> /* for std::forward_iterator_tag */
11#include <tenno/algorithm.hpp>
12#include <tenno/error.hpp>
13#include <tenno/expected.hpp>
14#include <tenno/ranges.hpp>
15#include <tenno/types.hpp>
16
17namespace tenno
18{
19
32template <typename T, tenno::size N> class array
33{
34public:
38 using value_type = T;
39
40 /* TODO: may be removed */
41 static constexpr T generate_default()
42 {
43 return T{}; // Default-initialize T
44 }
45
53 constexpr array() : _data{}
54 {
55 for (auto i : tenno::range<tenno::size>(N))
56 {
57 this->_data[i] = generate_default();
58 }
59 }
60
72 constexpr array(std::initializer_list<T> list)
73 {
74 tenno::copy(list.begin(), list.end(), this->_data);
75 }
76
77 constexpr array(const array &other) : _data{}
78 {
79 tenno::copy(other.cbegin(), other.cend(), this->_data);
80 }
81
93 constexpr tenno::size size() const noexcept
94 {
95 return this->_size;
96 }
97
106 static constexpr tenno::array<T, N> init() noexcept
107 {
108 auto arr = tenno::array<T, N>();
109 for (auto i : tenno::range<tenno::size>(N))
110 {
111 arr._data[i] = T();
112 }
113 return arr;
114 }
115
133 at(const tenno::size index) const noexcept
134 {
135 if (index >= this->_size)
136 {
138 }
139 return this->_data[index];
140 }
141
154 constexpr T front() const noexcept
155 {
156 return this->_data[0];
157 }
158
171 constexpr T back() const noexcept
172 {
173 return this->_data[this->_size - 1];
174 }
175
188 T *data() noexcept
189 {
190 return this->_data;
191 }
192
197 constexpr const T *data() const noexcept
198 {
199 return this->_data;
200 }
201
217 constexpr bool empty() const noexcept
218 {
219 return this->_size == 0;
220 }
221
233 constexpr tenno::size max_size() const noexcept
234 {
235 return this->_size;
236 }
237
249 void fill(const T &value) noexcept
250 {
251 tenno::for_each(this->begin(), this->end(),
252 [&value](T &elem) { elem = value; });
253 }
254
267 void swap(array &other) noexcept
268 {
269 for (auto i : tenno::range<tenno::size>(this->_size))
270 {
271 auto temp = this->_data[i];
272 this->_data[i] = other._data[i];
273 other._data[i] = temp;
274 }
275 }
276
289 constexpr T &operator[](const tenno::size index) noexcept
290 {
291 return this->_data[index];
292 }
293
297 constexpr const T &operator[](const tenno::size index) const noexcept
298 {
299 return this->_data[index];
300 }
301
302 constexpr array &operator=(const array &other) noexcept
303 {
304 tenno::copy(other.begin(), other.end(), this->_data);
305 return *this;
306 }
307
311 struct iterator
312 {
313 using iterator_category = std::forward_iterator_tag;
314 using difference_type = std::ptrdiff_t;
315 using value_type = T;
316 using pointer = T *;
317 using reference = T &;
318
321
322 explicit iterator(tenno::array<T, N> &array_in, tenno::size end) noexcept
323 : array(array_in), index(end)
324 {
325 }
326
328 {
329 ++index;
330 return *this;
331 }
332
333 iterator operator++(int) noexcept
334 {
335 iterator _iterator = *this;
336 ++index;
337 return _iterator;
338 }
339
340 bool operator==(const iterator &other) const noexcept
341 {
342 return index == other.index;
343 }
344
345 bool operator!=(const iterator &other) const noexcept
346 {
347 return !(*this == other);
348 }
349
350 reference operator*() const noexcept
351 {
352 return array[index];
353 }
354 };
355
367 iterator begin() noexcept
368 {
369 return iterator(*this, 0);
370 }
381 iterator end() noexcept
382 {
383 return iterator(*this, this->_size);
384 }
385
390 {
391 using iterator_category = std::forward_iterator_tag;
392 using difference_type = std::ptrdiff_t;
393 using value_type = T;
394 using pointer = T *;
395 using reference = T &;
396
399
400 constexpr explicit const_iterator(const tenno::array<T, N> &array_in,
401 const tenno::size end) noexcept
402 : array(array_in), index(end)
403 {
404 }
405
406 constexpr const_iterator &operator++() noexcept
407 {
408 index += 1;
409 return *this;
410 }
411
412 constexpr const_iterator operator++(int) noexcept
413 {
414 const_iterator _const_iterator = *this;
415 index += 1;
416 return _const_iterator;
417 }
418
419 constexpr bool operator==(const const_iterator &other) const noexcept
420 {
421 return index == other.index;
422 }
423
424 constexpr bool operator!=(const const_iterator &other) const noexcept
425 {
426 return !(*this == other);
427 }
428
429 constexpr T operator*() const noexcept
430 {
431 return array[index];
432 }
433 };
434
446 constexpr const_iterator cbegin() const noexcept
447 {
448 return const_iterator(*this, 0);
449 }
460 constexpr const_iterator cend() const noexcept
461 {
462 return const_iterator(*this, this->_size);
463 }
464
469 {
470 using iterator_category = std::forward_iterator_tag;
471 using difference_type = std::ptrdiff_t;
472 using value_type = T;
473 using pointer = T *;
474 using reference = T &;
475
478
480 tenno::size end) noexcept
481 : array(array_in), index(end)
482 {
483 }
484
486 {
487 --index;
488 return *this;
489 }
490
492 {
493 reverse_iterator _reverse_iterator = *this;
494 --index;
495 return _reverse_iterator;
496 }
497
498 bool operator==(const reverse_iterator &other) const noexcept
499 {
500 return index == other.index;
501 }
502
503 bool operator!=(const reverse_iterator &other) const noexcept
504 {
505 return !(*this == other);
506 }
507
508 reference operator*() const noexcept
509 {
510 return array[index];
511 }
512 };
513
526 {
527 return reverse_iterator(*this, this->_size - 1);
528 }
540 {
541 return reverse_iterator(*this, -1);
542 }
543
548 {
549 using iterator_category = std::forward_iterator_tag;
550 using difference_type = std::ptrdiff_t;
551 using value_type = T;
552 using pointer = T *;
553 using reference = T &;
554
557
558 constexpr explicit const_reverse_iterator(
559 const tenno::array<T, N> &array_in, const tenno::size end) noexcept
560 : array(array_in), index(end)
561 {
562 }
563
564 constexpr const_reverse_iterator &operator++() noexcept
565 {
566 --index;
567 return *this;
568 }
569
570 constexpr const_reverse_iterator operator++(int) noexcept
571 {
572 const_reverse_iterator _const_reverse_iterator = *this;
573 --index;
574 return _const_reverse_iterator;
575 }
576
577 constexpr bool
578 operator==(const const_reverse_iterator &other) const noexcept
579 {
580 return index == other.index;
581 }
582
583 constexpr bool
584 operator!=(const const_reverse_iterator &other) const noexcept
585 {
586 return !(*this == other);
587 }
588
589 constexpr T operator*() const noexcept
590 {
591 return array[index];
592 }
593 };
594
606 constexpr const_reverse_iterator crbegin() const noexcept
607 {
608 return const_reverse_iterator(*this, this->_size - 1);
609 }
620 constexpr const_reverse_iterator crend() const noexcept
621 {
622 return const_reverse_iterator(*this, -1);
623 }
624
625private:
626 T _data[N];
627 const tenno::size _size = N;
628
629}; // class array
630
631} // namespace tenno
A fixed-size array.
Definition array.hpp:33
constexpr const_reverse_iterator crend() const noexcept
Get an const_reverse_iterator to the end of the array.
Definition array.hpp:620
constexpr array()
Construct a new array object.
Definition array.hpp:53
iterator begin() noexcept
Get an iterator to the beginning of the array.
Definition array.hpp:367
constexpr tenno::size max_size() const noexcept
Get the maximum size of the array.
Definition array.hpp:233
reverse_iterator rend() noexcept
Get an reverse_iterator to the end of the array.
Definition array.hpp:539
constexpr const T & operator[](const tenno::size index) const noexcept
Access an element of the array constexpr.
Definition array.hpp:297
constexpr T & operator[](const tenno::size index) noexcept
Access an element of the array.
Definition array.hpp:289
constexpr tenno::size size() const noexcept
Get the size of the array.
Definition array.hpp:93
constexpr const_reverse_iterator crbegin() const noexcept
Get an const_reverse_iterator to the beginning of the array.
Definition array.hpp:606
iterator end() noexcept
Get an iterator to the end of the array.
Definition array.hpp:381
reverse_iterator rbegin() noexcept
Get an reverse_iterator to the beginning of the array.
Definition array.hpp:525
void fill(const T &value) noexcept
Fill the array with a value.
Definition array.hpp:249
constexpr const_iterator cbegin() const noexcept
Get an const_iterator to the beginning of the array.
Definition array.hpp:446
constexpr const T * data() const noexcept
Access the underlying data of the array.
Definition array.hpp:197
static constexpr tenno::array< T, N > init() noexcept
Initialize all elements of the array to the default value of T.
Definition array.hpp:106
constexpr array(std::initializer_list< T > list)
Construct a new array object with a list of elements.
Definition array.hpp:72
constexpr const_iterator cend() const noexcept
Get an const_iterator to the end of the array.
Definition array.hpp:460
constexpr array(const array &other)
Definition array.hpp:77
static constexpr T generate_default()
Definition array.hpp:41
constexpr array & operator=(const array &other) noexcept
Definition array.hpp:302
T value_type
The type of the elements in the array.
Definition array.hpp:38
T * data() noexcept
Access an element of the array.
Definition array.hpp:188
constexpr T front() const noexcept
Access an element of the array.
Definition array.hpp:154
constexpr bool empty() const noexcept
Check if the array is empty.
Definition array.hpp:217
constexpr tenno::expected< T, tenno::error > at(const tenno::size index) const noexcept
Access an element of the array.
Definition array.hpp:133
void swap(array &other) noexcept
Swap the contents of the array with another array.
Definition array.hpp:267
constexpr T back() const noexcept
Access an element of the array.
Definition array.hpp:171
An expected value.
Definition expected.hpp:38
A range of elements.
Definition ranges.hpp:21
An unexpected value.
Definition expected.hpp:20
size_t size
Definition types.hpp:13
constexpr UnaryFunc for_each(InputIt first, InputIt last, UnaryFunc f)
Applies the given function f to the result of dereferencing every element in the range [first,...
Definition algorithm.hpp:56
constexpr OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
Copies the elements in the range [first, last) to the range beginning at d_first.
Definition algorithm.hpp:28
A const iterator to iterate over the array.
Definition array.hpp:390
constexpr const_iterator(const tenno::array< T, N > &array_in, const tenno::size end) noexcept
Definition array.hpp:400
std::forward_iterator_tag iterator_category
Definition array.hpp:391
constexpr const_iterator operator++(int) noexcept
Definition array.hpp:412
constexpr bool operator==(const const_iterator &other) const noexcept
Definition array.hpp:419
constexpr bool operator!=(const const_iterator &other) const noexcept
Definition array.hpp:424
std::ptrdiff_t difference_type
Definition array.hpp:392
constexpr T operator*() const noexcept
Definition array.hpp:429
const tenno::array< T, N > & array
Definition array.hpp:397
constexpr const_iterator & operator++() noexcept
Definition array.hpp:406
A const iterator to iterate over the array.
Definition array.hpp:548
constexpr bool operator==(const const_reverse_iterator &other) const noexcept
Definition array.hpp:578
constexpr const_reverse_iterator & operator++() noexcept
Definition array.hpp:564
const tenno::array< T, N > & array
Definition array.hpp:555
std::forward_iterator_tag iterator_category
Definition array.hpp:549
constexpr const_reverse_iterator(const tenno::array< T, N > &array_in, const tenno::size end) noexcept
Definition array.hpp:558
constexpr T operator*() const noexcept
Definition array.hpp:589
constexpr bool operator!=(const const_reverse_iterator &other) const noexcept
Definition array.hpp:584
constexpr const_reverse_iterator operator++(int) noexcept
Definition array.hpp:570
An iterator to iterate over the array.
Definition array.hpp:312
reference operator*() const noexcept
Definition array.hpp:350
std::forward_iterator_tag iterator_category
Definition array.hpp:313
iterator & operator++() noexcept
Definition array.hpp:327
tenno::array< T, N > & array
Definition array.hpp:319
bool operator==(const iterator &other) const noexcept
Definition array.hpp:340
std::ptrdiff_t difference_type
Definition array.hpp:314
iterator(tenno::array< T, N > &array_in, tenno::size end) noexcept
Definition array.hpp:322
iterator operator++(int) noexcept
Definition array.hpp:333
bool operator!=(const iterator &other) const noexcept
Definition array.hpp:345
tenno::size index
Definition array.hpp:320
A reverse iterator over the array.
Definition array.hpp:469
std::forward_iterator_tag iterator_category
Definition array.hpp:470
reverse_iterator & operator++() noexcept
Definition array.hpp:485
bool operator==(const reverse_iterator &other) const noexcept
Definition array.hpp:498
std::ptrdiff_t difference_type
Definition array.hpp:471
reverse_iterator operator++(int) noexcept
Definition array.hpp:491
reverse_iterator(tenno::array< T, N > &array_in, tenno::size end) noexcept
Definition array.hpp:479
tenno::array< T, N > & array
Definition array.hpp:476
bool operator!=(const reverse_iterator &other) const noexcept
Definition array.hpp:503
reference operator*() const noexcept
Definition array.hpp:508