1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include "QtCore/qhash.h"
37
typedef QHash<T, QHashDummyValue> Hash;
41
inline QSet(const QSet<T> &other) : q_hash(other.q_hash) {}
43
inline QSet<T> &operator=(const QSet<T> &other)
44
{ q_hash = other.q_hash; return *this; }
46
inline bool operator==(const QSet<T> &other) const
47
{ return q_hash == other.q_hash; }
48
inline bool operator!=(const QSet<T> &other) const
49
{ return q_hash != other.q_hash; }
51
inline int size() const { return q_hash.size(); }
53
inline bool isEmpty() const { return q_hash.isEmpty(); }
55
inline int capacity() const { return q_hash.capacity(); }
56
inline void reserve(int size);
57
inline void squeeze() { q_hash.squeeze(); }
59
inline void detach() { q_hash.detach(); }
60
inline bool isDetached() const { return q_hash.isDetached(); }
62
inline void clear() { q_hash.clear(); }
64
inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
66
inline bool contains(const T &value) const { return q_hash.contains(value); }
70
typedef QHash<T, QHashDummyValue> Hash;
71
typename Hash::const_iterator i;
74
typedef std::bidirectional_iterator_tag iterator_category;
75
typedef ptrdiff_t difference_type;
80
inline const_iterator() {}
81
inline const_iterator(typename Hash::const_iterator o) : i(o) {}
82
inline const_iterator(const const_iterator &o) : i(o.i) {}
83
inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; }
84
inline const T &operator*() const { return i.key(); }
85
inline const T *operator->() const { return &i.key(); }
86
inline bool operator==(const const_iterator &o) const { return i == o.i; }
87
inline bool operator!=(const const_iterator &o) const { return i != o.i; }
88
inline const_iterator &operator++() { ++i; return *this; }
89
inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; }
90
inline const_iterator &operator--() { --i; return *this; }
91
inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; }
92
inline const_iterator operator+(int j) const { return i + j; }
93
inline const_iterator operator-(int j) const { return i - j; }
94
inline const_iterator &operator+=(int j) { i += j; return *this; }
95
inline const_iterator &operator-=(int j) { i -= j; return *this; }
99
inline const_iterator begin() const { return q_hash.begin(); }
100
inline const_iterator constBegin() const { return q_hash.constBegin(); }
101
inline const_iterator end() const { return q_hash.end(); }
102
inline const_iterator constEnd() const { return q_hash.constEnd(); }
105
typedef const_iterator ConstIterator;
106
inline int count() const { return q_hash.count(); }
107
inline const_iterator insert(const T &value)
108
{ return static_cast<typename Hash::const_iterator>(q_hash.insert(value,
109
QHashDummyValue())); }
110
QSet<T> &unite(const QSet<T> &other);
111
QSet<T> &intersect(const QSet<T> &other);
112
QSet<T> &subtract(const QSet<T> &other);
115
inline bool empty() const { return isEmpty(); }
118
inline QSet<T> &operator<<(const T &value) { insert(value); return *this; }
119
inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; }
120
inline QSet<T> &operator|=(const T &value) { insert(value); return *this; }
121
inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; }
122
inline QSet<T> &operator&=(const T &value)
123
{ QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); }
124
inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; }
125
inline QSet<T> &operator+=(const T &value) { insert(value); return *this; }
126
inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; }
127
inline QSet<T> &operator-=(const T &value) { subtract(value); return *this; }
128
inline QSet<T> operator|(const QSet<T> &other)
129
{ QSet<T> result = *this; result |= other; return result; }
130
inline QSet<T> operator&(const QSet<T> &other)
131
{ QSet<T> result = *this; result &= other; return result; }
132
inline QSet<T> operator+(const QSet<T> &other)
133
{ QSet<T> result = *this; result += other; return result; }
134
inline QSet<T> operator-(const QSet<T> &other)
135
{ QSet<T> result = *this; result -= other; return result; }
137
QList<T> toList() const;
138
inline QList<T> values() const { return toList(); }
140
static QSet<T> fromList(const QList<T> &list);
147
Q_INLINE_TEMPLATE void QSet<T>::reserve(int asize) { q_hash.reserve(asize); }
150
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
153
typename QSet<T>::const_iterator i = copy.constEnd();
154
while (i != copy.constBegin()) {
162
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
164
QSet<T> copy1(*this);
165
QSet<T> copy2(other);
166
typename QSet<T>::const_iterator i = copy1.constEnd();
167
while (i != copy1.constBegin()) {
169
if (!copy2.contains(*i))
176
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
178
QSet<T> copy1(*this);
179
QSet<T> copy2(other);
180
typename QSet<T>::const_iterator i = copy1.constEnd();
181
while (i != copy1.constBegin()) {
183
if (copy2.contains(*i))
189
template <typename T>
190
Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::toList() const
193
typename QSet<T>::const_iterator i = constBegin();
194
while (i != constEnd()) {
201
template <typename T>
202
Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const
205
result.reserve(size());
206
for (int i = 0; i < size(); ++i)
207
result.insert(at(i));
211
template <typename T>
212
QSet<T> QSet<T>::fromList(const QList<T> &list)
217
template <typename T>
218
QList<T> QList<T>::fromSet(const QSet<T> &set)
223
Q_DECLARE_SEQUENTIAL_ITERATOR(Set)