libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
set.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <set>
11#include <initializer_list>
14#include <promeki/core/list.h>
15
17
36template <typename T>
37class Set {
39 public:
42
44 using Data = std::set<T>;
45
47 using Iterator = typename Data::iterator;
48
50 using ConstIterator = typename Data::const_iterator;
51
53 using RevIterator = typename Data::reverse_iterator;
54
56 using ConstRevIterator = typename Data::const_reverse_iterator;
57
59 Set() = default;
60
62 Set(const Set &other) : d(other.d) {}
63
65 Set(Set &&other) noexcept : d(std::move(other.d)) {}
66
71 Set(std::initializer_list<T> initList) : d(initList) {}
72
74 ~Set() = default;
75
78 d = other.d;
79 return *this;
80 }
81
83 Set &operator=(Set &&other) noexcept {
84 d = std::move(other.d);
85 return *this;
86 }
87
88 // -- Iterators --
89
91 Iterator begin() noexcept { return d.begin(); }
92
94 ConstIterator begin() const noexcept { return d.cbegin(); }
95
97 ConstIterator cbegin() const noexcept { return d.cbegin(); }
98
100 ConstIterator constBegin() const noexcept { return d.cbegin(); }
101
103 Iterator end() noexcept { return d.end(); }
104
106 ConstIterator end() const noexcept { return d.cend(); }
107
109 ConstIterator cend() const noexcept { return d.cend(); }
110
112 ConstIterator constEnd() const noexcept { return d.cend(); }
113
115 RevIterator rbegin() noexcept { return d.rbegin(); }
116
118 RevIterator revBegin() noexcept { return d.rbegin(); }
119
121 ConstRevIterator crbegin() const noexcept { return d.crbegin(); }
122
125
127 RevIterator rend() noexcept { return d.rend(); }
128
130 RevIterator revEnd() noexcept { return d.rend(); }
131
133 ConstRevIterator crend() const noexcept { return d.crend(); }
134
137
138 // -- Capacity --
139
141 bool isEmpty() const noexcept { return d.empty(); }
142
144 size_t size() const noexcept { return d.size(); }
145
146 // -- Lookup --
147
149 bool contains(const T &value) const { return d.find(value) != d.end(); }
150
156 Iterator find(const T &value) { return d.find(value); }
157
159 ConstIterator find(const T &value) const { return d.find(value); }
160
166 Iterator lowerBound(const T &value) { return d.lower_bound(value); }
167
169 ConstIterator lowerBound(const T &value) const { return d.lower_bound(value); }
170
176 Iterator upperBound(const T &value) { return d.upper_bound(value); }
177
179 ConstIterator upperBound(const T &value) const { return d.upper_bound(value); }
180
181 // -- Modifiers --
182
189 std::pair<Iterator, bool> insert(const T &value) {
190 return d.insert(value);
191 }
192
198 std::pair<Iterator, bool> insert(T &&value) {
199 return d.insert(std::move(value));
200 }
201
207 bool remove(const T &value) {
208 return d.erase(value) > 0;
209 }
210
217 return d.erase(pos);
218 }
219
222 d.clear();
223 return;
224 }
225
230 void swap(Set &other) noexcept {
231 d.swap(other.d);
232 return;
233 }
234
235 // -- Set Operations --
236
242 Set unite(const Set &other) const {
243 Set ret = *this;
244 for(const auto &v : other.d) ret.d.insert(v);
245 return ret;
246 }
247
253 Set intersect(const Set &other) const {
254 Set ret;
255 for(const auto &v : d) {
256 if(other.contains(v)) ret.d.insert(v);
257 }
258 return ret;
259 }
260
266 Set subtract(const Set &other) const {
267 Set ret;
268 for(const auto &v : d) {
269 if(!other.contains(v)) ret.d.insert(v);
270 }
271 return ret;
272 }
273
274 // -- Convenience --
275
280 List<T> toList() const {
281 List<T> ret;
282 ret.reserve(d.size());
283 for(const auto &v : d) ret.pushToBack(v);
284 return ret;
285 }
286
292 template <typename Func>
293 void forEach(Func &&func) const {
294 for(const auto &v : d) func(v);
295 return;
296 }
297
298 // -- Comparison --
299
301 friend bool operator==(const Set &lhs, const Set &rhs) { return lhs.d == rhs.d; }
302
304 friend bool operator!=(const Set &lhs, const Set &rhs) { return lhs.d != rhs.d; }
305
306 private:
307 Data d;
308};
309
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
Ordered unique-element container wrapping std::set.
Definition set.h:37
friend bool operator!=(const Set &lhs, const Set &rhs)
Returns true if the sets differ.
Definition set.h:304
ConstIterator end() const noexcept
Returns a const iterator to one past the last element.
Definition set.h:106
bool contains(const T &value) const
Returns true if value exists in the set.
Definition set.h:149
ConstRevIterator constRevEnd() const noexcept
Returns a const reverse iterator to one before the first element.
Definition set.h:136
std::pair< Iterator, bool > insert(T &&value)
Inserts a value into the set (move overload).
Definition set.h:198
ConstRevIterator crbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition set.h:121
Set intersect(const Set &other) const
Returns the intersection of this set and other.
Definition set.h:253
friend bool operator==(const Set &lhs, const Set &rhs)
Returns true if both sets have identical contents.
Definition set.h:301
bool remove(const T &value)
Removes value from the set.
Definition set.h:207
Iterator remove(Iterator pos)
Removes the element at pos.
Definition set.h:216
RevIterator rend() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition set.h:127
Set unite(const Set &other) const
Returns the union of this set and other.
Definition set.h:242
Iterator find(const T &value)
Finds value in the set.
Definition set.h:156
std::set< T > Data
Underlying std::set storage type.
Definition set.h:44
typename Data::const_iterator ConstIterator
Const forward iterator.
Definition set.h:50
typename Data::iterator Iterator
Mutable forward iterator.
Definition set.h:47
~Set()=default
Destructor.
ConstRevIterator crend() const noexcept
Returns a const reverse iterator to one before the first element.
Definition set.h:133
Set & operator=(const Set &other)
Copy assignment operator.
Definition set.h:77
RevIterator revEnd() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition set.h:130
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last element.
Definition set.h:112
Set(Set &&other) noexcept
Move constructor.
Definition set.h:65
ConstIterator find(const T &value) const
Const overload of find().
Definition set.h:159
ConstRevIterator constRevBegin() const noexcept
Returns a const reverse iterator to the last element.
Definition set.h:124
bool isEmpty() const noexcept
Returns true if the set has no elements.
Definition set.h:141
List< T > toList() const
Converts the set to a List.
Definition set.h:280
ConstIterator lowerBound(const T &value) const
Const overload of lowerBound().
Definition set.h:169
void clear() noexcept
Removes all elements.
Definition set.h:221
std::pair< Iterator, bool > insert(const T &value)
Inserts a value into the set.
Definition set.h:189
ConstIterator begin() const noexcept
Returns a const iterator to the first element.
Definition set.h:94
ConstIterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition set.h:97
void swap(Set &other) noexcept
Swaps contents with another set.
Definition set.h:230
RevIterator rbegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition set.h:115
Set subtract(const Set &other) const
Returns this set minus other.
Definition set.h:266
ConstIterator constBegin() const noexcept
Returns a const iterator to the first element.
Definition set.h:100
Set & operator=(Set &&other) noexcept
Move assignment operator.
Definition set.h:83
ConstIterator upperBound(const T &value) const
Const overload of upperBound().
Definition set.h:179
Set(const Set &other)
Copy constructor.
Definition set.h:62
size_t size() const noexcept
Returns the number of elements.
Definition set.h:144
ConstIterator cend() const noexcept
Returns a const iterator to one past the last element.
Definition set.h:109
typename Data::reverse_iterator RevIterator
Mutable reverse iterator.
Definition set.h:53
Iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition set.h:91
RevIterator revBegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition set.h:118
Set(std::initializer_list< T > initList)
Constructs a set from an initializer list.
Definition set.h:71
Set()=default
Default constructor. Creates an empty set.
Iterator end() noexcept
Returns a mutable iterator to one past the last element.
Definition set.h:103
typename Data::const_reverse_iterator ConstRevIterator
Const reverse iterator.
Definition set.h:56
Iterator upperBound(const T &value)
Returns an iterator to the first element greater than value.
Definition set.h:176
Iterator lowerBound(const T &value)
Returns an iterator to the first element not less than value.
Definition set.h:166
void forEach(Func &&func) const
Calls func for every element.
Definition set.h:293
#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