~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qbitarray.h"
 
30
#include <qdatastream.h>
 
31
#include <string.h>
 
32
 
 
33
/*!
 
34
    \class QBitArray
 
35
    \brief The QBitArray class provides an array of bits.
 
36
 
 
37
    \ingroup tools
 
38
    \ingroup shared
 
39
    \reentrant
 
40
 
 
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.
 
47
 
 
48
    The following code constructs a QBitArray containing 200 bits
 
49
    initialized to false (0):
 
50
 
 
51
    \code
 
52
        QBitArray ba(200);
 
53
    \endcode
 
54
 
 
55
    To initialize the bits to true, either pass \c true as second
 
56
    argument to the constructor, or call fill() later on.
 
57
 
 
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
 
62
    example:
 
63
 
 
64
    \code
 
65
        QBitArray ba;
 
66
        ba.resize(3);
 
67
        ba[0] = true;
 
68
        ba[1] = false;
 
69
        ba[2] = true;
 
70
    \endcode
 
71
 
 
72
    For technical reasons, it is more efficient to use testBit() and
 
73
    setBit() to access bits in the array than operator[](). For
 
74
    example:
 
75
 
 
76
    \code
 
77
        QBitArray ba(3);
 
78
        ba.setBit(0, true);
 
79
        ba.setBit(1, false);
 
80
        ba.setBit(2, true);
 
81
    \endcode
 
82
 
 
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
 
88
    example:
 
89
 
 
90
    \code
 
91
        QBitArray x(5);
 
92
        x.setBit(3, true);
 
93
        // x: [ 0, 0, 0, 1, 0 ]
 
94
 
 
95
        QBitArray y(5);
 
96
        y.setBit(4, true);
 
97
        // y: [ 0, 0, 0, 0, 1 ]
 
98
 
 
99
        x |= y;
 
100
        // x: [ 0, 0, 0, 1, 1 ]
 
101
    \endcode
 
102
 
 
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
 
108
    null:
 
109
 
 
110
    \code
 
111
        QBitArray().isNull();           // returns true
 
112
        QBitArray().isEmpty();          // returns true
 
113
 
 
114
        QBitArray(0).isNull();          // returns false
 
115
        QBitArray(0).isEmpty();         // returns true
 
116
 
 
117
        QBitArray(3).isNull();          // returns false
 
118
        QBitArray(3).isEmpty();         // returns false
 
119
    \endcode
 
120
 
 
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
 
124
    avoid isNull().
 
125
 
 
126
    \sa QByteArray, QVector
 
127
*/
 
128
 
 
129
/*! \fn QBitArray::QBitArray()
 
130
 
 
131
    Constructs an empty bit array.
 
132
 
 
133
    \sa isEmpty()
 
134
*/
 
135
 
 
136
/*!
 
137
    Constructs a bit array containing \a size bits. The bits are
 
138
    initialized with \a value, which defaults to false (0).
 
139
*/
 
140
QBitArray::QBitArray(int size, bool value)
 
141
{
 
142
    if (!size) {
 
143
        d.resize(0);
 
144
        return;
 
145
    }
 
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;
 
152
}
 
153
 
 
154
/*! \fn int QBitArray::size() const
 
155
 
 
156
    Returns the number of bits stored in the bit array.
 
157
 
 
158
    \sa resize()
 
159
*/
 
160
 
 
161
/*! \fn int QBitArray::count() const
 
162
 
 
163
    Same as size().
 
164
*/
 
165
 
 
166
/*!
 
167
    Resizes the bit array to \a size bits.
 
168
 
 
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).
 
172
 
 
173
    If \a size is less than the current size, bits are removed from
 
174
    the end.
 
175
 
 
176
    \sa size()
 
177
*/
 
178
void QBitArray::resize(int size)
 
179
{
 
180
    if (!size) {
 
181
        d.resize(0);
 
182
    } else {
 
183
        int s = d.size();
 
184
        d.resize(1 + (size+7)/8);
 
185
        uchar* c = reinterpret_cast<uchar*>(d.data());
 
186
        if (size > (s << 3))
 
187
            memset(c + s, 0, d.size() - s);
 
188
        *c = d.size()*8 - size;
 
189
    }
 
190
}
 
191
 
 
192
/*! \fn bool QBitArray::isEmpty() const
 
193
 
 
194
    Returns true if this bit array has size 0; otherwise returns
 
195
    false.
 
196
 
 
197
    \sa size()
 
198
*/
 
199
 
 
200
/*! \fn bool QBitArray::isNull() const
 
201
 
 
202
    Returns true if this bit array is null; otherwise returns false.
 
203
 
 
204
    Example:
 
205
    \code
 
206
        QBitArray().isNull();           // returns true
 
207
        QBitArray(0).isNull();          // returns false
 
208
        QBitArray(3).isNull();          // returns false
 
209
    \endcode
 
210
 
 
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().
 
215
 
 
216
    \sa isEmpty()
 
217
*/
 
