| 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 | #include <QtCore/qttypetraits.h> |
| 10 | |
| 11 | #include <initializer_list> |
| 12 | #include <iterator> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | |
| 17 | template <class T> |
| 18 | class QSet |
| 19 | { |
| 20 | typedef QHash<T, QHashDummyValue> Hash; |
| 21 | |
| 22 | public: |
| 23 | inline QSet() noexcept {} |
| 24 | inline QSet(std::initializer_list<T> list) |
| 25 | : QSet(list.begin(), list.end()) {} |
| 26 | template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> |
| 27 | inline QSet(InputIterator first, InputIterator last) |
| 28 | { |
| 29 | QtPrivate::reserveIfForwardIterator(this, first, last); |
| 30 | for (; first != last; ++first) |
| 31 | insert(*first); |
| 32 | } |
| 33 | |
| 34 | // compiler-generated copy/move ctor/assignment operators are fine! |
| 35 | // compiler-generated destructor is fine! |
| 36 | |
| 37 | inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); } |
| 38 | |
| 39 | #ifndef Q_QDOC |
| 40 | private: |
| 41 | template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true> |
| 42 | friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept |
| 43 | { |
| 44 | return lhs.q_hash == rhs.q_hash; |
| 45 | } |
| 46 | QT_DECLARE_EQUALITY_OPERATORS_HELPER(QSet, QSet, /* non-constexpr */, noexcept, |
| 47 | template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>) |
| 48 | public: |
| 49 | #else |
| 50 | friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept; |
| 51 | friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept; |
| 52 | #endif |
| 53 | |
| 54 | inline qsizetype size() const { return q_hash.size(); } |
| 55 | |
| 56 | inline bool isEmpty() const { return q_hash.isEmpty(); } |
| 57 | |
| 58 | inline qsizetype capacity() const { return q_hash.capacity(); } |
| 59 | inline void reserve(qsizetype size); |
| 60 | inline void squeeze() { q_hash.squeeze(); } |
| 61 | |
| 62 | inline void detach() { q_hash.detach(); } |
| 63 | inline bool isDetached() const { return q_hash.isDetached(); } |
| 64 | |
| 65 | inline void clear() { q_hash.clear(); } |
| 66 | |
| 67 | bool remove(const T &value) { return q_hash.remove(value); } |
| 68 | |
| 69 | template <typename Pred> |
| 70 | inline qsizetype removeIf(Pred predicate) |
| 71 | { |
| 72 | return QtPrivate::qset_erase_if(*this, predicate); |
| 73 | } |
| 74 | |
| 75 | inline bool contains(const T &value) const { return q_hash.contains(value); } |
| 76 | |
| 77 | bool contains(const QSet<T> &set) const; |
| 78 | |
| 79 | class const_iterator; |
| 80 | |
| 81 | class iterator |
| 82 | { |
| 83 | typedef QHash<T, QHashDummyValue> Hash; |
| 84 | typename Hash::iterator i; |
| 85 | friend class const_iterator; |
| 86 | friend class QSet<T>; |
| 87 | |
| 88 | public: |
| 89 | typedef std::forward_iterator_tag iterator_category; |
| 90 | typedef qptrdiff difference_type; |
| 91 | typedef T value_type; |
| 92 | typedef const T *pointer; |
| 93 | typedef const T &reference; |
| 94 | |
| 95 | inline iterator() {} |
| 96 | inline iterator(typename Hash::iterator o) : i(o) {} |
| 97 | inline iterator(const iterator &o) : i(o.i) {} |
| 98 | inline iterator &operator=(const iterator &o) { i = o.i; return *this; } |
| 99 | inline const T &operator*() const { return i.key(); } |
| 100 | inline const T *operator->() const { return &i.key(); } |
| 101 | inline bool operator==(const iterator &o) const { return i == o.i; } |
| 102 | inline bool operator!=(const iterator &o) const { return i != o.i; } |
| 103 | inline bool operator==(const const_iterator &o) const |
| 104 | { return i == o.i; } |
| 105 | inline bool operator!=(const const_iterator &o) const |
| 106 | { return i != o.i; } |
| 107 | inline iterator &operator++() { ++i; return *this; } |
| 108 | inline iterator operator++(int) { iterator r = *this; ++i; return r; } |
| 109 | }; |
| 110 | |
| 111 | class const_iterator |
| 112 | { |
| 113 | typedef QHash<T, QHashDummyValue> Hash; |
| 114 | typename Hash::const_iterator i; |
| 115 | friend class iterator; |
| 116 | friend class QSet<T>; |
| 117 | |
| 118 | public: |
| 119 | typedef std::forward_iterator_tag iterator_category; |
| 120 | typedef qptrdiff difference_type; |
| 121 | typedef T value_type; |
| 122 | typedef const T *pointer; |
| 123 | typedef const T &reference; |
| 124 | |
| 125 | inline const_iterator() {} |
| 126 | inline const_iterator(typename Hash::const_iterator o) : i(o) {} |
| 127 | inline const_iterator(const const_iterator &o) : i(o.i) {} |
| 128 | inline const_iterator(const iterator &o) |
| 129 | : i(o.i) {} |
| 130 | inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; } |
| 131 | inline const T &operator*() const { return i.key(); } |
| 132 | inline const T *operator->() const { return &i.key(); } |
| 133 | inline bool operator==(const const_iterator &o) const { return i == o.i; } |
| 134 | inline bool operator!=(const const_iterator &o) const { return i != o.i; } |
| 135 | inline const_iterator &operator++() { ++i; return *this; } |
| 136 | inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; } |
| 137 | }; |
| 138 | |
| 139 | // STL style |
| 140 | inline iterator begin() { return q_hash.begin(); } |
| 141 | inline const_iterator begin() const noexcept { return q_hash.begin(); } |
| 142 | inline const_iterator cbegin() const noexcept { return q_hash.begin(); } |
| 143 | inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); } |
| 144 | inline iterator end() { return q_hash.end(); } |
| 145 | inline const_iterator end() const noexcept { return q_hash.end(); } |
| 146 | inline const_iterator cend() const noexcept { return q_hash.end(); } |
| 147 | inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); } |
| 148 | |
| 149 | iterator erase(const_iterator i) |
| 150 | { |
| 151 | Q_ASSERT(i != constEnd()); |
| 152 | return q_hash.erase(i.i); |
| 153 | } |
| 154 | |
| 155 | // more Qt |
| 156 | typedef iterator Iterator; |
| 157 | typedef const_iterator ConstIterator; |
| 158 | inline qsizetype count() const { return q_hash.size(); } |
| 159 | inline iterator insert(const T &value) |
| 160 | { return q_hash.insert(value, QHashDummyValue()); } |
| 161 | inline iterator insert(T &&value) |
| 162 | { return q_hash.emplace(std::move(value), QHashDummyValue()); } |
| 163 | iterator find(const T &value) { return q_hash.find(value); } |
| 164 | const_iterator find(const T &value) const { return q_hash.find(value); } |
| 165 | inline const_iterator constFind(const T &value) const { return find(value); } |
| 166 | QSet<T> &unite(const QSet<T> &other); |
| 167 | QSet &unite(QSet &&other); |
| 168 | QSet<T> &intersect(const QSet<T> &other); |
| 169 | bool intersects(const QSet<T> &other) const; |
| 170 | QSet<T> &subtract(const QSet<T> &other); |
| 171 | |
| 172 | // STL compatibility |
| 173 | typedef T key_type; |
| 174 | typedef T value_type; |
| 175 | typedef value_type *pointer; |
| 176 | typedef const value_type *const_pointer; |
| 177 | typedef value_type &reference; |
| 178 | typedef const value_type &const_reference; |
| 179 | typedef qptrdiff difference_type; |
| 180 | typedef qsizetype size_type; |
| 181 | |
| 182 | inline bool empty() const { return isEmpty(); } |
| 183 | |
| 184 | iterator insert(const_iterator, const T &value) { return insert(value); } |
| 185 | |
| 186 | // comfort |
| 187 | inline QSet<T> &operator<<(const T &value) { insert(value); return *this; } |
| 188 | inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; } |
| 189 | QSet &operator|=(QSet &&other) { return unite(std::move(other)); } |
| 190 | inline QSet<T> &operator|=(const T &value) { insert(value); return *this; } |
| 191 | inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; } |
| 192 | inline QSet<T> &operator&=(const T &value) |
| 193 | { QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); } |
| 194 | inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; } |
| 195 | QSet &operator+=(QSet &&other) { return unite(std::move(other)); } |
| 196 | inline QSet<T> &operator+=(const T &value) { insert(value); return *this; } |
| 197 | inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; } |
| 198 | inline QSet<T> &operator-=(const T &value) { remove(value); return *this; } |
| 199 | |
| 200 | friend QSet operator|(const QSet &lhs, const QSet &rhs) { return QSet(lhs) |= rhs; } |
| 201 | friend QSet operator|(QSet &&lhs, const QSet &rhs) { lhs |= rhs; return std::move(lhs); } |
| 202 | friend QSet operator|(const QSet &lhs, QSet &&rhs) { return lhs |= std::move(rhs); } |
| 203 | friend QSet operator|(QSet &&lhs, QSet &&rhs) { return std::move(lhs) |= std::move(rhs); } |
| 204 | |
| 205 | friend QSet operator&(const QSet &lhs, const QSet &rhs) { return QSet(lhs) &= rhs; } |
| 206 | friend QSet operator&(QSet &&lhs, const QSet &rhs) { lhs &= rhs; return std::move(lhs); } |
| 207 | |
| 208 | friend QSet operator+(const QSet &lhs, const QSet &rhs) { return QSet(lhs) += rhs; } |
| 209 | friend QSet operator+(QSet &&lhs, const QSet &rhs) { lhs += rhs; return std::move(lhs); } |
| 210 | friend QSet operator+(const QSet &lhs, QSet &&rhs) { return QSet(lhs) += std::move(rhs); } |
| 211 | friend QSet operator+(QSet &&lhs, QSet &&rhs) { return std::move(lhs) += std::move(rhs); } |
| 212 | |
| 213 | friend QSet operator-(const QSet &lhs, const QSet &rhs) { return QSet(lhs) -= rhs; } |
| 214 | friend QSet operator-(QSet &&lhs, const QSet &rhs) { lhs -= rhs; return std::move(lhs); } |
| 215 | |
| 216 | inline QList<T> values() const; |
| 217 | |
| 218 | private: |
| 219 | static inline QSet intersected_helper(const QSet &lhs, const QSet &rhs); |
| 220 | |
| 221 | template <typename E> |
| 222 | void _emplace_or_overwrite(E &&e); |
| 223 | |
| 224 | Hash q_hash; |
| 225 | }; |
| 226 | |
| 227 | template <typename InputIterator, |
| 228 | typename ValueType = typename std::iterator_traits<InputIterator>::value_type, |
| 229 | QtPrivate::IfIsInputIterator<InputIterator> = true> |
| 230 | QSet(InputIterator, InputIterator) -> QSet<ValueType>; |
| 231 | |
| 232 | template <typename T> |
| 233 | size_t qHash(const QSet<T> &key, size_t seed = 0) |
| 234 | noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed))) |
| 235 | { |
| 236 | return qHashRangeCommutative(key.begin(), key.end(), seed); |
| 237 | } |
| 238 | |
| 239 | // inline function implementations |
| 240 | |
| 241 | template <class T> |
| 242 | Q_INLINE_TEMPLATE void QSet<T>::reserve(qsizetype asize) { q_hash.reserve(asize); } |
| 243 | |
| 244 | template <class T> |
| 245 | Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other) |
| 246 | { |
| 247 | if (!q_hash.isSharedWith(other.q_hash)) { |
| 248 | for (const T &e : other) |
| 249 | insert(e); |
| 250 | } |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | template <class T> |
| 255 | Q_INLINE_TEMPLATE auto QSet<T>::unite(QSet &&other) -> QSet& |
| 256 | { |
| 257 | if (other.isDetached() && size() < other.size()) { |
| 258 | |
| 259 | // We can change the state of `other`, so take the smaller *this and |
| 260 | // insert it into the larger `other`, making sure we take equivalent |
| 261 | // elements from *this: |
| 262 | |
| 263 | swap(other); |
| 264 | |
| 265 | // Now: iterate over `other`, insert into *this, making sure we take |
| 266 | // equivalent elements from `other`: |
| 267 | |
| 268 | if (other.isDetached()) { // can move elements from `other` |
| 269 | for (auto &e : other) |
| 270 | _emplace_or_overwrite(std::move(e)); |
| 271 | } else { // need to copy elements from `other` |
| 272 | for (const auto &e : std::as_const(other)) |
| 273 | _emplace_or_overwrite(e); |
| 274 | } |
| 275 | |
| 276 | return *this; |
| 277 | } |
| 278 | |
| 279 | // in all other cases, the lvalue overload is not worse: |
| 280 | return unite(other); |
| 281 | } |
| 282 | |
| 283 | template <class T> |
| 284 | template <typename E> |
| 285 | Q_INLINE_TEMPLATE void QSet<T>::_emplace_or_overwrite(E &&e) |
| 286 | { |
| 287 | const auto r = q_hash.tryEmplace(std::forward<E>(e)); |
| 288 | if (!r.inserted) { |
| 289 | // QHash never overwrites the key, but that's what we need |
| 290 | // here, so do it using private QHash API: |
| 291 | // NB: `e` was _not_ moved from by tryEmplace()! |
| 292 | typename Hash::Data::Bucket(r.iterator.i).node()->key = std::forward<E>(e); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | template <class T> |
| 297 | Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other) |
| 298 | { |
| 299 | if (q_hash.isSharedWith(other.q_hash)) { |
| 300 | // nothing to do |
| 301 | } else if (isEmpty() || other.isEmpty()) { |
| 302 | // any set intersected with the empty set is the empty set |
| 303 | clear(); |
| 304 | } else if (q_hash.isDetached()) { |
| 305 | // do it in-place: |
| 306 | removeIf([&other] (const T &e) { return !other.contains(e); }); |
| 307 | } else { |
| 308 | // don't detach *this just to remove some items; create a new set |
| 309 | *this = intersected_helper(lhs: *this, rhs: other); |
| 310 | } |
| 311 | return *this; |
| 312 | } |
| 313 | |
| 314 | template <class T> |
| 315 | // static |
| 316 | auto QSet<T>::intersected_helper(const QSet &lhs, const QSet &rhs) -> QSet |
| 317 | { |
| 318 | QSet r; |
| 319 | |
| 320 | const auto l_size = lhs.size(); |
| 321 | const auto r_size = rhs.size(); |
| 322 | r.reserve(asize: (std::min)(l_size, r_size)); |
| 323 | |
| 324 | // Iterate the smaller of the two sets, but always take from lhs, for |
| 325 | // consistency with insert(): |
| 326 | |
| 327 | if (l_size <= r_size) { |
| 328 | // lhs is not larger |
| 329 | for (const auto &e : lhs) { |
| 330 | if (rhs.contains(e)) |
| 331 | r.insert(e); |
| 332 | } |
| 333 | } else { |
| 334 | // rhs is smaller |
| 335 | for (const auto &e : rhs) { |
| 336 | if (const auto it = lhs.find(e); it != lhs.end()) |
| 337 | r.insert(*it); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return r; |
| 342 | } |
| 343 | |
| 344 | template <class T> |
| 345 | Q_INLINE_TEMPLATE bool QSet<T>::intersects(const QSet<T> &other) const |
| 346 | { |
| 347 | const bool otherIsBigger = other.size() > size(); |
| 348 | const QSet &smallestSet = otherIsBigger ? *this : other; |
| 349 | const QSet &biggestSet = otherIsBigger ? other : *this; |
| 350 | typename QSet::const_iterator i = smallestSet.cbegin(); |
| 351 | typename QSet::const_iterator e = smallestSet.cend(); |
| 352 | |
| 353 | while (i != e) { |
| 354 | if (biggestSet.contains(*i)) |
| 355 | return true; |
| 356 | ++i; |
| 357 | } |
| 358 | |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | template <class T> |
| 363 | Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other) |
| 364 | { |
| 365 | if (q_hash.isSharedWith(other.q_hash)) { |
| 366 | clear(); |
| 367 | } else { |
| 368 | for (const auto &e : other) |
| 369 | remove(value: e); |
| 370 | } |
| 371 | return *this; |
| 372 | } |
| 373 | |
| 374 | template <class T> |
| 375 | Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const |
| 376 | { |
| 377 | typename QSet<T>::const_iterator i = other.constBegin(); |
| 378 | while (i != other.constEnd()) { |
| 379 | if (!contains(*i)) |
| 380 | return false; |
| 381 | ++i; |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | template <typename T> |
| 387 | QList<T> QSet<T>::values() const |
| 388 | { |
| 389 | QList<T> result; |
| 390 | result.reserve(size()); |
| 391 | typename QSet<T>::const_iterator i = constBegin(); |
| 392 | while (i != constEnd()) { |
| 393 | result.append(*i); |
| 394 | ++i; |
| 395 | } |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | Q_DECLARE_SEQUENTIAL_ITERATOR(Set) |
| 400 | |
| 401 | #if !defined(QT_NO_JAVA_STYLE_ITERATORS) |
| 402 | template <typename T> |
| 403 | class QMutableSetIterator |
| 404 | { |
| 405 | typedef typename QSet<T>::iterator iterator; |
| 406 | QSet<T> *c; |
| 407 | iterator i, n; |
| 408 | inline bool item_exists() const { return c->constEnd() != n; } |
| 409 | |
| 410 | public: |
| 411 | inline QMutableSetIterator(QSet<T> &container) |
| 412 | : c(&container) |
| 413 | { i = c->begin(); n = c->end(); } |
| 414 | inline QMutableSetIterator &operator=(QSet<T> &container) |
| 415 | { c = &container; i = c->begin(); n = c->end(); return *this; } |
| 416 | inline void toFront() { i = c->begin(); n = c->end(); } |
| 417 | inline void toBack() { i = c->end(); n = i; } |
| 418 | inline bool hasNext() const { return c->constEnd() != i; } |
| 419 | inline const T &next() { n = i++; return *n; } |
| 420 | inline const T &peekNext() const { return *i; } |
| 421 | inline void remove() |
| 422 | { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } } |
| 423 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } |
| 424 | inline bool findNext(const T &t) |
| 425 | { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; } |
| 426 | }; |
| 427 | #endif // QT_NO_JAVA_STYLE_ITERATORS |
| 428 | |
| 429 | template <typename T, typename Predicate> |
| 430 | qsizetype erase_if(QSet<T> &set, Predicate pred) |
| 431 | { |
| 432 | return QtPrivate::qset_erase_if(set, pred); |
| 433 | } |
| 434 | |
| 435 | QT_END_NAMESPACE |
| 436 | |
| 437 | #endif // QSET_H |
| 438 | |