FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
array.h
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16#ifndef ARRAY_HH
17#define ARRAY_HH
18
19#include <stdint.h>
20#include "initializer_list.h"
21
22namespace containers
23{
33 template<typename T_, uint8_t N_>
34 class array
35 {
36 public:
38 using T = T_;
40 static constexpr uint8_t N = N_;
42 using TREF = T_&;
44 using CTREF = const T_&;
46 using TPTR = T_*;
48 using CTPTR = const T_*;
49
53 array() = default;
54
58 array(T buffer[N])
59 {
60 T* dst = buffer_;
61 const T* src = buffer;
62 for (uint8_t i = 0; i < N; ++i)
63 *dst++ = *src++;
64 }
65
72 {
73 T* dst = buffer_;
74 auto src = list.begin();
75 for (uint8_t i = 0; i < N; ++i)
76 {
77 if (src != list.end())
78 *dst++ = *src++;
79 else
80 *dst++ = T{};
81 }
82 }
83
87 array(const array<T, N>& that)
88 {
89 T* dst = buffer_;
90 const T* src = that.buffer_;
91 for (uint8_t i = 0; i < N; ++i)
92 *dst++ = *src++;
93 }
94
98 array<T, N>& operator=(const T buffer[N])
99 {
100 T* dst = buffer_;
101 const T* src = buffer;
102 for (uint8_t i = 0; i < N; ++i)
103 *dst++ = *src++;
104 return *this;
105 }
106
113 {
114 T* dst = buffer_;
115 auto src = list.begin();
116 for (uint8_t i = 0; i < N; ++i)
117 {
118 if (src != list.end())
119 *dst++ = *src++;
120 else
121 *dst++ = T{};
122 }
123 }
124
128 array& operator=(const array& that)
129 {
130 if (this == &that) return *this;
131 T* dst = buffer_;
132 const T* src = that.buffer_;
133 for (uint8_t i = 0; i < N; ++i)
134 *dst++ = *src++;
135 return *this;
136 }
137
141 CTPTR data() const
142 {
143 return buffer_;
144 }
145
150 {
151 return buffer_;
152 }
153
158 constexpr uint8_t size() const
159 {
160 return N;
161 }
162
170 CTREF operator[](uint8_t index) const
171 {
172 return buffer_[index];
173 }
174
182 TREF operator[](uint8_t index)
183 {
184 return buffer_[index];
185 }
186
189 {
190 return buffer_;
191 }
192
195 {
196 return buffer_ + N;
197 }
198
200 CTPTR begin() const
201 {
202 return buffer_;
203 }
204
206 CTPTR end() const
207 {
208 return buffer_ + N;
209 }
210
215 template<uint8_t NN>
216 void set(uint8_t index, const T (&buffer)[NN])
217 {
218 if (index >= N) return;
219 const uint8_t nn = ((N - index) < NN) ? (N - index) : NN;
220 T* dst = &buffer_[index];
221 const T* src = buffer;
222 for (uint8_t i = 0; i < nn; ++i)
223 *dst++ = *src++;
224 }
225
230 template<uint8_t NN>
231 void set(uint8_t index, const array<T, NN>& buffer)
232 {
233 if (index >= N) return;
234 const uint8_t nn = ((N - index) < NN) ? (N - index) : NN;
235 T* dst = &buffer_[index];
236 const T* src = buffer.data();
237 for (uint8_t i = 0; i < nn; ++i)
238 *dst++ = *src++;
239 }
240
241 private:
242 T buffer_[N] = {};
243 };
244}
245#endif /* ARRAY_HH */
Container that encapsulates a fixed size array.
Definition: array.h:35
array()=default
Create a default array of N elements of type T.
array(T buffer[N])
Convert a C-style array to an array.
Definition: array.h:58
void set(uint8_t index, const array< T, NN > &buffer)
Replace NN elements of this array, starting at index element, with elements from buffer.
Definition: array.h:231
CTPTR data() const
Get a const pointer to the first element of this array.
Definition: array.h:141
TPTR begin()
Return an iterator to the first element of this array.
Definition: array.h:188
T_ & TREF
The reference type to T.
Definition: array.h:42
array & operator=(const array &that)
Overwrite every element of this array with every element of that.
Definition: array.h:128
constexpr uint8_t size() const
Get the number of elements in this array.
Definition: array.h:158
array(std::initializer_list< T > list)
Create an array from a braced-list of elements.
Definition: array.h:71
CTREF operator[](uint8_t index) const
Get a const reference to the element at index in this array.
Definition: array.h:170
CTPTR end() const
Return a const iterator to the last element of this array.
Definition: array.h:206
array(const array< T, N > &that)
Copy that array.
Definition: array.h:87
CTPTR begin() const
Return a const iterator to the first element of this array.
Definition: array.h:200
TPTR end()
Return an iterator to the last element of this array.
Definition: array.h:194
array< T, N > & operator=(const T buffer[N])
Overwrite every element of this array with every element of buffer.
Definition: array.h:98
T_ * TPTR
The pointer type to T.
Definition: array.h:46
const T_ * CTPTR
The const pointer type to T.
Definition: array.h:48
T_ T
The type of elements held by this array.
Definition: array.h:38
static constexpr uint8_t N
The number of elements in this array.
Definition: array.h:40
TREF operator[](uint8_t index)
Get a reference to the element at index in this array.
Definition: array.h:182
void set(uint8_t index, const T(&buffer)[NN])
Replace NN elements of this array, starting at index element, with elements from buffer.
Definition: array.h:216
TPTR data()
Get a pointer to the first element of this array.
Definition: array.h:149
const T_ & CTREF
The const reference type to T.
Definition: array.h:44
array< T, N > & operator=(std::initializer_list< T > list)
Overwrite every element of this array with every element of list.
Definition: array.h:112
C++ standard support for "initializer_list".
constexpr const T * begin() const
The first element of this initializer_list.
constexpr const T * end() const
One past the last element of this initializer_list.
Contains all FastArduino generic containers:
Definition: array.h:23