Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
10#include <initializer_list>
11#include <iterator>
12
14
15
16template <class T>
17class QSet
18{
19 typedef QHash<T, QHashDummyValue> Hash;
20
21public:
22 inline QSet() noexcept {}
23 inline QSet(std::initializer_list<T> list)
24 : QSet(list.begin(), list.end()) {}
25 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
26 inline QSet(InputIterator first, InputIterator last)
27 {
29 for (; first != last; ++first)
30 insert(*first);
31 }
32
33 // compiler-generated copy/move ctor/assignment operators are fine!
34 // compiler-generated destructor is fine!
35
36 inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); }
37
38#ifndef Q_QDOC
39 template <typename U = T>
41 { return q_hash == other.q_hash; }
42 template <typename U = T>
44 { return q_hash != other.q_hash; }
45#else
46 bool operator==(const QSet &other) const;
47 bool operator!=(const QSet &other) const;
48#endif
49
50 inline qsizetype size() const { return q_hash.size(); }
51
52 inline bool isEmpty() const { return q_hash.isEmpty(); }
53
54 inline qsizetype capacity() const { return q_hash.capacity(); }
55 inline void reserve(qsizetype size);
56 inline void squeeze() { q_hash.squeeze(); }
57
58 inline void detach() { q_hash.detach(); }
59 inline bool isDetached() const { return q_hash.isDetached(); }
60
61 inline void clear() { q_hash.clear(); }
62
63 inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
64
65 template <typename Pred>
67 {
69 }
70
71 inline bool contains(const T &value) const { return q_hash.contains(value); }
72
73 bool contains(const QSet<T> &set) const;
74
75 class const_iterator;
76
78 {
79 typedef QHash<T, QHashDummyValue> Hash;
80 typename Hash::iterator i;
81 friend class const_iterator;
82 friend class QSet<T>;
83
84 public:
85 typedef std::forward_iterator_tag iterator_category;
87 typedef T value_type;
88 typedef const T *pointer;
89 typedef const T &reference;
90
91 inline iterator() {}
92 inline iterator(typename Hash::iterator o) : i(o) {}
93 inline iterator(const iterator &o) : i(o.i) {}
94 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
95 inline const T &operator*() const { return i.key(); }
96 inline const T *operator->() const { return &i.key(); }
97 inline bool operator==(const iterator &o) const { return i == o.i; }
98 inline bool operator!=(const iterator &o) const { return i != o.i; }
99 inline bool operator==(const const_iterator &o) const
100 { return i == o.i; }
101 inline bool operator!=(const const_iterator &o) const
102 { return i != o.i; }
103 inline iterator &operator++() { ++i; return *this; }
104 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
105 };
106
108 {
109 typedef QHash<T, QHashDummyValue> Hash;
110 typename Hash::const_iterator i;
111 friend class iterator;
112 friend class QSet<T>;
113
114 public:
115 typedef std::forward_iterator_tag iterator_category;
117 typedef T value_type;
118 typedef const T *pointer;
119 typedef const T &reference;
120
121 inline const_iterator() {}
122 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
123 inline const_iterator(const const_iterator &o) : i(o.i) {}
124 inline const_iterator(const iterator &o)
125 : i(o.i) {}
126 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
127 inline const T &operator*() const { return i.key(); }
128 inline const T *operator->() const { return &i.key(); }
129 inline bool operator==(const const_iterator &o) const { return i == o.i; }
130 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
131 inline const_iterator &operator++() { ++i; return *this; }
132 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
133 };
134
135 // STL style
136 inline iterator begin() { return q_hash.begin(); }
137 inline const_iterator begin() const noexcept { return q_hash.begin(); }
138 inline const_iterator cbegin() const noexcept { return q_hash.begin(); }
139 inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); }
140 inline iterator end() { return q_hash.end(); }
141 inline const_iterator end() const noexcept { return q_hash.end(); }
142 inline const_iterator cend() const noexcept { return q_hash.end(); }
143 inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); }
144
146 {
147 Q_ASSERT(i != constEnd());
148 return q_hash.erase(i.i);
149 }
150
151 // more Qt
154 inline qsizetype count() const { return q_hash.size(); }
155 inline iterator insert(const T &value)
156 { return q_hash.insert(value, QHashDummyValue()); }
158 { return q_hash.emplace(std::move(value), QHashDummyValue()); }
159 iterator find(const T &value) { return q_hash.find(value); }
160 const_iterator find(const T &value) const { return q_hash.find(value); }
161 inline const_iterator constFind(const T &value) const { return find(value); }
162 QSet<T> &unite(const QSet<T> &other);
163 QSet<T> &intersect(const QSet<T> &other);
164 bool intersects(const QSet<T> &other) const;
165 QSet<T> &subtract(const QSet<T> &other);
166
167 // STL compatibility
168 typedef T key_type;
169 typedef T value_type;
171 typedef const value_type *const_pointer;
176
177 inline bool empty() const { return isEmpty(); }
178
180
181 // comfort
182 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
183 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
184 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
185 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
186 inline QSet<T> &operator&=(const T &value)
187 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
188 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
189 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
190 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
191 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
192
193 friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; }
194 friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); }
195
196 friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; }
197 friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); }
198
199 friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; }
200 friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); }
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
205 QList<T> values() const;
206
207private:
208 Hash q_hash;
209};
210
211template <typename InputIterator,
212 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
214QSet(InputIterator, InputIterator) -> QSet<ValueType>;
215
216template <typename T>
217size_t qHash(const QSet<T> &key, size_t seed = 0)
218noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
219{
220 return qHashRangeCommutative(key.begin(), key.end(), seed);
221}
222
223// inline function implementations
224
225template <class T>
226Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
227
228template <class T>
229Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
230{
231 if (q_hash.isSharedWith(other.q_hash))
232 return *this;
233 QSet<T> tmp = other;
234 if (size() < other.size())
235 swap(tmp);
236 for (const auto &e : std::as_const(tmp))
237 insert(e);
238 return *this;
239}
240
241template <class T>
242Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
243{
244 QSet<T> copy1;
245 QSet<T> copy2;
246 if (size() <= other.size()) {
247 copy1 = *this;
248 copy2 = other;
249 } else {
250 copy1 = other;
251 copy2 = *this;
252 *this = copy1;
253 }
254 for (const auto &e : std::as_const(copy1)) {
255 if (!copy2.contains(e))
256 remove(e);
257 }
258 return *this;
259}
260
261template <class T>
262Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
263{
264 const bool otherIsBigger = other.size() > size();
265 const QSet &smallestSet = otherIsBigger ? *this : other;
266 const QSet &biggestSet = otherIsBigger ? other : *this;
267 typename QSet::const_iterator i = smallestSet.cbegin();
268 typename QSet::const_iterator e = smallestSet.cend();
269
270 while (i != e) {
271 if (biggestSet.contains(*i))
272 return true;
273 ++i;
274 }
275
276 return false;
277}
278
279template <class T>
280Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
281{
282 if (q_hash.isSharedWith(other.q_hash)) {
283 clear();
284 } else {
285 for (const auto &e : other)
286 remove(e);
287 }
288 return *this;
289}
290
291template <class T>
292Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
293{
294 typename QSet<T>::const_iterator i = other.constBegin();
295 while (i != other.constEnd()) {
296 if (!contains(*i))
297 return false;
298 ++i;
299 }
300 return true;
301}
302
303template <typename T>
304Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const
305{
306 QList<T> result;
307 result.reserve(size());
308 typename QSet<T>::const_iterator i = constBegin();
309 while (i != constEnd()) {
310 result.append(*i);
311 ++i;
312 }
313 return result;
314}
315
317
318#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
319template <typename T>
321{
322 typedef typename QSet<T>::iterator iterator;
323 QSet<T> *c;
324 iterator i, n;
325 inline bool item_exists() const { return c->constEnd() != n; }
326
327public:
328 inline QMutableSetIterator(QSet<T> &container)
329 : c(&container)
330 { i = c->begin(); n = c->end(); }
331 inline QMutableSetIterator &operator=(QSet<T> &container)
332 { c = &container; i = c->begin(); n = c->end(); return *this; }
333 inline void toFront() { i = c->begin(); n = c->end(); }
334 inline void toBack() { i = c->end(); n = i; }
335 inline bool hasNext() const { return c->constEnd() != i; }
336 inline const T &next() { n = i++; return *n; }
337 inline const T &peekNext() const { return *i; }
338 inline void remove()
339 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
340 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
341 inline bool findNext(const T &t)
342 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
343};
344#endif // QT_NO_JAVA_STYLE_ITERATORS
345
346template <typename T, typename Predicate>
347qsizetype erase_if(QSet<T> &set, Predicate pred)
348{
349 return QtPrivate::qset_erase_if(set, pred);
350}
351
353
354#endif // QSET_H
void squeeze()
Reduces the size of the QHash's internal hash table to save memory.
Definition qhash.h:941
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:958
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
qsizetype size() const noexcept
Returns the number of items in the hash.
Definition qhash.h:927
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1291
iterator emplace(const Key &key, Args &&... args)
Definition qhash.h:1324
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
void detach()
Definition qhash.h:947
bool isDetached() const noexcept
Definition qhash.h:948
iterator erase(const_iterator it)
Definition qhash.h:1233
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
void swap(QHash &other) noexcept
Definition qhash.h:900
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
friend class const_iterator
Definition qhash.h:1182
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
qsizetype capacity() const noexcept
Returns the number of buckets in the QHash's internal hash table.
Definition qhash.h:930
bool isEmpty() const noexcept
Returns true if the hash contains no items; otherwise returns false.
Definition qhash.h:928
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
const T & next()
Definition qset.h:336
QMutableSetIterator & operator=(QSet< T > &container)
Definition qset.h:331
QMutableSetIterator(QSet< T > &container)
Definition qset.h:328
bool findNext(const T &t)
Definition qset.h:341
bool hasNext() const
Definition qset.h:335
const T & value() const
Definition qset.h:340
const T & peekNext() const
Definition qset.h:337
std::forward_iterator_tag iterator_category
Definition qset.h:115
const_iterator(const const_iterator &o)
Definition qset.h:123
const T * operator->() const
Definition qset.h:128
const_iterator & operator++()
Definition qset.h:131
const_iterator(typename Hash::const_iterator o)
Definition qset.h:122
bool operator!=(const const_iterator &o) const
Definition qset.h:130
const T * pointer
Definition qset.h:118
const T & reference
Definition qset.h:119
const T & operator*() const
Definition qset.h:127
qptrdiff difference_type
Definition qset.h:116
const_iterator & operator=(const const_iterator &o)
Definition qset.h:126
bool operator==(const const_iterator &o) const
Definition qset.h:129
const_iterator operator++(int)
Definition qset.h:132
const_iterator(const iterator &o)
Definition qset.h:124
iterator & operator++()
Definition qset.h:103
qptrdiff difference_type
Definition qset.h:86
iterator operator++(int)
Definition qset.h:104
iterator(const iterator &o)
Definition qset.h:93
std::forward_iterator_tag iterator_category
Definition qset.h:85
const T & reference
Definition qset.h:89
iterator & operator=(const iterator &o)
Definition qset.h:94
const T * pointer
Definition qset.h:88
const T * operator->() const
Definition qset.h:96
bool operator!=(const iterator &o) const
Definition qset.h:98
const T & operator*() const
Definition qset.h:95
bool operator!=(const const_iterator &o) const
Definition qset.h:101
iterator(typename Hash::iterator o)
Definition qset.h:92
bool operator==(const const_iterator &o) const
Definition qset.h:99
bool operator==(const iterator &o) const
Definition qset.h:97
Definition qset.h:18
QTypeTraits::compare_eq_result_container< QSet, U > operator!=(const QSet< T > &other) const
Definition qset.h:43
QSet< T > & operator-=(const T &value)
Definition qset.h:191
qsizetype size_type
Definition qset.h:175
QSet< T > & operator|=(const T &value)
Definition qset.h:184
const_iterator end() const noexcept
Definition qset.h:141
qsizetype size() const
Definition qset.h:50
void detach()
Definition qset.h:58
QList< T > values() const
Definition qset.h:304
const value_type * const_pointer
Definition qset.h:171
QSet< T > & intersect(const QSet< T > &other)
Definition qset.h:242
void squeeze()
Definition qset.h:56
const_iterator ConstIterator
Definition qset.h:153
bool remove(const T &value)
Definition qset.h:63
void swap(QSet< T > &other) noexcept
Definition qset.h:36
bool contains(const QSet< T > &set) const
Definition qset.h:292
QSet() noexcept
Definition qset.h:22
friend QSet operator|(QSet &&lhs, const QSet &rhs)
Definition qset.h:194
const value_type & const_reference
Definition qset.h:173
iterator begin()
Definition qset.h:136
QSet< T > & operator-=(const QSet< T > &other)
Definition qset.h:190
value_type * pointer
Definition qset.h:170
QSet< T > & operator+=(const QSet< T > &other)
Definition qset.h:188
iterator end()
Definition qset.h:140
friend QSet operator+(const QSet &lhs, const QSet &rhs)
Definition qset.h:199
bool isEmpty() const
Definition qset.h:52
const_iterator constBegin() const noexcept
Definition qset.h:139
const_iterator cend() const noexcept
Definition qset.h:142
T value_type
Definition qset.h:169
qsizetype count() const
Definition qset.h:154
const_iterator constEnd() const noexcept
Definition qset.h:143
T key_type
Definition qset.h:168
friend QSet operator&(const QSet &lhs, const QSet &rhs)
Definition qset.h:196
void clear()
Definition qset.h:61
iterator Iterator
Definition qset.h:152
QTypeTraits::compare_eq_result_container< QSet, U > operator==(const QSet< T > &other) const
Definition qset.h:40
QSet< T > & operator|=(const QSet< T > &other)
Definition qset.h:183
iterator erase(const_iterator i)
Definition qset.h:145
QSet(InputIterator first, InputIterator last)
Definition qset.h:26
iterator insert(T &&value)
Definition qset.h:157
value_type & reference
Definition qset.h:172
QSet< T > & operator+=(const T &value)
Definition qset.h:189
const_iterator constFind(const T &value) const
Definition qset.h:161
friend QSet operator|(const QSet &lhs, const QSet &rhs)
Definition qset.h:193
QSet< T > & operator<<(const T &value)
Definition qset.h:182
QSet< T > & operator&=(const T &value)
Definition qset.h:186
iterator insert(const_iterator, const T &value)
Definition qset.h:179
iterator find(const T &value)
Definition qset.h:159
qptrdiff difference_type
Definition qset.h:174
friend QSet operator-(QSet &&lhs, const QSet &rhs)
Definition qset.h:203
QSet< T > & unite(const QSet< T > &other)
Definition qset.h:229
QSet< T > & operator&=(const QSet< T > &other)
Definition qset.h:185
friend QSet operator+(QSet &&lhs, const QSet &rhs)
Definition qset.h:200
void reserve(qsizetype size)
Definition qset.h:226
bool isDetached() const
Definition qset.h:59
const_iterator begin() const noexcept
Definition qset.h:137
bool intersects(const QSet< T > &other) const
Definition qset.h:262
const_iterator find(const T &value) const
Definition qset.h:160
bool contains(const T &value) const
Definition qset.h:71
qsizetype capacity() const
Definition qset.h:54
const_iterator cbegin() const noexcept
Definition qset.h:138
friend QSet operator&(QSet &&lhs, const QSet &rhs)
Definition qset.h:197
iterator insert(const T &value)
Definition qset.h:155
qsizetype removeIf(Pred predicate)
Definition qset.h:66
friend QSet operator-(const QSet &lhs, const QSet &rhs)
Definition qset.h:202
QSet(std::initializer_list< T > list)
Definition qset.h:23
bool empty() const
Definition qset.h:177
QSet< T > & subtract(const QSet< T > &other)
Definition qset.h:280
#define this
Definition dialogs.cpp:9
b clear()
cache insert(employee->id(), employee)
const auto predicate
Combined button and popup list for selecting options.
std::enable_if_t< std::conjunction_v< QTypeTraits::has_operator_equal_container< Container, T >... >, bool > compare_eq_result_container
Definition qtypeinfo.h:337
qsizetype qset_erase_if(QSet< T > &set, Predicate &pred)
typename std::enable_if< std::is_convertible< typename std::iterator_traits< Iterator >::iterator_category, std::input_iterator_tag >::value, bool >::type IfIsInputIterator
void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:788
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHashRangeCommutative(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
#define Q_DECLARE_SEQUENTIAL_ITERATOR(C)
Definition qiterator.h:21
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLuint GLuint end
GLint first
GLfloat n
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
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:217
ptrdiff_t qptrdiff
Definition qtypes.h:164
ptrdiff_t qsizetype
Definition qtypes.h:165
QList< int > list
[14]
QFuture< QSet< QChar > > set
[10]
settings remove("monkey")
QSharedPointer< T > other(t)
[5]
this swap(other)