218
 
 
219
/*! \fn bool QBitArray::fill(bool value, int size = -1)
 
220
 
 
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
 
223
    size beforehand.
 
224
 
 
225
    Example:
 
226
    \code
 
227
        QBitArray ba(8);
 
228
        ba.fill(true);
 
229
        // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ]
 
230
 
 
231
        ba.fill(false, 2);
 
232
        // ba: [ 0, 0 ]
 
233
    \endcode
 
234
 
 
235
    \sa resize()
 
236
*/
 
237
 
 
238
/*!
 
239
    \overload
 
240
 
 
241
    Sets bits at index positions \a begin up to and excluding \a end
 
242
    to \a value.
 
243
 
 
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()).
 
246
*/
 
247
 
 
248
void QBitArray::fill(bool value, int begin, int end)
 
249
{
 
250
    while (begin < end && begin & 0x7)
 
251
        setBit(begin++, value);
 
252
    int len = end - begin;
 
253
    if (len <= 0)
 
254
        return;
 
255
    int s = len & ~0x7;
 
256
    uchar *c = reinterpret_cast<uchar*>(d.data());
 
257
    memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
 
258
    begin += s;
 
259
    while (begin < end)
 
260
        setBit(begin++, value);
 
261
}
 
262
 
 
263
/*! \fn bool QBitArray::isDetached() const
 
264
 
 
265
    \internal
 
266
*/
 
267
 
 
268
/*! \fn void QBitArray::detach()
 
269
 
 
270
    \internal
 
271
*/
 
272
 
 
273
/*! \fn void QBitArray::clear()
 
274
 
 
275
    Clears the contents of the bit array and makes it empty.
 
276
 
 
277
    \sa resize(), isEmpty()
 
278
*/
 
279
 
 
280
/*! \fn void QBitArray::truncate(int pos)
 
281
 
 
282
    Truncates the bit array at index position \a pos.
 
283
 
 
284
    If \a pos is beyond the end of the array, nothing happens.
 
285
 
 
286
    \sa resize()
 
287
*/
 
288
 
 
289
/*! \fn bool QBitArray::toggleBit(int i)
 
290
 
 
291
    Inverts the value of the bit at index position \a i.
 
292
 
 
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.
 
295
 
 
296
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
297
    \a i < size()).
 
298
 
 
299
    \sa setBit(), clearBit()
 
300
*/
 
301
 
 
302
/*! \fn bool QBitArray::testBit(int i) const
 
303
 
 
304
    Returns true if the bit at index position \a i is 1; otherwise
 
305
    returns false.
 
306
 
 
307
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
308
    \a i < size()).
 
309
 
 
310
    \sa setBit(), clearBit()
 
311
*/
 
312
 
 
313
/*! \fn bool QBitArray::setBit(int i)
 
314
 
 
315
    Sets the bit at index position \a i to 1.
 
316
 
 
317
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
318
    \a i < size()).
 
319
 
 
320
    \sa clearBit(), toggleBit()
 
321
*/
 
322
 
 
323
/*! \fn void QBitArray::setBit(int i, bool value)
 
324
 
 
325
    \overload
 
326
 
 
327
    Sets the bit at index position \a i to \a value.
 
328
*/
 
329
 
 
330
/*! \fn void QBitArray::clearBit(int i)
 
331
 
 
332
    Sets the bit at index position \a i to 0.
 
333
 
 
334
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
335
    \a i < size()).
 
336
 
 
337
    \sa setBit(), toggleBit()
 
338
*/
 
339
 
 
340
/*! \fn bool QBitArray::at(int i) const
 
341
 
 
342
    Returns the value of the bit at index position \a i.
 
343
 
 
344
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
345
    \a i < size()).
 
346
 
 
347
    \sa operator[]()
 
348
*/
 
349
 
 
350
/*! \fn QBitRef QBitArray::operator[](int i)
 
351
 
 
352
    Returns the bit at index position \a i as a modifiable reference.
 
353
 
 
354
    \a i must be a valid index position in the bit array (i.e., 0 <=
 
355
    \a i < size()).
 
356
 
 
357
    Example:
 
358
    \code
 
359
        QBitArray a(3);
 
360
        a[0] = false;
 
361
        a[1] = true;
 
362
        a[2] = a[0] ^ a[1];
 
363
    \endcode
 
364
 
 
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.
 
369
 
 
370
    The functions testBit(), setBit(), and clearBit() are slightly
 
371
    faster.
 
372
 
 
373
    \sa at(), testBit(), setBit(), clearBit()
 
374
*/
 
