FastLED 3.9.15
Loading...
Searching...
No Matches
unordered_set.h
Go to the documentation of this file.
1#pragma once
2
3
5#include "fl/stl/hash.h"
6#include "fl/stl/move.h"
8#include "fl/stl/int.h"
10#include "fl/stl/noexcept.h"
11
12namespace fl {
13
14
15template <typename Key, typename Hash = Hash<Key>,
16 typename KeyEqual = EqualTo<Key>>
18 public:
19 // Iterator support - wrap the underlying map's iterators
20 class iterator {
21 private:
24
25 public:
26 using value_type = Key;
27 using reference = const Key&;
28 using pointer = const Key*;
30
31 iterator() FL_NOEXCEPT = default;
32 explicit iterator(map_iterator it) : mIt(it) {}
33
34 reference operator*() const { return mIt->first; }
35 pointer operator->() const { return &(mIt->first); }
36
37 iterator& operator++() { ++mIt; return *this; }
38 iterator operator++(int) { iterator tmp = *this; ++mIt; return tmp; }
39
40 bool operator==(const iterator& other) const { return mIt == other.mIt; }
41 bool operator!=(const iterator& other) const { return mIt != other.mIt; }
42
43 friend class unordered_set;
44 };
45
47 private:
50
51 public:
52 using value_type = Key;
53 using reference = const Key&;
54 using pointer = const Key*;
56
59 const_iterator(const iterator& other) : mIt(static_cast<map_const_iterator>(other.mIt)) {}
60
61 reference operator*() const { return mIt->first; }
62 pointer operator->() const { return &(mIt->first); }
63
64 const_iterator& operator++() { ++mIt; return *this; }
65 const_iterator operator++(int) { const_iterator tmp = *this; ++mIt; return tmp; }
66
67 bool operator==(const const_iterator& other) const { return mIt == other.mIt; }
68 bool operator!=(const const_iterator& other) const { return mIt != other.mIt; }
69 };
70
72 explicit unordered_set(memory_resource* resource) : data(resource) {}
73 unordered_set(const unordered_set& other) FL_NOEXCEPT = default;
78
79 // Iterator methods
80 iterator begin() { return iterator(data.begin()); }
81 iterator end() { return iterator(data.end()); }
82 const_iterator begin() const { return const_iterator(data.begin()); }
83 const_iterator end() const { return const_iterator(data.end()); }
84 const_iterator cbegin() const { return const_iterator(data.cbegin()); }
85 const_iterator cend() const { return const_iterator(data.cend()); }
86
87 // Insert operations
88 bool insert(const Key &key) {
89 return data.insert(key, true).second;
90 }
91
92 // Move version of insert
93 bool insert(Key &&key) {
94 return data.insert(fl::move(key), true).second;
95 }
96
97 // Emplace - construct in place
98 template<typename... Args>
99 bool emplace(Args&&... args) {
100 Key key(fl::forward<Args>(args)...);
101 return insert(fl::move(key));
102 }
103
104 // Find operations
105 iterator find(const Key &key) {
106 auto map_it = data.find(key);
107 return iterator(map_it);
108 }
109
110 const_iterator find(const Key &key) const {
111 auto map_it = data.find(key);
112 return const_iterator(map_it);
113 }
114
115 // Count - returns 0 or 1 for sets
116 fl::size count(const Key &key) const {
117 return has(key) ? 1 : 0;
118 }
119
120 // Erase operations
121 bool erase(const Key &key) {
122 return data.erase(key);
123 }
124
126 // Extract the key from the iterator before erasing
127 Key key = *pos;
128 data.erase(key);
129 // Return iterator to next element
130 return ++pos;
131 }
132
134 // Extract the key from the const_iterator before erasing
135 Key key = *pos;
136 data.erase(key);
137 // Find the next element after erasing
138 return find(key);
139 }
140
141 // Capacity operations
142 fl::size size() const { return data.size(); }
143 fl::size capacity() const { return data.capacity(); }
144 bool empty() const { return data.empty(); }
145
146 // Modifiers
147 void clear() { data.clear(); }
148
149 // Lookup operations
150 bool has(const Key &key) const { return data.contains(key); }
151 bool contains(const Key &key) const { return has(key); } // C++20 style
152
154 bool operator==(const unordered_set& other) const {
155 if (size() != other.size()) return false;
156 for (const auto& key : *this) {
157 if (!other.has(key)) return false;
158 }
159 return true;
160 }
161
163 bool operator!=(const unordered_set& other) const {
164 return !(*this == other);
165 }
166
167 memory_resource* get_memory_resource() const { return data.get_memory_resource(); }
168
169 private:
171};
172
173} // namespace fl
uint8_t pos
Definition Blur.ino:11
Polymorphic memory resource base class (PMR-style).
bool operator!=(const const_iterator &other) const
typename fl::unordered_map< Key, bool, Hash, KeyEqual >::const_iterator map_const_iterator
fl::forward_iterator_tag iterator_category
const_iterator(const iterator &other)
bool operator==(const const_iterator &other) const
const_iterator() FL_NOEXCEPT=default
reference operator*() const
typename fl::unordered_map< Key, bool, Hash, KeyEqual >::iterator map_iterator
bool operator==(const iterator &other) const
iterator() FL_NOEXCEPT=default
fl::forward_iterator_tag iterator_category
bool operator!=(const iterator &other) const
bool contains(const Key &key) const
fl::unordered_map< Key, bool, Hash, KeyEqual > data
const_iterator cbegin() const
const_iterator find(const Key &key) const
unordered_set(const unordered_set &other) FL_NOEXCEPT=default
unordered_set & operator=(unordered_set &&other) FL_NOEXCEPT=default
~unordered_set() FL_NOEXCEPT=default
bool emplace(Args &&... args)
bool has(const Key &key) const
bool operator==(const unordered_set &other) const
Equality comparison (set equality: same elements, order-independent)
bool operator!=(const unordered_set &other) const
Inequality comparison.
fl::size size() const
bool insert(Key &&key)
memory_resource * get_memory_resource() const
iterator find(const Key &key)
bool empty() const
fl::size count(const Key &key) const
unordered_set & operator=(const unordered_set &other) FL_NOEXCEPT=default
unordered_set() FL_NOEXCEPT=default
const_iterator end() const
iterator erase(const_iterator pos)
const_iterator begin() const
fl::size capacity() const
unordered_set(unordered_set &&other) FL_NOEXCEPT=default
bool erase(const Key &key)
bool insert(const Key &key)
const_iterator cend() const
iterator erase(iterator pos)
PMR-style polymorphic memory resource for type-erased allocation.
constexpr T && forward(typename remove_reference< T >::type &t) FL_NOEXCEPT
Definition s16x16x4.h:234
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FL_NOEXCEPT
Definition Keyboard.h:22