Embedded Template Library 1.0
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_ARRAY_INCLUDED
32#define ETL_ARRAY_INCLUDED
33
34#include "platform.h"
35#include "algorithm.h"
36#include "iterator.h"
37#include "functional.h"
38#include "exception.h"
39#include "type_traits.h"
40#include "parameter_type.h"
41#include "static_assert.h"
42#include "error_handler.h"
43#include "nth_type.h"
44#include "initializer_list.h"
45
46#include <stddef.h>
47
51
52namespace etl
53{
54 //***************************************************************************
57 //***************************************************************************
67
68 //***************************************************************************
71 //***************************************************************************
81
82 //***************************************************************************
85 //***************************************************************************
86 template <typename T, size_t SIZE_>
87 class array
88 {
89 private:
90
92
93 public:
94
95 static ETL_CONSTANT size_t SIZE = SIZE_;
96
97 typedef T value_type;
98 typedef size_t size_type;
100 typedef T& reference;
101 typedef const T& const_reference;
102 typedef T* pointer;
103 typedef const T* const_pointer;
104 typedef T* iterator;
105 typedef const T* const_iterator;
106 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
107 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
108
109 //*************************************************************************
110 // Element access
111 //*************************************************************************
112
113 //*************************************************************************
116 //*************************************************************************
117 ETL_NODISCARD
118 ETL_CONSTEXPR14
119 reference at(size_t i)
120 {
121 ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
122
123 return _buffer[i];
124 }
125
126 //*************************************************************************
129 //*************************************************************************
130 ETL_NODISCARD
131 ETL_CONSTEXPR14
132 const_reference at(size_t i) const
133 {
134 ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
135
136 return _buffer[i];
137 }
138
139 //*************************************************************************
143 //*************************************************************************
144 ETL_NODISCARD
145 ETL_CONSTEXPR14
147 {
148 return _buffer[i];
149 }
150
151 //*************************************************************************
155 //*************************************************************************
156 ETL_NODISCARD
157 ETL_CONSTEXPR const_reference operator[](size_t i) const
158 {
159 return _buffer[i];
160 }
161
162 //*************************************************************************
164 //*************************************************************************
165 ETL_NODISCARD
166 ETL_CONSTEXPR14
168 {
169 return _buffer[0];
170 }
171
172 //*************************************************************************
174 //*************************************************************************
175 ETL_NODISCARD
176 ETL_CONSTEXPR const_reference front() const
177 {
178 return _buffer[0];
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 ETL_NODISCARD
185 ETL_CONSTEXPR14
187 {
188 return _buffer[SIZE - 1];
189 }
190
191 //*************************************************************************
193 //*************************************************************************
194 ETL_NODISCARD
195 ETL_CONSTEXPR const_reference back() const
196 {
197 return _buffer[SIZE - 1];
198 }
199
200 //*************************************************************************
202 //*************************************************************************
203 ETL_NODISCARD
204 ETL_CONSTEXPR14
205 pointer data() ETL_NOEXCEPT
206 {
207 return &_buffer[0];
208 }
209
210 //*************************************************************************
212 //*************************************************************************
213 ETL_NODISCARD
214 ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
215 {
216 return &_buffer[0];
217 }
218
219 //*************************************************************************
220 // Iterators
221 //*************************************************************************
222
223 //*************************************************************************
225 //*************************************************************************
226 ETL_NODISCARD
227 ETL_CONSTEXPR14
228 iterator begin() ETL_NOEXCEPT
229 {
230 return &_buffer[0];
231 }
232
233 //*************************************************************************
235 //*************************************************************************
236 ETL_NODISCARD
237 ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
238 {
239 return &_buffer[0];
240 }
241
242 //*************************************************************************
244 //*************************************************************************
245 ETL_NODISCARD
246 ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
247 {
248 return begin();
249 }
250
251 //*************************************************************************
253 //*************************************************************************
254 ETL_NODISCARD
255 ETL_CONSTEXPR14
256 iterator end() ETL_NOEXCEPT
257 {
258 return _buffer + SIZE;
259 }
260
261 //*************************************************************************
263 //*************************************************************************
264 ETL_NODISCARD
265 ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
266 {
267 return _buffer + SIZE;
268 }
269
270 //*************************************************************************
271 // Returns a const iterator to the end of the array.
272 //*************************************************************************
273 ETL_NODISCARD
274 ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
275 {
276 return _buffer + SIZE;
277 }
278
279 //*************************************************************************
280 // Returns an reverse iterator to the reverse beginning of the array.
281 //*************************************************************************
282 ETL_NODISCARD
283 ETL_CONSTEXPR14
284 reverse_iterator rbegin() ETL_NOEXCEPT
285 {
286 return reverse_iterator(end());
287 }
288
289 //*************************************************************************
291 //*************************************************************************
292 ETL_NODISCARD
293 ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
294 {
295 return const_reverse_iterator(end());
296 }
297
298 //*************************************************************************
300 //*************************************************************************
301 ETL_NODISCARD
302 ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
303 {
304 return const_reverse_iterator(end());
305 }
306
307 //*************************************************************************
309 //*************************************************************************
310 ETL_NODISCARD
311 ETL_CONSTEXPR14
312 reverse_iterator rend() ETL_NOEXCEPT
313 {
314 return reverse_iterator(begin());
315 }
316
317 //*************************************************************************
319 //*************************************************************************
320 ETL_NODISCARD
321 ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
322 {
323 return const_reverse_iterator(begin());
324 }
325
326 //*************************************************************************
328 //*************************************************************************
329 ETL_NODISCARD
330 ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
331 {
332 return const_reverse_iterator(begin());
333 }
334
335 //*************************************************************************
336 // Capacity
337 //*************************************************************************
338
339 //*************************************************************************
341 //*************************************************************************
342 ETL_NODISCARD
343 ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
344 {
345 return (SIZE == 0);
346 }
347
348 //*************************************************************************
350 //*************************************************************************
351 ETL_NODISCARD
352 ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
353 {
354 return SIZE;
355 }
356
357 //*************************************************************************
359 //*************************************************************************
360 ETL_NODISCARD
361 ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
362 {
363 return SIZE;
364 }
365
366 //*************************************************************************
367 // Operations
368 //*************************************************************************
369
370 //*************************************************************************
373 //*************************************************************************
374 ETL_CONSTEXPR14 void fill(parameter_t value)
375 {
376 etl::fill(_buffer, _buffer + SIZE, value);
377 }
378
379 //*************************************************************************
382 //*************************************************************************
383 ETL_CONSTEXPR14 void swap(array& other) ETL_NOEXCEPT
384 {
385 using ETL_OR_STD::swap; // Allow ADL
386
387 for (size_t i = 0UL; i < SIZE; ++i)
388 {
389 swap(_buffer[i], other._buffer[i]);
390 }
391 }
392
393 //*************************************************************************
399 //*************************************************************************
400 template <typename TIterator>
402 {
403 return etl::copy_s(first, last, begin(), end());
404 }
405
406 //*************************************************************************
412 //*************************************************************************
413 template <typename TIterator>
415 {
416 // Copy from the range.
417 iterator p = etl::copy_s(first, last, begin(), end());
418
419 // Initialise any that are left.
420 etl::fill(p, end(), value);
421
422 return p;
423 }
424
425 //*************************************************************************
429 //*************************************************************************
430 inline iterator insert_at(size_t position, parameter_t value)
431 {
432 return insert(begin() + position, value);
433 }
434
435 //*************************************************************************
439 //*************************************************************************
441 {
442 iterator p = to_iterator(position);
443
444 etl::move_backward(p, end() - 1, end());
445 *p = value;
446
447 return p;
448 }
449
450 //*************************************************************************
455 //*************************************************************************
456 template <typename TIterator>
457 inline iterator insert_at(size_t position, TIterator first, const TIterator last)
458 {
459 return insert(begin() + position, first, last);
460 }
461
462 //*************************************************************************
467 //*************************************************************************
468 template <typename TIterator>
469 iterator insert(const_iterator position, TIterator first, const TIterator last)
470 {
471 iterator p = to_iterator(position);
472 iterator result(p);
473
474 size_t source_size = etl::distance(first, last);
475 size_t destination_space = etl::distance(position, cend());
476
477 // Do we need to move anything?
479 {
480 size_t length = SIZE - (etl::distance(begin(), p) + source_size);
481 etl::move_backward(p, p + length, end());
482 }
483
484 // Copy from the range.
485 etl::copy_s(first, last, p, end());
486
487 return result;
488 }
489
490 //*************************************************************************
494 //*************************************************************************
495 inline iterator erase_at(size_t position)
496 {
497 return erase(begin() + position);
498 }
499
500 //*************************************************************************
504 //*************************************************************************
506 {
507 iterator p = to_iterator(position);
508 etl::move(p + 1, end(), p);
509
510 return p;
511 }
512
513 //*************************************************************************
518 //*************************************************************************
519 iterator erase_range(size_t first, size_t last)
520 {
521 return erase(begin() + first, begin() + last);
522 }
523
524 //*************************************************************************
529 //*************************************************************************
531 {
532 iterator p = to_iterator(first);
533 etl::move(last, cend(), p);
534 return p;
535 }
536
537 //*************************************************************************
541 //*************************************************************************
542 inline iterator erase_at(size_t position, parameter_t value)
543 {
544 return erase(begin() + position, value);
545 }
546
547 //*************************************************************************
551 //*************************************************************************
553 {
554 iterator p = to_iterator(position);
555
556 etl::move(p + 1, end(), p);
557 back() = value;
558
559 return p;
560 }
561
562 //*************************************************************************
567 //*************************************************************************
568 iterator erase_range(size_t first, size_t last, parameter_t value)
569 {
570 return erase(begin() + first, begin() + last, value);
571 }
572
573 //*************************************************************************
577 //*************************************************************************
579 {
580 iterator p = to_iterator(first);
581
582 p = etl::move(last, cend(), p);
583 etl::fill(p, end(), value);
584
585 return to_iterator(first);
586 }
587
589 T _buffer[SIZE];
590
591 private:
592
593 //*************************************************************************
595 //*************************************************************************
596 iterator to_iterator(const_iterator itr) const
597 {
598 return const_cast<iterator>(itr);
599 }
600 };
601
602 template <typename T, size_t SIZE_>
603 ETL_CONSTANT size_t array<T, SIZE_>::SIZE;
604
605 //*************************************************************************
607 //*************************************************************************
608#if ETL_USING_CPP17
609 template <typename... T>
610 array(T...) -> array<typename etl::common_type<T...>::type, sizeof...(T)>;
611#endif
612
613 //*************************************************************************
615 //*************************************************************************
616#if ETL_HAS_INITIALIZER_LIST
617 template <typename T, typename... TValues>
618 constexpr auto make_array(TValues&&... values) -> etl::array<T, sizeof...(TValues)>
619 {
620 return { etl::forward<T>(values)... };
621 }
622#endif
623
624 //*************************************************************************
628 //*************************************************************************
629 template <typename T, const size_t SIZE>
631 {
632 lhs.swap(rhs);
633 }
634
635 //*************************************************************************
640 //*************************************************************************
641 template <typename T, size_t SIZE>
643 {
644 return etl::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin());
645 }
646
647 //*************************************************************************
652 //*************************************************************************
653 template <typename T, size_t SIZE>
655 {
656 return !(lhs == rhs);
657 }
658
659 //*************************************************************************
664 //*************************************************************************
665 template <typename T, size_t SIZE>
667 {
668 return etl::lexicographical_compare(lhs.cbegin(),
669 lhs.cend(),
670 rhs.cbegin(),
671 rhs.cend());
672 }
673
674 //*************************************************************************
679 //*************************************************************************
680 template <typename T, size_t SIZE>
682 {
683 return !(lhs > rhs);
684 }
685
686 //*************************************************************************
691 template <typename T, size_t SIZE>
692 //*************************************************************************
694 {
695 return (rhs < lhs);
696 }
697
698 //*************************************************************************
703 //*************************************************************************
704 template <typename T, size_t SIZE>
706 {
707 return !(lhs < rhs);
708 }
709
710 //*************************************************************************
717 //*************************************************************************
718 template <size_t I, typename T, size_t MAXN>
719 inline T& get(array<T, MAXN>& a)
720 {
721 ETL_STATIC_ASSERT(I < MAXN, "Index out of bounds");
722 return a[I];
723 }
724
725 //*************************************************************************
732 //*************************************************************************
733 template <size_t I, typename T, size_t MAXN>
734 inline const T& get(const array<T, MAXN>& a)
735 {
736 ETL_STATIC_ASSERT(I < MAXN, "Index out of bounds");
737 return a[I];
738 }
739}
740
741#endif
ETL_CONSTEXPR14 etl::enable_if< etl::is_random_iterator< TInputIterator >::value &&etl::is_random_iterator< TOutputIterator >::value, TOutputIterator >::type copy_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end)
Definition algorithm.h:2260
ETL_NODISCARD ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition array.h:195
iterator erase(const_iterator first, const_iterator last)
Definition array.h:530
ETL_CONSTEXPR14 void swap(array &other) ETL_NOEXCEPT
Definition array.h:383
ETL_NODISCARD ETL_CONSTEXPR14 reverse_iterator rend() ETL_NOEXCEPT
Returns a reverse iterator to the end of the array.
Definition array.h:312
ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition array.h:302
ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition array.h:293
ETL_NODISCARD ETL_CONSTEXPR14 iterator begin() ETL_NOEXCEPT
Returns an iterator to the beginning of the array.
Definition array.h:228
T _buffer[SIZE]
The array data.
Definition array.h:589
ETL_NODISCARD ETL_CONSTEXPR14 reference back()
Returns a reference to the last element.
Definition array.h:186
ETL_NODISCARD ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition array.h:237
ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const
Definition array.h:132
ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i)
Definition array.h:119
ETL_NODISCARD ETL_CONSTEXPR14 pointer data() ETL_NOEXCEPT
Returns a pointer to the first element of the internal buffer.
Definition array.h:205
ETL_NODISCARD ETL_CONSTEXPR14 reference front()
Returns a reference to the first element.
Definition array.h:167
iterator erase_range(size_t first, size_t last)
Definition array.h:519
ETL_CONSTEXPR14 void fill(parameter_t value)
Definition array.h:374
iterator insert_at(size_t position, parameter_t value)
Definition array.h:430
iterator erase_range(size_t first, size_t last, parameter_t value)
Definition array.h:568
ETL_NODISCARD ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition array.h:265
ETL_NODISCARD ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition array.h:176
iterator erase_at(size_t position)
Definition array.h:495
iterator erase(const_iterator first, const_iterator last, parameter_t value)
Definition array.h:578
iterator erase(const_iterator position, parameter_t value)
Definition array.h:552
ETL_NODISCARD ETL_CONSTEXPR14 iterator end() ETL_NOEXCEPT
Returns an iterator to the end of the array.
Definition array.h:256
iterator insert(const_iterator position, parameter_t value)
Definition array.h:440
ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition array.h:330
ETL_NODISCARD ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition array.h:343
ETL_NODISCARD ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal buffer.
Definition array.h:214
ETL_NODISCARD ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition array.h:352
ETL_NODISCARD ETL_CONSTEXPR const_reference operator[](size_t i) const
Definition array.h:157
iterator insert(const_iterator position, TIterator first, const TIterator last)
Definition array.h:469
iterator assign(TIterator first, const TIterator last)
Definition array.h:401
ETL_NODISCARD ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition array.h:246
iterator erase(const_iterator position)
Definition array.h:505
iterator assign(TIterator first, const TIterator last, parameter_t value)
Definition array.h:414
ETL_NODISCARD ETL_CONSTEXPR14 reference operator[](size_t i)
Definition array.h:146
iterator erase_at(size_t position, parameter_t value)
Definition array.h:542
iterator insert_at(size_t position, TIterator first, const TIterator last)
Definition array.h:457
ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition array.h:321
ETL_NODISCARD ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition array.h:361
Definition array.h:88
Definition array.h:59
Definition array.h:73
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
bitset_ext
Definition absolute.h:38
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:693
T & get(array< T, MAXN > &a)
Definition array.h:719
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:705
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:654
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition array.h:630
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:642
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:666
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:681
pair holds two objects of arbitrary type
Definition utility.h:164