libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
set.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <set>
14#include <initializer_list>
15#include <promeki/namespace.h>
16#include <promeki/sharedptr.h>
17#include <promeki/list.h>
18#include <promeki/pair.h>
19
20PROMEKI_NAMESPACE_BEGIN
21
45template <typename T> class Set {
46 PROMEKI_SHARED_FINAL(Set)
47 public:
49 using Ptr = SharedPtr<Set>;
50
52 using Data = std::set<T>;
53
55 using Iterator = typename Data::iterator;
56
58 using ConstIterator = typename Data::const_iterator;
59
61 using RevIterator = typename Data::reverse_iterator;
62
64 using ConstRevIterator = typename Data::const_reverse_iterator;
65
67 Set() = default;
68
70 Set(const Set &other) : d(other.d) {}
71
73 Set(Set &&other) noexcept : d(std::move(other.d)) {}
74
79 Set(std::initializer_list<T> initList) : d(initList) {}
80
82 ~Set() = default;
83
85 Set &operator=(const Set &other) {
86 d = other.d;
87 return *this;
88 }
89
91 Set &operator=(Set &&other) noexcept {
92 d = std::move(other.d);
93 return *this;
94 }
95
96 // -- Iterators --
97
99 Iterator begin() noexcept { return d.begin(); }
100
102 ConstIterator begin() const noexcept { return d.cbegin(); }
103
105 ConstIterator cbegin() const noexcept { return d.cbegin(); }
106
108 ConstIterator constBegin() const noexcept { return d.cbegin(); }
109
111 Iterator end() noexcept { return d.end(); }
112
114 ConstIterator end() const noexcept { return d.cend(); }
115
117 ConstIterator cend() const noexcept { return d.cend(); }
118
120 ConstIterator constEnd() const noexcept { return d.cend(); }
121
123 RevIterator rbegin() noexcept { return d.rbegin(); }
124
126 RevIterator revBegin() noexcept { return d.rbegin(); }
127
129 ConstRevIterator crbegin() const noexcept { return d.crbegin(); }
130
132 ConstRevIterator constRevBegin() const noexcept { return d.crbegin(); }
133
135 RevIterator rend() noexcept { return d.rend(); }
136
138 RevIterator revEnd() noexcept { return d.rend(); }
139
141 ConstRevIterator crend() const noexcept { return d.crend(); }
142
144 ConstRevIterator constRevEnd() const noexcept { return d.crend(); }
145
146 // -- Capacity --
147
149 bool isEmpty() const noexcept { return d.empty(); }
150
152 size_t size() const noexcept { return d.size(); }
153
154 // -- Lookup --
155
157 bool contains(const T &value) const { return d.find(value) != d.end(); }
158
164 Iterator find(const T &value) { return d.find(value); }
165
167 ConstIterator find(const T &value) const { return d.find(value); }
168
174 Iterator lowerBound(const T &value) { return d.lower_bound(value); }
175
177 ConstIterator lowerBound(const T &value) const { return d.lower_bound(value); }
178
184 Iterator upperBound(const T &value) { return d.upper_bound(value); }
185
187 ConstIterator upperBound(const T &value) const { return d.upper_bound(value); }
188
189 // -- Modifiers --
190
199 Pair<Iterator, bool> insert(const T &value) { return Pair<Iterator, bool>(d.insert(value)); }
200
207 Pair<Iterator, bool> insert(T &&value) { return Pair<Iterator, bool>(d.insert(std::move(value))); }
208
214 bool remove(const T &value) { return d.erase(value) > 0; }
215
221 Iterator remove(Iterator pos) { return d.erase(pos); }
222
224 void clear() noexcept {
225 d.clear();
226 return;
227 }
228
233 void swap(Set &other) noexcept {
234 d.swap(other.d);
235 return;
236 }
237
238 // -- Set Operations --
239
245 Set unite(const Set &other) const {
246 Set ret = *this;
247 for (const auto &v : other.d) ret.d.insert(v);
248 return ret;
249 }
250
256 Set intersect(const Set &other) const {
257 Set ret;
258 for (const auto &v : d) {
259 if (other.contains(v)) ret.d.insert(v);
260 }
261 return ret;
262 }
263
269 Set subtract(const Set &other) const {
270 Set ret;
271 for (const auto &v : d) {
272 if (!other.contains(v)) ret.d.insert(v);
273 }
274 return ret;
275 }
276
277 // -- Convenience --
278
283 List<T> toList() const {
284 List<T> ret;
285 ret.reserve(d.size());
286 for (const auto &v : d) ret.pushToBack(v);
287 return ret;
288 }
289
295 template <typename Func> void forEach(Func &&func) const {
296 for (const auto &v : d) func(v);
297 return;
298 }
299
300 // -- Comparison --
301
303 friend bool operator==(const Set &lhs, const Set &rhs) { return lhs.d == rhs.d; }
304
306 friend bool operator!=(const Set &lhs, const Set &rhs) { return lhs.d != rhs.d; }
307
308 private:
309 Data d;
310};
311
312PROMEKI_NAMESPACE_END
313
314#endif // PROMEKI_ENABLE_CORE