FastLED 3.9.15
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_NOEXCEPT {
45 if (pos >= N) {
46 return error_value();
47 }
48 return mData[pos];
49 }
50
51 const T &at(fl::size pos) const FL_NOEXCEPT {
52 if (pos >= N) {
53 return error_value();
54 }
55 return mData[pos];
56 }
57
58 T &operator[](fl::size pos) FL_NOEXCEPT { return mData[pos]; }
59
60 const_reference operator[](fl::size pos) const FL_NOEXCEPT { return mData[pos]; }
61
62 T &front() FL_NOEXCEPT { return mData[0]; }
63
64 const T &front() const FL_NOEXCEPT { return mData[0]; }
65
66 T &back() FL_NOEXCEPT { return mData[N - 1]; }
67
68 const T &back() const FL_NOEXCEPT { return mData[N - 1]; }
69
71
72 const_pointer data() const FL_NOEXCEPT { return mData; }
73
74 // Iterators
76
78
80
81 iterator end() FL_NOEXCEPT { return mData + N; }
82
83 const_iterator end() const FL_NOEXCEPT { return mData + N; }
84
85 const_iterator cend() const FL_NOEXCEPT { return mData + N; }
86
87 // Capacity
88 bool empty() const FL_NOEXCEPT { return N == 0; }
89
90 fl::size size() const FL_NOEXCEPT { return N; }
91
92 fl::size max_size() const FL_NOEXCEPT { return N; }
93
94 // Operations
95 void fill(const T &value) FL_NOEXCEPT {
96 for (fl::size i = 0; i < N; ++i) {
97 mData[i] = value;
98 }
99 }
100
101 void swap(array &other) FL_NOEXCEPT {
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_NOEXCEPT { return error_value(); }
129 const T &at(fl::size) const FL_NOEXCEPT { return error_value(); }
130 T &operator[](fl::size) FL_NOEXCEPT { return error_value(); }
131 const_reference operator[](fl::size) const FL_NOEXCEPT { return error_value(); }
132 T &front() FL_NOEXCEPT { return error_value(); }
133 const T &front() const FL_NOEXCEPT { return error_value(); }
134 T &back() FL_NOEXCEPT { return error_value(); }
135 const T &back() const FL_NOEXCEPT { return error_value(); }
136 pointer data() FL_NOEXCEPT { return nullptr; }
137 const_pointer data() const FL_NOEXCEPT { return nullptr; }
138
139 // Iterators — begin == end for empty container
140 iterator begin() FL_NOEXCEPT { return nullptr; }
141 const_iterator begin() const FL_NOEXCEPT { return nullptr; }
142 const_iterator cbegin() const FL_NOEXCEPT { return nullptr; }
143 iterator end() FL_NOEXCEPT { return nullptr; }
144 const_iterator end() const FL_NOEXCEPT { return nullptr; }
145 const_iterator cend() const FL_NOEXCEPT { return nullptr; }
146
147 // Capacity
148 bool empty() const FL_NOEXCEPT { return true; }
149 fl::size size() const FL_NOEXCEPT { return 0; }
150 fl::size max_size() const FL_NOEXCEPT { return 0; }
151
152 // Operations
153 void fill(const T &) FL_NOEXCEPT {}
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT {
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
T & back() FL_NOEXCEPT
Definition array.h:134
pointer iterator
Definition array.h:124
const_pointer const_iterator
Definition array.h:125
fl::size size_type
Definition array.h:118
const_iterator end() const FL_NOEXCEPT
Definition array.h:144
T & front() FL_NOEXCEPT
Definition array.h:132
value_type * pointer
Definition array.h:122
bool empty() const FL_NOEXCEPT
Definition array.h:148
ptrdiff_t difference_type
Definition array.h:119
const T & at(fl::size) const FL_NOEXCEPT
Definition array.h:129
const_iterator cend() const FL_NOEXCEPT
Definition array.h:145
void fill(const T &) FL_NOEXCEPT
Definition array.h:153
T & operator[](fl::size) FL_NOEXCEPT
Definition array.h:130
const value_type * const_pointer
Definition array.h:123
T & at(fl::size) FL_NOEXCEPT
Definition array.h:128
const_reference operator[](fl::size) const FL_NOEXCEPT
Definition array.h:131
fl::size size() const FL_NOEXCEPT
Definition array.h:149
iterator end() FL_NOEXCEPT
Definition array.h:143
static T & error_value() FL_NOEXCEPT
Definition array.h:157
const_iterator begin() const FL_NOEXCEPT
Definition array.h:141
const value_type & const_reference
Definition array.h:121
const_iterator cbegin() const FL_NOEXCEPT
Definition array.h:142
fl::size max_size() const FL_NOEXCEPT
Definition array.h:150
const_pointer data() const FL_NOEXCEPT
Definition array.h:137
iterator begin() FL_NOEXCEPT
Definition array.h:140
pointer data() FL_NOEXCEPT
Definition array.h:136
const T & front() const FL_NOEXCEPT
Definition array.h:133
value_type & reference
Definition array.h:120
const T & back() const FL_NOEXCEPT
Definition array.h:135
void swap(array &) FL_NOEXCEPT
Definition array.h:154
const_reference operator[](fl::size pos) const FL_NOEXCEPT
Definition array.h:60
iterator end() FL_NOEXCEPT
Definition array.h:81
value_type & reference
Definition array.h:33
ptrdiff_t difference_type
Definition array.h:32
const_pointer const_iterator
Definition array.h:38
const_iterator end() const FL_NOEXCEPT
Definition array.h:83
const_pointer data() const FL_NOEXCEPT
Definition array.h:72
void fill(const T &value) FL_NOEXCEPT
Definition array.h:95
bool empty() const FL_NOEXCEPT
Definition array.h:88
const value_type * const_pointer
Definition array.h:36
fl::size max_size() const FL_NOEXCEPT
Definition array.h:92
const T & back() const FL_NOEXCEPT
Definition array.h:68
const_iterator begin() const FL_NOEXCEPT
Definition array.h:77
T & operator[](fl::size pos) FL_NOEXCEPT
Definition array.h:58
const value_type & const_reference
Definition array.h:34
const_iterator cend() const FL_NOEXCEPT
Definition array.h:85
T & front() FL_NOEXCEPT
Definition array.h:62
const T & at(fl::size pos) const FL_NOEXCEPT
Definition array.h:51
fl::size size_type
Definition array.h:31
value_type * pointer
Definition array.h:35
void swap(array &other) FL_NOEXCEPT
Definition array.h:101
const_iterator cbegin() const FL_NOEXCEPT
Definition array.h:79
T & back() FL_NOEXCEPT
Definition array.h:66
static T & error_value() FL_NOEXCEPT
Definition array.h:108
fl::size size() const FL_NOEXCEPT
Definition array.h:90
pointer iterator
Definition array.h:37
iterator begin() FL_NOEXCEPT
Definition array.h:75
pointer data() FL_NOEXCEPT
Definition array.h:70
T & at(fl::size pos) FL_NOEXCEPT
Definition array.h:44
const T & front() const FL_NOEXCEPT
Definition array.h:64
A fixed-size array implementation similar to std::array.
Definition array.h:27
void swap(T &a, T &b) FL_NOEXCEPT
Definition s16x16x4.h:877
constexpr int type_rank< T >::value
array< T, N > to_array(fl::span< const T, fl::size(-1)> s) FL_NOEXCEPT
Definition array.h:217
FASTLED_FORCE_INLINE bool operator!=(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if two CRGB objects do not have the same color data.
Definition crgb.h:739
void swap(array< T, N > &lhs, array< T, N > &rhs) FL_NOEXCEPT
Definition array.h:209
FASTLED_FORCE_INLINE bool operator<(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if the sum of the color channels in one CRGB object is less than another.
Definition crgb.h:745
FASTLED_FORCE_INLINE bool operator==(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if two CRGB objects have the same color data.
Definition crgb.h:733
FASTLED_FORCE_INLINE bool operator>(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if the sum of the color channels in one CRGB object is greater than another.
Definition crgb.h:754
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
FASTLED_FORCE_INLINE bool operator<=(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if the sum of the color channels in one CRGB object is less than or equal to another.
Definition crgb.h:772
fl::ptrdiff ptrdiff_t
Definition s16x16x4.h:226
FASTLED_FORCE_INLINE bool operator>=(const CRGB &lhs, const CRGB &rhs) FL_NOEXCEPT
Check if the sum of the color channels in one CRGB object is greater than or equal to another.
Definition crgb.h:763
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT