Qt 6.x
The Qt SDK
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{
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 {
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 {
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); }
164 bool intersects(const QSet<T> &other) const;
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 inline QSet<T> operator|(const QSet<T> &other) const
193 { QSet<T> result = *this; result |= other; return result; }
194 inline QSet<T> operator&(const QSet<T> &other) const
195 { QSet<T> result = *this; result &= other; return result; }
196 inline QSet<T> operator+(const QSet<T> &other) const
197 { QSet<T> result = *this; result += other; return result; }
198 inline QSet<T> operator-(const QSet<T> &other) const
199 { QSet<T> result = *this; result -= other; return result; }
200
202
203private:
204 Hash q_hash;
205};
206
207template <typename InputIterator,
208 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
210QSet(InputIterator, InputIterator) -> QSet<ValueType>;
211
212template <typename T>
213size_t qHash(const QSet<T> &key, size_t seed = 0)
214noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
215{
216 return qHashRangeCommutative(key.begin(), key.end(), seed);
217}
218
219// inline function implementations
220
221template <class T>
222Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
223
224template <class T>
225Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
226{
227 if (!q_hash.isSharedWith(other.q_hash)) {
228 for (const T &e : other)
229 insert(e);
230 }
231 return *this;
232}
233
234template <class T>
235Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
236{
237 QSet<T> copy1;
238 QSet<T> copy2;
239 if (size() <= other.size()) {
240 copy1 = *this;
241 copy2 = other;
242 } else {
243 copy1 = other;
244 copy2 = *this;
245 *this = copy1;
246 }
247 for (const auto &e : std::as_const(copy1)) {
248 if (!copy2.contains(e))
249 remove(e);
250 }
251 return *this;
252}
253
254template <class T>
255Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
256{
257 const bool otherIsBigger = other.size() > size();
258 const QSet &smallestSet = otherIsBigger ? *this : other;
259 const QSet &biggestSet = otherIsBigger ? other : *this;
260 typename QSet::const_iterator i = smallestSet.cbegin();
261 typename QSet::const_iterator e = smallestSet.cend();
262
263 while (i != e) {
264 if (biggestSet.contains(*i))
265 return true;
266 ++i;
267 }
268
269 return false;
270}
271
272template <class T>
273Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
274{
275 if (q_hash.isSharedWith(other.q_hash)) {
276 clear();
277 } else {
278 for (const auto &e : other)
279 remove(e);
280 }
281 return *this;
282}
283
284template <class T>
285Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
286{
287 typename QSet<T>::const_iterator i = other.constBegin();
288 while (i != other.constEnd()) {
289 if (!contains(*i))
290 return false;
291 ++i;
292 }
293 return true;
294}
295
296template <typename T>
297Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const
298{
300 result.reserve(size());
302 while (i != constEnd()) {
303 result.append(*i);
304 ++i;
305 }
306 return result;
307}
308
310
311#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
312template <typename T>
314{
315 typedef typename QSet<T>::iterator iterator;
316 QSet<T> *c;
317 iterator i, n;
318 inline bool item_exists() const { return c->constEnd() != n; }
319
320public:
321 inline QMutableSetIterator(QSet<T> &container)
322 : c(&container)
323 { i = c->begin(); n = c->end(); }
325 { c = &container; i = c->begin(); n = c->end(); return *this; }
326 inline void toFront() { i = c->begin(); n = c->end(); }
327 inline void toBack() { i = c->end(); n = i; }
328 inline bool hasNext() const { return c->constEnd() != i; }
329 inline const T &next() { n = i++; return *n; }
330 inline const T &peekNext() const { return *i; }
331 inline void remove()
332 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
333 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
334 inline bool findNext(const T &t)
335 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
336};
337#endif // QT_NO_JAVA_STYLE_ITERATORS
338
339template <typename T, typename Predicate>
340qsizetype erase_if(QSet<T> &set, Predicate pred)
341{
342 return QtPrivate::qset_erase_if(set, pred);
343}
344
346
347#endif // QSET_H
\inmodule QtCore
Definition qhash.h:818
void squeeze()
Reduces the size of the QHash's internal hash table to save memory.
Definition qhash.h:939
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:956
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1202
qsizetype size() const noexcept
Returns the number of items in the hash.
Definition qhash.h:925
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:1209
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1258
iterator emplace(const Key &key, Args &&... args)
Definition qhash.h:1304
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:1205
void detach()
Definition qhash.h:945
bool isDetached() const noexcept
Definition qhash.h:946
iterator erase(const_iterator it)
Definition qhash.h:1223
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:991
void swap(QHash &other) noexcept
Definition qhash.h:898
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
friend class const_iterator
Definition qhash.h:1172
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:949
qsizetype capacity() const noexcept
Returns the number of buckets in the QHash's internal hash table.
Definition qhash.h:928
bool isEmpty() const noexcept
Returns true if the hash contains no items; otherwise returns false.
Definition qhash.h:926
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
Definition qlist.h:74
const T & next()
Definition qset.h:329
QMutableSetIterator & operator=(QSet< T > &container)
Definition qset.h:324
QMutableSetIterator(QSet< T > &container)
Definition qset.h:321
bool findNext(const T &t)
Definition qset.h:334
bool hasNext() const
Definition qset.h:328
const T & value() const
Definition qset.h:333
const T & peekNext() const
Definition qset.h:330
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:297
const value_type * const_pointer
Definition qset.h:171
QSet< T > & intersect(const QSet< T > &other)
Definition qset.h:235
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:285
QSet() noexcept
Definition qset.h:22
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
bool isEmpty() const
Definition qset.h:52
QSet< T > operator+(const QSet< T > &other) const
Definition qset.h:196
const_iterator constBegin() const noexcept
Definition qset.h:139
QSet< T > operator&(const QSet< T > &other) const
Definition qset.h:194
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
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
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
QSet< T > & unite(const QSet< T > &other)
Definition qset.h:225
QSet< T > & operator&=(const QSet< T > &other)
Definition qset.h:185
void reserve(qsizetype size)
Definition qset.h:222
QSet< T > operator|(const QSet< T > &other) const
Definition qset.h:192
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:255
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
iterator insert(const T &value)
Definition qset.h:155
qsizetype removeIf(Pred predicate)
Definition qset.h:66
QSet(std::initializer_list< T > list)
Definition qset.h:23
bool empty() const
Definition qset.h:177
QSet< T > operator-(const QSet< T > &other) const
Definition qset.h:198
QSet< T > & subtract(const QSet< T > &other)
Definition qset.h:273
#define this
Definition dialogs.cpp:9
b clear()
double e
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:320
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)
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:20
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)
size_t qHash(const QSet< T > &key, size_t seed=0) noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
Definition qset.h:213
qsizetype erase_if(QSet< T > &set, Predicate pred)
Definition qset.h:340
ptrdiff_t qptrdiff
Definition qtypes.h:69
ptrdiff_t qsizetype
Definition qtypes.h:70
QList< int > list
[14]
QFuture< QSet< QChar > > set
[10]
settings remove("monkey")
QSharedPointer< T > other(t)
[5]