~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qset.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QSET_H
 
30
#define QSET_H
 
31
 
 
32
#include "QtCore/qhash.h"
 
33
 
 
34
template <class T>
 
35
class QSet
 
36
{
 
37
    typedef QHash<T, QHashDummyValue> Hash;
 
38
 
 
39
public:
 
40
    inline QSet() {}
 
41
    inline QSet(const QSet<T> &other) : q_hash(other.q_hash) {}
 
42
 
 
43
    inline QSet<T> &operator=(const QSet<T> &other)
 
44
        { q_hash = other.q_hash; return *this; }
 
45
 
 
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; }
 
50
 
 
51
    inline int size() const { return q_hash.size(); }
 
52
 
 
53
    inline bool isEmpty() const { return q_hash.isEmpty(); }
 
54
 
 
55
    inline int capacity() const { return q_hash.capacity(); }
 
56
    inline void reserve(int size);
 
57
    inline void squeeze() { q_hash.squeeze(); }
 
58
 
 
59
    inline void detach() { q_hash.detach(); }
 
60
    inline bool isDetached() const { return q_hash.isDetached(); }
 
61
 
 
62
    inline void clear() { q_hash.clear(); }
 
63
 
 
64
    inline bool remove(const T &value) { return q_hash.remove(value) != 0; }
 
65
 
 
66
    inline bool contains(const T &value) const { return q_hash.contains(value); }
 
67
 
 
68
    class const_iterator
 
69
    {
 
70
        typedef QHash<T, QHashDummyValue> Hash;
 
71
        typename Hash::const_iterator i;
 
72
 
 
73
    public:
 
74
        typedef std::bidirectional_iterator_tag iterator_category;
 
75
        typedef ptrdiff_t difference_type;
 
76
        typedef T value_type;
 
77
        typedef T *pointer;
 
78
        typedef T &reference;
 
79
 
 
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; }
 
96
    };
 
97
 
 
98
    // STL style
 
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(); }
 
103
 
 
104
    // more Qt
 
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);
 
113
 
 
114
    // STL compatibility
 
115
    inline bool empty() const { return isEmpty(); }
 
116
 
 
117
    // comfort
 
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; }
 
136
 
 
137
    QList<T> toList() const;
 
138
    inline QList<T> values() const { return toList(); }
 
139
 
 
140
    static QSet<T> fromList(const QList<T> &list);
 
141
 
 
142
private:
 
143
    Hash q_hash;
 
144
};
 
145
 
 
146
template <class T>
 
147
Q_INLINE_TEMPLATE void QSet<T>::reserve(int asize) { q_hash.reserve(asize); }
 
148
 
 
149
template <class T>
 
150
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::unite(const QSet<T> &other)
 
151
{
 
152
    QSet<T> copy(other);
 
153
    typename QSet<T>::const_iterator i = copy.constEnd();
 
154
    while (i != copy.constBegin()) {
 
155
        --i;
 
156
        insert(*i);
 
157
    }
 
158
    return *this;
 
159
}
 
160
 
 
161
template <class T>
 
162
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::intersect(const QSet<T> &other)
 
163
{
 
164
    QSet<T> copy1(*this);
 
165
    QSet<T> copy2(other);
 
166
    typename QSet<T>::const_iterator i = copy1.constEnd();
 
167
    while (i != copy1.constBegin()) {
 
168
        --i;
 
169
        if (!copy2.contains(*i))
 
170
            remove(*i);
 
171
    }
 
172
    return *this;
 
173
}
 
174
 
 
175
template <class T>
 
176
Q_INLINE_TEMPLATE QSet<T> &QSet<T>::subtract(const QSet<T> &other)
 
177
{
 
178
    QSet<T> copy1(*this);
 
179
    QSet<T> copy2(other);
 
180
    typename QSet<T>::const_iterator i = copy1.constEnd();
 
181
    while (i != copy1.constBegin()) {
 
182
        --i;
 
183
        if (copy2.contains(*i))
 
184
            remove(*i);
 
185
    }
 
186
    return *this;
 
187
}
 
188
 
 
189
template <typename T>
 
190
Q_OUTOFLINE_TEMPLATE QList<T> QSet<T>::toList() const
 
191
{
 
192
    QList<T> result;
 
193
    typename QSet<T>::const_iterator i = constBegin();
 
194
    while (i != constEnd()) {
 
195
        result.append(*i);
 
196
        ++i;
 
197
    }
 
198
    return result;
 
199
}
 
200
 
 
201
template <typename T>
 
202
Q_OUTOFLINE_TEMPLATE QSet<T> QList<T>::toSet() const
 
203
{
 
204
    QSet<T> result;
 
205
    result.reserve(size());
 
206
    for (int i = 0; i < size(); ++i)
 
207
        result.insert(at(i));
 
208
    return result;
 
209
}
 
210
 
 
211
template <typename T>
 
212
QSet<T> QSet<T>::fromList(const QList<T> &list)
 
213
{
 
214
    return list.toSet();
 
215
}
 
216
 
 
217
template <typename T>
 
218
QList<T> QList<T>::fromSet(const QSet<T> &set)
 
219
{
 
220
    return set.toList();
 
221
}
 
222
 
 
223
Q_DECLARE_SEQUENTIAL_ITERATOR(Set)
 
224
 
 
225
#endif