Speeduino
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <stdint.h>
9
10// LCOV_EXCL_START
11
12// 1. Primary template for N > 0
13template <typename T, size_t N>
14struct array {
15 // Public member data allows aggregate initialization
16 T _elements[N] = {};
17
18 // Types
19 using value_type = T;
21 using reference = T&;
22 using const_reference = const T&;
23 using iterator = T*;
24 using const_iterator = const T*;
25
26 // Constructors
27 constexpr array() = default;
28
29 // Element Access
30 constexpr reference operator[](size_type index) noexcept {
31 return _elements[index];
32 }
33
34 constexpr const_reference operator[](size_type index) const noexcept {
35 return _elements[index];
36 }
37
38 constexpr reference at(size_type index) {
39 return _elements[index];
40 }
41
42 constexpr const_reference at(size_type index) const {
43 return _elements[index];
44 }
45
46 constexpr reference front() noexcept { return _elements[0]; }
47 constexpr const_reference front() const noexcept { return _elements[0]; }
48
49 constexpr reference back() noexcept { return _elements[N - 1]; }
50 constexpr const_reference back() const noexcept { return _elements[N - 1]; }
51
52 constexpr T* data() noexcept { return _elements; }
53 constexpr const T* data() const noexcept { return _elements; }
54
55 // Iterators
56 constexpr iterator begin() noexcept { return _elements; }
58 constexpr iterator end() noexcept { return _elements + N; }
59 constexpr const_iterator end() const noexcept { return _elements + N; }
60
61 // Capacity
62 constexpr bool empty() const noexcept { return false; }
63 constexpr size_type size() const noexcept { return N; }
64 constexpr size_type max_size() const noexcept { return N; }
65
66 // Operations
67 constexpr void fill(const T& value) {
68 for (size_t i = 0; i < N; ++i) {
69 _elements[i] = value;
70 }
71 }
72};
73
75template <typename T>
76static constexpr void copy(T *target, size_t N, const T *src, size_t M) {
77 (void)memcpy(target, src, N<M ? N : M);
78}
79
81template <typename T, size_t N, size_t M>
82static constexpr void copy(T (&target)[N], const T (&src)[M]) {
83 copy(target, N, src, M);
84}
85
86// LCOV_EXCL_STOP
static constexpr void copy(T *target, size_t N, const T *src, size_t M)
A free function to populate a C-style array from a source C-style array.
Definition array.h:76
Definition array.h:14
T _elements[N]
Definition array.h:16
constexpr const T * data() const noexcept
Definition array.h:53
constexpr array()=default
constexpr reference operator[](size_type index) noexcept
Definition array.h:30
constexpr reference back() noexcept
Definition array.h:49
constexpr reference at(size_type index)
Definition array.h:38
constexpr size_type size() const noexcept
Definition array.h:63
constexpr const_iterator end() const noexcept
Definition array.h:59
constexpr iterator end() noexcept
Definition array.h:58
constexpr const_reference front() const noexcept
Definition array.h:47
constexpr const_reference back() const noexcept
Definition array.h:50
constexpr const_reference operator[](size_type index) const noexcept
Definition array.h:34
constexpr void fill(const T &value)
Definition array.h:67
constexpr const_iterator begin() const noexcept
Definition array.h:57
constexpr bool empty() const noexcept
Definition array.h:62
constexpr T * data() noexcept
Definition array.h:52
constexpr size_type max_size() const noexcept
Definition array.h:64
constexpr iterator begin() noexcept
Definition array.h:56
constexpr reference front() noexcept
Definition array.h:46
constexpr const_reference at(size_type index) const
Definition array.h:42