FastLED 3.9.15
Loading...
Searching...
No Matches
set.h
Go to the documentation of this file.
1#pragma once
2
3//#include <stddef.h>
4#include "fl/stdint.h"
5
6#include "fl/namespace.h"
7#include "fl/vector.h"
8#include "fl/map.h"
9#include "fl/rbtree.h"
10#include "fl/allocator.h"
11#include "fl/pair.h"
12
13
14namespace fl {
15
16template <typename Key, typename Allocator> class set;
17
18// VectorSet stores values in order of insertion.
19template <typename Key, size N> class VectorSetFixed;
20template <typename Key, typename Allocator> class VectorSet;
21
22template <typename Key, size N>
23using FixedSet = VectorSetFixed<Key, N>; // Backwards compatibility
24
25// A simple unordered set implementation with a fixed size.
26// The user is responsible for making sure that the inserts
27// do not exceed the capacity of the set, otherwise they will
28// fail. Because of this limitation, this set is not a drop in
29// replacement for std::set.
30template <typename Key, size N> class VectorSetFixed {
31 public:
35
36 // Constructor
37 constexpr VectorSetFixed() = default;
38
39 iterator begin() { return data.begin(); }
40 iterator end() { return data.end(); }
41 const_iterator begin() const { return data.begin(); }
42 const_iterator end() const { return data.end(); }
43
44 iterator find(const Key &key) {
45 for (auto it = begin(); it != end(); ++it) {
46 if (*it == key) {
47 return it;
48 }
49 }
50 return end();
51 }
52
53 const_iterator find(const Key &key) const {
54 for (auto it = begin(); it != end(); ++it) {
55 if (*it == key) {
56 return it;
57 }
58 }
59 return end();
60 }
61
62 bool insert(const Key &key) {
63 if (data.size() < N) {
64 auto it = find(key);
65 if (it == end()) {
66 data.push_back(key);
67 return true;
68 }
69 }
70 return false;
71 }
72
73 // Move version of insert
74 bool insert(Key &&key) {
75 if (data.size() < N) {
76 auto it = find(key);
77 if (it == end()) {
78 data.push_back(fl::move(key));
79 return true;
80 }
81 }
82 return false;
83 }
84
85 // Emplace - construct in place with perfect forwarding
86 template<typename... Args>
87 bool emplace(Args&&... args) {
88 if (data.size() < N) {
89 // Create a temporary to check if it already exists
90 Key temp_key(fl::forward<Args>(args)...);
91 auto it = find(temp_key);
92 if (it == end()) {
93 data.push_back(fl::move(temp_key));
94 return true;
95 }
96 }
97 return false;
98 }
99
100 bool erase(const Key &key) {
101 auto it = find(key);
102 if (it != end()) {
103 data.erase(it);
104 return true;
105 }
106 return false;
107 }
108
110 if (pos != end()) {
111 data.erase(pos);
112 return true;
113 }
114 return false;
115 }
116
117 bool next(const Key &key, Key *next_key,
118 bool allow_rollover = false) const {
119 const_iterator it = find(key);
120 if (it != end()) {
121 ++it;
122 if (it != end()) {
123 *next_key = *it;
124 return true;
125 } else if (allow_rollover && !empty()) {
126 *next_key = *begin();
127 return true;
128 }
129 }
130 return false;
131 }
132
133 bool prev(const Key &key, Key *prev_key,
134 bool allow_rollover = false) const {
135 const_iterator it = find(key);
136 if (it != end()) {
137 if (it != begin()) {
138 --it;
139 *prev_key = *it;
140 return true;
141 } else if (allow_rollover && !empty()) {
142 *prev_key = data[data.size() - 1];
143 return true;
144 }
145 }
146 return false;
147 }
148
149 // Get the current size of the set
150 constexpr fl::size size() const { return data.size(); }
151
152 constexpr bool empty() const { return data.empty(); }
153
154 // Get the capacity of the set
155 constexpr fl::size capacity() const { return N; }
156
157 // Clear the set
158 void clear() { data.clear(); }
159
160 bool has(const Key &key) const { return find(key) != end(); }
161
162 // Return the first element of the set
163 const Key &front() const { return data.front(); }
164
165 // Return the last element of the set
166 const Key &back() const { return data.back(); }
167
168 private:
170};
171
172template <typename Key, typename Allocator = fl::allocator<Key>> class VectorSet {
173 public:
177
178 // Constructor
179 constexpr VectorSet() = default;
180
181 iterator begin() { return data.begin(); }
182 iterator end() { return data.end(); }
183 const_iterator begin() const { return data.begin(); }
184 const_iterator end() const { return data.end(); }
185
186 iterator find(const Key &key) {
187 for (auto it = begin(); it != end(); ++it) {
188 if (*it == key) {
189 return it;
190 }
191 }
192 return end();
193 }
194
195 const_iterator find(const Key &key) const {
196 for (auto it = begin(); it != end(); ++it) {
197 if (*it == key) {
198 return it;
199 }
200 }
201 return end();
202 }
203
204 bool insert(const Key &key) {
205 auto it = find(key);
206 if (it == end()) {
207 data.push_back(key);
208 return true;
209 }
210 return false;
211 }
212
213 // Move version of insert
214 bool insert(Key &&key) {
215 auto it = find(key);
216 if (it == end()) {
217 data.push_back(fl::move(key));
218 return true;
219 }
220 return false;
221 }
222
223 // Emplace - construct in place with perfect forwarding
224 template<typename... Args>
225 bool emplace(Args&&... args) {
226 // Create a temporary to check if it already exists
227 Key temp_key(fl::forward<Args>(args)...);
228 auto it = find(temp_key);
229 if (it == end()) {
230 data.push_back(fl::move(temp_key));
231 return true;
232 }
233 return false;
234 }
235
236 bool erase(const Key &key) {
237 auto it = find(key);
238 if (it != end()) {
239 data.erase(it);
240 return true;
241 }
242 return false;
243 }
244
246 if (pos != end()) {
247 data.erase(pos);
248 return true;
249 }
250 return false;
251 }
252
253 // Get the current size of the set
254 constexpr fl::size size() const { return data.size(); }
255
256 constexpr bool empty() const { return data.empty(); }
257
258 // Get the capacity of the set
259 constexpr fl::size capacity() const { return data.capacity(); }
260
261 // Clear the set
262 void clear() { data.clear(); }
263
264 bool has(const Key &key) const { return find(key) != end(); }
265
266 // Return the first element of the set
267 const Key &front() const { return data.front(); }
268
269 // Return the last element of the set
270 const Key &back() const { return data.back(); }
271
272 private:
274};
275
276// fl::set<T, Allocator> - Ordered set implementation using SetRedBlackTree
277// This is an ordered set that keeps elements sorted, similar to std::set
278template <typename Key, typename Allocator = fl::allocator_slab<Key>> class set {
279 private:
282
283 public:
284 // Standard set typedefs
285 using key_type = Key;
287 using size_type = fl::size;
289 using reference = const Key&;
290 using const_reference = const Key&;
291 using pointer = const Key*;
292 using const_pointer = const Key*;
293
294 // Iterator types - we only provide const iterators since set elements are immutable
296 using iterator = const_iterator; // set only provides const iterators
297
298 // Constructors
299 set() = default;
300 set(const set& other) = default;
301 set(set&& other) = default;
302 set& operator=(const set& other) = default;
303 set& operator=(set&& other) = default;
304
305 // Iterators
306 const_iterator begin() const { return tree_.begin(); }
307 const_iterator end() const { return tree_.end(); }
308 const_iterator cbegin() const { return tree_.cbegin(); }
309 const_iterator cend() const { return tree_.cend(); }
310
311 // Capacity
312 bool empty() const { return tree_.empty(); }
313 size_type size() const { return tree_.size(); }
314 size_type max_size() const { return tree_.max_size(); }
315
316 // Modifiers
317 void clear() { tree_.clear(); }
318
320 return tree_.insert(key);
321 }
322
324 return tree_.insert(fl::move(key));
325 }
326
327 template<typename... Args>
329 return tree_.emplace(fl::forward<Args>(args)...);
330 }
331
333 return tree_.erase(pos);
334 }
335
336 size_type erase(const Key& key) {
337 return tree_.erase(key);
338 }
339
340 void swap(set& other) {
341 tree_.swap(other.tree_);
342 }
343
344 // Lookup
345 size_type count(const Key& key) const {
346 return tree_.count(key);
347 }
348
349 const_iterator find(const Key& key) const {
350 return tree_.find(key);
351 }
352
353 bool contains(const Key& key) const {
354 return tree_.contains(key);
355 }
356
357 bool has(const Key& key) const {
358 return contains(key);
359 }
360
362 return tree_.equal_range(key);
363 }
364
365 const_iterator lower_bound(const Key& key) const {
366 return tree_.lower_bound(key);
367 }
368
369 const_iterator upper_bound(const Key& key) const {
370 return tree_.upper_bound(key);
371 }
372};
373
374// fl::set_inlined<T, N> - Inlined set implementation using proper allocator
375// This is a using declaration that sets the proper allocator for fl::set
376template <typename T, fl::size N>
378
379} // namespace fl
uint8_t pos
Definition Blur.ino:11
const Key * const_iterator
Definition vector.h:88
typename TreeType::const_iterator const_iterator
Definition rbtree.h:927
VectorType::iterator iterator
Definition set.h:175
constexpr VectorSet()=default
constexpr bool empty() const
Definition set.h:256
const_iterator end() const
Definition set.h:184
bool erase(iterator pos)
Definition set.h:245
constexpr fl::size size() const
Definition set.h:254
void clear()
Definition set.h:262
VectorType data
Definition set.h:273
iterator end()
Definition set.h:182
bool insert(const Key &key)
Definition set.h:204
const_iterator find(const Key &key) const
Definition set.h:195
bool has(const Key &key) const
Definition set.h:264
VectorType::const_iterator const_iterator
Definition set.h:176
iterator find(const Key &key)
Definition set.h:186
const Key & front() const
Definition set.h:267
fl::HeapVector< Key, Allocator > VectorType
Definition set.h:174
bool emplace(Args &&... args)
Definition set.h:225
const_iterator begin() const
Definition set.h:183
constexpr fl::size capacity() const
Definition set.h:259
bool insert(Key &&key)
Definition set.h:214
bool erase(const Key &key)
Definition set.h:236
iterator begin()
Definition set.h:181
const Key & back() const
Definition set.h:270
constexpr fl::size capacity() const
Definition set.h:155
bool insert(Key &&key)
Definition set.h:74
VectorType::iterator iterator
Definition set.h:33
void clear()
Definition set.h:158
iterator end()
Definition set.h:40
const_iterator find(const Key &key) const
Definition set.h:53
bool erase(const Key &key)
Definition set.h:100
constexpr fl::size size() const
Definition set.h:150
constexpr bool empty() const
Definition set.h:152
VectorType data
Definition set.h:169
VectorType::const_iterator const_iterator
Definition set.h:34
bool next(const Key &key, Key *next_key, bool allow_rollover=false) const
Definition set.h:117
iterator begin()
Definition set.h:39
FixedVector< Key, N > VectorType
Definition set.h:32
const_iterator begin() const
Definition set.h:41
bool erase(iterator pos)
Definition set.h:109
bool prev(const Key &key, Key *prev_key, bool allow_rollover=false) const
Definition set.h:133
const Key & back() const
Definition set.h:166
bool emplace(Args &&... args)
Definition set.h:87
const_iterator end() const
Definition set.h:42
bool insert(const Key &key)
Definition set.h:62
const Key & front() const
Definition set.h:163
bool has(const Key &key) const
Definition set.h:160
iterator find(const Key &key)
Definition set.h:44
constexpr VectorSetFixed()=default
void clear()
Definition set.h:317
bool empty() const
Definition set.h:312
size_type max_size() const
Definition set.h:314
ptrdiff_t difference_type
Definition set.h:288
set(const set &other)=default
void swap(set &other)
Definition set.h:340
fl::pair< const_iterator, bool > insert(Key &&key)
Definition set.h:323
const_iterator begin() const
Definition set.h:306
bool has(const Key &key) const
Definition set.h:357
size_type size() const
Definition set.h:313
fl::pair< const_iterator, bool > insert(const Key &key)
Definition set.h:319
const Key & const_reference
Definition set.h:290
const Key * pointer
Definition set.h:291
bool contains(const Key &key) const
Definition set.h:353
const_iterator cbegin() const
Definition set.h:308
fl::pair< const_iterator, bool > emplace(Args &&... args)
Definition set.h:328
typename TreeType::const_iterator const_iterator
Definition set.h:295
fl::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition set.h:361
const_iterator find(const Key &key) const
Definition set.h:349
size_type count(const Key &key) const
Definition set.h:345
const_iterator lower_bound(const Key &key) const
Definition set.h:365
set(set &&other)=default
const_iterator erase(const_iterator pos)
Definition set.h:332
const Key & reference
Definition set.h:289
set & operator=(set &&other)=default
const Key * const_pointer
Definition set.h:292
const_iterator cend() const
Definition set.h:309
const_iterator end() const
Definition set.h:307
set & operator=(const set &other)=default
Key value_type
Definition set.h:286
const_iterator upper_bound(const Key &key) const
Definition set.h:369
fl::SetRedBlackTree< Key, fl::less< Key >, Allocator > TreeType
Definition set.h:280
Key key_type
Definition set.h:285
set()=default
size_type erase(const Key &key)
Definition set.h:336
const_iterator iterator
Definition set.h:296
fl::size size_type
Definition set.h:287
Definition set.h:278
Implements the FastLED namespace macros.
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
fl::set< T, fl::allocator_inlined_slab< T, N > > set_inlined
Definition set.h:377
__PTRDIFF_TYPE__ ptrdiff_t
Definition cstddef.h:22
VectorSetFixed< Key, N > FixedSet
Definition set.h:23
constexpr T && forward(typename remove_reference< T >::type &t) noexcept
IMPORTANT!
Definition crgb.h:20
corkscrew_args args
Definition old.h:150
Definition Keyboard.h:22
Definition pair.h:9