1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QBITARRAY_H
|
---|
43 | #define QBITARRAY_H
|
---|
44 |
|
---|
45 | #include <QtCore/qbytearray.h>
|
---|
46 |
|
---|
47 | QT_BEGIN_HEADER
|
---|
48 |
|
---|
49 | QT_BEGIN_NAMESPACE
|
---|
50 |
|
---|
51 | QT_MODULE(Core)
|
---|
52 |
|
---|
53 | class QBitRef;
|
---|
54 | class Q_CORE_EXPORT QBitArray
|
---|
55 | {
|
---|
56 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
|
---|
57 | friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
|
---|
58 | friend Q_CORE_EXPORT uint qHash(const QBitArray &key);
|
---|
59 | QByteArray d;
|
---|
60 |
|
---|
61 | public:
|
---|
62 | inline QBitArray() {}
|
---|
63 | explicit QBitArray(int size, bool val = false);
|
---|
64 | QBitArray(const QBitArray &other) : d(other.d) {}
|
---|
65 | inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
|
---|
66 |
|
---|
67 | inline int size() const { return (d.size() << 3) - *d.constData(); }
|
---|
68 | inline int count() const { return (d.size() << 3) - *d.constData(); }
|
---|
69 | int count(bool on) const;
|
---|
70 | // ### Qt 5: Store the number of set bits separately
|
---|
71 |
|
---|
72 | inline bool isEmpty() const { return d.isEmpty(); }
|
---|
73 | inline bool isNull() const { return d.isNull(); }
|
---|
74 |
|
---|
75 | void resize(int size);
|
---|
76 |
|
---|
77 | inline void detach() { d.detach(); }
|
---|
78 | inline bool isDetached() const { return d.isDetached(); }
|
---|
79 | inline void clear() { d.clear(); }
|
---|
80 |
|
---|
81 | bool testBit(int i) const;
|
---|
82 | void setBit(int i);
|
---|
83 | void setBit(int i, bool val);
|
---|
84 | void clearBit(int i);
|
---|
85 | bool toggleBit(int i);
|
---|
86 |
|
---|
87 | bool at(int i) const;
|
---|
88 | QBitRef operator[](int i);
|
---|
89 | bool operator[](int i) const;
|
---|
90 | QBitRef operator[](uint i);
|
---|
91 | bool operator[](uint i) const;
|
---|
92 |
|
---|
93 | QBitArray& operator&=(const QBitArray &);
|
---|
94 | QBitArray& operator|=(const QBitArray &);
|
---|
95 | QBitArray& operator^=(const QBitArray &);
|
---|
96 | QBitArray operator~() const;
|
---|
97 |
|
---|
98 | inline bool operator==(const QBitArray& a) const { return d == a.d; }
|
---|
99 | inline bool operator!=(const QBitArray& a) const { return d != a.d; }
|
---|
100 |
|
---|
101 | inline bool fill(bool val, int size = -1);
|
---|
102 | void fill(bool val, int first, int last);
|
---|
103 |
|
---|
104 | inline void truncate(int pos) { if (pos < size()) resize(pos); }
|
---|
105 |
|
---|
106 | public:
|
---|
107 | typedef QByteArray::DataPtr DataPtr;
|
---|
108 | inline DataPtr &data_ptr() { return d.data_ptr(); }
|
---|
109 | };
|
---|
110 |
|
---|
111 | inline bool QBitArray::fill(bool aval, int asize)
|
---|
112 | { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
|
---|
113 |
|
---|
114 | Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
|
---|
115 | Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
|
---|
116 | Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
|
---|
117 |
|
---|
118 | inline bool QBitArray::testBit(int i) const
|
---|
119 | { Q_ASSERT(i >= 0 && i < size());
|
---|
120 | return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
|
---|
121 |
|
---|
122 | inline void QBitArray::setBit(int i)
|
---|
123 | { Q_ASSERT(i >= 0 && i < size());
|
---|
124 | *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
|
---|
125 |
|
---|
126 | inline void QBitArray::clearBit(int i)
|
---|
127 | { Q_ASSERT(i >= 0 && i < size());
|
---|
128 | *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
|
---|
129 |
|
---|
130 | inline void QBitArray::setBit(int i, bool val)
|
---|
131 | { if (val) setBit(i); else clearBit(i); }
|
---|
132 |
|
---|
133 | inline bool QBitArray::toggleBit(int i)
|
---|
134 | { Q_ASSERT(i >= 0 && i < size());
|
---|
135 | uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
|
---|
136 | uchar c = uchar(*p&b); *p^=b; return c!=0; }
|
---|
137 |
|
---|
138 | inline bool QBitArray::operator[](int i) const { return testBit(i); }
|
---|
139 | inline bool QBitArray::operator[](uint i) const { return testBit(i); }
|
---|
140 | inline bool QBitArray::at(int i) const { return testBit(i); }
|
---|
141 |
|
---|
142 | class Q_CORE_EXPORT QBitRef
|
---|
143 | {
|
---|
144 | private:
|
---|
145 | QBitArray& a;
|
---|
146 | int i;
|
---|
147 | inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {}
|
---|
148 | friend class QBitArray;
|
---|
149 | public:
|
---|
150 | inline operator bool() const { return a.testBit(i); }
|
---|
151 | inline bool operator!() const { return !a.testBit(i); }
|
---|
152 | QBitRef& operator=(const QBitRef& val) { a.setBit(i, val); return *this; }
|
---|
153 | QBitRef& operator=(bool val) { a.setBit(i, val); return *this; }
|
---|
154 | };
|
---|
155 |
|
---|
156 | inline QBitRef QBitArray::operator[](int i)
|
---|
157 | { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
|
---|
158 | inline QBitRef QBitArray::operator[](uint i)
|
---|
159 | { return QBitRef(*this, i); }
|
---|
160 |
|
---|
161 |
|
---|
162 | #ifndef QT_NO_DATASTREAM
|
---|
163 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
|
---|
164 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE);
|
---|
168 | Q_DECLARE_SHARED(QBitArray)
|
---|
169 |
|
---|
170 | QT_END_NAMESPACE
|
---|
171 |
|
---|
172 | QT_END_HEADER
|
---|
173 |
|
---|
174 | #endif // QBITARRAY_H
|
---|