375
 
 
376
/*! \fn bool QBitArray::operator[](int i) const
 
377
 
 
378
    \overload
 
379
*/
 
380
 
 
381
/*! \fn bool QBitArray::operator[](uint i)
 
382
 
 
383
    \overload
 
384
*/
 
385
 
 
386
/*! \fn bool QBitArray::operator[](uint i) const
 
387
 
 
388
    \overload
 
389
*/
 
390
 
 
391
/*! \fn QBitArray::QBitArray(const QBitArray &other)
 
392
 
 
393
    Constructs a copy of \a other.
 
394
 
 
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}.
 
399
 
 
400
    \sa operator=()
 
401
*/
 
402
 
 
403
/*! \fn QBitArray &QBitArray::operator=(const QBitArray &other)
 
404
 
 
405
    Assigns \a other to this bit array and returns a reference to
 
406
    this bit array.
 
407
*/
 
408
 
 
409
/*! \fn bool QBitArray::operator==(const QBitArray &other) const
 
410
 
 
411
    Returns true if \a other is equal to this bit array; otherwise
 
412
    returns false.
 
413
 
 
414
    \sa operator!=()
 
415
*/
 
416
 
 
417
/*! \fn bool QBitArray::operator!=(const QBitArray &other) const
 
418
 
 
419
    Returns true if \a other is not equal to this bit array;
 
420
    otherwise returns false.
 
421
 
 
422
    \sa operator==()
 
423
*/
 
424
 
 
425
/*!
 
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
 
428
    reference to it.
 
429
 
 
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)
 
432
    taken to be 0.
 
433
 
 
434
    Example:
 
435
    \code
 
436
        QBitArray a(3);
 
437
        QBitArray b(2);
 
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 ]
 
441
    \endcode
 
442
 
 
443
    \sa operator&(), operator|=(), operator^=(), operator~()
 
444
*/
 
445
 
 
446
QBitArray &QBitArray::operator&=(const QBitArray &other)
 
447
{
 
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;
 
453
    while (n-- > 0)
 
454
        *a1++ &= *a2++;
 
455
    while (p-- > 0)
 
456
        *a1++ = 0;
 
457
    return *this;
 
458
}
 
459
 
 
460
/*!
 
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
 
463
    reference to it.
 
464
 
 
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)
 
467
    taken to be 0.
 
468
 
 
469
    Example:
 
470
    \code
 
471
        QBitArray a(3);
 
472
        QBitArray b(2);
 
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 ]
 
476
    \endcode
 
477
 
 
478
    \sa operator|(), operator&=(), operator^=(), operator~()
 
479
*/
 
480
 
 
481
QBitArray &QBitArray::operator|=(const QBitArray &other)
 
482
{
 
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;   
 
487
    while (n-- > 0)
 
488
        *a1++ |= *a2++;
 
489
    return *this;
 
490
}
 
491
 
 
492
/*!
 
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
 
495
    reference to it.
 
496
 
 
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)
 
499
    taken to be 0.
 
500
 
 
501
    Example:
 
502
    \code
 
503
        QBitArray a(3);
 
504
        QBitArray b(2);
 
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 ]
 
508
    \endcode
 
509
 
 
510
    \sa operator^(), operator&=(), operator|=(), operator~()
 
511
*/
 
512
 
 
513
QBitArray &QBitArray::operator^=(const QBitArray &other)
 
514
{
 
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;
 
519
    while (n-- > 0)
 
520
        *a1++ ^= *a2++;
 
521
    return *this;
 
522
}
 
523
 
 
524
/*!
 
525
    Returns a bit array that contains the inverted bits of this bit
 
526
    array.
 
527
 
 
528
    Example:
 
529
    \code
 
530
        QBitArray a(3);
 
531
        QBitArray b;
 
532
        a[0] = 1; a[1] = 0; a[2] = 1;   // a: [ 1, 0, 1 ]
 
533
        b = ~a;                         // b: [ 0, 1, 0 ]
 
534
    \endcode
 
535
 
 
536
    \sa operator&(), operator|(), operator^()
 
537
*/
 
538
 
 
539
QBitArray QBitArray::operator~() const
 
540
{
 
541
    int sz = size();
 
542
    QBitArray a(sz);
 
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;
 
546
    while (n--)
 
547
        *a2++ = ~*a1++;
 
548
     if (sz && sz%8)
 
549
         *(a2-1) &= (1 << (sz%8)) - 1;
 
550
    return a;
 
551
}
 
552
 
 
553
/*!
 
554
    \relates QBitArray
 
555
 
 
556
    Returns a bit array that is the AND of the bit arrays \a a1 and \a
 
557
    a2.
 
558
 
 
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)
 
561
    taken to be 0.
 
562
 
 
563
    Example:
 
564
    \code
 
565
        QBitArray a(3);
 
566
        QBitArray b(2);
 
567
        QBitArray c;
 
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 ]
 
571
    \endcode
 
572
 
 
573
    \sa QBitArray::operator&=(), operator|(), operator^()
 
574
*/
 
