FastLED 3.9.15
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#pragma once
2
3#include <string.h>
4
5#include "fl/inplacenew.h"
6#include "fl/type_traits.h"
7
8namespace fl {
9
19template <typename T, size_t N> class array {
20 public:
21 // Standard container type definitions
22 using value_type = T;
23 using size_type = size_t;
24 using difference_type = ptrdiff_t;
26 using const_reference = const value_type &;
28 using const_pointer = const value_type *;
31
32 // Default constructor - elements are default-initialized
33 array() = default;
34
35 // Fill constructor
36 explicit array(const T &value) {
37 // std::fill_n(begin(), N, value);
38 fill_n(data_, N, value);
39 }
40
41 // Copy constructor
42 array(const array &) = default;
43
44 // Move constructor
45 array(array &&) = default;
46
47 // Copy assignment
48 array &operator=(const array &) = default;
49
50 // Move assignment
51 array &operator=(array &&) = default;
52
53 // Element access
55 if (pos >= N) {
56 return error_value();
57 }
58 return data_[pos];
59 }
60
61 const T &at(size_type pos) const {
62 if (pos >= N) {
63 return error_value();
64 }
65 return data_[pos];
66 }
67
68 T &operator[](size_type pos) { return data_[pos]; }
69
71
72 T &front() { return data_[0]; }
73
74 const T &front() const { return data_[0]; }
75
76 T &back() { return data_[N - 1]; }
77
78 const T &back() const { return data_[N - 1]; }
79
80 pointer data() noexcept { return data_; }
81
82 const_pointer data() const noexcept { return data_; }
83
84 // Iterators
85 iterator begin() noexcept { return data_; }
86
87 const_iterator begin() const noexcept { return data_; }
88
89 const_iterator cbegin() const noexcept { return data_; }
90
91 iterator end() noexcept { return data_ + N; }
92
93 const_iterator end() const noexcept { return data_ + N; }
94
95 // Capacity
96 bool empty() const noexcept { return N == 0; }
97
98 size_type size() const noexcept { return N; }
99
100 size_type max_size() const noexcept { return N; }
101
102 // Operations
103 void fill(const T &value) {
104 for (size_type i = 0; i < N; ++i) {
105 data_[i] = value;
106 }
107 }
108
109 void swap(array &other) {
110 for (size_type i = 0; i < N; ++i) {
111 fl::swap(data_[i], other.data_[i]);
112 }
113 }
114
115 private:
116 static T &error_value() {
117 static T empty_value;
118 return empty_value;
119 }
120 T data_[N];
121};
122
123// Non-member functions
124template <typename T, size_t N>
125bool operator==(const array<T, N> &lhs, const array<T, N> &rhs) {
126 // return std::equal(lhs.begin(), lhs.end(), rhs.begin());
127 for (size_t i = 0; i < N; ++i) {
128 if (lhs[i] != rhs[i]) {
129 return false;
130 }
131 }
132}
133
134template <typename T, size_t N>
135bool operator!=(const array<T, N> &lhs, const array<T, N> &rhs) {
136 return !(lhs == rhs);
137}
138
139template <typename T, size_t N>
141 array<T, N> &rhs) noexcept(noexcept(lhs.swap(rhs))) {
142 lhs.swap(rhs);
143}
144
145} // namespace fl
146
147// FASTLED_STACK_ARRAY
148// An array of variable length that is allocated on the stack using
149// either alloca or a variable length array (VLA) support built into the
150// the compiler.
151// Example:
152// Instead of: int array[buff_size];
153// You'd use: FASTLED_STACK_ARRAY(int, array, buff_size);
154
155#ifndef FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
156#if defined(__clang__) || defined(ARDUINO_GIGA_M7) || defined(ARDUINO_GIGA)
157// Clang doesn't have variable length arrays. Therefore we need to emulate them
158// using alloca. It's been found that Arduino Giga M7 also doesn't support
159// variable length arrays for some reason so we force it to emulate them as well
160// in this case.
161#define FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 1
162#else
163// Else, assume the compiler is gcc, which has variable length arrays
164#define FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION 0
165#endif
166#endif // FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
167
168#if !FASTLED_VARIABLE_LENGTH_ARRAY_NEEDS_EMULATION
169#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
170 TYPE NAME[SIZE]; \
171 memset(NAME, 0, sizeof(TYPE) * (SIZE))
172#elif __has_include(<alloca.h>)
173#include <alloca.h>
174#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
175 TYPE *NAME = reinterpret_cast<TYPE *>(alloca(sizeof(TYPE) * (SIZE))); \
176 memset(NAME, 0, sizeof(TYPE) * (SIZE))
177#elif __has_include(<cstdlib>)
178#include <cstdlib>
179#define FASTLED_STACK_ARRAY(TYPE, NAME, SIZE) \
180 TYPE *NAME = reinterpret_cast<TYPE *>(alloca(sizeof(TYPE) * (SIZE))); \
181 memset(NAME, 0, sizeof(TYPE) * (SIZE))
182#else
183#error "Compiler does not allow variable type arrays."
184#endif
uint8_t pos
Definition Blur.ino:11
T & front()
Definition array.h:72
const_reference operator[](size_type pos) const
Definition array.h:70
array(array &&)=default
value_type & reference
Definition array.h:25
pointer data() noexcept
Definition array.h:80
ptrdiff_t difference_type
Definition array.h:24
const_pointer const_iterator
Definition array.h:30
array()=default
bool empty() const noexcept
Definition array.h:96
size_type size() const noexcept
Definition array.h:98
T & back()
Definition array.h:76
array & operator=(array &&)=default
array(const array &)=default
size_type max_size() const noexcept
Definition array.h:100
const value_type * const_pointer
Definition array.h:28
const_iterator end() const noexcept
Definition array.h:93
array(const T &value)
Definition array.h:36
T data_[N]
Definition array.h:120
const T & back() const
Definition array.h:78
void swap(array &other)
Definition array.h:109
T value_type
Definition array.h:22
const value_type & const_reference
Definition array.h:26
void fill(const T &value)
Definition array.h:103
const T & front() const
Definition array.h:74
T & operator[](size_type pos)
Definition array.h:68
array & operator=(const array &)=default
iterator begin() noexcept
Definition array.h:85
size_t size_type
Definition array.h:23
T & at(size_type pos)
Definition array.h:54
const T & at(size_type pos) const
Definition array.h:61
value_type * pointer
Definition array.h:27
iterator end() noexcept
Definition array.h:91
const_iterator begin() const noexcept
Definition array.h:87
static T & error_value()
Definition array.h:116
pointer iterator
Definition array.h:29
const_pointer data() const noexcept
Definition array.h:82
const_iterator cbegin() const noexcept
Definition array.h:89
A fixed-size array implementation similar to std::array.
Definition array.h:19
void swap(array< T, N > &lhs, array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition array.h:140
bool operator!=(const array< T, N > &lhs, const array< T, N > &rhs)
Definition array.h:135
bool operator==(const array< T, N > &lhs, const array< T, N > &rhs)
Definition array.h:125
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16