~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/tools/qbitarray.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
#include "qbitarray.h"
 
43
#include <qdatastream.h>
 
44
#include <qdebug.h>
 
45
#include <string.h>
 
46
 
 
47
QT_BEGIN_NAMESPACE
 
48
 
 
49
/*!
 
50
    \class QBitArray
 
51
    \inmodule QtCore
 
52
    \brief The QBitArray class provides an array of bits.
 
53
 
 
54
    \ingroup tools
 
55
    \ingroup shared
 
56
    \reentrant
 
57
 
 
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.
 
63
 
 
64
    The following code constructs a QBitArray containing 200 bits
 
65
    initialized to false (0):
 
66
 
 
67
    \snippet code/src_corelib_tools_qbitarray.cpp 0
 
68
 
 
69
    To initialize the bits to true, either pass \c true as second
 
70
    argument to the constructor, or call fill() later on.
 
71
 
 
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
 
76
    example:
 
77
 
 
78
    \snippet code/src_corelib_tools_qbitarray.cpp 1
 
79
 
 
80
    For technical reasons, it is more efficient to use testBit() and
 
81
    setBit() to access bits in the array than operator[](). For
 
82
    example:
 
83
 
 
84
    \snippet code/src_corelib_tools_qbitarray.cpp 2
 
85
 
 
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
 
91
    example:
 
92
 
 
93
    \snippet code/src_corelib_tools_qbitarray.cpp 3
 
94
 
 
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
 
100
    null:
 
101
 
 
102
    \snippet code/src_corelib_tools_qbitarray.cpp 4
 
103
 
 
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
 
107
    avoid isNull().
 
108
 
 
109
    \sa QByteArray, QVector
 
110
*/
 
111
 
 
112
/*! \fn QBitArray::QBitArray()
 
113
 
 
114
    Constructs an empty bit array.
 
115
 
 
116
    \sa isEmpty()
 
117
*/
 
118
 
 
119
/*!
 
120
    Constructs a bit array containing \a size bits. The bits are
 
121
    initialized with \a value, which defaults to false (0).
 
122
*/
 
123
QBitArray::QBitArray(int size, bool value)
 
124
{
 
125
    if (!size) {
 
126
        d.resize(0);
 
127
        return;
 
128
    }
 
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;
 
135
}
 
136
 
 
137
/*! \fn int QBitArray::size() const
 
138
 
 
139
    Returns the number of bits stored in the bit array.
 
140
 
 
141
    \sa resize()
 
142
*/
 
143
 
 
144
/*! \fn int QBitArray::count() const
 
145
 
 
146
    Same as size().
 
147
*/
 
148
 
 
149
/*!
 
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.
 
153
*/
 
154
int QBitArray::count(bool on) const
 
155
{
 
156
    int numBits = 0;
 
157
    int len = size();
 
158
#if 0
 
159
    for (int i = 0; i < len; ++i)
 
160
        numBits += testBit(i);
 
161
#else
 
162
    // See http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
 
163
    const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1;
 
164
    while (len >= 32) {
 
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;
 
169
        len -= 32;
 
170
        bits += 4;
 
171
        numBits += int(c);
 
172
    }
 
173
    while (len >= 24) {
 
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;    
 
177
        len -= 24;
 
178
        bits += 3;
 
179
        numBits += int(c);
 
180
    }
 
181
    while (len >= 0) {
 
182
        if (bits[len / 8] & (1 << ((len - 1) & 7)))
 
183
            ++numBits;
 
184
        --len;
 
185
    }
 
186
#endif
 
187
    return on ? numBits : size() - numBits;
 
188
}
 
189
 
 
190
/*!
 
191
    Resizes the bit array to \a size bits.
 
192
 
 
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).
 
196
 
 
197
    If \a size is less than the current size, bits are removed from
 
198
    the end.
 
199
 
 
200
    \sa size()
 
201
*/
 
202
void QBitArray::resize(int size)
 
