FastLED 3.10.6
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/cstring.h" // IWYU pragma: keep
5#include "fl/stl/bit_cast.h" // IWYU pragma: keep
6#include "fl/stl/new.h" // for placement new operator // IWYU pragma: keep
7#include "fl/stl/allocator.h" // IWYU pragma: keep
8
9#include "fl/stl/initializer_list.h" // IWYU pragma: keep
10#include "fl/stl/alloca.h" // IWYU pragma: keep
11#include "fl/stl/noexcept.h"
12
13namespace fl {
14
15// Forward declaration for span (defined in fl/stl/span.h)
16template <typename T, fl::size Extent> class span; // IWYU pragma: keep
17
27template <typename T, fl::size N> class array {
28 public:
29 // Standard container type definitions
30 using value_type = T;
31 using size_type = fl::size;
34 using const_reference = const value_type &;
36 using const_pointer = const value_type *;
39
40 // Data member - public to allow aggregate initialization
41 T mData[N];
42
43 // Element access
44 T &at(fl::size pos) FL_NO_EXCEPT {
45 if (pos >= N) {
46 return error_value();
47 }
48 return mData[pos];
49 }
50
51 const T &at(fl::size pos) const FL_NO_EXCEPT {
52 if (pos >= N) {
53 return error_value();
54 }
55 return mData[pos];
56 }
57
58 T &operator[](fl::size pos) FL_NO_EXCEPT { return mData[pos]; }
59
60 const_reference operator[](fl::size pos) const FL_NO_EXCEPT { return mData[pos]; }
61
62 T &front() FL_NO_EXCEPT { return mData[0]; }
63
64 const T &front() const FL_NO_EXCEPT { return mData[0]; }
65
66 T &back() FL_NO_EXCEPT { return mData[N - 1]; }
67
68 const T &back() const FL_NO_EXCEPT { return mData[N - 1]; }
69
71
73
74 // Iterators
76
78
80
81 iterator end() FL_NO_EXCEPT { return mData + N; }
82
83 const_iterator end() const FL_NO_EXCEPT { return mData + N; }
84
85 const_iterator cend() const FL_NO_EXCEPT { return mData + N; }
86
87 // Capacity
88 bool empty() const FL_NO_EXCEPT { return N == 0; }
89
90 fl::size size() const FL_NO_EXCEPT { return N; }
91
92 fl::size max_size() const FL_NO_EXCEPT { return N; }
93
94 // Operations
95 void fill(const T &value) FL_NO_EXCEPT {
96 for (fl::size i = 0; i < N; ++i) {
97 mData[i] = value;
98 }
99 }
100
101 void swap(array &other) FL_NO_EXCEPT {
102 for (fl::size i = 0; i < N; ++i) {
103 fl::swap(mData[i], other.mData[i]);
104 }
105 }
106
107 private:
109 static T empty_value;
110 return empty_value;
111 }
112};
113
114// Specialization for zero-size array to avoid T mData[0] UB.
115template <typename T> class array<T, 0> {
116 public:
117 using value_type = T;
118 using size_type = fl::size;
123 using const_pointer = const value_type *;
126
127 // Element access — all return error_value since there are no elements
128 T &at(fl::size) FL_NO_EXCEPT { return error_value(); }
129 const T &at(fl::size) const FL_NO_EXCEPT { return error_value(); }
130 T &operator[](fl::size) FL_NO_EXCEPT { return error_value(); }
132 T &front() FL_NO_EXCEPT { return error_value(); }
133 const T &front() const FL_NO_EXCEPT { return error_value(); }
134 T &back() FL_NO_EXCEPT { return error_value(); }
135 const T &back() const FL_NO_EXCEPT { return error_value(); }
136 pointer data() FL_NO_EXCEPT { return nullptr; }
137 const_pointer data() const FL_NO_EXCEPT { return nullptr; }
138
139 // Iterators — begin == end for empty container
140 iterator begin() FL_NO_EXCEPT { return nullptr; }
141 const_iterator begin() const FL_NO_EXCEPT { return nullptr; }
142 const_iterator cbegin() const FL_NO_EXCEPT { return nullptr; }
143 iterator end() FL_NO_EXCEPT { return nullptr; }
144 const_iterator end() const FL_NO_EXCEPT { return nullptr; }
145 const_iterator cend() const FL_NO_EXCEPT { return nullptr; }
146
147 // Capacity
148 bool empty() const FL_NO_EXCEPT { return true; }
149 fl::size size() const FL_NO_EXCEPT { return 0; }
150 fl::size max_size() const FL_NO_EXCEPT { return 0; }
151
152 // Operations
153 void fill(const T &) FL_NO_EXCEPT {}
155
156 private:
158 static T empty_value;
159 return empty_value;
160 }
161};
162
163// Non-member functions
164template <typename T, fl::size N>
165bool operator==(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
166 // return std::equal(lhs.begin(), lhs.end(), rhs.begin());
167 for (fl::size i = 0; i < N; ++i) {
168 if (lhs[i] != rhs[i]) {
169 return false;
170 }
171 }
172 return true;
173}
174
175template <typename T, fl::size N>
176bool operator!=(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
177 return !(lhs == rhs);
178}
179
180template <typename T, fl::size N>
181bool operator<(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
182 for (fl::size i = 0; i < N; ++i) {
183 if (lhs[i] < rhs[i]) {
184 return true;
185 }
186 if (rhs[i] < lhs[i]) {
187 return false;
188 }
189 }
190 return false;
191}
192
193template <typename T, fl::size N>
194bool operator<=(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
195 return !(rhs < lhs);
196}
197
198template <typename T, fl::size N>
199bool operator>(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
200 return rhs < lhs;
201}
202
203template <typename T, fl::size N>
204bool operator>=(const array<T, N> &lhs, const array<T, N> &rhs) FL_NO_EXCEPT {
205 return !(lhs < rhs);
206}
207
208template <typename T, fl::size N>
211 lhs.swap(rhs);
212}
213
214// Helper function to create array from span (dynamic extent)
215// Copies exactly N elements from span (span must have at least N elements)
216template <fl::size N, typename T>
217array<T, N> to_array(fl::span<const T, fl::size(-1)> s) FL_NO_EXCEPT {
219 for (fl::size i = 0; i < N; ++i) {
220 result.mData[i] = s[i];
221 }
222 return result;
223}
224
225// Helper function to create array from span (static extent)
226template <typename T, fl::size N>
229 for (fl::size i = 0; i < N; ++i) {
230 result.mData[i] = s[i];
231 }
232 return result;
233}
234
235} // namespace fl
uint8_t pos
Definition Blur.ino:11
const T & front() const FL_NO_EXCEPT
Definition array.h:133
pointer iterator
Definition array.h:124
fl::size max_size() const FL_NO_EXCEPT
Definition array.h:150
T & at(fl::size) FL_NO_EXCEPT
Definition array.h:128
const_pointer const_iterator
Definition array.h:125
fl::size size_type
Definition array.h:118
const_reference operator[](fl::size) const FL_NO_EXCEPT
Definition array.h:131
const_pointer data() const FL_NO_EXCEPT
Definition array.h:137
bool empty() const FL_NO_EXCEPT
Definition array.h:148
T & back() FL_NO_EXCEPT
Definition array.h:134
iterator end() FL_NO_EXCEPT
Definition array.h:143
value_type * pointer
Definition array.h:122
static T & error_value() FL_NO_EXCEPT
Definition array.h:157
pointer data() FL_NO_EXCEPT
Definition array.h:136
ptrdiff_t difference_type
Definition array.h:119
void swap(array &) FL_NO_EXCEPT
Definition array.h:154
const_iterator cbegin() const FL_NO_EXCEPT
Definition array.h:142
const value_type * const_pointer
Definition array.h:123
const T & at(fl::size) const FL_NO_EXCEPT
Definition array.h:129
T & operator[](fl::size) FL_NO_EXCEPT
Definition array.h:130
const value_type & const_reference
Definition array.h:121
const_iterator cend() const FL_NO_EXCEPT
Definition array.h:145
fl::size size() const FL_NO_EXCEPT
Definition array.h:149
const_iterator end() const FL_NO_EXCEPT
Definition array.h:144
value_type & reference
Definition array.h:120
const_iterator begin() const FL_NO_EXCEPT
Definition array.h:141
iterator begin() FL_NO_EXCEPT
Definition array.h:140
T & front() FL_NO_EXCEPT
Definition array.h:132
void fill(const T &) FL_NO_EXCEPT
Definition array.h:153
const T & back() const FL_NO_EXCEPT
Definition array.h:135
T & front() FL_NO_EXCEPT
Definition array.h:62
const_iterator begin() const FL_NO_EXCEPT
Definition array.h:77
value_type & reference
Definition array.h:33
const_iterator cbegin() const FL_NO_EXCEPT
Definition array.h:79
ptrdiff_t difference_type
Definition array.h:32
const T & back() const FL_NO_EXCEPT
Definition array.h:68
const_pointer const_iterator
Definition array.h:38
bool empty() const FL_NO_EXCEPT
Definition array.h:88
void fill(const T &value) FL_NO_EXCEPT
Definition array.h:95
pointer data() FL_NO_EXCEPT
Definition array.h:70
const value_type * const_pointer
Definition array.h:36
static T & error_value() FL_NO_EXCEPT
Definition array.h:108
const value_type & const_reference
Definition array.h:34
const_iterator end() const FL_NO_EXCEPT
Definition array.h:83
fl::size size() const FL_NO_EXCEPT
Definition array.h:90
const T & front() const FL_NO_EXCEPT
Definition array.h:64
fl::size max_size() const FL_NO_EXCEPT
Definition array.h:92
fl::size size_type
Definition array.h:31
T & at(fl::size pos) FL_NO_EXCEPT
Definition array.h:44
iterator end() FL_NO_EXCEPT
Definition array.h:81
value_type * pointer
Definition array.h:35
T & back() FL_NO_EXCEPT
Definition array.h:66
void swap(array &other) FL_NO_EXCEPT
Definition array.h:101
iterator begin() FL_NO_EXCEPT
Definition array.h:75
const_reference operator[](fl::size pos) const FL_NO_EXCEPT
Definition array.h:60
const_iterator cend() const FL_NO_EXCEPT
Definition array.h:85
pointer iterator
Definition array.h:37
const_pointer data() const FL_NO_EXCEPT
Definition array.h:72
T & operator[](fl::size pos) FL_NO_EXCEPT
Definition array.h:58
const T & at(fl::size pos) const FL_NO_EXCEPT
Definition array.h:51
A fixed-size array implementation similar to std::array.
Definition array.h:27
void swap(array< T, N > &lhs, array< T, N > &rhs) FL_NO_EXCEPT
Definition array.h:209
constexpr int type_rank< T >::value
FASTLED_FORCE_INLINE bool operator==(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if two CRGB objects have the same color data.
Definition crgb.h:740
array< T, N > to_array(fl::span< const T, fl::size(-1)> s) FL_NO_EXCEPT
Definition array.h:217
FASTLED_FORCE_INLINE bool operator>(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is greater than another.
Definition crgb.h:761
FASTLED_FORCE_INLINE bool operator<=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is less than or equal to another.
Definition crgb.h:779
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
fl::ptrdiff_t ptrdiff_t
Definition s16x16x4.h:63
FASTLED_FORCE_INLINE bool operator!=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if two CRGB objects do not have the same color data.
Definition crgb.h:746
FASTLED_FORCE_INLINE bool operator>=(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is greater than or equal to another.
Definition crgb.h:770
FASTLED_FORCE_INLINE bool operator<(const CRGB &lhs, const CRGB &rhs) FL_NO_EXCEPT
Check if the sum of the color channels in one CRGB object is less than another.
Definition crgb.h:752
Base definition for an LED controller.
Definition crgb.hpp:179