Qt 4.8
qbitarray.h
Go to the documentation of this file.
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 
48 
50 
51 QT_MODULE(Core)
52 
53 class QBitRef;
55 {
58  friend Q_CORE_EXPORT uint qHash(const QBitArray &key);
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:
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 
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 
149 {
150 private:
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 
163 { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
165 { return QBitRef(*this, i); }
166 
167 
168 #ifndef QT_NO_DATASTREAM
171 #endif
172 
175 
177 
179 
180 #endif // QBITARRAY_H
void clear()
Clears the contents of the bit array and makes it empty.
Definition: qbitarray.h:85
double d
Definition: qnumeric_p.h:62
QBitArray()
Constructs an empty bit array.
Definition: qbitarray.h:62
bool isDetached() const
Definition: qbitarray.h:84
uint qHash(const QProcEnvKey &key)
Definition: qprocess_p.h:96
QBitRef & operator=(const QBitRef &val)
Sets the value referenced by the QBitRef to that referenced by QBitRef v.
Definition: qbitarray.h:158
unsigned char c[8]
Definition: qnumeric_p.h:62
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
Data * d
Definition: qbytearray.h:386
#define at(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int i
Definition: qbitarray.h:152
QBitRef(QBitArray &array, int idx)
Constructs a reference to element i in the QBitArray a.
Definition: qbitarray.h:153
QBitArray(const QBitArray &other)
Constructs a copy of other.
Definition: qbitarray.h:64
bool isNull() const
Returns true if this bit array is null; otherwise returns false.
Definition: qbitarray.h:79
long ASN1_INTEGER_get ASN1_INTEGER * a
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_DECLARE_SHARED(TYPE)
Definition: qglobal.h:2214
QByteArray d
Definition: qbitarray.h:59
Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &)
unsigned char uchar
Definition: qglobal.h:994
bool fill(bool val, int size=-1)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false...
Definition: qbitarray.h:117
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QBitRef & operator=(bool val)
Sets the value referenced by the QBitRef to v.
Definition: qbitarray.h:159
DataPtr & data_ptr()
Definition: qbitarray.h:114
bool operator!=(const QBitArray &a) const
Returns true if other is not equal to this bit array; otherwise returns false.
Definition: qbitarray.h:105
bool operator!() const
Definition: qbitarray.h:157
bool toggleBit(int i)
Inverts the value of the bit at index position i, returning the previous value of that bit as either ...
Definition: qbitarray.h:139
unsigned int uint
Definition: qglobal.h:996
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QBitArray &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QBitArray &)
bool isNull() const
Returns true if this byte array is null; otherwise returns false.
void swap(QBitArray &other)
Swaps bit array other with this bit array.
Definition: qbitarray.h:71
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
QBitArray & a
Definition: qbitarray.h:151
bool isDetached() const
Definition: qbytearray.h:437
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
The QBitRef class is an internal class, used with QBitArray.
Definition: qbitarray.h:148
QByteArray::DataPtr DataPtr
Definition: qbitarray.h:113
bool operator==(const QBitArray &a) const
Returns true if other is equal to this bit array; otherwise returns false.
Definition: qbitarray.h:104
DataPtr & data_ptr()
Definition: qbytearray.h:397
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
Q_CORE_EXPORT QBitArray operator &(const QBitArray &, const QBitArray &)
void truncate(int pos)
Truncates the bit array at index position pos.
Definition: qbitarray.h:110
int key
int count() const
Same as size().
Definition: qbitarray.h:74
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QBitRef operator[](int i)
Returns the bit at index position i as a modifiable reference.
Definition: qbitarray.h:162
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &)
bool at(int i) const
Returns the value of the bit at index position i.
Definition: qbitarray.h:146
#define QT_END_HEADER
Definition: qglobal.h:137
int size() const
Returns the number of bits stored in the bit array.
Definition: qbitarray.h:73
void clearBit(int i)
Sets the bit at index position i to 0.
Definition: qbitarray.h:132
void detach()
Definition: qbytearray.h:435
QBitArray & operator=(const QBitArray &other)
Assigns other to this bit array and returns a reference to this bit array.
Definition: qbitarray.h:65
bool isEmpty() const
Returns true if this bit array has size 0; otherwise returns false.
Definition: qbitarray.h:78
void clear()
Clears the contents of the byte array and makes it empty.
void detach()
Definition: qbitarray.h:83