1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
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 | #ifdef Q_COMPILER_RVALUE_REFS |
67 | inline QBitArray &operator=(QBitArray &&other) |
68 | { qSwap(d, other.d); return *this; } |
69 | #endif |
70 | |
71 | inline void swap(QBitArray &other) { qSwap(d, other.d); } |
72 | |
73 | inline int size() const { return (d.size() << 3) - *d.constData(); } |
74 | inline int count() const { return (d.size() << 3) - *d.constData(); } |
75 | int count(bool on) const; |
76 | // ### Qt 5: Store the number of set bits separately |
77 | |
78 | inline bool isEmpty() const { return d.isEmpty(); } |
79 | inline bool isNull() const { return d.isNull(); } |
80 | |
81 | void resize(int size); |
82 | |
83 | inline void detach() { d.detach(); } |
84 | inline bool isDetached() const { return d.isDetached(); } |
85 | inline void clear() { d.clear(); } |
86 | |
87 | bool testBit(int i) const; |
88 | void setBit(int i); |
89 | void setBit(int i, bool val); |
90 | void clearBit(int i); |
91 | bool toggleBit(int i); |
92 | |
93 | bool at(int i) const; |
94 | QBitRef operator[](int i); |
95 | bool operator[](int i) const; |
96 | QBitRef operator[](uint i); |
97 | bool operator[](uint i) const; |
98 | |
99 | QBitArray& operator&=(const QBitArray &); |
100 | QBitArray& operator|=(const QBitArray &); |
101 | QBitArray& operator^=(const QBitArray &); |
102 | QBitArray operator~() const; |
103 | |
104 | inline bool operator==(const QBitArray& a) const { return d == a.d; } |
105 | inline bool operator!=(const QBitArray& a) const { return d != a.d; } |
106 | |
107 | inline bool fill(bool val, int size = -1); |
108 | void fill(bool val, int first, int last); |
109 | |
110 | inline void truncate(int pos) { if (pos < size()) resize(pos); } |
111 | |
112 | public: |
113 | typedef QByteArray::DataPtr DataPtr; |
114 | inline DataPtr &data_ptr() { return d.data_ptr(); } |
115 | }; |
116 | |
117 | inline bool QBitArray::fill(bool aval, int asize) |
118 | { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; } |
119 | |
120 | Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &); |
121 | Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &); |
122 | Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &); |
123 | |
124 | inline bool QBitArray::testBit(int i) const |
125 | { Q_ASSERT(uint(i) < uint(size())); |
126 | return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; } |
127 | |
128 | inline void QBitArray::setBit(int i) |
129 | { Q_ASSERT(uint(i) < uint(size())); |
130 | *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); } |
131 | |
132 | inline void QBitArray::clearBit(int i) |
133 | { Q_ASSERT(uint(i) < uint(size())); |
134 | *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); } |
135 | |
136 | inline void QBitArray::setBit(int i, bool val) |
137 | { if (val) setBit(i); else clearBit(i); } |
138 | |
139 | inline bool QBitArray::toggleBit(int i) |
140 | { Q_ASSERT(uint(i) < uint(size())); |
141 | uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3); |
142 | uchar c = uchar(*p&b); *p^=b; return c!=0; } |
143 | |
144 | inline bool QBitArray::operator[](int i) const { return testBit(i); } |
145 | inline bool QBitArray::operator[](uint i) const { return testBit(i); } |
146 | inline bool QBitArray::at(int i) const { return testBit(i); } |
147 | |
148 | class Q_CORE_EXPORT QBitRef |
149 | { |
150 | private: |
151 | QBitArray& a; |
152 | int i; |
153 | inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {} |
154 | friend class QBitArray; |
155 | public: |
156 | inline operator bool() const { return a.testBit(i); } |
157 | inline bool operator!() const { return !a.testBit(i); } |
158 | QBitRef& operator=(const QBitRef& val) { a.setBit(i, val); return *this; } |
159 | QBitRef& operator=(bool val) { a.setBit(i, val); return *this; } |
160 | }; |
161 | |
162 | inline QBitRef QBitArray::operator[](int i) |
163 | { Q_ASSERT(i >= 0); return QBitRef(*this, i); } |
164 | inline QBitRef QBitArray::operator[](uint i) |
165 | { return QBitRef(*this, i); } |
166 | |
167 | |
168 | #ifndef QT_NO_DATASTREAM |
169 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &); |
170 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &); |
171 | #endif |
172 | |
173 | Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE); |
174 | Q_DECLARE_SHARED(QBitArray) |
175 | |
176 | QT_END_NAMESPACE |
177 | |
178 | QT_END_HEADER |
179 | |
180 | #endif // QBITARRAY_H |
181 | |