203
{
 
204
    if (!size) {
 
205
        d.resize(0);
 
206
    } else {
 
207
        int s = d.size();
 
208
        d.resize(1 + (size+7)/8);
 
209
        uchar* c = reinterpret_cast<uchar*>(d.data());
 
210
        if (size > (s << 3))
 
211
            memset(c + s, 0, d.size() - s);
 
212
        else if ( size % 8)
 
213
            *(c+1+size/8) &= (1 << (size%8)) - 1;
 
214
        *c = d.size()*8 - size;
 
215
    }
 
216
}
 
217
 
 
218
/*! \fn bool QBitArray::isEmpty() const
 
219
 
 
220
    Returns true if this bit array has size 0; otherwise returns
 
221
    false.
 
222
 
 
223
    \sa size()
 
224
*/
 
225
 
 
226
/*! \fn bool QBitArray::isNull() const
 
227
 
 
228
    Returns true if this bit array is null; otherwise returns false.
 
229
 
 
230
    Example:
 
231
    \snippet code/src_corelib_tools_qbitarray.cpp 5
 
232
 
 
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().
 
237
 
 
238
    \sa isEmpty()
 
239
*/
 
240
 
 
241
/*! \fn bool QBitArray::fill(bool value, int size = -1)
 
242
 
 
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.
 
246
 
 
247
    Example:
 
248
    \snippet code/src_corelib_tools_qbitarray.cpp 6
 
249
 
 
250
    \sa resize()
 
251
*/
 
252
 
 
253
/*!
 
254
    \overload
 
255
 
 
256
    Sets bits at index positions \a begin up to and excluding \a end
 
257
    to \a value.
 
258
 
 
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()).
 
261
*/
 
262
 
 
263
void QBitArray::fill(bool value, int begin, int end)
 
264
{
 
265
    while (begin < end && begin & 0x7)
 
266
        setBit(begin++, value);
 
267
    int len = end - begin;
 
268
    if (len <= 0)
 
269
        return;
 
270
    int s = len & ~0x7;
 
271
    uchar *c = reinterpret_cast<uchar*>(d.data());
 
272
    memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
 
273
    begin += s;
 
274
    while (begin < end)
 
275
        setBit(begin++, value);
 
276
}
 
277
 
 
278
/*! \fn bool QBitArray::isDetached() const
 
279
 
 
280
    \internal
 
281
*/
 
282
 
 
283
/*! \fn void QBitArray::detach()
 
284
 
 
285
    \internal
 
286
*/
 
287
 
 
288
/*! \fn void QBitArray::clear()
 
289
 
 
290
    Clears the contents of the bit array and makes it empty.
 
291
 
 
292
    \sa resize(), isEmpty()
 
293
*/
 
294
 
 
295
/*! \fn void QBitArray::truncate(int pos)
 
296
 
 
297
    Truncates the bit array at index position \a pos.
 
298
 
 
299
    If \a pos is beyond the end of the array, nothing happens.
 
300
 
 
301
    \sa resize()
 
302
*/
 
303
 
 
304
/*! \fn bool QBitArray::toggleBit(int i)
 
305
 
 
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
 
308
    it was unset).
 
309
 
 
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.
 
312
 
 
313
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
314
    \a i < size()).
 
315
 
 
316
    \sa setBit(), clearBit()
 
317
*/
 
318
 
 
319
/*! \fn bool QBitArray::testBit(int i) const
 
320
 
 
321
    Returns true if the bit at index position \a i is 1; otherwise
 
322
    returns false.
 
323
 
 
324
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
325
    \a i < size()).
 
326
 
 
327
    \sa setBit(), clearBit()
 
328
*/
 
329
 
 
330
/*! \fn bool QBitArray::setBit(int i)
 
331
 
 
332
    Sets the bit at index position \a i to 1.
 
333
 
 
334
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
335
    \a i < size()).
 
336
 
 
337
    \sa clearBit(), toggleBit()
 
338
*/
 
339
 
 
340
/*! \fn void QBitArray::setBit(int i, bool value)
 
341
 
 
342
    \overload
 
343
 
 
344
    Sets the bit at index position \a i to \a value.
 
345
*/
 
