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 template <typename U = T>
75 QTypeTraits::compare_eq_result<U> operator==(const QSet<T> &other) const
76 { return q_hash == other.q_hash; }
77 template <typename U = T>
78 QTypeTraits::compare_eq_result<U> operator!=(const QSet<T> &other) const
79 { return q_hash != other.q_hash; }
80
81 inline qsizetype size() const { return q_hash.size(); }
82
83 inline bool isEmpty() const { return q_hash.isEmpty(); }
84
85 inline qsizetype capacity() const { return q_hash.capacity(); }
86 inline void reserve(qsizetype size);
87 inline void squeeze() { q_hash.squeeze(); }
88
89 inline void detach() { q_hash.detach(); }
90 inline bool isDetached() const { return q_hash.isDetached(); }
91
92 inline void clear() { q_hash.clear(); }
93
94 inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
95
96 template <typename Pred>
97 inline qsizetype removeIf(Pred predicate)
98 {
99 return QtPrivate::qset_erase_if(*this, predicate);
100 }
101
102 inline bool contains(const T &value) const { return q_hash.contains(value); }
103
104 bool contains(const QSet<T> &set) const;
105
106 class const_iterator;
107
108 class iterator
109 {
110 typedef QHash<T, QHashDummyValue> Hash;
111 typename Hash::iterator i;
112 friend class const_iterator;
113 friend class QSet<T>;
114
115 public:
116 typedef std::forward_iterator_tag iterator_category;
117 typedef qptrdiff difference_type;
118 typedef T value_type;
119 typedef const T *pointer;
120 typedef const T &reference;
121
122 inline iterator() {}
123 inline iterator(typename Hash::iterator o) : i(o) {}
124 inline iterator(const iterator &o) : i(o.i) {}
125 inline iterator &operator=(const iterator &o) { i = o.i; return *this; }
126 inline const T &operator*() const { return i.key(); }
127 inline const T *operator->() const { return &i.key(); }
128 inline bool operator==(const iterator &o) const { return i == o.i; }
129 inline bool operator!=(const iterator &o) const { return i != o.i; }
130 inline bool operator==(const const_iterator &o) const
131 { return i == o.i; }
132 inline bool operator!=(const const_iterator &o) const
133 { return i != o.i; }
134 inline iterator &operator++() { ++i; return *this; }
135 inline iterator operator++(int) { iterator r = *this; ++i; return r; }
136 };
137
138 class const_iterator
139 {
140 typedef QHash<T, QHashDummyValue> Hash;
141 typename Hash::const_iterator i;
142 friend class iterator;
143 friend class QSet<T>;
144
145 public:
146 typedef std::forward_iterator_tag iterator_category;
147 typedef qptrdiff difference_type;
148 typedef T value_type;
149 typedef const T *pointer;
150 typedef const T &reference;
151
152 inline const_iterator() {}
153 inline const_iterator(typename Hash::const_iterator o) : i(o) {}
154 inline const_iterator(const const_iterator &o) : i(o.i) {}
155 inline const_iterator(const iterator &o)
156 : i(o.i) {}
157 inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
158 inline const T &operator*() const { return i.key(); }
159 inline const T *operator->() const { return &i.key(); }
160 inline bool operator==(const const_iterator &o) const { return i == o.i; }
161 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
162 inline const_iterator &operator++() { ++i; return *this; }
163 inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
164 };
165
166 // STL style
167 inline iterator begin() { return q_hash.begin(); }
168 inline const_iterator begin() const noexcept { return q_hash.begin(); }
169 inline const_iterator cbegin() const noexcept { return q_hash.begin(); }
170 inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); }
171 inline iterator end() { return q_hash.end(); }
172 inline const_iterator end() const noexcept { return q_hash.end(); }
173 inline const_iterator cend() const noexcept { return q_hash.end(); }
174 inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); }
175
176 iterator erase(const_iterator i)
177 {
178 Q_ASSERT(i != constEnd());
179 return q_hash.erase(i.i);
180 }
181
182 // more Qt
183 typedef iterator Iterator;
184 typedef const_iterator ConstIterator;
185 inline qsizetype count() const { return q_hash.count(); }
186 inline iterator insert(const T &value)
187 { return q_hash.insert(value, QHashDummyValue()); }
188 inline iterator insert(T &&value)
189 { return q_hash.emplace(std::move(value), QHashDummyValue()); }
190 iterator find(const T &value) { return q_hash.find(value); }
191 const_iterator find(const T &value) const { return q_hash.find(value); }
192 inline const_iterator constFind(const T &value) const { return find(value); }
193 QSet<T> &unite(const QSet<T> &other);
194 QSet<T> &intersect(const QSet<T> &other);
195 bool intersects(const QSet<T> &other) const;
196 QSet<T> &subtract(const QSet<T> &other);
197
198 // STL compatibility
199 typedef T key_type;
200 typedef T value_type;
201 typedef value_type *pointer;
202 typedef const value_type *const_pointer;
203 typedef value_type &reference;
204 typedef const value_type &const_reference;
205 typedef qptrdiff difference_type;
206 typedef qsizetype size_type;
207
208 inline bool empty() const { return isEmpty(); }
209
210 iterator insert(const_iterator, const T &value) { return insert(value); }
211
212 // comfort
213 inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
214 inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
215 inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
216 inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
217 inline QSet<T> &operator&=(const T &value)
218 { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
219 inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
220 inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
221 inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
222 inline QSet<T> &operator-=(const T &value) { remove(value); return *this; }
223 inline QSet<T> operator|(const QSet<T> &other) const
224 { QSet<T> result = *this; result |= other; return result; }
225 inline QSet<T> operator&(const QSet<T> &other) const
226 { QSet<T> result = *this; result &= other; return result; }
227 inline QSet<T> operator+(const QSet<T> &other) const
228 { QSet<T> result = *this; result += other; return result; }
229 inline QSet<T> operator-(const QSet<T> &other) const
230 { QSet<T> result = *this; result -= other; return result; }
231
232 QList<T> values() const;
233
234private:
235 Hash q_hash;
236};
237
238#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
239template <typename InputIterator,
240 typename ValueType = typename std::iterator_traits<InputIterator>::value_type,
241 QtPrivate::IfIsInputIterator<InputIterator> = true>
242QSet(InputIterator, InputIterator) -> QSet<ValueType>;
243#endif
244
245template <typename T>
246size_t qHash(const QSet<T> &key, size_t seed = 0)
247noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed)))
248{
249 return qHashRangeCommutative(key.begin(), key.end(), seed);
250}
251
252// inline function implementations
253
254template <class T>
255Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); }
256
257template <class T>
258Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
259{
260 if (!q_hash.isSharedWith(other.q_hash)) {
261 for (const T &e : other)
262 insert(e);
263 }
264 return *this;
265}
266
267template <class T>
268Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
269{
270 QSet<T> copy1;
271 QSet<T> copy2;
272 if (size() <= other.size()) {
273 copy1 = *this;
274 copy2 = other;
275 } else {
276 copy1 = other;
277 copy2 = *this;
278 *this = copy1;
279 }
280 for (const auto &e : qAsConst(copy1)) {
281 if (!copy2.contains(e))
282 remove(e);
283 }
284 return *this;
285}
286
287template <class T>
288Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const
289{
290 const bool otherIsBigger = other.size() > size();
291 const QSet &smallestSet = otherIsBigger ? *this : other;
292 const QSet &biggestSet = otherIsBigger ? other : *this;
293 typename QSet::const_iterator i = smallestSet.cbegin();
294 typename QSet::const_iterator e = smallestSet.cend();
295
296 while (i != e) {
297 if (biggestSet.contains(*i))
298 return true;
299 ++i;
300 }
301
302 return false;
303}
304
305template <class T>
306Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
307{
308 if (q_hash.isSharedWith(other.q_hash)) {
309 clear();
310 } else {
311 for (const auto &e : other)
312 remove(e);
313 }
314 return *this;
315}
316
317template <class T>
318Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const
319{
320 typename QSet<T>::const_iterator i = other.constBegin();
321 while (i != other.constEnd()) {
322 if (!contains(*i))
323 return false;
324 ++i;
325 }
326 return true;
327}
328
329template <typename T>
330Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::values() const
331{
332 QList<T> result;
333 result.reserve(size());
334 typename QSet<T>::const_iterator i = constBegin();
335 while (i != constEnd()) {
336 result.append(*i);
337 ++i;
338 }
339 return result;
340}
341
342Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
343
344#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
345template <typename T>
346class QMutableSetIterator
347{
348 typedef typename QSet<T>::iterator iterator;
349 QSet<T> *c;
350 iterator i, n;
351 inline bool item_exists() const { return c->constEnd() != n; }
352
353public:
354 inline QMutableSetIterator(QSet<T> &container)
355 : c(&container)
356 { i = c->begin(); n = c->end(); }
357 inline QMutableSetIterator &operator=(QSet<T> &container)
358 { c = &container; i = c->begin(); n = c->end(); return *this; }
359 inline void toFront() { i = c->begin(); n = c->end(); }
360 inline void toBack() { i = c->end(); n = i; }
361 inline bool hasNext() const { return c->constEnd() != i; }
362 inline const T &next() { n = i++; return *n; }
363 inline const T &peekNext() const { return *i; }
364 inline void remove()
365 { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } }
366 inline const T &value() const { Q_ASSERT(item_exists()); return *n; }
367 inline bool findNext(const T &t)
368 { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; }
369};
370#endif // QT_NO_JAVA_STYLE_ITERATORS
371
372template <typename T, typename Predicate>
373qsizetype erase_if(QSet<T> &set, Predicate pred)
374{
375 return QtPrivate::qset_erase_if(set, pred);
376}
377
378QT_END_NAMESPACE
379
380#endif // QSET_H
381