575
 
 
576
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
 
577
{
 
578
    QBitArray tmp = a1;
 
579
    tmp &= a2;
 
580
    return tmp;
 
581
}
 
582
 
 
583
/*!
 
584
    \relates QBitArray
 
585
 
 
586
    Returns a bit array that is the OR of the bit arrays \a a1 and \a
 
587
    a2.
 
588
 
 
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)
 
591
    taken to be 0.
 
592
 
 
593
    Example:
 
594
    \code
 
595
        QBitArray a(3);
 
596
        QBitArray b(2);
 
597
        QBitArray c;
 
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 ]
 
601
    \endcode
 
602
 
 
603
    \sa QBitArray::operator|=(), operator&(), operator^()
 
604
*/
 
605
 
 
606
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
 
607
{
 
608
    QBitArray tmp = a1;
 
609
    tmp |= a2;
 
610
    return tmp;
 
611
}
 
612
 
 
613
/*!
 
614
    \relates QBitArray
 
615
 
 
616
    Returns a bit array that is the XOR of the bit arrays \a a1 and \a
 
617
    a2.
 
618
 
 
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)
 
621
    taken to be 0.
 
622
 
 
623
    Example:
 
624
    \code
 
625
        QBitArray a(3);
 
626
        QBitArray b(2);
 
627
        QBitArray c;
 
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 ]
 
631
    \endcode
 
632
 
 
633
    \sa QBitArray::operator^=(), operator&(), operator|()
 
634
*/
 
635
 
 
636
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
 
637
{
 
638
    QBitArray tmp = a1;
 
639
    tmp ^= a2;
 
640
    return tmp;
 
641
}
 
642
 
 
643
/*!
 
644
    \class QBitRef
 
645
    \reentrant
 
646
    \brief The QBitRef class is an internal class, used with QBitArray.
 
647
 
 
648
    \internal
 
649
 
 
650
    The QBitRef is required by the indexing [] operator on bit arrays.
 
651
    It is not for use in any other context.
 
652
*/
 
653
 
 
654
/*! \fn QBitRef::QBitRef (QBitArray& a, int i)
 
655
 
 
656
    Constructs a reference to element \a i in the QBitArray \a a.
 
657
    This is what QBitArray::operator[] constructs its return value
 
658
    with.
 
659
*/
 
660
 
 
661
/*! \fn QBitRef::operator bool() const
 
662
 
 
663
    Returns the value referenced by the QBitRef.
 
664
*/
 
665
 
 
666
/*! \fn bool QBitRef::operator!() const
 
667
 
 
668
    \internal
 
669
*/
 
670
 
 
671
/*! \fn QBitRef& QBitRef::operator= (const QBitRef& v)
 
672
 
 
673
    Sets the value referenced by the QBitRef to that referenced by
 
674
    QBitRef \a v.
 
675
*/
 
676
 
 
677
/*! \fn QBitRef& QBitRef::operator= (bool v)
 
678
    \overload
 
679
 
 
680
    Sets the value referenced by the QBitRef to \a v.
 
681
*/
 
682
 
 
683
 
 
684
/*****************************************************************************
 
685
  QBitArray stream functions
 
686
 *****************************************************************************/
 
687
 
 
688
/*!
 
689
    \relates QBitArray
 
690
 
 
691
    Writes bit array \a ba to stream \a out.
 
692
 
 
693
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
694
*/
 
695
#ifndef QT_NO_DATASTREAM
 
696
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
 
697
{
 
698
    quint32 len = ba.size();
 
699
    out << len;
 
700
    if (len > 0)
 
701
        out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
 
702
    return out;
 
703
}
 
704
 
 
705
/*!
 
706
    \relates QBitArray
 
707
 
 
708
    Reads a bit array into \a ba from stream \a in.
 
709
 
 
710
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
711
*/
 
712
 
 
713
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
 
714
{
 
715
    ba.clear();
 
716
    quint32 len;
 
717
    in >> len;
 
718
 
 
719
    const quint32 Step = 8 * 1024 * 1024;
 
720
    quint32 allocated = 0;
 
721
 
 
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) {
 
726
            ba.clear();
 
727
            in.setStatus(QDataStream::ReadPastEnd);
 
728
            return in;
 
729
        }
 
730
        allocated += blockSize;
 
731
    }
 
732
 
 
733
    int paddingMask = ~((0x1 << (len & 0x7)) - 1);
 
734
    if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) {
 
735
        ba.clear();
 
736
        in.setStatus(QDataStream::ReadCorruptData);
 
737
    }
 
738
    return in;
 
739
}
 
740
#endif