346
 
 
347
/*! \fn void QBitArray::clearBit(int i)
 
348
 
 
349
    Sets the bit at index position \a i to 0.
 
350
 
 
351
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
352
    \a i < size()).
 
353
 
 
354
    \sa setBit(), toggleBit()
 
355
*/
 
356
 
 
357
/*! \fn bool QBitArray::at(int i) const
 
358
 
 
359
    Returns the value of the bit at index position \a i.
 
360
 
 
361
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
362
    \a i < size()).
 
363
 
 
364
    \sa operator[]()
 
365
*/
 
366
 
 
367
/*! \fn QBitRef QBitArray::operator[](int i)
 
368
 
 
369
    Returns the bit at index position \a i as a modifiable reference.
 
370
 
 
371
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
372
    \a i < size()).
 
373
 
 
374
    Example:
 
375
    \snippet code/src_corelib_tools_qbitarray.cpp 7
 
376
 
 
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.
 
381
 
 
382
    The functions testBit(), setBit(), and clearBit() are slightly
 
383
    faster.
 
384
 
 
385
    \sa at(), testBit(), setBit(), clearBit()
 
386
*/
 
387
 
 
388
/*! \fn bool QBitArray::operator[](int i) const
 
389
 
 
390
    \overload
 
391
*/
 
392
 
 
393
/*! \fn QBitRef QBitArray::operator[](uint i)
 
394
 
 
395
    \overload
 
396
*/
 
397
 
 
398
/*! \fn bool QBitArray::operator[](uint i) const
 
399
 
 
400
    \overload
 
401
*/
 
402
 
 
403
/*! \fn QBitArray::QBitArray(const QBitArray &other)
 
404
 
 
405
    Constructs a copy of \a other.
 
406
 
 
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}.
 
411
 
 
412
    \sa operator=()
 
413
*/
 
414
 
 
415
/*! \fn QBitArray &QBitArray::operator=(const QBitArray &other)
 
416
 
 
417
    Assigns \a other to this bit array and returns a reference to
 
418
    this bit array.
 
419
*/
 
420
 
 
421
/*! \fn QBitArray &QBitArray::operator=(QBitArray &&other)
 
422
 
 
423
    Moves \a other to this bit array and returns a reference to
 
424
    this bit array.
 
425
*/
 
426
 
 
427
/*! \fn void QBitArray::swap(QBitArray &other)
 
428
    \since 4.8
 
429
 
 
430
    Swaps bit array \a other with this bit array. This operation is very
 
431
    fast and never fails.
 
432
*/
 
433
 
 
434
/*! \fn bool QBitArray::operator==(const QBitArray &other) const
 
435
 
 
436
    Returns true if \a other is equal to this bit array; otherwise
 
437
    returns false.
 
438
 
 
439
    \sa operator!=()
 
440
*/
 
441
 
 
442
/*! \fn bool QBitArray::operator!=(const QBitArray &other) const
 
443
 
 
444
    Returns true if \a other is not equal to this bit array;
 
445
    otherwise returns false.
 
446
 
 
447
    \sa operator==()
 
448
*/
 
449
 
 
450
/*!
 
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
 
453
    reference to it.
 
454
 
 
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)
 
457
    taken to be 0.
 
458
 
 
459
    Example:
 
460
    \snippet code/src_corelib_tools_qbitarray.cpp 8
 
461
 
 
462
    \sa operator&(), operator|=(), operator^=(), operator~()
 
463
*/
 
464
 
 
465
QBitArray &QBitArray::operator&=(const QBitArray &other)
 
466
{
 
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;
 
472
    while (n-- > 0)
 
473
        *a1++ &= *a2++;
 
474
    while (p-- > 0)
 
475
        *a1++ = 0;
 
476
    return *this;
 
477
}
 
478
 
 
479
/*!
 
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
 
482
    reference to it.
 
483
 
 
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)
 
486
    taken to be 0.
 
487
 
 
488
    Example:
 
489
    \snippet code/src_corelib_tools_qbitarray.cpp 9
 
490
 
 
491
    \sa operator|(), operator&=(), operator^=(), operator~()
 
492
*/
 
