1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "qbitarray.h"
30
#include <qdatastream.h>
35
\brief The QBitArray class provides an array of bits.
41
A QBitArray is an array that gives access to individual bits and
42
provides operators (\link operator&() AND\endlink, \link
43
operator|() OR\endlink, \link operator^() XOR\endlink, and \link
44
operator~() NOT\endlink) that work on entire arrays of bits. It
45
uses \l{implicit sharing} (copy-on-write) to reduce memory usage
46
and to avoid the needless copying of data.
48
The following code constructs a QBitArray containing 200 bits
49
initialized to false (0):
55
To initialize the bits to true, either pass \c true as second
56
argument to the constructor, or call fill() later on.
58
QBitArray uses 0-based indexes, just like C++ arrays. To access
59
the bit at a particular index position, you can use operator[]().
60
On non-const bit arrays, operator[]() returns a reference to a
61
bit that can be used on the left side of an assignment. For
72
For technical reasons, it is more efficient to use testBit() and
73
setBit() to access bits in the array than operator[](). For
83
QBitArray supports \c{&} (\link operator&() AND\endlink), \c{|}
84
(\link operator|() OR\endlink), \c{^} (\link operator^()
85
XOR\endlink), \c{~} (\link operator~() NOT\endlink), as well as
86
\c{&=}, \c{|=}, and \c{^=}. These operators work in the same way
87
as the built-in C++ bitwise operators of the same name. For
93
// x: [ 0, 0, 0, 1, 0 ]
97
// y: [ 0, 0, 0, 0, 1 ]
100
// x: [ 0, 0, 0, 1, 1 ]
103
For historical reasons, QBitArray distinguishes between a null
104
bit array and an empty bit array. A \e null bit array is a bit
105
array that is initialized using QBitArray's default constructor.
106
An \e empty bit array is any bit array with size 0. A null bit
107
array is always empty, but an empty bit array isn't necessarily
111
QBitArray().isNull(); // returns true
112
QBitArray().isEmpty(); // returns true
114
QBitArray(0).isNull(); // returns false
115
QBitArray(0).isEmpty(); // returns true
117
QBitArray(3).isNull(); // returns false
118
QBitArray(3).isEmpty(); // returns false
121
All functions except isNull() treat null bit arrays the same as
122
empty bit arrays; for example, QBitArray() compares equal to
123
QBitArray(0). We recommend that you always use isEmpty() and
126
\sa QByteArray, QVector
129
/*! \fn QBitArray::QBitArray()
131
Constructs an empty bit array.
137
Constructs a bit array containing \a size bits. The bits are
138
initialized with \a value, which defaults to false (0).
140
QBitArray::QBitArray(int size, bool value)
146
d.resize(1 + (size+7)/8);
147
uchar* c = reinterpret_cast<uchar*>(d.data());
148
memset(c, value ? 0xff : 0, d.size());
149
*c = d.size()*8 - size;
150
if (value && size && size % 8)
151
*(c+1+size/8) &= (1 << (size%8)) - 1;
154
/*! \fn int QBitArray::size() const
156
Returns the number of bits stored in the bit array.
161
/*! \fn int QBitArray::count() const
167
Resizes the bit array to \a size bits.
169
If \a size is greater than the current size, the bit array is
170
extended to make it \a size bits with the extra bits added to the
171
end. The new bits are initialized to false (0).
173
If \a size is less than the current size, bits are removed from
178
void QBitArray::resize(int size)
184
d.resize(1 + (size+7)/8);
185
uchar* c = reinterpret_cast<uchar*>(d.data());
187
memset(c + s, 0, d.size() - s);
188
*c = d.size()*8 - size;
192
/*! \fn bool QBitArray::isEmpty() const
194
Returns true if this bit array has size 0; otherwise returns
200
/*! \fn bool QBitArray::isNull() const
202
Returns true if this bit array is null; otherwise returns false.
206
QBitArray().isNull(); // returns true
207
QBitArray(0).isNull(); // returns false
208
QBitArray(3).isNull(); // returns false
211
Qt makes a distinction between null bit arrays and empty bit
212
arrays for historical reasons. For most applications, what
213
matters is whether or not a bit array contains any data,
214
and this can be determined using isEmpty().
219
/*! \fn bool QBitArray::fill(bool value, int size = -1)
221
Sets every bit in the bit array to \a value. If \a size is
222
different from -1 (the default), the bit array is resized to \a
229
// ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
241
Sets bits at index positions \a begin up to and excluding \a end
244
\a begin and \a end must be a valid index position in the bit
245
array (i.e., 0 <= \a begin <= size() and 0 <= \a end <= size()).
248
void QBitArray::fill(bool value, int begin, int end)
250
while (begin < end && begin & 0x7)
251
setBit(begin++, value);
252
int len = end - begin;
256
uchar *c = reinterpret_cast<uchar*>(d.data());
257
memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
260
setBit(begin++, value);
263
/*! \fn bool QBitArray::isDetached() const
268
/*! \fn void QBitArray::detach()
273
/*! \fn void QBitArray::clear()
275
Clears the contents of the bit array and makes it empty.
277
\sa resize(), isEmpty()
280
/*! \fn void QBitArray::truncate(int pos)
282
Truncates the bit array at index position \a pos.
284
If \a pos is beyond the end of the array, nothing happens.
289
/*! \fn bool QBitArray::toggleBit(int i)
291
Inverts the value of the bit at index position \a i.
293
If the previous value was 0, the new value will be 1. If the
294
previous value was 1, the new value will be 0.
296
\a i must be a valid index position in the bit array (i.e., 0 <=
299
\sa setBit(), clearBit()
302
/*! \fn bool QBitArray::testBit(int i) const
304
Returns true if the bit at index position \a i is 1; otherwise
307
\a i must be a valid index position in the bit array (i.e., 0 <=
310
\sa setBit(), clearBit()
313
/*! \fn bool QBitArray::setBit(int i)
315
Sets the bit at index position \a i to 1.
317
\a i must be a valid index position in the bit array (i.e., 0 <=
320
\sa clearBit(), toggleBit()
323
/*! \fn void QBitArray::setBit(int i, bool value)
327
Sets the bit at index position \a i to \a value.
330
/*! \fn void QBitArray::clearBit(int i)
332
Sets the bit at index position \a i to 0.
334
\a i must be a valid index position in the bit array (i.e., 0 <=
337
\sa setBit(), toggleBit()
340
/*! \fn bool QBitArray::at(int i) const
342
Returns the value of the bit at index position \a i.
344
\a i must be a valid index position in the bit array (i.e., 0 <=
350
/*! \fn QBitRef QBitArray::operator[](int i)
352
Returns the bit at index position \a i as a modifiable reference.
354
\a i must be a valid index position in the bit array (i.e., 0 <=
365
The return value is of type QBitRef, a helper class for QBitArray.
366
When you get an object of type QBitRef, you can assign to
367
it, and the assignment will apply to the bit in the QBitArray
368
from which you got the reference.
370
The functions testBit(), setBit(), and clearBit() are slightly
373
\sa at(), testBit(), setBit(), clearBit()
376
/*! \fn bool QBitArray::operator[](int i) const
381
/*! \fn bool QBitArray::operator[](uint i)
386
/*! \fn bool QBitArray::operator[](uint i) const
391
/*! \fn QBitArray::QBitArray(const QBitArray &other)
393
Constructs a copy of \a other.
395
This operation takes \l{constant time}, because QBitArray is
396
\l{implicitly shared}. This makes returning a QBitArray from a
397
function very fast. If a shared instance is modified, it will be
398
copied (copy-on-write), and that takes \l{linear time}.
403
/*! \fn QBitArray &QBitArray::operator=(const QBitArray &other)
405
Assigns \a other to this bit array and returns a reference to
409
/*! \fn bool QBitArray::operator==(const QBitArray &other) const
411
Returns true if \a other is equal to this bit array; otherwise
417
/*! \fn bool QBitArray::operator!=(const QBitArray &other) const
419
Returns true if \a other is not equal to this bit array;
420
otherwise returns false.
426
Performs the AND operation between all bits in this bit array and
427
\a other. Assigns the result to this bit array, and returns a
430
The result has the length of the longest of the two bit arrays,
431
with any missing bits (if one array is shorter than the other)
438
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
439
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
440
a &= b; // a: [ 1, 0, 0 ]
443
\sa operator&(), operator|=(), operator^=(), operator~()
446
QBitArray &QBitArray::operator&=(const QBitArray &other)
448
resize(qMax(size(), other.size()));
449
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
450
const uchar *a2 = reinterpret_cast<const uchar*>(other.d.constData()) + 1;
451
int n = other.d.size() -1 ;
452
int p = d.size() - 1 - n;
461
Performs the OR operation between all bits in this bit array and
462
\a other. Assigns the result to this bit array, and returns a
465
The result has the length of the longest of the two bit arrays,
466
with any missing bits (if one array is shorter than the other)
473
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
474
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
475
a |= b; // a: [ 1, 1, 1 ]
478
\sa operator|(), operator&=(), operator^=(), operator~()
481
QBitArray &QBitArray::operator|=(const QBitArray &other)
483
resize(qMax(size(), other.size()));
484
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
485
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
486
int n = other.d.size() - 1;
493
Performs the XOR operation between all bits in this bit array and
494
\a other. Assigns the result to this bit array, and returns a
497
The result has the length of the longest of the two bit arrays,
498
with any missing bits (if one array is shorter than the other)
505
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
506
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
507
a ^= b; // a: [ 0, 1, 1 ]
510
\sa operator^(), operator&=(), operator|=(), operator~()
513
QBitArray &QBitArray::operator^=(const QBitArray &other)
515
resize(qMax(size(), other.size()));
516
uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1;
517
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
518
int n = other.d.size() - 1;
525
Returns a bit array that contains the inverted bits of this bit
532
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
533
b = ~a; // b: [ 0, 1, 0 ]
536
\sa operator&(), operator|(), operator^()
539
QBitArray QBitArray::operator~() const
543
const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1;
544
uchar *a2 = reinterpret_cast<uchar*>(a.d.data()) + 1;
545
int n = d.size() - 1;
549
*(a2-1) &= (1 << (sz%8)) - 1;
556
Returns a bit array that is the AND of the bit arrays \a a1 and \a
559
The result has the length of the longest of the two bit arrays,
560
with any missing bits (if one array is shorter than the other)
568
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
569
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
570
c = a & b; // c: [ 1, 0, 0 ]
573
\sa QBitArray::operator&=(), operator|(), operator^()
576
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
586
Returns a bit array that is the OR of the bit arrays \a a1 and \a
589
The result has the length of the longest of the two bit arrays,
590
with any missing bits (if one array is shorter than the other)
598
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
599
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
600
c = a | b; // c: [ 1, 1, 1 ]
603
\sa QBitArray::operator|=(), operator&(), operator^()
606
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
616
Returns a bit array that is the XOR of the bit arrays \a a1 and \a
619
The result has the length of the longest of the two bit arrays,
620
with any missing bits (if one array is shorter than the other)
628
a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ]
629
b[0] = 1; b[1] = 0; // b: [ 1, 1 ]
630
c = a ^ b; // c: [ 0, 1, 1 ]
633
\sa QBitArray::operator^=(), operator&(), operator|()
636
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
646
\brief The QBitRef class is an internal class, used with QBitArray.
650
The QBitRef is required by the indexing [] operator on bit arrays.
651
It is not for use in any other context.
654
/*! \fn QBitRef::QBitRef (QBitArray& a, int i)
656
Constructs a reference to element \a i in the QBitArray \a a.
657
This is what QBitArray::operator[] constructs its return value
661
/*! \fn QBitRef::operator bool() const
663
Returns the value referenced by the QBitRef.
666
/*! \fn bool QBitRef::operator!() const
671
/*! \fn QBitRef& QBitRef::operator= (const QBitRef& v)
673
Sets the value referenced by the QBitRef to that referenced by
677
/*! \fn QBitRef& QBitRef::operator= (bool v)
680
Sets the value referenced by the QBitRef to \a v.
684
/*****************************************************************************
685
QBitArray stream functions
686
*****************************************************************************/
691
Writes bit array \a ba to stream \a out.
693
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
695
#ifndef QT_NO_DATASTREAM
696
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
698
quint32 len = ba.size();
701
out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
708
Reads a bit array into \a ba from stream \a in.
710
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
713
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
719
const quint32 Step = 8 * 1024 * 1024;
720
quint32 allocated = 0;
722
while (allocated < len) {
723
int blockSize = qMin(Step, len - allocated);
724
ba.resize(allocated + blockSize);
725
if (in.readRawData(ba.d.data() + 1 + ((allocated + 7) / 8), (blockSize + 7) / 8) != (blockSize + 7) / 8) {
727
in.setStatus(QDataStream::ReadPastEnd);
730
allocated += blockSize;
733
int paddingMask = ~((0x1 << (len & 0x7)) - 1);
734
if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) {
736
in.setStatus(QDataStream::ReadCorruptData);