FastLED 3.9.3
Loading...
Searching...
No Matches
fixed_set.h
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>
5
6#include "namespace.h"
7#include "fixed_vector.h"
8
9FASTLED_NAMESPACE_BEGIN
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>
17class FixedSet {
18public:
20 typedef typename VectorType::iterator iterator;
21 typedef typename VectorType::const_iterator const_iterator;
22
23 // Constructor
24 constexpr FixedSet() = default;
25
26 iterator begin() {
27 return data.begin();
28 }
29 iterator end() {
30 return data.end();
31 }
32 const_iterator begin() const {
33 return data.begin();
34 }
35 const_iterator end() const {
36 return data.end();
37 }
38
39 iterator find(const Key& key) {
40 for (auto it = begin(); it != end(); ++it) {
41 if (*it == key) {
42 return it;
43 }
44 }
45 return end();
46 }
47
48 const_iterator find(const Key& key) const {
49 for (auto it = begin(); it != end(); ++it) {
50 if (*it == key) {
51 return it;
52 }
53 }
54 return end();
55 }
56
57 bool insert(const Key& key) {
58 if (data.size() < N) {
59 auto it = find(key);
60 if (it == end()) {
61 data.push_back(key);
62 return true;
63 }
64 }
65 return false;
66 }
67
68 bool erase(const Key& key) {
69 auto it = find(key);
70 if (it != end()) {
71 data.erase(it);
72 return true;
73 }
74 return false;
75 }
76
77 bool erase(iterator pos) {
78 if (pos != end()) {
79 data.erase(pos);
80 return true;
81 }
82 return false;
83 }
84
85 bool next(const Key& key, Key* next_key, bool allow_rollover = false) const {
86 const_iterator it = find(key);
87 if (it != end()) {
88 ++it;
89 if (it != end()) {
90 *next_key = *it;
91 return true;
92 } else if (allow_rollover && !empty()) {
93 *next_key = *begin();
94 return true;
95 }
96 }
97 return false;
98 }
99
100 bool prev(const Key& key, Key* prev_key, bool allow_rollover = false) const {
101 const_iterator it = find(key);
102 if (it != end()) {
103 if (it != begin()) {
104 --it;
105 *prev_key = *it;
106 return true;
107 } else if (allow_rollover && !empty()) {
108 *prev_key = data[data.size() - 1];
109 return true;
110 }
111 }
112 return false;
113 }
114
115 // Get the current size of the set
116 constexpr size_t size() const {
117 return data.size();
118 }
119
120 constexpr bool empty() const {
121 return data.empty();
122 }
123
124 // Get the capacity of the set
125 constexpr size_t capacity() const {
126 return N;
127 }
128
129 // Clear the set
130 void clear() {
131 data.clear();
132 }
133
134 bool has(const Key& key) const {
135 return find(key) != end();
136 }
137
138 // Return the first element of the set
139 const Key& front() const {
140 return data.front();
141 }
142
143 // Return the last element of the set
144 const Key& back() const {
145 return data.back();
146 }
147
148
149private:
150 VectorType data;
151};
152
153FASTLED_NAMESPACE_END