FastLED 3.9.15
Loading...
Searching...
No Matches
unordered_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/hash_map.h"
9#include "fl/allocator.h"
10
11namespace fl {
12
13
14template <typename Key>
16 public:
17 // Iterator support - wrap the underlying map's iterators
18 class iterator {
19 private:
20 using map_iterator = typename fl::unordered_map<Key, bool>::iterator;
22
23 public:
24 using value_type = Key;
25 using reference = const Key&;
26 using pointer = const Key*;
27
28 iterator() = default;
29 explicit iterator(map_iterator it) : it_(it) {}
30
31 reference operator*() const { return it_->first; }
32 pointer operator->() const { return &(it_->first); }
33
34 iterator& operator++() { ++it_; return *this; }
35 iterator operator++(int) { iterator tmp = *this; ++it_; return tmp; }
36
37 bool operator==(const iterator& other) const { return it_ == other.it_; }
38 bool operator!=(const iterator& other) const { return it_ != other.it_; }
39
40 friend class unordered_set;
41 };
42
44 private:
45 using map_const_iterator = typename fl::unordered_map<Key, bool>::const_iterator;
47
48 public:
49 using value_type = Key;
50 using reference = const Key&;
51 using pointer = const Key*;
52
53 const_iterator() = default;
55 const_iterator(const iterator& other) : it_(static_cast<map_const_iterator>(other.it_)) {}
56
57 reference operator*() const { return it_->first; }
58 pointer operator->() const { return &(it_->first); }
59
60 const_iterator& operator++() { ++it_; return *this; }
61 const_iterator operator++(int) { const_iterator tmp = *this; ++it_; return tmp; }
62
63 bool operator==(const const_iterator& other) const { return it_ == other.it_; }
64 bool operator!=(const const_iterator& other) const { return it_ != other.it_; }
65 };
66
67 unordered_set() = default;
68 ~unordered_set() = default;
69
70 // Iterator methods
71 iterator begin() { return iterator(data.begin()); }
72 iterator end() { return iterator(data.end()); }
73 const_iterator begin() const { return const_iterator(data.begin()); }
74 const_iterator end() const { return const_iterator(data.end()); }
75 const_iterator cbegin() const { return const_iterator(data.cbegin()); }
76 const_iterator cend() const { return const_iterator(data.cend()); }
77
78 // Insert operations
79 bool insert(const Key &key) {
80 return data.insert(key, true);
81 }
82
83 // Move version of insert
84 bool insert(Key &&key) {
85 return data.insert(fl::move(key), true);
86 }
87
88 // Emplace - construct in place
89 template<typename... Args>
90 bool emplace(Args&&... args) {
91 Key key(fl::forward<Args>(args)...);
92 return insert(fl::move(key));
93 }
94
95 // Find operations
96 iterator find(const Key &key) {
97 auto map_it = data.find(key);
98 return iterator(map_it);
99 }
100
101 const_iterator find(const Key &key) const {
102 auto map_it = data.find(key);
103 return const_iterator(map_it);
104 }
105
106 // Count - returns 0 or 1 for sets
107 fl::size count(const Key &key) const {
108 return has(key) ? 1 : 0;
109 }
110
111 // Erase operations
112 bool erase(const Key &key) {
113 return data.erase(key);
114 }
115
117 // Extract the key from the iterator before erasing
118 Key key = *pos;
119 data.erase(key);
120 // Return iterator to next element
121 return ++pos;
122 }
123
125 // Extract the key from the const_iterator before erasing
126 Key key = *pos;
127 data.erase(key);
128 // Find the next element after erasing
129 return find(key);
130 }
131
132 // Capacity operations
133 fl::size size() const { return data.size(); }
134 fl::size capacity() const { return data.capacity(); }
135 bool empty() const { return data.empty(); }
136
137 // Modifiers
138 void clear() { data.clear(); }
139
140 // Lookup operations
141 bool has(const Key &key) const { return data.has(key); }
142 bool contains(const Key &key) const { return has(key); } // C++20 style
143
144 private:
146};
147
148} // namespace fl
uint8_t pos
Definition Blur.ino:11
const_iterator(map_const_iterator it)
bool operator!=(const const_iterator &other) const
bool operator==(const const_iterator &other) const
typename fl::unordered_map< Key, bool >::const_iterator map_const_iterator
const_iterator(const iterator &other)
typename fl::unordered_map< Key, bool >::iterator map_iterator
bool operator!=(const iterator &other) const
bool operator==(const iterator &other) const
reference operator*() const
bool empty() const
const_iterator cbegin() const
iterator erase(const_iterator pos)
~unordered_set()=default
bool insert(Key &&key)
const_iterator end() const
iterator find(const Key &key)
fl::size size() const
fl::size count(const Key &key) const
const_iterator find(const Key &key) const
const_iterator begin() const
const_iterator cend() const
unordered_set()=default
bool insert(const Key &key)
fl::size capacity() const
iterator erase(iterator pos)
fl::unordered_map< Key, bool > data
bool erase(const Key &key)
bool contains(const Key &key) const
bool emplace(Args &&... args)
bool has(const Key &key) const
Implements the FastLED namespace macros.
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
HashMap< Key, T, Hash, KeyEqual > unordered_map
Definition hash_map.h:720
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