1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "qbitarray.h"
43
#include <qdatastream.h>
52
\brief The QBitArray class provides an array of bits.
58
A QBitArray is an array that gives access to individual bits and
59
provides operators (\l{operator&()}{AND}, \l{operator|()}{OR},
60
\l{operator^()}{XOR}, and \l{operator~()}{NOT}) that work on
61
entire arrays of bits. It uses \l{implicit sharing} (copy-on-write)
62
to reduce memory usage and to avoid the needless copying of data.
64
The following code constructs a QBitArray containing 200 bits
65
initialized to false (0):
67
\snippet code/src_corelib_tools_qbitarray.cpp 0
69
To initialize the bits to true, either pass \c true as second
70
argument to the constructor, or call fill() later on.
72
QBitArray uses 0-based indexes, just like C++ arrays. To access
73
the bit at a particular index position, you can use operator[]().
74
On non-const bit arrays, operator[]() returns a reference to a
75
bit that can be used on the left side of an assignment. For
78
\snippet code/src_corelib_tools_qbitarray.cpp 1
80
For technical reasons, it is more efficient to use testBit() and
81
setBit() to access bits in the array than operator[](). For
84
\snippet code/src_corelib_tools_qbitarray.cpp 2
86
QBitArray supports \c{&} (\l{operator&()}{AND}), \c{|}
87
(\l{operator|()}{OR}), \c{^} (\l{operator^()}{XOR}),
88
\c{~} (\l{operator~()}{NOT}), as well as
89
\c{&=}, \c{|=}, and \c{^=}. These operators work in the same way
90
as the built-in C++ bitwise operators of the same name. For
93
\snippet code/src_corelib_tools_qbitarray.cpp 3
95
For historical reasons, QBitArray distinguishes between a null
96
bit array and an empty bit array. A \e null bit array is a bit
97
array that is initialized using QBitArray's default constructor.
98
An \e empty bit array is any bit array with size 0. A null bit
99
array is always empty, but an empty bit array isn't necessarily
102
\snippet code/src_corelib_tools_qbitarray.cpp 4
104
All functions except isNull() treat null bit arrays the same as
105
empty bit arrays; for example, QBitArray() compares equal to
106
QBitArray(0). We recommend that you always use isEmpty() and
109
\sa QByteArray, QVector
112
/*! \fn QBitArray::QBitArray()
114
Constructs an empty bit array.
120
Constructs a bit array containing \a size bits. The bits are
121
initialized with \a value, which defaults to false (0).
123
QBitArray::QBitArray(int size, bool value)
129
d.resize(1 + (size+7)/8);
130
uchar* c = reinterpret_cast<uchar*>(d.data());
131
memset(c, value ? 0xff : 0, d.size());
132
*c = d.size()*8 - size;
133
if (value && size && size % 8)
134
*(c+1+size/8) &= (1 << (size%8)) - 1;
137
/*! \fn int QBitArray::size() const
139
Returns the number of bits stored in the bit array.
144
/*! \fn int QBitArray::count() const
150
If \a on is true, this function returns the number of
151
1-bits stored in the bit array; otherwise the number
152
of 0-bits is returned.
154
int QBitArray::count(bool on) const
159
for (int i = 0; i < len; ++i)
160
numBits += testBit(i);
162
// See http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
163
const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1;
165
quint32 v = quint32(bits[0]) | (quint32(bits[1]) << 8) | (quint32(bits[2]) << 16) | (quint32(bits[3]) << 24);
166
quint32 c = ((v & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
167
c += (((v & 0xfff000) >> 12) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
168
c += ((v >> 24) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
174
quint32 v = quint32(bits[0]) | (quint32(bits[1]) << 8) | (quint32(bits[2]) << 16);
175
quint32 c = ((v & 0xfff) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
176
c += (((v & 0xfff000) >> 12) * Q_UINT64_C(0x1001001001001) & Q_UINT64_C(0x84210842108421)) % 0x1f;
182
if (bits[len / 8] & (1 << ((len - 1) & 7)))
187
return on ? numBits : size() - numBits;
191
Resizes the bit array to \a size bits.
193
If \a size is greater than the current size, the bit array is
194
extended to make it \a size bits with the extra bits added to the
195
end. The new bits are initialized to false (0).
197
If \a size is less than the current size, bits are removed from
202
void QBitArray::resize(int size)
208
d.resize(1 + (size+7)/8);
209
uchar* c = reinterpret_cast<uchar*>(d.data());
211
memset(c + s, 0, d.size() - s);
213
*(c+1+size/8) &= (1 << (size%8)) - 1;
214
*c = d.size()*8 - size;
218
/*! \fn bool QBitArray::isEmpty() const
220
Returns true if this bit array has size 0; otherwise returns
226
/*! \fn bool QBitArray::isNull() const
228
Returns true if this bit array is null; otherwise returns false.
231
\snippet code/src_corelib_tools_qbitarray.cpp 5
233
Qt makes a distinction between null bit arrays and empty bit
234
arrays for historical reasons. For most applications, what
235
matters is whether or not a bit array contains any data,
236
and this can be determined using isEmpty().
241
/*! \fn bool QBitArray::fill(bool value, int size = -1)
243
Sets every bit in the bit array to \a value, returning true if successful;
244
otherwise returns false. If \a size is different from -1 (the default),
245
the bit array is resized to \a size beforehand.
248
\snippet code/src_corelib_tools_qbitarray.cpp 6
256
Sets bits at index positions \a begin up to and excluding \a end
259
\a begin and \a end must be a valid index position in the bit
260
array (i.e., 0 <= \a begin <= size() and 0 <= \a end <= size()).
263
void QBitArray::fill(bool value, int begin, int end)
265
while (begin < end && begin & 0x7)
266
setBit(begin++, value);
267
int len = end - begin;
271
uchar *c = reinterpret_cast<uchar*>(d.data());
272
memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
275
setBit(begin++, value);
278
/*! \fn bool QBitArray::isDetached() const
283
/*! \fn void QBitArray::detach()
288
/*! \fn void QBitArray::clear()
290
Clears the contents of the bit array and makes it empty.
292
\sa resize(), isEmpty()
295
/*! \fn void QBitArray::truncate(int pos)
297
Truncates the bit array at index position \a pos.
299
If \a pos is beyond the end of the array, nothing happens.
304
/*! \fn bool QBitArray::toggleBit(int i)
306
Inverts the value of the bit at index position \a i, returning the
307
previous value of that bit as either true (if it was set) or false (if
310
If the previous value was 0, the new value will be 1. If the
311
previous value was 1, the new value will be 0.
313
\a i must be a valid index position in the bit array (i.e., 0 <=
316
\sa setBit(), clearBit()
319
/*! \fn bool QBitArray::testBit(int i) const
321
Returns true if the bit at index position \a i is 1; otherwise
324
\a i must be a valid index position in the bit array (i.e., 0 <=
327
\sa setBit(), clearBit()
330
/*! \fn bool QBitArray::setBit(int i)
332
Sets the bit at index position \a i to 1.
334
\a i must be a valid index position in the bit array (i.e., 0 <=
337
\sa clearBit(), toggleBit()
340
/*! \fn void QBitArray::setBit(int i, bool value)
344
Sets the bit at index position \a i to \a value.
347
/*! \fn void QBitArray::clearBit(int i)
349
Sets the bit at index position \a i to 0.
351
\a i must be a valid index position in the bit array (i.e., 0 <=
354
\sa setBit(), toggleBit()
357
/*! \fn bool QBitArray::at(int i) const
359
Returns the value of the bit at index position \a i.
361
\a i must be a valid index position in the bit array (i.e., 0 <=
367
/*! \fn QBitRef QBitArray::operator[](int i)
369
Returns the bit at index position \a i as a modifiable reference.
371
\a i must be a valid index position in the bit array (i.e., 0 <=
375
\snippet code/src_corelib_tools_qbitarray.cpp 7
377
The return value is of type QBitRef, a helper class for QBitArray.
378
When you get an object of type QBitRef, you can assign to
379
it, and the assignment will apply to the bit in the QBitArray
380
from which you got the reference.
382
The functions testBit(), setBit(), and clearBit() are slightly
385
\sa at(), testBit(), setBit(), clearBit()
388
/*! \fn bool QBitArray::operator[](int i) const
393
/*! \fn QBitRef QBitArray::operator[](uint i)
398
/*! \fn bool QBitArray::operator[](uint i) const
403
/*! \fn QBitArray::QBitArray(const QBitArray &other)
405
Constructs a copy of \a other.
407
This operation takes \l{constant time}, because QBitArray is
408
\l{implicitly shared}. This makes returning a QBitArray from a
409
function very fast. If a shared instance is modified, it will be
410
copied (copy-on-write), and that takes \l{linear time}.
415
/*! \fn QBitArray &QBitArray::operator=(const QBitArray &other)
417
Assigns \a other to this bit array and returns a reference to
421
/*! \fn QBitArray &QBitArray::operator=(QBitArray &&other)
423
Moves \a other to this bit array and returns a reference to
427
/*! \fn void QBitArray::swap(QBitArray &other)
430
Swaps bit array \a other with this bit array. This operation is very
431
fast and never fails.
434
/*! \fn bool QBitArray::operator==(const QBitArray &other) const
436
Returns true if \a other is equal to this bit array; otherwise
442
/*! \fn bool QBitArray::operator!=(const QBitArray &other) const
444
Returns true if \a other is not equal to this bit array;
445
otherwise returns false.
451
Performs the AND operation between all bits in this bit array and
452
\a other. Assigns the result to this bit array, and returns a
455
The result has the length of the longest of the two bit arrays,
456
with any missing bits (if one array is shorter than the other)
460
\snippet code/src_corelib_tools_qbitarray.cpp 8
462
\sa operator&(), operator|=(), operator^=(), operator~()
465
QBitArray &QBitArray::operator&=(const QBitArray &other)
467
resize(qMax(size(), other.size()));
468
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
469
const uchar *a2 = reinterpret_cast<const uchar*>(other.d.constData()) + 1;
470
int n = other.d.size() -1 ;
471
int p = d.size() - 1 - n;
480
Performs the OR operation between all bits in this bit array and
481
\a other. Assigns the result to this bit array, and returns a
484
The result has the length of the longest of the two bit arrays,
485
with any missing bits (if one array is shorter than the other)
489
\snippet code/src_corelib_tools_qbitarray.cpp 9
491
\sa operator|(), operator&=(), operator^=(), operator~()
494
QBitArray &QBitArray::operator|=(const QBitArray &other)
496
resize(qMax(size(), other.size()));
497
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
498
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
499
int n = other.d.size() - 1;
506
Performs the XOR operation between all bits in this bit array and
507
\a other. Assigns the result to this bit array, and returns a
510
The result has the length of the longest of the two bit arrays,
511
with any missing bits (if one array is shorter than the other)
515
\snippet code/src_corelib_tools_qbitarray.cpp 10
517
\sa operator^(), operator&=(), operator|=(), operator~()
520
QBitArray &QBitArray::operator^=(const QBitArray &other)
522
resize(qMax(size(), other.size()));
523
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
524
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
525
int n = other.d.size() - 1;
532
Returns a bit array that contains the inverted bits of this bit
536
\snippet code/src_corelib_tools_qbitarray.cpp 11
538
\sa operator&(), operator|(), operator^()
541
QBitArray QBitArray::operator~() const
545
const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1;
546
uchar *a2 = reinterpret_cast<uchar*>(a.d.data()) + 1;
547
int n = d.size() - 1;
553
*(a2-1) &= (1 << (sz%8)) - 1;
560
Returns a bit array that is the AND of the bit arrays \a a1 and \a
563
The result has the length of the longest of the two bit arrays,
564
with any missing bits (if one array is shorter than the other)
568
\snippet code/src_corelib_tools_qbitarray.cpp 12
570
\sa QBitArray::operator&=(), operator|(), operator^()
573
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
583
Returns a bit array that is the OR of the bit arrays \a a1 and \a
586
The result has the length of the longest of the two bit arrays,
587
with any missing bits (if one array is shorter than the other)
591
\snippet code/src_corelib_tools_qbitarray.cpp 13
593
\sa QBitArray::operator|=(), operator&(), operator^()
596
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
606
Returns a bit array that is the XOR of the bit arrays \a a1 and \a
609
The result has the length of the longest of the two bit arrays,
610
with any missing bits (if one array is shorter than the other)
614
\snippet code/src_corelib_tools_qbitarray.cpp 14
616
\sa QBitArray::operator^=(), operator&(), operator|()
619
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
630
\brief The QBitRef class is an internal class, used with QBitArray.
634
The QBitRef is required by the indexing [] operator on bit arrays.
635
It is not for use in any other context.
638
/*! \fn QBitRef::QBitRef (QBitArray& a, int i)
640
Constructs a reference to element \a i in the QBitArray \a a.
641
This is what QBitArray::operator[] constructs its return value
645
/*! \fn QBitRef::operator bool() const
647
Returns the value referenced by the QBitRef.
650
/*! \fn bool QBitRef::operator!() const
655
/*! \fn QBitRef& QBitRef::operator= (const QBitRef& v)
657
Sets the value referenced by the QBitRef to that referenced by
661
/*! \fn QBitRef& QBitRef::operator= (bool v)
664
Sets the value referenced by the QBitRef to \a v.
668
/*****************************************************************************
669
QBitArray stream functions
670
*****************************************************************************/
672
#ifndef QT_NO_DATASTREAM
676
Writes bit array \a ba to stream \a out.
678
\sa {Serializing Qt Data Types}{Format of the QDataStream operators}
681
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
683
quint32 len = ba.size();
686
out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
693
Reads a bit array into \a ba from stream \a in.
695
\sa {Serializing Qt Data Types}{Format of the QDataStream operators}
698
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
708
const quint32 Step = 8 * 1024 * 1024;
709
quint32 totalBytes = (len + 7) / 8;
710
quint32 allocated = 0;
712
while (allocated < totalBytes) {
713
int blockSize = qMin(Step, totalBytes - allocated);
714
ba.d.resize(allocated + blockSize + 1);
715
if (in.readRawData(ba.d.data() + 1 + allocated, blockSize) != blockSize) {
717
in.setStatus(QDataStream::ReadPastEnd);
720
allocated += blockSize;
723
int paddingMask = ~((0x1 << (len & 0x7)) - 1);
724
if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) {
726
in.setStatus(QDataStream::ReadCorruptData);
730
*ba.d.data() = ba.d.size() * 8 - len;
733
#endif // QT_NO_DATASTREAM
735
#ifndef QT_NO_DEBUG_STREAM
736
QDebug operator<<(QDebug dbg, const QBitArray &array)
738
dbg.nospace() << "QBitArray(";
739
for (int i = 0; i < array.size();) {
740
if (array.testBit(i))
741
dbg.nospace() << '1';
743
dbg.nospace() << '0';
745
if (!(i % 4) && (i < array.size()))
746
dbg.nospace() << ' ';
748
dbg.nospace() << ')';
754
\fn DataPtr &QBitArray::data_ptr()
759
\typedef QBitArray::DataPtr