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 <stdint.h>
5
6#include "fl/namespace.h"
7#include "fl/vector.h"
8
9namespace fl {
10
11// A simple unordered set implementation with a fixed size.
12// The user is responsible for making sure that the inserts
13// do not exceed the capacity of the set, otherwise they will
14// fail. Because of this limitation, this set is not a drop in
15// replacement for std::set.
16template <typename Key, size_t N> class FixedSet {
17 public:
21
22 // Constructor
23 constexpr FixedSet() = default;
24
25 iterator begin() { return data.begin(); }
26 iterator end() { return data.end(); }
27 const_iterator begin() const { return data.begin(); }
28 const_iterator end() const { return data.end(); }
29
30 iterator find(const Key &key) {
31 for (auto it = begin(); it != end(); ++it) {
32 if (*it == key) {
33 return it;
34 }
35 }
36 return end();
37 }
38
39 const_iterator find(const Key &key) const {
40 for (auto it = begin(); it != end(); ++it) {
41 if (*it == key) {
42 return it;
43 }
44 }
45 return end();
46 }
47
48 bool insert(const Key &key) {
49 if (data.size() < N) {
50 auto it = find(key);
51 if (it == end()) {
52 data.push_back(key);
53 return true;
54 }
55 }
56 return false;
57 }
58
59 bool erase(const Key &key) {
60 auto it = find(key);
61 if (it != end()) {
62 data.erase(it);
63 return true;
64 }
65 return false;
66 }
67
69 if (pos != end()) {
70 data.erase(pos);
71 return true;
72 }
73 return false;
74 }
75
76 bool next(const Key &key, Key *next_key,
77 bool allow_rollover = false) const {
78 const_iterator it = find(key);
79 if (it != end()) {
80 ++it;
81 if (it != end()) {
82 *next_key = *it;
83 return true;
84 } else if (allow_rollover && !empty()) {
85 *next_key = *begin();
86 return true;
87 }
88 }
89 return false;
90 }
91
92 bool prev(const Key &key, Key *prev_key,
93 bool allow_rollover = false) const {
94 const_iterator it = find(key);
95 if (it != end()) {
96 if (it != begin()) {
97 --it;
98 *prev_key = *it;
99 return true;
100 } else if (allow_rollover && !empty()) {
101 *prev_key = data[data.size() - 1];
102 return true;
103 }
104 }
105 return false;
106 }
107
108 // Get the current size of the set
109 constexpr size_t size() const { return data.size(); }
110
111 constexpr bool empty() const { return data.empty(); }
112
113 // Get the capacity of the set
114 constexpr size_t capacity() const { return N; }
115
116 // Clear the set
117 void clear() { data.clear(); }
118
119 bool has(const Key &key) const { return find(key) != end(); }
120
121 // Return the first element of the set
122 const Key &front() const { return data.front(); }
123
124 // Return the last element of the set
125 const Key &back() const { return data.back(); }
126
127 private:
129};
130
131} // namespace fl
uint8_t pos
Definition Blur.ino:11
iterator find(const Key &key)
Definition set.h:30
constexpr size_t size() const
Definition set.h:109
bool next(const Key &key, Key *next_key, bool allow_rollover=false) const
Definition set.h:76
bool has(const Key &key) const
Definition set.h:119
VectorType data
Definition set.h:128
const_iterator find(const Key &key) const
Definition set.h:39
bool insert(const Key &key)
Definition set.h:48
const_iterator end() const
Definition set.h:28
constexpr FixedSet()=default
iterator end()
Definition set.h:26
bool erase(iterator pos)
Definition set.h:68
iterator begin()
Definition set.h:25
const Key & front() const
Definition set.h:122
bool erase(const Key &key)
Definition set.h:59
bool prev(const Key &key, Key *prev_key, bool allow_rollover=false) const
Definition set.h:92
FixedVector< Key, N > VectorType
Definition set.h:18
void clear()
Definition set.h:117
const_iterator begin() const
Definition set.h:27
VectorType::const_iterator const_iterator
Definition set.h:20
constexpr size_t capacity() const
Definition set.h:114
const Key & back() const
Definition set.h:125
constexpr bool empty() const
Definition set.h:111
VectorType::iterator iterator
Definition set.h:19
const Key * const_iterator
Definition vector.h:80
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Definition Keyboard.h:22