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
4#ifndef QSET_H
5#define QSET_H
6
7#include <QtCore/qhash.h>
8#include <QtCore/qcontainertools_impl.h>
9#include <QtCore/qttypetraits.h>
10
11#include <initializer_list>
12#include <iterator>
13
14QT_BEGIN_NAMESPACE
15
16
17template <class T>
18class QSet
19{
20 typedef QHash<T, QHashDummyValue> Hash;
21
22public:
23 inline QSet() noexcept {}
24 inline QSet(std::initializer_list<T> list)
25 : QSet(list.begin(), list.end()) {}
26 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
27 inline QSet(InputIterator first, InputIterator last)
28 {
29 QtPrivate::reserveIfForwardIterator(this, first, last);
30 for (; first != last; ++first)
31 insert(*first);
32 }
33
34 // compiler-generated copy/move ctor/assignment operators are fine!
35 // compiler-generated destructor is fine!
36
37 inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); }
38
39#ifndef Q_QDOC
40private:
41 template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>
42 friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept
43 {
44 return lhs.q_hash == rhs.q_hash;
45 }
46 QT_DECLARE_EQUALITY_OPERATORS_HELPER(QSet, QSet, /* non-constexpr */, noexcept,
47 template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>)
48public:
49#else
50 friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept;
51 friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept;
52#endif
53
54 inline qsizetype size() const { return q_hash.size(); }
55
56 inline bool isEmpty() const { return q_hash.isEmpty(); }
57
58 inline qsizetype capacity() const { return q_hash.capacity(); }
59 inline void reserve(qsizetype size);
60 inline void squeeze() { q_hash.squeeze(); }
61
62 inline void detach() { q_hash.detach(); }
63 inline bool isDetached() const { return q_hash.isDetached(); }
64
65 inline void clear() { q_hash.clear(); }
66
67 inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
68
69 template <typename Pred>
70 inline qsizetype removeIf(Pred predicate)
71 {
72 return QtPrivate::qset_erase_if(*this, predicate);
73 }
74
75 inline bool contains(const T &value) const { return q_hash.contains(value); }
76
77 bool contains(const QSet<T> &set) const;
78
79 class const_iterator;
80
81 class iterator
82 {
83 typedef QHash<T, QHashDummyValue> Hash;
84 typename Hash::iterator i;
85 friend class const_iterator;
86 friend class QSet<T>;
87
88 public:
89 typedef std::forward_iterator_tag iterator_category;
90 typedef qptrdiff difference_type;
91 typedef T value_type;
92 typedef const T *pointer;
93 typedef const T &reference;
94
95 inline iterator() {}
96 inline iterator(typename Hash::iterator o) : i(o) {}
97 inline iterator(const iterator &o) : i(o.i) {}
98 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
99 inline const T &operator*() const { return i.key(); }
100 inline const T *operator->() const { return &i.key(); }
101 inline bool operator==(const iterator &o) const { return i == o.i; }
102 inline bool operator!=(const iterator &o) const { return i != o.i; }
103 inline bool operator==(const const_iterator &o) const
104 { return i == o.i; }
105 inline bool operator!=(const const_iterator &o) const
106 { return i != o.i; }
107 inline iterator &operator++() { ++i; return *this; }
108 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
109 };
110
111 class const_iterator
112 {
113 typedef QHash<T, QHashDummyValue> Hash;
114 typename Hash::const_iterator i;
115 friend class iterator;
116 friend class QSet<T>;
117
118 public:
119 typedef std::forward_iterator_tag iterator_category;
120 typedef qptrdiff difference_type;
121 typedef T value_type;
122 typedef const T *pointer;
123 typedef const T &reference;
124
125 inline const_iterator() {}
126 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
127 inline const_iterator(const const_iterator &o) : i(o.i) {}
128 inline const_iterator(const iterator &o)
129 : i(o.i) {}
130 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
131 inline const T &operator*() const { return i.key(); }
132 inline const T *operator->() const { return &i.key(); }
133 inline bool operator==(const const_iterator &o) const { return i == o.i; }
134 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
135 inline const_iterator &operator++() { ++i; return *this; }
136 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
137 };
138
139 // STL style
140 inline iterator begin() { return q_hash.begin(); }
141 inline const_iterator begin() const noexcept { return q_hash.begin(); }
142 inline const_iterator cbegin() const noexcept { return q_hash.begin(); }
143 inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); }
144 inline iterator end() { return q_hash.end(); }
145 inline const_iterator end() const noexcept { return q_hash.end(); }
146 inline const_iterator cend() const noexcept { return q_hash.end(); }
147 inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); }
148
149 iterator erase(const_iterator i)
150 {
151 Q_ASSERT(i != constEnd());
152 return q_hash.erase(i.i);
153 }
154
155 // more Qt
156 typedef iterator Iterator;
157 typedef const_iterator ConstIterator;
158 inline qsizetype count() const { return q_hash.size(); }
159 inline iterator insert(const T &value)
160 { return q_hash.insert(value, QHashDummyValue()); }
161 inline iterator insert(T &&value)
162 { return q_hash.emplace(std::move(value), QHashDummyValue()); }
163 iterator find(const T &value) { return q_hash.find(value); }
164 const_iterator find(const T &value) const { return q_hash.find(value); }
165 inline const_iterator constFind(const T &value) const { return find(value); }
166 QSet<T> &unite(const QSet<T> &other);
167 QSet<T> &intersect(const QSet<T> &other);
168 bool intersects(const QSet<T> &other) const;
169 QSet<T> &subtract(const QSet<T> &other);
170
171 // STL compatibility
172 typedef T key_type;
173 typedef T value_type;
174 typedef value_type *pointer;
175 typedef const value_type *const_pointer;
176 typedef value_type &reference;
177 typedef const value_type &const_reference;
178 typedef qptrdiff difference_type;
179 typedef qsizetype size_type;
180
181 inline bool empty() const { return isEmpty(); }
182
183 iterator insert(const_iterator, const T &value) { return insert(value); }
184
185 // comfort
186 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
187 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
188 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
189 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
190 inline QSet<T> &operator&=(const T &value)
191 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
192 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
193 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
194 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
195 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
196
197 friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; }
198 friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); }
199
200 friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; }
201 friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); }
202
203 friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; }
204 friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); }
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 QList<T> values() const;
210
211private:
212 Hash q_hash;
213};
214
215template <typename InputIterator,
216 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
217 QtPrivate::IfIsInputIterator<InputIterator> = true>
218QSet(InputIterator, InputIterator) -> QSet<ValueType>;
219
220template <typename T>
222noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
223{
224 return qHashRangeCommutative(key.begin(), key.end(), seed);
225}
226
227// inline function implementations
228
229template <class T>
230Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
231
232template <class T>
233Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
234{
235 if (q_hash.isSharedWith(other.q_hash))
236 return *this;
237 QSet<T> tmp = other;
238 if (size() < other.size())
239 swap(tmp);
240 for (const auto &e : std::as_const(tmp))
241 insert(e);
242 return *this;
243}
244
245template <class T>
246Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
247{
248 QSet<T> copy1;
249 QSet<T> copy2;
250 if (size() <= other.size()) {
251 copy1 = *this;
252 copy2 = other;
253 } else {
254 copy1 = other;
255 copy2 = *this;
256 *this = copy1;
257 }
258 for (const auto &e : std::as_const(copy1)) {
259 if (!copy2.contains(e))
260 remove(e);
261 }
262 return *this;
263}
264
265template <class T>
266Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
267{
268 const bool otherIsBigger = other.size() > size();
269 const QSet &smallestSet = otherIsBigger ? *this : other;
270 const QSet &biggestSet = otherIsBigger ? other : *this;
271 typename QSet::const_iterator i = smallestSet.cbegin();
272 typename QSet::const_iterator e = smallestSet.cend();
273
274 while (i != e) {
275 if (biggestSet.contains(*i))
276 return true;
277 ++i;
278 }
279
280 return false;
281}
282
283template <class T>
284Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
285{
286 if (q_hash.isSharedWith(other.q_hash)) {
287 clear();
288 } else {
289 for (const auto &e : other)
290 remove(e);
291 }
292 return *this;
293}
294
295template <class T>
296Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
297{
298 typename QSet<T>::const_iterator i = other.constBegin();
299 while (i != other.constEnd()) {
300 if (!contains(*i))
301 return false;
302 ++i;
303 }
304 return true;
305}
306
307template <typename T>
308Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const
309{
310 QList<T> result;
311 result.reserve(size());
312 typename QSet<T>::const_iterator i = constBegin();
313 while (i != constEnd()) {
314 result.append(*i);
315 ++i;
316 }
317 return result;
318}
319
320Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
321
322#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
323template <typename T>
324class QMutableSetIterator
325{
326 typedef typename QSet<T>::iterator iterator;
327 QSet<T> *c;
328 iterator i, n;
329 inline bool item_exists() const { return c->constEnd() != n; }
330
331public:
332 inline QMutableSetIterator(QSet<T> &container)
333 : c(&container)
334 { i = c->begin(); n = c->end(); }
335 inline QMutableSetIterator &operator=(QSet<T> &container)
336 { c = &container; i = c->begin(); n = c->end(); return *this; }
337 inline void toFront() { i = c->begin(); n = c->end(); }
338 inline void toBack() { i = c->end(); n = i; }
339 inline bool hasNext() const { return c->constEnd() != i; }
340 inline const T &next() { n = i++; return *n; }
341 inline const T &peekNext() const { return *i; }
342 inline void remove()
343 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
344 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
345 inline bool findNext(const T &t)
346 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
347};
348#endif // QT_NO_JAVA_STYLE_ITERATORS
349
350template <typename T, typename Predicate>
351qsizetype erase_if(QSet<T> &set, Predicate pred)
352{
353 return QtPrivate::qset_erase_if(set, pred);
354}
355
356QT_END_NAMESPACE
357
358#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:221
qsizetype erase_if(QSet< T > &set, Predicate pred)
Definition qset.h:351