493
 
 
494
QBitArray &QBitArray::operator|=(const QBitArray &other)
 
495
{
 
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;   
 
500
    while (n-- > 0)
 
501
        *a1++ |= *a2++;
 
502
    return *this;
 
503
}
 
504
 
 
505
/*!
 
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
 
508
    reference to it.
 
509
 
 
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)
 
512
    taken to be 0.
 
513
 
 
514
    Example:
 
515
    \snippet code/src_corelib_tools_qbitarray.cpp 10
 
516
 
 
517
    \sa operator^(), operator&=(), operator|=(), operator~()
 
518
*/
 
519
 
 
520
QBitArray &QBitArray::operator^=(const QBitArray &other)
 
521
{
 
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;
 
526
    while (n-- > 0)
 
527
        *a1++ ^= *a2++;
 
528
    return *this;
 
529
}
 
530
 
 
531
/*!
 
532
    Returns a bit array that contains the inverted bits of this bit
 
533
    array.
 
534
 
 
535
    Example:
 
536
    \snippet code/src_corelib_tools_qbitarray.cpp 11
 
537
 
 
538
    \sa operator&(), operator|(), operator^()
 
539
*/
 
540
 
 
541
QBitArray QBitArray::operator~() const
 
542
{
 
543
    int sz = size();
 
544
    QBitArray a(sz);
 
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;
 
548
 
 
549
    while (n-- > 0)
 
550
        *a2++ = ~*a1++;
 
551
 
 
552
    if (sz && sz%8)
 
553
        *(a2-1) &= (1 << (sz%8)) - 1;
 
554
    return a;
 
555
}
 
556
 
 
557
/*!
 
558
    \relates QBitArray
 
559
 
 
560
    Returns a bit array that is the AND of the bit arrays \a a1 and \a
 
561
    a2.
 
562
 
 
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)
 
565
    taken to be 0.
 
566
 
 
567
    Example:
 
568
    \snippet code/src_corelib_tools_qbitarray.cpp 12
 
569
 
 
570
    \sa QBitArray::operator&=(), operator|(), operator^()
 
571
*/
 
572
 
 
573
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
 
574
{
 
575
    QBitArray tmp = a1;
 
576
    tmp &= a2;
 
577
    return tmp;
 
578
}
 
579
 
 
580
/*!
 
581
    \relates QBitArray
 
582
 
 
583
    Returns a bit array that is the OR of the bit arrays \a a1 and \a
 
584
    a2.
 
585
 
 
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)
 
588
    taken to be 0.
 
589
 
 
590
    Example:
 
591
    \snippet code/src_corelib_tools_qbitarray.cpp 13
 
592
 
 
593
    \sa QBitArray::operator|=(), operator&(), operator^()
 
594
*/
 
595
 
 
596
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
 
597
{
 
598
    QBitArray tmp = a1;
 
599
    tmp |= a2;
 
600
    return tmp;
 
601
}
 
602
 
 
603
/*!
 
604
    \relates QBitArray
 
605
 
 
606
    Returns a bit array that is the XOR of the bit arrays \a a1 and \a
 
607
    a2.
 
608
 
 
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)
 
611
    taken to be 0.
 
612
 
 
613
    Example:
 
614
    \snippet code/src_corelib_tools_qbitarray.cpp 14
 
615
 
 
616
    \sa QBitArray::operator^=(), operator&(), operator|()
 
617
*/
 
618
 
 
619
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
 
620
{
 
621
    QBitArray tmp = a1;
 
622
    tmp ^= a2;
 
623
    return tmp;
 
624
}
 
625
 
 
626
/*!
 
627
    \class QBitRef
 
628
    \inmodule QtCore
 
629
    \reentrant
 
630
    \brief The QBitRef class is an internal class, used with QBitArray.
 
631
 
 
632
    \internal
 
633
 
 
634
    The QBitRef is required by the indexing [] operator on bit arrays.
 
635
    It is not for use in any other context.
 
636
*/
 
