/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QSET_H #define QSET_H #include #include #include #include QT_BEGIN_NAMESPACE template class QSet { typedef QHash Hash; public: inline QSet() noexcept {} inline QSet(std::initializer_list list) : QSet(list.begin(), list.end()) {} template = true> inline QSet(InputIterator first, InputIterator last) { QtPrivate::reserveIfForwardIterator(this, first, last); for (; first != last; ++first) insert(*first); } // compiler-generated copy/move ctor/assignment operators are fine! // compiler-generated destructor is fine! inline void swap(QSet &other) noexcept { q_hash.swap(other.q_hash); } inline bool operator==(const QSet &other) const { return q_hash == other.q_hash; } inline bool operator!=(const QSet &other) const { return q_hash != other.q_hash; } inline int size() const { return q_hash.size(); } inline bool isEmpty() const { return q_hash.isEmpty(); } inline int capacity() const { return q_hash.capacity(); } inline void reserve(int size); inline void squeeze() { q_hash.squeeze(); } inline void detach() { q_hash.detach(); } inline bool isDetached() const { return q_hash.isDetached(); } #if !defined(QT_NO_UNSHARABLE_CONTAINERS) inline void setSharable(bool sharable) { q_hash.setSharable(sharable); } #endif inline void clear() { q_hash.clear(); } inline bool remove(const T &value) { return q_hash.remove(value) != 0; } inline bool contains(const T &value) const { return q_hash.contains(value); } bool contains(const QSet &set) const; class const_iterator; class iterator { typedef QHash Hash; typename Hash::iterator i; friend class const_iterator; friend class QSet; public: #if QT_DEPRECATED_SINCE(5, 15) typedef std::bidirectional_iterator_tag iterator_category; #else typedef std::forward_iterator_tag iterator_category; #endif typedef qptrdiff difference_type; typedef T value_type; typedef const T *pointer; typedef const T &reference; inline iterator() {} inline iterator(typename Hash::iterator o) : i(o) {} inline iterator(const iterator &o) : i(o.i) {} inline iterator &operator=(const iterator &o) { i = o.i; return *this; } inline const T &operator*() const { return i.key(); } inline const T *operator->() const { return &i.key(); } inline bool operator==(const iterator &o) const { return i == o.i; } inline bool operator!=(const iterator &o) const { return i != o.i; } inline bool operator==(const const_iterator &o) const { return i == o.i; } inline bool operator!=(const const_iterator &o) const { return i != o.i; } inline iterator &operator++() { ++i; return *this; } inline iterator operator++(int) { iterator r = *this; ++i; return r; } #if QT_DEPRECATED_SINCE(5, 15) inline QT_DEPRECATED iterator &operator--() { --i; return *this; } inline QT_DEPRECATED iterator operator--(int) { iterator r = *this; --i; return r; } inline QT_DEPRECATED iterator operator+(int j) const { return i + j; } inline QT_DEPRECATED iterator operator-(int j) const { return i - j; } friend inline QT_DEPRECATED iterator operator+(int j, iterator k) { return k + j; } inline QT_DEPRECATED iterator &operator+=(int j) { i += j; return *this; } inline QT_DEPRECATED iterator &operator-=(int j) { i -= j; return *this; } #endif }; class const_iterator { typedef QHash Hash; typename Hash::const_iterator i; friend class iterator; friend class QSet; public: #if QT_DEPRECATED_SINCE(5, 15) typedef std::bidirectional_iterator_tag iterator_category; #else typedef std::forward_iterator_tag iterator_category; #endif typedef qptrdiff difference_type; typedef T value_type; typedef const T *pointer; typedef const T &reference; inline const_iterator() {} inline const_iterator(typename Hash::const_iterator o) : i(o) {} inline const_iterator(const const_iterator &o) : i(o.i) {} inline const_iterator(const iterator &o) : i(o.i) {} inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; } inline const T &operator*() const { return i.key(); } inline const T *operator->() const { return &i.key(); } inline bool operator==(const const_iterator &o) const { return i == o.i; } inline bool operator!=(const const_iterator &o) const { return i != o.i; } inline const_iterator &operator++() { ++i; return *this; } inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; } #if QT_DEPRECATED_SINCE(5, 15) inline QT_DEPRECATED const_iterator &operator--() { --i; return *this; } inline QT_DEPRECATED const_iterator operator--(int) { const_iterator r = *this; --i; return r; } inline QT_DEPRECATED const_iterator operator+(int j) const { return i + j; } inline QT_DEPRECATED const_iterator operator-(int j) const { return i - j; } friend inline QT_DEPRECATED const_iterator operator+(int j, const_iterator k) { return k + j; } inline QT_DEPRECATED const_iterator &operator+=(int j) { i += j; return *this; } inline QT_DEPRECATED const_iterator &operator-=(int j) { i -= j; return *this; } #endif }; // STL style inline iterator begin() { return q_hash.begin(); } inline const_iterator begin() const noexcept { return q_hash.begin(); } inline const_iterator cbegin() const noexcept { return q_hash.begin(); } inline const_iterator constBegin() const noexcept { return q_hash.constBegin(); } inline iterator end() { return q_hash.end(); } inline const_iterator end() const noexcept { return q_hash.end(); } inline const_iterator cend() const noexcept { return q_hash.end(); } inline const_iterator constEnd() const noexcept { return q_hash.constEnd(); } #if QT_DEPRECATED_SINCE(5, 15) typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; reverse_iterator QT_DEPRECATED rbegin() { return reverse_iterator(end()); } reverse_iterator QT_DEPRECATED rend() { return reverse_iterator(begin()); } const_reverse_iterator QT_DEPRECATED rbegin() const noexcept { return const_reverse_iterator(end()); } const_reverse_iterator QT_DEPRECATED rend() const noexcept { return const_reverse_iterator(begin()); } const_reverse_iterator QT_DEPRECATED crbegin() const noexcept { return const_reverse_iterator(end()); } const_reverse_iterator QT_DEPRECATED crend() const noexcept { return const_reverse_iterator(begin()); } #endif iterator erase(iterator i) { return erase(m2c(i)); } iterator erase(const_iterator i) { Q_ASSERT_X(isValidIterator(i), "QSet::erase", "The specified const_iterator argument 'i' is invalid"); return q_hash.erase(reinterpret_cast(i)); } // more Qt typedef iterator Iterator; typedef const_iterator ConstIterator; inline int count() const { return q_hash.count(); } inline iterator insert(const T &value) { return static_cast(q_hash.insert(value, QHashDummyValue())); } iterator find(const T &value) { return q_hash.find(value); } const_iterator find(const T &value) const { return q_hash.find(value); } inline const_iterator constFind(const T &value) const { return find(value); } QSet &unite(const QSet &other); QSet &intersect(const QSet &other); bool intersects(const QSet &other) const; QSet &subtract(const QSet &other); // STL compatibility typedef T key_type; typedef T value_type; typedef value_type *pointer; typedef const value_type *const_pointer; typedef value_type &reference; typedef const value_type &const_reference; typedef qptrdiff difference_type; typedef int size_type; inline bool empty() const { return isEmpty(); } // comfort inline QSet &operator<<(const T &value) { insert(value); return *this; } inline QSet &operator|=(const QSet &other) { unite(other); return *this; } inline QSet &operator|=(const T &value) { insert(value); return *this; } inline QSet &operator&=(const QSet &other) { intersect(other); return *this; } inline QSet &operator&=(const T &value) { QSet result; if (contains(value)) result.insert(value); return (*this = result); } inline QSet &operator+=(const QSet &other) { unite(other); return *this; } inline QSet &operator+=(const T &value) { insert(value); return *this; } inline QSet &operator-=(const QSet &other) { subtract(other); return *this; } inline QSet &operator-=(const T &value) { remove(value); return *this; } inline QSet operator|(const QSet &other) const { QSet result = *this; result |= other; return result; } inline QSet operator&(const QSet &other) const { QSet result = *this; result &= other; return result; } inline QSet operator+(const QSet &other) const { QSet result = *this; result += other; return result; } inline QSet operator-(const QSet &other) const { QSet result = *this; result -= other; return result; } QList values() const; private: Hash q_hash; static const_iterator m2c(iterator it) noexcept { return const_iterator(typename Hash::const_iterator(it.i.i)); } bool isValidIterator(const iterator &i) const { return q_hash.isValidIterator(reinterpret_cast(i)); } bool isValidIterator(const const_iterator &i) const noexcept { return q_hash.isValidIterator(reinterpret_cast(i)); } }; #if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 template ::value_type, QtPrivate::IfIsInputIterator = true> QSet(InputIterator, InputIterator) -> QSet; #endif template uint qHash(const QSet &key, uint seed = 0) noexcept(noexcept(qHashRangeCommutative(key.begin(), key.end(), seed))) { return qHashRangeCommutative(key.begin(), key.end(), seed); } // inline function implementations template Q_INLINE_TEMPLATE void QSet::reserve(int asize) { q_hash.reserve(asize); } template Q_INLINE_TEMPLATE QSet &QSet::unite(const QSet &other) { if (!q_hash.isSharedWith(other.q_hash)) { for (const T &e : other) insert(e); } return *this; } template Q_INLINE_TEMPLATE QSet &QSet::intersect(const QSet &other) { QSet copy1; QSet copy2; if (size() <= other.size()) { copy1 = *this; copy2 = other; } else { copy1 = other; copy2 = *this; *this = copy1; } for (const auto &e : qAsConst(copy1)) { if (!copy2.contains(e)) remove(e); } return *this; } template Q_INLINE_TEMPLATE bool QSet::intersects(const QSet &other) const { const bool otherIsBigger = other.size() > size(); const QSet &smallestSet = otherIsBigger ? *this : other; const QSet &biggestSet = otherIsBigger ? other : *this; const bool equalSeeds = q_hash.d->seed == other.q_hash.d->seed; typename QSet::const_iterator i = smallestSet.cbegin(); typename QSet::const_iterator e = smallestSet.cend(); if (Q_LIKELY(equalSeeds)) { // If seeds are equal we take the fast path so no hash is recalculated. while (i != e) { if (*biggestSet.q_hash.findNode(*i, i.i.i->h) != biggestSet.q_hash.e) return true; ++i; } } else { while (i != e) { if (biggestSet.contains(*i)) return true; ++i; } } return false; } template Q_INLINE_TEMPLATE QSet &QSet::subtract(const QSet &other) { if (q_hash.isSharedWith(other.q_hash)) { clear(); } else { for (const auto &e : other) remove(e); } return *this; } template Q_INLINE_TEMPLATE bool QSet::contains(const QSet &other) const { typename QSet::const_iterator i = other.constBegin(); while (i != other.constEnd()) { if (!contains(*i)) return false; ++i; } return true; } template Q_OUTOFLINE_TEMPLATE QList QSet::values() const { QList result; result.reserve(size()); typename QSet::const_iterator i = constBegin(); while (i != constEnd()) { result.append(*i); ++i; } return result; } Q_DECLARE_SEQUENTIAL_ITERATOR(Set) #if !defined(QT_NO_JAVA_STYLE_ITERATORS) template class QMutableSetIterator { typedef typename QSet::iterator iterator; QSet *c; iterator i, n; inline bool item_exists() const { return c->constEnd() != n; } public: inline QMutableSetIterator(QSet &container) : c(&container) { i = c->begin(); n = c->end(); } inline QMutableSetIterator &operator=(QSet &container) { c = &container; i = c->begin(); n = c->end(); return *this; } inline void toFront() { i = c->begin(); n = c->end(); } inline void toBack() { i = c->end(); n = i; } inline bool hasNext() const { return c->constEnd() != i; } inline const T &next() { n = i++; return *n; } inline const T &peekNext() const { return *i; } inline void remove() { if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } } inline const T &value() const { Q_ASSERT(item_exists()); return *n; } inline bool findNext(const T &t) { while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; } #if QT_DEPRECATED_SINCE(5, 15) inline QT_DEPRECATED bool hasPrevious() const { return c->constBegin() != i; } inline QT_DEPRECATED const T &previous() { n = --i; return *n; } inline QT_DEPRECATED const T &peekPrevious() const { iterator p = i; return *--p; } inline QT_DEPRECATED bool findPrevious(const T &t) { while (c->constBegin() != i) if (*(n = --i) == t) return true; n = c->end(); return false; } #endif }; #endif // QT_NO_JAVA_STYLE_ITERATORS QT_END_NAMESPACE #endif // QSET_H