1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSET_H
41#define QSET_H
42
43#include <QtCore/qhash.h>
44#include <QtCore/qcontainertools_impl.h>
45
46#include <initializer_list>
47#include <iterator>
48
49QT_BEGIN_NAMESPACE
50
51
52template <class T>
53class QSet
54{
55 typedef QHash<T, QHashDummyValue> Hash;
56
57public:
58 inline QSet() noexcept {}
59 inline QSet(std::initializer_list<T> list)
60 : QSet(list.begin(), list.end()) {}
61 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
62 inline QSet(InputIterator first, InputIterator last)
63 {
64 QtPrivate::reserveIfForwardIterator(this, first, last);
65 for (; first != last; ++first)
66 insert(*first);
67 }
68
69 // compiler-generated copy/move ctor/assignment operators are fine!
70 // compiler-generated destructor is fine!
71
72 inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); }
73
74 inline bool operator==(const QSet<T> &other) const
75 { return q_hash == other.q_hash; }
76 inline bool operator!=(const QSet<T> &other) const
77 { return q_hash != other.q_hash; }
78
79 inline int size() const { return q_hash.size(); }
80
81 inline bool isEmpty() const { return q_hash.isEmpty(); }
82
83 inline int capacity() const { return q_hash.capacity(); }
84 inline void reserve(int size);
85 inline void squeeze() { q_hash.squeeze(); }
86
87 inline void detach() { q_hash.detach(); }
88 inline bool isDetached() const { return q_hash.isDetached(); }
89#if !defined(QT_NO_UNSHARABLE_CONTAINERS)
90 inline void setSharable(bool sharable) { q_hash.setSharable(sharable); }
91#endif
92
93 inline void clear() { q_hash.clear(); }
94
95 inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
96
97 inline bool contains(const T &value) const { return q_hash.contains(value); }
98
99 bool contains(const QSet<T> &set) const;
100
101 class const_iterator;
102
103 class iterator
104 {
105 typedef QHash<T, QHashDummyValue> Hash;
106 typename Hash::iterator i;
107 friend class const_iterator;
108 friend class QSet<T>;
109
110 public:
111 typedef std::bidirectional_iterator_tag iterator_category;
112 typedef qptrdiff difference_type;
113 typedef T value_type;
114 typedef const T *pointer;
115 typedef const T &reference;
116
117 inline iterator() {}
118 inline iterator(typename Hash::iterator o) : i(o) {}
119 inline iterator(const iterator &o) : i(o.i) {}
120 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
121 inline const T &operator*() const { return i.key(); }
122 inline const T *operator->() const { return &i.key(); }
123 inline bool operator==(const iterator &o) const { return i == o.i; }
124 inline bool operator!=(const iterator &o) const { return i != o.i; }
125 inline bool operator==(const const_iterator &o) const
126 { return i == o.i; }
127 inline bool operator!=(const const_iterator &o) const
128 { return i != o.i; }
129 inline iterator &operator++() { ++i; return *this; }
130 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
131 inline iterator &operator--() { --i; return *this; }
132 inline iterator operator--(int) { iterator r = *this; --i; return r; }
133 inline iterator operator+(int j) const { return i + j; }
134 inline iterator operator-(int j) const { return i - j; }
135 friend inline iterator operator+(int j, iterator k) { return k + j; }
136 inline iterator &operator+=(int j) { i += j; return *this; }
137 inline iterator &operator-=(int j) { i -= j; return *this; }
138 };
139
140 class const_iterator
141 {
142 typedef QHash<T, QHashDummyValue> Hash;
143 typename Hash::const_iterator i;
144 friend class iterator;
145 friend class QSet<T>;
146
147 public:
148 typedef std::bidirectional_iterator_tag iterator_category;
149 typedef qptrdiff difference_type;
150 typedef T value_type;
151 typedef const T *pointer;
152 typedef const T &reference;
153
154 inline const_iterator() {}
155 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
156 inline const_iterator(const const_iterator &o) : i(o.i) {}
157 inline const_iterator(const iterator &o)
158 : i(o.i) {}
159 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
160 inline const T &operator*() const { return i.key(); }
161 inline const T *operator->() const { return &i.key(); }
162 inline bool operator==(const const_iterator &o) const { return i == o.i; }
163 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
164 inline const_iterator &operator++() { ++i; return *this; }
165 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
166 inline const_iterator &operator--() { --i; return *this; }
167 inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; }
168 inline const_iterator operator+(int j) const { return i + j; }
169 inline const_iterator operator-(int j) const { return i - j; }
170 friend inline const_iterator operator+(int j, const_iterator k) { return k + j; }
171 inline const_iterator &operator+=(int j) { i += j; return *this; }
172 inline const_iterator &operator-=(int j) { i -= j; return *this; }
173 };
174
175 // STL style
176 typedef std::reverse_iterator<iterator> reverse_iterator;
177 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
178
179 inline iterator begin() { return q_hash.begin(); }
180 inline const_iterator begin() const noexcept { return q_hash.begin(); }
181 inline const_iterator cbegin() const noexcept { return q_hash.begin(); }
182 inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); }
183 inline iterator end() { return q_hash.end(); }
184 inline const_iterator end() const noexcept { return q_hash.end(); }
185 inline const_iterator cend() const noexcept { return q_hash.end(); }
186 inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); }
187
188 reverse_iterator rbegin() { return reverse_iterator(end()); }
189 reverse_iterator rend() { return reverse_iterator(begin()); }
190 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
191 const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
192 const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
193 const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
194
195 iterator erase(iterator i)
196 { return erase(m2c(i)); }
197 iterator erase(const_iterator i)
198 {
199 Q_ASSERT_X(isValidIterator(i), "QSet::erase", "The specified const_iterator argument 'i' is invalid");
200 return q_hash.erase(reinterpret_cast<typename Hash::const_iterator &>(i));
201 }
202
203 // more Qt
204 typedef iterator Iterator;
205 typedef const_iterator ConstIterator;
206 inline int count() const { return q_hash.count(); }
207 inline iterator insert(const T &value)
208 { return static_cast<typename Hash::iterator>(q_hash.insert(value, QHashDummyValue())); }
209 iterator find(const T &value) { return q_hash.find(value); }
210 const_iterator find(const T &value) const { return q_hash.find(value); }
211 inline const_iterator constFind(const T &value) const { return find(value); }
212 QSet<T> &unite(const QSet<T> &other);
213 QSet<T> &intersect(const QSet<T> &other);
214 bool intersects(const QSet<T> &other) const;
215 QSet<T> &subtract(const QSet<T> &other);
216
217 // STL compatibility
218 typedef T key_type;
219 typedef T value_type;
220 typedef value_type *pointer;
221 typedef const value_type *const_pointer;
222 typedef value_type &reference;
223 typedef const value_type &const_reference;
224 typedef qptrdiff difference_type;
225 typedef int size_type;
226
227 inline bool empty() const { return isEmpty(); }
228 // comfort
229 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
230 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
231 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
232 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
233 inline QSet<T> &operator&=(const T &value)
234 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
235 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
236 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
237 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
238 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
239 inline QSet<T> operator|(const QSet<T> &other) const
240 { QSet<T> result = *this; result |= other; return result; }
241 inline QSet<T> operator&(const QSet<T> &other) const
242 { QSet<T> result = *this; result &= other; return result; }
243 inline QSet<T> operator+(const QSet<T> &other) const
244 { QSet<T> result = *this; result += other; return result; }
245 inline QSet<T> operator-(const QSet<T> &other) const
246 { QSet<T> result = *this; result -= other; return result; }
247
248 QList<T> values() const;
249#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
250 Q_DECL_DEPRECATED_X("Use values() instead.")
251 QList<T> toList() const { return values(); }
252 Q_DECL_DEPRECATED_X("Use QSet<T>(list.begin(), list.end()) instead.")
253 static QSet<T> fromList(const QList<T> &list);
254#endif
255
256private:
257 Hash q_hash;
258
259 static const_iterator m2c(iterator it) noexcept
260 { return const_iterator(typename Hash::const_iterator(it.i.i)); }
261
262 bool isValidIterator(const iterator &i) const
263 {
264 return q_hash.isValidIterator(reinterpret_cast<const typename Hash::iterator&>(i));
265 }
266 bool isValidIterator(const const_iterator &i) const noexcept
267 {
268 return q_hash.isValidIterator(reinterpret_cast<const typename Hash::const_iterator&>(i));
269 }
270};
271
272template <typename T>
273uint qHash(const QSet<T> &key, uint seed = 0)
274noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
275{
276 return qHashRangeCommutative(key.begin(), key.end(), seed);
277}
278
279// inline function implementations
280
281template <class T>
282Q_INLINE_TEMPLATE void QSet<T>::reserve(int asize) { q_hash.reserve(asize); }
283
284template <class T>
285Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
286{
287 if (!q_hash.isSharedWith(other.q_hash)) {
288 for (const T &e : other)
289 insert(e);
290 }
291 return *this;
292}
293
294template <class T>
295Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
296{
297 QSet<T> copy1;
298 QSet<T> copy2;
299 if (size() <= other.size()) {
300 copy1 = *this;
301 copy2 = other;
302 } else {
303 copy1 = other;
304 copy2 = *this;
305 *this = copy1;
306 }
307 for (const auto &e : qAsConst(copy1)) {
308 if (!copy2.contains(e))
309 remove(e);
310 }
311 return *this;
312}
313
314template <class T>
315Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
316{
317 const bool otherIsBigger = other.size() > size();
318 const QSet &smallestSet = otherIsBigger ? *this : other;
319 const QSet &biggestSet = otherIsBigger ? other : *this;
320 const bool equalSeeds = q_hash.d->seed == other.q_hash.d->seed;
321 typename QSet::const_iterator i = smallestSet.cbegin();
322 typename QSet::const_iterator e = smallestSet.cend();
323
324 if (Q_LIKELY(equalSeeds)) {
325 // If seeds are equal we take the fast path so no hash is recalculated.
326 while (i != e) {
327 if (*biggestSet.q_hash.findNode(*i, i.i.i->h) != biggestSet.q_hash.e)
328 return true;
329 ++i;
330 }
331 } else {
332 while (i != e) {
333 if (biggestSet.contains(*i))
334 return true;
335 ++i;
336 }
337 }
338
339 return false;
340}
341
342template <class T>
343Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
344{
345 if (q_hash.isSharedWith(other.q_hash)) {
346 clear();
347 } else {
348 for (const auto &e : other)
349 remove(e);
350 }
351 return *this;
352}
353
354template <class T>
355Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
356{
357 typename QSet<T>::const_iterator i = other.constBegin();
358 while (i != other.constEnd()) {
359 if (!contains(*i))
360 return false;
361 ++i;
362 }
363 return true;
364}
365
366template <typename T>
367Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const
368{
369 QList<T> result;
370 result.reserve(size());
371 typename QSet<T>::const_iterator i = constBegin();
372 while (i != constEnd()) {
373 result.append(*i);
374 ++i;
375 }
376 return result;
377}
378
379#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
380template <typename T>
381Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const
382{
383 QSet<T> result;
384 result.reserve(size());
385 for (int i = 0; i < size(); ++i)
386 result.insert(at(i));
387 return result;
388}
389
390template <typename T>
391QSet<T> QSet<T>::fromList(const QList<T> &list)
392{
393 return list.toSet();
394}
395
396template <typename T>
397QList<T> QList<T>::fromSet(const QSet<T> &set)
398{
399 return set.toList();
400}
401#endif
402
403Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
404
405#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
406template <typename T>
407class QMutableSetIterator
408{
409 typedef typename QSet<T>::iterator iterator;
410 QSet<T> *c;
411 iterator i, n;
412 inline bool item_exists() const { return c->constEnd() != n; }
413
414public:
415 inline QMutableSetIterator(QSet<T> &container)
416 : c(&container)
417 { i = c->begin(); n = c->end(); }
418 inline QMutableSetIterator &operator=(QSet<T> &container)
419 { c = &container; i = c->begin(); n = c->end(); return *this; }
420 inline void toFront() { i = c->begin(); n = c->end(); }
421 inline void toBack() { i = c->end(); n = i; }
422 inline bool hasNext() const { return c->constEnd() != i; }
423 inline const T &next() { n = i++; return *n; }
424 inline const T &peekNext() const { return *i; }
425 inline bool hasPrevious() const { return c->constBegin() != i; }
426 inline const T &previous() { n = --i; return *n; }
427 inline const T &peekPrevious() const { iterator p = i; return *--p; }
428 inline void remove()
429 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
430 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
431 inline bool findNext(const T &t)
432 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
433 inline bool findPrevious(const T &t)
434 { while (c->constBegin() != i) if (*(n = --i) == t) return true;
435 n = c->end(); return false; }
436};
437#endif // QT_NO_JAVA_STYLE_ITERATORS
438
439QT_END_NAMESPACE
440
441#endif // QSET_H
442