author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QSET_H |
|
43 |
#define QSET_H |
|
44 |
||
45 |
#include <QtCore/qhash.h> |
|
46 |
||
47 |
QT_BEGIN_HEADER |
|
48 |
||
49 |
QT_BEGIN_NAMESPACE |
|
50 |
||
51 |
QT_MODULE(Core) |
|
52 |
||
53 |
template <class T> |
|
54 |
class QSet |
|
55 |
{ |
|
56 |
typedef QHash<T, QHashDummyValue> Hash; |
|
57 |
||
58 |
public: |
|
59 |
inline QSet() {} |
|
60 |
inline QSet(const QSet<T> &other) : q_hash(other.q_hash) {} |
|
61 |
||
62 |
inline QSet<T> &operator=(const QSet<T> &other) |
|
63 |
{ q_hash = other.q_hash; return *this; } |
|
64 |
||
65 |
inline bool operator==(const QSet<T> &other) const |
|
66 |
{ return q_hash == other.q_hash; } |
|
67 |
inline bool operator!=(const QSet<T> &other) const |
|
68 |
{ return q_hash != other.q_hash; } |
|
69 |
||
70 |
inline int size() const { return q_hash.size(); } |
|
71 |
||
72 |
inline bool isEmpty() const { return q_hash.isEmpty(); } |
|
73 |
||
74 |
inline int capacity() const { return q_hash.capacity(); } |
|
75 |
inline void reserve(int size); |
|
76 |
inline void squeeze() { q_hash.squeeze(); } |
|
77 |
||
78 |
inline void detach() { q_hash.detach(); } |
|
79 |
inline bool isDetached() const { return q_hash.isDetached(); } |
|
80 |
inline void setSharable(bool sharable) { q_hash.setSharable(sharable); } |
|
81 |
||
82 |
inline void clear() { q_hash.clear(); } |
|
83 |
||
84 |
inline bool remove(const T &value) { return q_hash.remove(value) != 0; } |
|
85 |
||
86 |
inline bool contains(const T &value) const { return q_hash.contains(value); } |
|
87 |
||
88 |
bool contains(const QSet<T> &set) const; |
|
89 |
||
90 |
class const_iterator; |
|
91 |
||
92 |
class iterator |
|
93 |
{ |
|
94 |
typedef QHash<T, QHashDummyValue> Hash; |
|
95 |
typename Hash::iterator i; |
|
96 |
friend class const_iterator; |
|
97 |
||
98 |
public: |
|
99 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
100 |
typedef qptrdiff difference_type; |
0 | 101 |
typedef T value_type; |
102 |
typedef const T *pointer; |
|
103 |
typedef const T &reference; |
|
104 |
||
105 |
inline iterator() {} |
|
106 |
inline iterator(typename Hash::iterator o) : i(o) {} |
|
107 |
inline iterator(const iterator &o) : i(o.i) {} |
|
108 |
inline iterator &operator=(const iterator &o) { i = o.i; return *this; } |
|
109 |
inline const T &operator*() const { return i.key(); } |
|
110 |
inline const T *operator->() const { return &i.key(); } |
|
111 |
inline bool operator==(const iterator &o) const { return i == o.i; } |
|
112 |
inline bool operator!=(const iterator &o) const { return i != o.i; } |
|
113 |
inline bool operator==(const const_iterator &o) const |
|
114 |
{ return i == o.i; } |
|
115 |
inline bool operator!=(const const_iterator &o) const |
|
116 |
{ return i != o.i; } |
|
117 |
inline iterator &operator++() { ++i; return *this; } |
|
118 |
inline iterator operator++(int) { iterator r = *this; ++i; return r; } |
|
119 |
inline iterator &operator--() { --i; return *this; } |
|
120 |
inline iterator operator--(int) { iterator r = *this; --i; return r; } |
|
121 |
inline iterator operator+(int j) const { return i + j; } |
|
122 |
inline iterator operator-(int j) const { return i - j; } |
|
123 |
inline iterator &operator+=(int j) { i += j; return *this; } |
|
124 |
inline iterator &operator-=(int j) { i -= j; return *this; } |
|
125 |
}; |
|
126 |
||
127 |
class const_iterator |
|
128 |
{ |
|
129 |
typedef QHash<T, QHashDummyValue> Hash; |
|
130 |
typename Hash::const_iterator i; |
|
131 |
friend class iterator; |
|
132 |
||
133 |
public: |
|
134 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
135 |
typedef qptrdiff difference_type; |
0 | 136 |
typedef T value_type; |
137 |
typedef const T *pointer; |
|
138 |
typedef const T &reference; |
|
139 |
||
140 |
inline const_iterator() {} |
|
141 |
inline const_iterator(typename Hash::const_iterator o) : i(o) {} |
|
142 |
inline const_iterator(const const_iterator &o) : i(o.i) {} |
|
143 |
inline const_iterator(const iterator &o) |
|
144 |
: i(o.i) {} |
|
145 |
inline const_iterator &operator=(const const_iterator &o) { i = o.i; return *this; } |
|
146 |
inline const T &operator*() const { return i.key(); } |
|
147 |
inline const T *operator->() const { return &i.key(); } |
|
148 |
inline bool operator==(const const_iterator &o) const { return i == o.i; } |
|
149 |
inline bool operator!=(const const_iterator &o) const { return i != o.i; } |
|
150 |
inline const_iterator &operator++() { ++i; return *this; } |
|
151 |
inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; } |
|
152 |
inline const_iterator &operator--() { --i; return *this; } |
|
153 |
inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; } |
|
154 |
inline const_iterator operator+(int j) const { return i + j; } |
|
155 |
inline const_iterator operator-(int j) const { return i - j; } |
|
156 |
inline const_iterator &operator+=(int j) { i += j; return *this; } |
|
157 |
inline const_iterator &operator-=(int j) { i -= j; return *this; } |
|
158 |
}; |
|
159 |
||
160 |
// STL style |
|
161 |
inline iterator begin() { return q_hash.begin(); } |
|
162 |
inline const_iterator begin() const { return q_hash.begin(); } |
|
163 |
inline const_iterator constBegin() const { return q_hash.constBegin(); } |
|
164 |
inline iterator end() { return q_hash.end(); } |
|
165 |
inline const_iterator end() const { return q_hash.end(); } |
|
166 |
inline const_iterator constEnd() const { return q_hash.constEnd(); } |
|
167 |
iterator erase(iterator i) |
|
168 |
{ return q_hash.erase(reinterpret_cast<typename Hash::iterator &>(i)); } |
|
169 |
||
170 |
// more Qt |
|
171 |
typedef iterator Iterator; |
|
172 |
typedef const_iterator ConstIterator; |
|
173 |
inline int count() const { return q_hash.count(); } |
|
174 |
inline const_iterator insert(const T &value) // ### Qt 5: should return an 'iterator' |
|
175 |
{ return static_cast<typename Hash::const_iterator>(q_hash.insert(value, |
|
176 |
QHashDummyValue())); } |
|
177 |
iterator find(const T &value) { return q_hash.find(value); } |
|
178 |
const_iterator find(const T &value) const { return q_hash.find(value); } |
|
179 |
inline const_iterator constFind(const T &value) const { return find(value); } |
|
180 |
QSet<T> &unite(const QSet<T> &other); |
|
181 |
QSet<T> &intersect(const QSet<T> &other); |
|
182 |
QSet<T> &subtract(const QSet<T> &other); |
|
183 |
||
184 |
// STL compatibility |
|
185 |
typedef T key_type; |
|
186 |
typedef T value_type; |
|
187 |
typedef value_type *pointer; |
|
188 |
typedef const value_type *const_pointer; |
|
189 |
typedef value_type &reference; |
|
190 |
typedef const value_type &const_reference; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
191 |
typedef qptrdiff difference_type; |
0 | 192 |
typedef int size_type; |
193 |
||
194 |
inline bool empty() const { return isEmpty(); } |
|
195 |
// comfort |
|
196 |
inline QSet<T> &operator<<(const T &value) { insert(value); return *this; } |
|
197 |
inline QSet<T> &operator|=(const QSet<T> &other) { unite(other); return *this; } |
|
198 |
inline QSet<T> &operator|=(const T &value) { insert(value); return *this; } |
|
199 |
inline QSet<T> &operator&=(const QSet<T> &other) { intersect(other); return *this; } |
|
200 |
inline QSet<T> &operator&=(const T &value) |
|
201 |
{ QSet<T> result; if (contains(value)) result.insert(value); return (*this = result); } |
|
202 |
inline QSet<T> &operator+=(const QSet<T> &other) { unite(other); return *this; } |
|
203 |
inline QSet<T> &operator+=(const T &value) { insert(value); return *this; } |
|
204 |
inline QSet<T> &operator-=(const QSet<T> &other) { subtract(other); return *this; } |
|
205 |
inline QSet<T> &operator-=(const T &value) { remove(value); return *this; } |
|
206 |
inline QSet<T> operator|(const QSet<T> &other) const |
|
207 |
{ QSet<T> result = *this; result |= other; return result; } |
|
208 |
inline QSet<T> operator&(const QSet<T> &other) const |
|
209 |
{ QSet<T> result = *this; result &= other; return result; } |
|
210 |
inline QSet<T> operator+(const QSet<T> &other) const |
|
211 |
{ QSet<T> result = *this; result += other; return result; } |
|
212 |
inline QSet<T> operator-(const QSet<T> &other) const |
|
213 |
{ QSet<T> result = *this; result -= other; return result; } |
|
214 |
#if QT_VERSION < 0x050000 |
|
215 |
// ### Qt 5: remove |
|
216 |
inline QSet<T> operator|(const QSet<T> &other) |
|
217 |
{ QSet<T> result = *this; result |= other; return result; } |
|
218 |
inline QSet<T> operator&(const QSet<T> &other) |
|
219 |
{ QSet<T> result = *this; result &= other; return result; } |
|
220 |
inline QSet<T> operator+(const QSet<T> &other) |
|
221 |
{ QSet<T> result = *this; result += other; return result; } |
|
222 |
inline QSet<T> operator-(const QSet<T> &other) |
|
223 |
{ QSet<T> result = *this; result -= other; return result; } |
|
224 |
#endif |
|
225 |
||
226 |
QList<T> toList() const; |
|
227 |
inline QList<T> values() const { return toList(); } |
|
228 |
||
229 |
static QSet<T> fromList(const QList<T> &list); |
|
230 |
||
231 |
private: |
|
232 |
Hash q_hash; |
|
233 |
}; |
|
234 |
||
235 |
template <class T> |
|
236 |
Q_INLINE_TEMPLATE void QSet<T>::reserve(int asize) { q_hash.reserve(asize); } |
|
237 |
||
238 |
template <class T> |
|
239 |
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other) |
|
240 |
{ |
|
241 |
QSet<T> copy(other); |
|
242 |
typename QSet<T>::const_iterator i = copy.constEnd(); |
|
243 |
while (i != copy.constBegin()) { |
|
244 |
--i; |
|
245 |
insert(*i); |
|
246 |
} |
|
247 |
return *this; |
|
248 |
} |
|
249 |
||
250 |
template <class T> |
|
251 |
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other) |
|
252 |
{ |
|
253 |
QSet<T> copy1(*this); |
|
254 |
QSet<T> copy2(other); |
|
255 |
typename QSet<T>::const_iterator i = copy1.constEnd(); |
|
256 |
while (i != copy1.constBegin()) { |
|
257 |
--i; |
|
258 |
if (!copy2.contains(*i)) |
|
259 |
remove(*i); |
|
260 |
} |
|
261 |
return *this; |
|
262 |
} |
|
263 |
||
264 |
template <class T> |
|
265 |
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other) |
|
266 |
{ |
|
267 |
QSet<T> copy1(*this); |
|
268 |
QSet<T> copy2(other); |
|
269 |
typename QSet<T>::const_iterator i = copy1.constEnd(); |
|
270 |
while (i != copy1.constBegin()) { |
|
271 |
--i; |
|
272 |
if (copy2.contains(*i)) |
|
273 |
remove(*i); |
|
274 |
} |
|
275 |
return *this; |
|
276 |
} |
|
277 |
||
278 |
template <class T> |
|
279 |
Q_INLINE_TEMPLATE bool QSet<T>::contains(const QSet<T> &other) const |
|
280 |
{ |
|
281 |
typename QSet<T>::const_iterator i = other.constBegin(); |
|
282 |
while (i != other.constEnd()) { |
|
283 |
if (!contains(*i)) |
|
284 |
return false; |
|
285 |
++i; |
|
286 |
} |
|
287 |
return true; |
|
288 |
} |
|
289 |
||
290 |
template <typename T> |
|
291 |
Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::toList() const |
|
292 |
{ |
|
293 |
QList<T> result; |
|
294 |
typename QSet<T>::const_iterator i = constBegin(); |
|
295 |
while (i != constEnd()) { |
|
296 |
result.append(*i); |
|
297 |
++i; |
|
298 |
} |
|
299 |
return result; |
|
300 |
} |
|
301 |
||
302 |
template <typename T> |
|
303 |
Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const |
|
304 |
{ |
|
305 |
QSet<T> result; |
|
306 |
result.reserve(size()); |
|
307 |
for (int i = 0; i < size(); ++i) |
|
308 |
result.insert(at(i)); |
|
309 |
return result; |
|
310 |
} |
|
311 |
||
312 |
template <typename T> |
|
313 |
QSet<T> QSet<T>::fromList(const QList<T> &list) |
|
314 |
{ |
|
315 |
return list.toSet(); |
|
316 |
} |
|
317 |
||
318 |
template <typename T> |
|
319 |
QList<T> QList<T>::fromSet(const QSet<T> &set) |
|
320 |
{ |
|
321 |
return set.toList(); |
|
322 |
} |
|
323 |
||
324 |
Q_DECLARE_SEQUENTIAL_ITERATOR(Set) |
|
325 |
||
326 |
template <typename T> |
|
327 |
class QMutableSetIterator |
|
328 |
{ |
|
329 |
typedef typename QSet<T>::iterator iterator; |
|
330 |
QSet<T> *c; |
|
331 |
iterator i, n; |
|
332 |
inline bool item_exists() const { return n != c->constEnd(); } |
|
333 |
||
334 |
public: |
|
335 |
inline QMutableSetIterator(QSet<T> &container) |
|
336 |
: c(&container) |
|
337 |
{ c->setSharable(false); i = c->begin(); n = c->end(); } |
|
338 |
inline ~QMutableSetIterator() |
|
339 |
{ c->setSharable(true); } |
|
340 |
inline QMutableSetIterator &operator=(QSet<T> &container) |
|
341 |
{ c->setSharable(true); c = &container; c->setSharable(false); |
|
342 |
i = c->begin(); n = c->end(); return *this; } |
|
343 |
inline void toFront() { i = c->begin(); n = c->end(); } |
|
344 |
inline void toBack() { i = c->end(); n = i; } |
|
345 |
inline bool hasNext() const { return c->constEnd() != i; } |
|
346 |
inline const T &next() { n = i++; return *n; } |
|
347 |
inline const T &peekNext() const { return *i; } |
|
348 |
inline bool hasPrevious() const { return c->constBegin() != i; } |
|
349 |
inline const T &previous() { n = --i; return *n; } |
|
350 |
inline const T &peekPrevious() const { iterator p = i; return *--p; } |
|
351 |
inline void remove() |
|
352 |
{ if (c->constEnd() != n) { i = c->erase(n); n = c->end(); } } |
|
353 |
inline const T &value() const { Q_ASSERT(item_exists()); return *n; } |
|
354 |
inline bool findNext(const T &t) |
|
355 |
{ while (c->constEnd() != (n = i)) if (*i++ == t) return true; return false; } |
|
356 |
inline bool findPrevious(const T &t) |
|
357 |
{ while (c->constBegin() != i) if (*(n = --i) == t) return true; |
|
358 |
n = c->end(); return false; } |
|
359 |
}; |
|
360 |
||
361 |
QT_END_NAMESPACE |
|
362 |
||
363 |
QT_END_HEADER |
|
364 |
||
365 |
#endif // QSET_H |