637
 
 
638
/*! \fn QBitRef::QBitRef (QBitArray& a, int i)
 
639
 
 
640
    Constructs a reference to element \a i in the QBitArray \a a.
 
641
    This is what QBitArray::operator[] constructs its return value
 
642
    with.
 
643
*/
 
644
 
 
645
/*! \fn QBitRef::operator bool() const
 
646
 
 
647
    Returns the value referenced by the QBitRef.
 
648
*/
 
649
 
 
650
/*! \fn bool QBitRef::operator!() const
 
651
 
 
652
    \internal
 
653
*/
 
654
 
 
655
/*! \fn QBitRef& QBitRef::operator= (const QBitRef& v)
 
656
 
 
657
    Sets the value referenced by the QBitRef to that referenced by
 
658
    QBitRef \a v.
 
659
*/
 
660
 
 
661
/*! \fn QBitRef& QBitRef::operator= (bool v)
 
662
    \overload
 
663
 
 
664
    Sets the value referenced by the QBitRef to \a v.
 
665
*/
 
666
 
 
667
 
 
668
/*****************************************************************************
 
669
  QBitArray stream functions
 
670
 *****************************************************************************/
 
671
 
 
672
#ifndef QT_NO_DATASTREAM
 
673
/*!
 
674
    \relates QBitArray
 
675
 
 
676
    Writes bit array \a ba to stream \a out.
 
677
 
 
678
    \sa {Serializing Qt Data Types}{Format of the QDataStream operators}
 
679
*/
 
680
 
 
681
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
 
682
{
 
683
    quint32 len = ba.size();
 
684
    out << len;
 
685
    if (len > 0)
 
686
        out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
 
687
    return out;
 
688
}
 
689
 
 
690
/*!
 
691
    \relates QBitArray
 
692
 
 
693
    Reads a bit array into \a ba from stream \a in.
 
694
 
 
695
    \sa {Serializing Qt Data Types}{Format of the QDataStream operators}
 
696
*/
 
697
 
 
698
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
 
699
{
 
700
    ba.clear();
 
701
    quint32 len;
 
702
    in >> len;
 
703
    if (len == 0) {
 
704
        ba.clear();
 
705
        return in;
 
706
    }
 
707
 
 
708
    const quint32 Step = 8 * 1024 * 1024;
 
709
    quint32 totalBytes = (len + 7) / 8;
 
710
    quint32 allocated = 0;
 
711
 
 
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) {
 
716
            ba.clear();
 
717
            in.setStatus(QDataStream::ReadPastEnd);
 
718
            return in;
 
719
        }
 
720
        allocated += blockSize;
 
721
    }
 
722
 
 
723
    int paddingMask = ~((0x1 << (len & 0x7)) - 1);
 
724
    if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) {
 
725
        ba.clear();
 
726
        in.setStatus(QDataStream::ReadCorruptData);
 
727
        return in;
 
728
    }
 
729
 
 
730
    *ba.d.data() = ba.d.size() * 8 - len;
 
731
    return in;
 
732
}
 
733
#endif // QT_NO_DATASTREAM
 
734
 
 
735
#ifndef QT_NO_DEBUG_STREAM
 
736
QDebug operator<<(QDebug dbg, const QBitArray &array)
 
737
{
 
738
    dbg.nospace() << "QBitArray(";
 
739
    for (int i = 0; i < array.size();) {
 
740
        if (array.testBit(i))
 
741
            dbg.nospace() << '1';
 
742
        else
 
743
            dbg.nospace() << '0';
 
744
        i += 1;
 
745
        if (!(i % 4) && (i < array.size()))
 
746
            dbg.nospace() << ' ';
 
747
    }
 
748
    dbg.nospace() << ')';
 
749
    return dbg.space();
 
750
}
 
751
#endif
 
752
 
 
753
/*!
 
754
    \fn DataPtr &QBitArray::data_ptr()
 
755
    \internal
 
756
*/
 
757
 
 
758
/*!
 
759
    \typedef QBitArray::DataPtr
 
760
    \internal
 
761
*/
 
762
 
 
763
QT_END_NAMESPACE