FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
array.h
1 // Copyright 2016-2021 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 
22 namespace 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 
129  {
130  T* dst = buffer_;
131  const T* src = that.buffer_;
132  for (uint8_t i = 0; i < N; ++i)
133  *dst++ = *src++;
134  return *this;
135  }
136 
140  CTPTR data() const
141  {
142  return buffer_;
143  }
144 
149  {
150  return buffer_;
151  }
152 
157  constexpr uint8_t size() const
158  {
159  return N;
160  }
161 
169  CTREF operator[](uint8_t index) const
170  {
171  return buffer_[index];
172  }
173 
181  TREF operator[](uint8_t index)
182  {
183  return buffer_[index];
184  }
185 
188  {
189  return buffer_;
190  }
191 
194  {
195  return buffer_ + N;
196  }
197 
199  CTPTR begin() const
200  {
201  return buffer_;
202  }
203 
205  CTPTR end() const
206  {
207  return buffer_ + N;
208  }
209 
214  template<uint8_t NN>
215  void set(uint8_t index, const T (&buffer)[NN])
216  {
217  if (index >= N) return;
218  const uint8_t nn = ((N - index) < NN) ? (N - index) : NN;
219  T* dst = &buffer_[index];
220  const T* src = buffer;
221  for (uint8_t i = 0; i < nn; ++i)
222  *dst++ = *src++;
223  }
224 
229  template<uint8_t NN>
230  void set(uint8_t index, const array<T, NN>& buffer)
231  {
232  if (index >= N) return;
233  const uint8_t nn = ((N - index) < NN) ? (N - index) : NN;
234  T* dst = &buffer_[index];
235  const T* src = buffer.data();
236  for (uint8_t i = 0; i < nn; ++i)
237  *dst++ = *src++;
238  }
239 
240  private:
241  T buffer_[N] = {};
242  };
243 }
244 #endif /* ARRAY_HH */
245 
containers::array::data
TPTR data()
Get a pointer to the first element of this array.
Definition: array.h:148
containers::array::begin
TPTR begin()
Return an iterator to the first element of this array.
Definition: array.h:187
containers::array::operator=
array< T, N > & operator=(std::initializer_list< T > list)
Overwrite every element of this array with every element of list.
Definition: array.h:112
containers::array::operator[]
CTREF operator[](uint8_t index) const
Get a const reference to the element at index in this array.
Definition: array.h:169
containers::array::CTREF
const T_ & CTREF
The const reference type to T.
Definition: array.h:44
containers::array::array
array()=default
Create a default array of N elements of type T.
containers::array::CTPTR
const T_ * CTPTR
The const pointer type to T.
Definition: array.h:48
containers::array::TPTR
T_ * TPTR
The pointer type to T.
Definition: array.h:46
containers::array::operator=
array< T, N > & operator=(const array< T, N > &that)
Overwrite every element of this array with every element of that.
Definition: array.h:128
containers
Contains all FastArduino generic containers:
Definition: array.h:23
containers::array::data
CTPTR data() const
Get a const pointer to the first element of this array.
Definition: array.h:140
containers::array::TREF
T_ & TREF
The reference type to T.
Definition: array.h:42
std::initializer_list::begin
constexpr const T * begin() const
The first element of this initializer_list.
Definition: initializer_list.h:79
containers::array::array
array(const array< T, N > &that)
Copy that array.
Definition: array.h:87
containers::array::begin
CTPTR begin() const
Return a const iterator to the first element of this array.
Definition: array.h:199
containers::array::end
CTPTR end() const
Return a const iterator to the last element of this array.
Definition: array.h:205
containers::array::set
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:215
containers::array::operator=
array< T, N > & operator=(const T buffer[N])
Overwrite every element of this array with every element of buffer.
Definition: array.h:98
containers::array::operator[]
TREF operator[](uint8_t index)
Get a reference to the element at index in this array.
Definition: array.h:181
containers::array::N
static constexpr uint8_t N
The number of elements in this array.
Definition: array.h:40
containers::array::size
constexpr uint8_t size() const
Get the number of elements in this array.
Definition: array.h:157
containers::array::array
array(T buffer[N])
Convert a C-style array to an array.
Definition: array.h:58
containers::array::array
array(std::initializer_list< T > list)
Create an array from a braced-list of elements.
Definition: array.h:71
std::initializer_list::end
constexpr const T * end() const
One past the last element of this initializer_list.
Definition: initializer_list.h:85
containers::array
Container that encapsulates a fixed size array.
Definition: array.h:35
std::initializer_list
C++ standard support for "initializer_list".
Definition: initializer_list.h:57
containers::array::end
TPTR end()
Return an iterator to the last element of this array.
Definition: array.h:193
containers::array::T
T_ T
The type of elements held by this array.
Definition: array.h:38
containers::array::set
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:230