Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qset.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSET_H
6#define QSET_H
7
8#include <QtCore/qhash.h>
9#include <QtCore/qcontainertools_impl.h>
10#include <QtCore/qttypetraits.h>
11
12#include <initializer_list>
13#include <iterator>
14
15QT_BEGIN_NAMESPACE
16
17
18template <class T>
19class QSet
20{
21 typedef QHash<T, QHashDummyValue> Hash;
22
23public:
24 inline QSet() noexcept {}
25 inline QSet(std::initializer_list<T> list)
26 : QSet(list.begin(), list.end()) {}
27 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
28 inline QSet(InputIterator first, InputIterator last)
29 {
30 QtPrivate::reserveIfForwardIterator(this, first, last);
31 for (; first != last; ++first)
32 insert(*first);
33 }
34
35 // compiler-generated copy/move ctor/assignment operators are fine!
36 // compiler-generated destructor is fine!
37
38 inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); }
39
40#ifndef Q_QDOC
41private:
42 template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>
43 friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept
44 {
45 return lhs.q_hash == rhs.q_hash;
46 }
47 QT_DECLARE_EQUALITY_OPERATORS_HELPER(QSet, QSet, /* non-constexpr */, noexcept,
48 template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>)
49public:
50#else
51 friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept;
52 friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept;
53#endif
54
55 inline qsizetype size() const { return q_hash.size(); }
56
57 inline bool isEmpty() const { return q_hash.isEmpty(); }
58
59 inline qsizetype capacity() const { return q_hash.capacity(); }
60 inline void reserve(qsizetype size);
61 inline void squeeze() { q_hash.squeeze(); }
62
63 inline void detach() { q_hash.detach(); }
64 inline bool isDetached() const { return q_hash.isDetached(); }
65
66 inline void clear() { q_hash.clear(); }
67
68 bool remove(const T &value) { return q_hash.remove(value); }
69
70 template <typename Pred>
71 inline qsizetype removeIf(Pred predicate)
72 {
73 return QtPrivate::qset_erase_if(*this, predicate);
74 }
75
76 inline bool contains(const T &value) const { return q_hash.contains(value); }
77
78 bool contains(const QSet<T> &set) const;
79
80 class const_iterator;
81
82 class iterator
83 {
84 typedef QHash<T, QHashDummyValue> Hash;
85 typename Hash::iterator i;
86 friend class const_iterator;
87 friend class QSet<T>;
88
89 public:
90 typedef std::forward_iterator_tag iterator_category;
91 typedef qptrdiff difference_type;
92 typedef T value_type;
93 typedef const T *pointer;
94 typedef const T &reference;
95
96 inline iterator() {}
97 inline iterator(typename Hash::iterator o) : i(o) {}
98 inline iterator(const iterator &o) : i(o.i) {}
99 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
100 inline const T &operator*() const { return i.key(); }
101 inline const T *operator->() const { return &i.key(); }
102 inline bool operator==(const iterator &o) const { return i == o.i; }
103 inline bool operator!=(const iterator &o) const { return i != o.i; }
104 inline bool operator==(const const_iterator &o) const
105 { return i == o.i; }
106 inline bool operator!=(const const_iterator &o) const
107 { return i != o.i; }
108 inline iterator &operator++() { ++i; return *this; }
109 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
110 };
111
112 class const_iterator
113 {
114 typedef QHash<T, QHashDummyValue> Hash;
115 typename Hash::const_iterator i;
116 friend class iterator;
117 friend class QSet<T>;
118
119 public:
120 typedef std::forward_iterator_tag iterator_category;
121 typedef qptrdiff difference_type;
122 typedef T value_type;
123 typedef const T *pointer;
124 typedef const T &reference;
125
126 inline const_iterator() {}
127 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
128 inline const_iterator(const const_iterator &o) : i(o.i) {}
129 inline const_iterator(const iterator &o)
130 : i(o.i) {}
131 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
132 inline const T &operator*() const { return i.key(); }
133 inline const T *operator->() const { return &i.key(); }
134 inline bool operator==(const const_iterator &o) const { return i == o.i; }
135 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
136 inline const_iterator &operator++() { ++i; return *this; }
137 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
138 };
139
140 // STL style
141 inline iterator begin() { return q_hash.begin(); }
142 inline const_iterator begin() const noexcept { return q_hash.begin(); }
143 inline const_iterator cbegin() const noexcept { return q_hash.begin(); }
144 inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); }
145 inline iterator end() { return q_hash.end(); }
146 inline const_iterator end() const noexcept { return q_hash.end(); }
147 inline const_iterator cend() const noexcept { return q_hash.end(); }
148 inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); }
149
150 iterator erase(const_iterator i)
151 {
152 Q_ASSERT(i != constEnd());
153 return q_hash.erase(i.i);
154 }
155
156 // more Qt
157 typedef iterator Iterator;
158 typedef const_iterator ConstIterator;
159 inline qsizetype count() const { return q_hash.size(); }
160 inline iterator insert(const T &value)
161 { return q_hash.insert(value, QHashDummyValue()); }
162 inline iterator insert(T &&value)
163 { return q_hash.emplace(std::move(value), QHashDummyValue()); }
164 iterator find(const T &value) { return q_hash.find(value); }
165 const_iterator find(const T &value) const { return q_hash.find(value); }
166 inline const_iterator constFind(const T &value) const { return find(value); }
167 QSet<T> &unite(const QSet<T> &other);
168 QSet &unite(QSet &&other);
169 QSet<T> &intersect(const QSet<T> &other);
170 bool intersects(const QSet<T> &other) const;
171 QSet<T> &subtract(const QSet<T> &other);
172
173 // STL compatibility
174 typedef T key_type;
175 typedef T value_type;
176 typedef value_type *pointer;
177 typedef const value_type *const_pointer;
178 typedef value_type &reference;
179 typedef const value_type &const_reference;
180 typedef qptrdiff difference_type;
181 typedef qsizetype size_type;
182
183 inline bool empty() const { return isEmpty(); }
184
185 iterator insert(const_iterator, const T &value) { return insert(value); }
186 iterator insert(const_iterator, T &&value) { return insert(std::move(value)); }
187
188 // comfort
189 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
190 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
191 QSet &operator|=(QSet &&other) { return unite(std::move(other)); }
192 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
193 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
194 inline QSet<T> &operator&=(const T &value)
195 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
196 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
197 QSet &operator+=(QSet &&other) { return unite(std::move(other)); }
198 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
199 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
200 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
201
202 friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; }
203 friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); }
204 friend QSet operator|(const QSet &lhs, QSet &&rhs) { return QSet(lhs) |= std::move(rhs); }
205 friend QSet operator|(QSet &&lhs, QSet &&rhs) { return std::move(lhs) |= std::move(rhs); }
206
207 friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; }
208 friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); }
209
210 friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; }
211 friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); }
212 friend QSet operator+(const QSet &lhs, QSet &&rhs) { return QSet(lhs) += std::move(rhs); }
213 friend QSet operator+(QSet &&lhs, QSet &&rhs) { return std::move(lhs) += std::move(rhs); }
214
215 friend QSet operator-(const QSet &lhs, const QSet &rhs) { return QSet(lhs) -= rhs; }
216 friend QSet operator-(QSet &&lhs, const QSet &rhs) { lhs -= rhs; return std::move(lhs); }
217
218 inline QList<T> values() const &;
219 inline QList<T> values() &&;
220
221private:
222 static inline QSet intersected_helper(const QSet &lhs, const QSet &rhs);
223
224 template <typename E>
225 void _emplace_or_overwrite(E &&e);
226
227 Hash q_hash;
228};
229
230template <typename InputIterator,
231 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
232 QtPrivate::IfIsInputIterator<InputIterator> = true>
233QSet(InputIterator, InputIterator) -> QSet<ValueType>;
234
235template <typename T>
237noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
238{
239 return qHashRangeCommutative(key.begin(), key.end(), seed);
240}
241
242// inline function implementations
243
244template <class T>
245Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
246
247template <class T>
248Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
249{
250 if (!q_hash.isSharedWith(other.q_hash)) {
251 for (const T &e : other)
252 insert(e);
253 }
254 return *this;
255}
256
257template <class T>
258Q_INLINE_TEMPLATE auto QSet<T>::unite(QSet &&other) -> QSet&
259{
260 if (other.isDetached() && size() < other.size()) {
261
262 // We can change the state of `other`, so take the smaller *this and
263 // insert it into the larger `other`, making sure we take equivalent
264 // elements from *this:
265
266 swap(other);
267
268 // Now: iterate over `other`, insert into *this, making sure we take
269 // equivalent elements from `other`:
270
271 if (other.isDetached()) { // can move elements from `other`
272 for (auto it = other.q_hash.begin(), end = other.q_hash.end(); it != end; ++it)
273 _emplace_or_overwrite(std::move(it.i.node()->key));
274 } else { // need to copy elements from `other`
275 for (const auto &e : std::as_const(other))
276 _emplace_or_overwrite(e);
277 }
278
279 return *this;
280 }
281
282 // in all other cases, the lvalue overload is not worse:
283 return unite(other);
284}
285
286template <class T>
287template <typename E>
288Q_INLINE_TEMPLATE void QSet<T>::_emplace_or_overwrite(E &&e)
289{
290 const auto r = q_hash.tryEmplace(std::forward<E>(e));
291 if (!r.inserted) {
292 // QHash never overwrites the key, but that's what we need
293 // here, so do it using private QHash API:
294 // NB: `e` was _not_ moved from by tryEmplace()!
295 typename Hash::Data::Bucket(r.iterator.i).node()->key = std::forward<E>(e);
296 }
297}
298
299template <class T>
300Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
301{
302 if (q_hash.isSharedWith(other.q_hash)) {
303 // nothing to do
304 } else if (isEmpty() || other.isEmpty()) {
305 // any set intersected with the empty set is the empty set
306 clear();
307 } else if (q_hash.isDetached()) {
308 // do it in-place:
309 removeIf([&other] (const T &e) { return !other.contains(e); });
310 } else {
311 // don't detach *this just to remove some items; create a new set
312 *this = intersected_helper(*this, other);
313 }
314 return *this;
315}
316
317template <class T>
318// static
319auto QSet<T>::intersected_helper(const QSet &lhs, const QSet &rhs) -> QSet
320{
321 QSet r;
322
323 const auto l_size = lhs.size();
324 const auto r_size = rhs.size();
325 r.reserve((std::min)(l_size, r_size));
326
327 // Iterate the smaller of the two sets, but always take from lhs, for
328 // consistency with insert():
329
330 if (l_size <= r_size) {
331 // lhs is not larger
332 for (const auto &e : lhs) {
333 if (rhs.contains(e))
334 r.insert(e);
335 }
336 } else {
337 // rhs is smaller
338 for (const auto &e : rhs) {
339 if (const auto it = lhs.find(e); it != lhs.end())
340 r.insert(*it);
341 }
342 }
343
344 return r;
345}
346
347template <class T>
348Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
349{
350 const bool otherIsBigger = other.size() > size();
351 const QSet &smallestSet = otherIsBigger ? *this : other;
352 const QSet &biggestSet = otherIsBigger ? other : *this;
353 typename QSet::const_iterator i = smallestSet.cbegin();
354 typename QSet::const_iterator e = smallestSet.cend();
355
356 while (i != e) {
357 if (biggestSet.contains(*i))
358 return true;
359 ++i;
360 }
361
362 return false;
363}
364
365template <class T>
366Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
367{
368 if (q_hash.isSharedWith(other.q_hash)) {
369 clear();
370 } else {
371 for (const auto &e : other)
372 remove(e);
373 }
374 return *this;
375}
376
377template <class T>
378Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
379{
380 typename QSet<T>::const_iterator i = other.constBegin();
381 while (i != other.constEnd()) {
382 if (!contains(*i))
383 return false;
384 ++i;
385 }
386 return true;
387}
388
389template <typename T>
390QList<T> QSet<T>::values() const &
391{
392 QList<T> result;
393 result.reserve(size());
394 typename QSet<T>::const_iterator i = constBegin();
395 while (i != constEnd()) {
396 result.append(*i);
397 ++i;
398 }
399 return result;
400}
401
402template <typename T>
403QList<T> QSet<T>::values() &&
404{
405 if (!isDetached())
406 return values(); // lvalue overload
407 // we can move from *this:
408 return [&] {
409 QList<T> result;
410 result.reserve(size());
411 for (auto it = q_hash.begin(), end = q_hash.end(); it != end; ++it)
412 result.push_back(std::move(it.i.node()->key));
413 return result;
414 }();
415}
416
417Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
418
419#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
420template <typename T>
421class QMutableSetIterator
422{
423 typedef typename QSet<T>::iterator iterator;
424 QSet<T> *c;
425 iterator i, n;
426 inline bool item_exists() const { return c->constEnd() != n; }
427
428public:
429 inline QMutableSetIterator(QSet<T> &container)
430 : c(&container)
431 { i = c->begin(); n = c->end(); }
432 inline QMutableSetIterator &operator=(QSet<T> &container)
433 { c = &container; i = c->begin(); n = c->end(); return *this; }
434 inline void toFront() { i = c->begin(); n = c->end(); }
435 inline void toBack() { i = c->end(); n = i; }
436 inline bool hasNext() const { return c->constEnd() != i; }
437 inline const T &next() { n = i++; return *n; }
438 inline const T &peekNext() const { return *i; }
439 inline void remove()
440 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
441 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
442 inline bool findNext(const T &t)
443 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
444};
445#endif // QT_NO_JAVA_STYLE_ITERATORS
446
447template <typename T, typename Predicate>
448qsizetype erase_if(QSet<T> &set, Predicate pred)
449{
450 return QtPrivate::qset_erase_if(set, pred);
451}
452
453QT_END_NAMESPACE
454
455#endif // QSET_H
void connectEngine(QFileSystemWatcherEngine *e)
void fileChanged(const QString &path, bool removed)
QFileSystemWatcherEngine * native
QFileSystemWatcherEngine * poller
void directoryChanged(const QString &path, bool removed)
QT_REQUIRE_CONFIG(animation)
static QStringList empty_paths_pruned(const QStringList &paths)
QT_REQUIRE_CONFIG(filesystemwatcher)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
QSet(InputIterator, InputIterator) -> QSet< ValueType >
size_t qHash(const QSet< T > &key, size_t seed=0) noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
Definition qset.h:236
qsizetype erase_if(QSet< T > &set, Predicate pred)
Definition qset.h:448