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#ifdef Q_COMPILER_INITIALIZER_LISTS
45#include <initializer_list>
46#endif
47
48#include <iterator>
49
50QT_BEGIN_NAMESPACE
51
52
53template <class T>
54class QSet
55{
56 typedef QHash<T, QHashDummyValue> Hash;
57
58public:
59 inline QSet() Q_DECL_NOTHROW {}
60#ifdef Q_COMPILER_INITIALIZER_LISTS
61 inline QSet(std::initializer_list<T> list)
62 {
63 reserve(int(list.size()));
64 for (typename std::initializer_list<T>::const_iterator it = list.begin(); it != list.end(); ++it)
65 insert(*it);
66 }
67#endif
68 // compiler-generated copy/move ctor/assignment operators are fine!
69 // compiler-generated destructor is fine!
70
71 inline void swap(QSet<T> &other) Q_DECL_NOTHROW { q_hash.swap(other.q_hash); }
72
73 inline bool operator==(const QSet<T> &other) const
74 { return q_hash == other.q_hash; }
75 inline bool operator!=(const QSet<T> &other) const
76 { return q_hash != other.q_hash; }
77
78 inline int size() const { return q_hash.size(); }
79
80 inline bool isEmpty() const { return q_hash.isEmpty(); }
81
82 inline int capacity() const { return q_hash.capacity(); }
83 inline void reserve(int size);
84 inline void squeeze() { q_hash.squeeze(); }
85
86 inline void detach() { q_hash.detach(); }
87 inline bool isDetached() const { return q_hash.isDetached(); }
88#if !defined(QT_NO_UNSHARABLE_CONTAINERS)
89 inline void setSharable(bool sharable) { q_hash.setSharable(sharable); }
90#endif
91
92 inline void clear() { q_hash.clear(); }
93
94 inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
95
96 inline bool contains(const T &value) const { return q_hash.contains(value); }
97
98 bool contains(const QSet<T> &set) const;
99
100 class const_iterator;
101
102 class iterator
103 {
104 typedef QHash<T, QHashDummyValue> Hash;
105 typename Hash::iterator i;
106 friend class const_iterator;
107 friend class QSet<T>;
108
109 public:
110 typedef std::bidirectional_iterator_tag iterator_category;
111 typedef qptrdiff difference_type;
112 typedef T value_type;
113 typedef const T *pointer;
114 typedef const T &reference;
115
116 inline iterator() {}
117 inline iterator(typename Hash::iterator o) : i(o) {}
118 inline iterator(const iterator &o) : i(o.i) {}
119 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
120 inline const T &operator*() const { return i.key(); }
121 inline const T *operator->() const { return &i.key(); }
122 inline bool operator==(const iterator &o) const { return i == o.i; }
123 inline bool operator!=(const iterator &o) const { return i != o.i; }
124 inline bool operator==(const const_iterator &o) const
125 { return i == o.i; }
126 inline bool operator!=(const const_iterator &o) const
127 { return i != o.i; }
128 inline iterator &operator++() { ++i; return *this; }
129 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
130 inline iterator &operator--() { --i; return *this; }
131 inline iterator operator--(int) { iterator r = *this; --i; return r; }
132 inline iterator operator+(int j) const { return i + j; }
133 inline iterator operator-(int j) const { return i - j; }
134 friend inline iterator operator+(int j, iterator k) { return k + j; }
135 inline iterator &operator+=(int j) { i += j; return *this; }
136 inline iterator &operator-=(int j) { i -= j; return *this; }
137 };
138
139 class const_iterator
140 {
141 typedef QHash<T, QHashDummyValue> Hash;
142 typename Hash::const_iterator i;
143 friend class iterator;
144 friend class QSet<T>;
145
146 public:
147 typedef std::bidirectional_iterator_tag iterator_category;
148 typedef qptrdiff difference_type;
149 typedef T value_type;
150 typedef const T *pointer;
151 typedef const T &reference;
152
153 inline const_iterator() {}
154 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
155 inline const_iterator(const const_iterator &o) : i(o.i) {}
156 inline const_iterator(const iterator &o)
157 : i(o.i) {}
158 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
159 inline const T &operator*() const { return i.key(); }
160 inline const T *operator->() const { return &i.key(); }
161 inline bool operator==(const const_iterator &o) const { return i == o.i; }
162 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
163 inline const_iterator &operator++() { ++i; return *this; }
164 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
165 inline const_iterator &operator--() { --i; return *this; }
166 inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; }
167 inline const_iterator operator+(int j) const { return i + j; }
168 inline const_iterator operator-(int j) const { return i - j; }
169 friend inline const_iterator operator+(int j, const_iterator k) { return k + j; }
170 inline const_iterator &operator+=(int j) { i += j; return *this; }
171 inline const_iterator &operator-=(int j) { i -= j; return *this; }
172 };
173
174 // STL style
175 typedef std::reverse_iterator<iterator> reverse_iterator;
176 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
177
178 inline iterator begin() { return q_hash.begin(); }
179 inline const_iterator begin() const Q_DECL_NOTHROW { return q_hash.begin(); }
180 inline const_iterator cbegin() const Q_DECL_NOTHROW { return q_hash.begin(); }
181 inline const_iterator constBegin() const Q_DECL_NOTHROW { return q_hash.constBegin(); }
182 inline iterator end() { return q_hash.end(); }
183 inline const_iterator end() const Q_DECL_NOTHROW { return q_hash.end(); }
184 inline const_iterator cend() const Q_DECL_NOTHROW { return q_hash.end(); }
185 inline const_iterator constEnd() const Q_DECL_NOTHROW { return q_hash.constEnd(); }
186
187 reverse_iterator rbegin() { return reverse_iterator(end()); }
188 reverse_iterator rend() { return reverse_iterator(begin()); }
189 const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
190 const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
191 const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
192 const_reverse_iterator crend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
193
194 iterator erase(iterator i)
195 { return erase(m2c(i)); }
196 iterator erase(const_iterator i)
197 {
198 Q_ASSERT_X(isValidIterator(i), "QSet::erase", "The specified const_iterator argument 'i' is invalid");
199 return q_hash.erase(reinterpret_cast<typename Hash::const_iterator &>(i));
200 }
201
202 // more Qt
203 typedef iterator Iterator;
204 typedef const_iterator ConstIterator;
205 inline int count() const { return q_hash.count(); }
206 inline iterator insert(const T &value)
207 { return static_cast<typename Hash::iterator>(q_hash.insert(value, QHashDummyValue())); }
208 iterator find(const T &value) { return q_hash.find(value); }
209 const_iterator find(const T &value) const { return q_hash.find(value); }
210 inline const_iterator constFind(const T &value) const { return find(value); }
211 QSet<T> &unite(const QSet<T> &other);
212 QSet<T> &intersect(const QSet<T> &other);
213 bool intersects(const QSet<T> &other) const;
214 QSet<T> &subtract(const QSet<T> &other);
215
216 // STL compatibility
217 typedef T key_type;
218 typedef T value_type;
219 typedef value_type *pointer;
220 typedef const value_type *const_pointer;
221 typedef value_type &reference;
222 typedef const value_type &const_reference;
223 typedef qptrdiff difference_type;
224 typedef int size_type;
225
226 inline bool empty() const { return isEmpty(); }
227 // comfort
228 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
229 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
230 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
231 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
232 inline QSet<T> &operator&=(const T &value)
233 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
234 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
235 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
236 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
237 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
238 inline QSet<T> operator|(const QSet<T> &other) const
239 { QSet<T> result = *this; result |= other; return result; }
240 inline QSet<T> operator&(const QSet<T> &other) const
241 { QSet<T> result = *this; result &= other; return result; }
242 inline QSet<T> operator+(const QSet<T> &other) const
243 { QSet<T> result = *this; result += other; return result; }
244 inline QSet<T> operator-(const QSet<T> &other) const
245 { QSet<T> result = *this; result -= other; return result; }
246
247 QList<T> toList() const;
248 inline QList<T> values() const { return toList(); }
249
250 static QSet<T> fromList(const QList<T> &list);
251
252private:
253 Hash q_hash;
254
255 static const_iterator m2c(iterator it) Q_DECL_NOTHROW
256 { return const_iterator(typename Hash::const_iterator(it.i.i)); }
257
258 bool isValidIterator(const iterator &i) const
259 {
260 return q_hash.isValidIterator(reinterpret_cast<const typename Hash::iterator&>(i));
261 }
262 bool isValidIterator(const const_iterator &i) const Q_DECL_NOTHROW
263 {
264 return q_hash.isValidIterator(reinterpret_cast<const typename Hash::const_iterator&>(i));
265 }
266};
267
268template <typename T>
269uint qHash(const QSet<T> &key, uint seed = 0)
270Q_DECL_NOEXCEPT_EXPR(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
271{
272 return qHashRangeCommutative(key.begin(), key.end(), seed);
273}
274
275// inline function implementations
276
277template <class T>
278Q_INLINE_TEMPLATE void QSet<T>::reserve(int asize) { q_hash.reserve(asize); }
279
280template <class T>
281Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
282{
283 QSet<T> copy(other);
284 typename QSet<T>::const_iterator i = copy.constEnd();
285 while (i != copy.constBegin()) {
286 --i;
287 insert(*i);
288 }
289 return *this;
290}
291
292template <class T>
293Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
294{
295 QSet<T> copy1;
296 QSet<T> copy2;
297 if (size() <= other.size()) {
298 copy1 = *this;
299 copy2 = other;
300 } else {
301 copy1 = other;
302 copy2 = *this;
303 *this = copy1;
304 }
305 typename QSet<T>::const_iterator i = copy1.constEnd();
306 while (i != copy1.constBegin()) {
307 --i;
308 if (!copy2.contains(*i))
309 remove(*i);
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 (&other == this) {
346 clear();
347 } else {
348 auto i = other.constEnd();
349 while (i != other.constBegin()) {
350 --i;
351 remove(*i);
352 }
353 }
354 return *this;
355}
356
357template <class T>
358Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
359{
360 typename QSet<T>::const_iterator i = other.constBegin();
361 while (i != other.constEnd()) {
362 if (!contains(*i))
363 return false;
364 ++i;
365 }
366 return true;
367}
368
369template <typename T>
370Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::toList() const
371{
372 QList<T> result;
373 result.reserve(size());
374 typename QSet<T>::const_iterator i = constBegin();
375 while (i != constEnd()) {
376 result.append(*i);
377 ++i;
378 }
379 return result;
380}
381
382template <typename T>
383Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const
384{
385 QSet<T> result;
386 result.reserve(size());
387 for (int i = 0; i < size(); ++i)
388 result.insert(at(i));
389 return result;
390}
391
392template <typename T>
393QSet<T> QSet<T>::fromList(const QList<T> &list)
394{
395 return list.toSet();
396}
397
398template <typename T>
399QList<T> QList<T>::fromSet(const QSet<T> &set)
400{
401 return set.toList();
402}
403
404Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
405
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
438QT_END_NAMESPACE
439
440#endif // QSET_H
441