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
187 // comfort
188 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
189 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
190 QSet &operator|=(QSet &&other) { return unite(std::move(other)); }
191 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
192 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
193 inline QSet<T> &operator&=(const T &value)
194 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
195 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
196 QSet &operator+=(QSet &&other) { return unite(std::move(other)); }
197 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
198 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
199 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
200
201 friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; }
202 friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); }
203 friend QSet operator|(const QSet &lhs, QSet &&rhs) { return lhs |= std::move(rhs); }
204 friend QSet operator|(QSet &&lhs, QSet &&rhs) { return std::move(lhs) |= std::move(rhs); }
205
206 friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; }
207 friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); }
208
209 friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; }
210 friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); }
211 friend QSet operator+(const QSet &lhs, QSet &&rhs) { return QSet(lhs) += std::move(rhs); }
212 friend QSet operator+(QSet &&lhs, QSet &&rhs) { return std::move(lhs) += std::move(rhs); }
213
214 friend QSet operator-(const QSet &lhs, const QSet &rhs) { return QSet(lhs) -= rhs; }
215 friend QSet operator-(QSet &&lhs, const QSet &rhs) { lhs -= rhs; return std::move(lhs); }
216
217 inline QList<T> values() const;
218
219private:
220 static inline QSet intersected_helper(const QSet &lhs, const QSet &rhs);
221
222 template <typename E>
223 void _emplace_or_overwrite(E &&e);
224
225 Hash q_hash;
226};
227
228template <typename InputIterator,
229 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
230 QtPrivate::IfIsInputIterator<InputIterator> = true>
231QSet(InputIterator, InputIterator) -> QSet<ValueType>;
232
233template <typename T>
235noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
236{
237 return qHashRangeCommutative(key.begin(), key.end(), seed);
238}
239
240// inline function implementations
241
242template <class T>
243Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
244
245template <class T>
246Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
247{
248 if (!q_hash.isSharedWith(other.q_hash)) {
249 for (const T &e : other)
250 insert(e);
251 }
252 return *this;
253}
254
255template <class T>
256Q_INLINE_TEMPLATE auto QSet<T>::unite(QSet &&other) -> QSet&
257{
258 if (other.isDetached() && size() < other.size()) {
259
260 // We can change the state of `other`, so take the smaller *this and
261 // insert it into the larger `other`, making sure we take equivalent
262 // elements from *this:
263
264 swap(other);
265
266 // Now: iterate over `other`, insert into *this, making sure we take
267 // equivalent elements from `other`:
268
269 if (other.isDetached()) { // can move elements from `other`
270 for (auto &e : other)
271 _emplace_or_overwrite(std::move(e));
272 } else { // need to copy elements from `other`
273 for (const auto &e : std::as_const(other))
274 _emplace_or_overwrite(e);
275 }
276
277 return *this;
278 }
279
280 // in all other cases, the lvalue overload is not worse:
281 return unite(other);
282}
283
284template <class T>
285template <typename E>
286Q_INLINE_TEMPLATE void QSet<T>::_emplace_or_overwrite(E &&e)
287{
288 const auto r = q_hash.tryEmplace(std::forward<E>(e));
289 if (!r.inserted) {
290 // QHash never overwrites the key, but that's what we need
291 // here, so do it using private QHash API:
292 // NB: `e` was _not_ moved from by tryEmplace()!
293 typename Hash::Data::Bucket(r.iterator.i).node()->key = std::forward<E>(e);
294 }
295}
296
297template <class T>
298Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
299{
300 if (q_hash.isSharedWith(other.q_hash)) {
301 // nothing to do
302 } else if (isEmpty() || other.isEmpty()) {
303 // any set intersected with the empty set is the empty set
304 clear();
305 } else if (q_hash.isDetached()) {
306 // do it in-place:
307 removeIf([&other] (const T &e) { return !other.contains(e); });
308 } else {
309 // don't detach *this just to remove some items; create a new set
310 *this = intersected_helper(*this, other);
311 }
312 return *this;
313}
314
315template <class T>
316// static
317auto QSet<T>::intersected_helper(const QSet &lhs, const QSet &rhs) -> QSet
318{
319 QSet r;
320
321 const auto l_size = lhs.size();
322 const auto r_size = rhs.size();
323 r.reserve((std::min)(l_size, r_size));
324
325 // Iterate the smaller of the two sets, but always take from lhs, for
326 // consistency with insert():
327
328 if (l_size <= r_size) {
329 // lhs is not larger
330 for (const auto &e : lhs) {
331 if (rhs.contains(e))
332 r.insert(e);
333 }
334 } else {
335 // rhs is smaller
336 for (const auto &e : rhs) {
337 if (const auto it = lhs.find(e); it != lhs.end())
338 r.insert(*it);
339 }
340 }
341
342 return r;
343}
344
345template <class T>
346Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
347{
348 const bool otherIsBigger = other.size() > size();
349 const QSet &smallestSet = otherIsBigger ? *this : other;
350 const QSet &biggestSet = otherIsBigger ? other : *this;
351 typename QSet::const_iterator i = smallestSet.cbegin();
352 typename QSet::const_iterator e = smallestSet.cend();
353
354 while (i != e) {
355 if (biggestSet.contains(*i))
356 return true;
357 ++i;
358 }
359
360 return false;
361}
362
363template <class T>
364Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
365{
366 if (q_hash.isSharedWith(other.q_hash)) {
367 clear();
368 } else {
369 for (const auto &e : other)
370 remove(e);
371 }
372 return *this;
373}
374
375template <class T>
376Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
377{
378 typename QSet<T>::const_iterator i = other.constBegin();
379 while (i != other.constEnd()) {
380 if (!contains(*i))
381 return false;
382 ++i;
383 }
384 return true;
385}
386
387template <typename T>
388QList<T> QSet<T>::values() const
389{
390 QList<T> result;
391 result.reserve(size());
392 typename QSet<T>::const_iterator i = constBegin();
393 while (i != constEnd()) {
394 result.append(*i);
395 ++i;
396 }
397 return result;
398}
399
400Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
401
402#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
403template <typename T>
404class QMutableSetIterator
405{
406 typedef typename QSet<T>::iterator iterator;
407 QSet<T> *c;
408 iterator i, n;
409 inline bool item_exists() const { return c->constEnd() != n; }
410
411public:
412 inline QMutableSetIterator(QSet<T> &container)
413 : c(&container)
414 { i = c->begin(); n = c->end(); }
415 inline QMutableSetIterator &operator=(QSet<T> &container)
416 { c = &container; i = c->begin(); n = c->end(); return *this; }
417 inline void toFront() { i = c->begin(); n = c->end(); }
418 inline void toBack() { i = c->end(); n = i; }
419 inline bool hasNext() const { return c->constEnd() != i; }
420 inline const T &next() { n = i++; return *n; }
421 inline const T &peekNext() const { return *i; }
422 inline void remove()
423 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
424 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
425 inline bool findNext(const T &t)
426 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
427};
428#endif // QT_NO_JAVA_STYLE_ITERATORS
429
430template <typename T, typename Predicate>
431qsizetype erase_if(QSet<T> &set, Predicate pred)
432{
433 return QtPrivate::qset_erase_if(set, pred);
434}
435
436QT_END_NAMESPACE
437
438#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:234
qsizetype erase_if(QSet< T > &set, Predicate pred)
Definition qset.h:431