libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
hashset.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <unordered_set>
11#include <initializer_list>
14#include <promeki/core/list.h>
15
17
27template <typename T>
28class HashSet {
30 public:
33
35 using Data = std::unordered_set<T>;
36
38 using Value = T;
39
41 using Iterator = typename Data::iterator;
42
44 using ConstIterator = typename Data::const_iterator;
45
47 HashSet() = default;
48
50 HashSet(const HashSet &other) : d(other.d) {}
51
53 HashSet(HashSet &&other) noexcept : d(std::move(other.d)) {}
54
59 HashSet(std::initializer_list<T> initList) : d(initList) {}
60
62 ~HashSet() = default;
63
66 d = other.d;
67 return *this;
68 }
69
72 d = std::move(other.d);
73 return *this;
74 }
75
76 // -- Iterators --
77
79 Iterator begin() noexcept { return d.begin(); }
80
82 ConstIterator begin() const noexcept { return d.cbegin(); }
83
85 ConstIterator cbegin() const noexcept { return d.cbegin(); }
86
88 ConstIterator constBegin() const noexcept { return d.cbegin(); }
89
91 Iterator end() noexcept { return d.end(); }
92
94 ConstIterator end() const noexcept { return d.cend(); }
95
97 ConstIterator cend() const noexcept { return d.cend(); }
98
100 ConstIterator constEnd() const noexcept { return d.cend(); }
101
102 // -- Capacity --
103
105 bool isEmpty() const noexcept { return d.empty(); }
106
108 size_t size() const noexcept { return d.size(); }
109
110 // -- Lookup --
111
113 bool contains(const T &value) const { return d.find(value) != d.end(); }
114
115 // -- Modifiers --
116
122 bool insert(const T &value) {
123 return d.insert(value).second;
124 }
125
131 bool insert(T &&value) {
132 return d.insert(std::move(value)).second;
133 }
134
140 bool remove(const T &value) {
141 return d.erase(value) > 0;
142 }
143
150 return d.erase(pos);
151 }
152
155 d.clear();
156 return;
157 }
158
163 void swap(HashSet &other) noexcept {
164 d.swap(other.d);
165 return;
166 }
167
168 // -- Set Operations --
169
175 HashSet unite(const HashSet &other) const {
176 HashSet ret = *this;
177 for(const auto &v : other.d) ret.d.insert(v);
178 return ret;
179 }
180
187 HashSet ret;
188 for(const auto &v : d) {
189 if(other.contains(v)) ret.d.insert(v);
190 }
191 return ret;
192 }
193
200 HashSet ret;
201 for(const auto &v : d) {
202 if(!other.contains(v)) ret.d.insert(v);
203 }
204 return ret;
205 }
206
207 // -- Convenience --
208
213 List<T> toList() const {
214 List<T> ret;
215 ret.reserve(d.size());
216 for(const auto &v : d) ret.pushToBack(v);
217 return ret;
218 }
219
225 template <typename Func>
226 void forEach(Func &&func) const {
227 for(const auto &v : d) func(v);
228 return;
229 }
230
231 // -- Comparison --
232
234 friend bool operator==(const HashSet &lhs, const HashSet &rhs) { return lhs.d == rhs.d; }
235
237 friend bool operator!=(const HashSet &lhs, const HashSet &rhs) { return lhs.d != rhs.d; }
238
239 private:
240 Data d;
241};
242
Unordered unique-element container wrapping std::unordered_set.
Definition hashset.h:28
HashSet & operator=(HashSet &&other) noexcept
Move assignment operator.
Definition hashset.h:71
Iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition hashset.h:79
HashSet()=default
Default constructor. Creates an empty hash set.
HashSet & operator=(const HashSet &other)
Copy assignment operator.
Definition hashset.h:65
void swap(HashSet &other) noexcept
Swaps contents with another hash set.
Definition hashset.h:163
ConstIterator cend() const noexcept
Returns a const iterator to one past the last element.
Definition hashset.h:97
HashSet intersect(const HashSet &other) const
Returns the intersection of this set and other.
Definition hashset.h:186
size_t size() const noexcept
Returns the number of elements.
Definition hashset.h:108
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last element.
Definition hashset.h:100
bool remove(const T &value)
Removes value from the hash set.
Definition hashset.h:140
List< T > toList() const
Converts the hash set to a List.
Definition hashset.h:213
ConstIterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition hashset.h:85
HashSet unite(const HashSet &other) const
Returns the union of this set and other.
Definition hashset.h:175
HashSet(const HashSet &other)
Copy constructor.
Definition hashset.h:50
typename Data::iterator Iterator
Mutable forward iterator.
Definition hashset.h:41
ConstIterator constBegin() const noexcept
Returns a const iterator to the first element.
Definition hashset.h:88
std::unordered_set< T > Data
Underlying std::unordered_set storage type.
Definition hashset.h:35
bool insert(const T &value)
Inserts a value into the hash set.
Definition hashset.h:122
void forEach(Func &&func) const
Calls func for every element.
Definition hashset.h:226
friend bool operator==(const HashSet &lhs, const HashSet &rhs)
Returns true if both hash sets have identical contents.
Definition hashset.h:234
void clear() noexcept
Removes all elements.
Definition hashset.h:154
~HashSet()=default
Destructor.
bool isEmpty() const noexcept
Returns true if the hash set has no elements.
Definition hashset.h:105
HashSet(std::initializer_list< T > initList)
Constructs a hash set from an initializer list.
Definition hashset.h:59
HashSet(HashSet &&other) noexcept
Move constructor.
Definition hashset.h:53
Iterator end() noexcept
Returns a mutable iterator to one past the last element.
Definition hashset.h:91
Iterator remove(Iterator pos)
Removes the element at pos.
Definition hashset.h:149
ConstIterator begin() const noexcept
Returns a const iterator to the first element.
Definition hashset.h:82
bool insert(T &&value)
Inserts a value into the hash set (move overload).
Definition hashset.h:131
bool contains(const T &value) const
Returns true if value exists in the hash set.
Definition hashset.h:113
typename Data::const_iterator ConstIterator
Const forward iterator.
Definition hashset.h:44
friend bool operator!=(const HashSet &lhs, const HashSet &rhs)
Returns true if the hash sets differ.
Definition hashset.h:237
HashSet subtract(const HashSet &other) const
Returns this set minus other.
Definition hashset.h:199
ConstIterator end() const noexcept
Returns a const iterator to one past the last element.
Definition hashset.h:94
Dynamic array container wrapping std::vector.
Definition list.h:40
bool contains(const T &val) const
Returns true if the list contains the given value.
Definition list.h:593
void reserve(size_t newCapacity)
Pre-allocates storage for at least newCapacity elements.
Definition list.h:314
void pushToBack(const T &value)
Pushes an item onto the back of the list.
Definition list.h:455
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88