1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // Copyright (C) 2019 Intel Corporation. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "qbitarray.h" |
6 | #include <qalgorithms.h> |
7 | #include <qdatastream.h> |
8 | #include <qdebug.h> |
9 | #include <qendian.h> |
10 | #include <string.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | /*! |
15 | \class QBitArray |
16 | \inmodule QtCore |
17 | \brief The QBitArray class provides an array of bits. |
18 | |
19 | \ingroup tools |
20 | \ingroup shared |
21 | \reentrant |
22 | |
23 | A QBitArray is an array that gives access to individual bits and |
24 | provides operators (\l{operator&()}{AND}, \l{operator|()}{OR}, |
25 | \l{operator^()}{XOR}, and \l{operator~()}{NOT}) that work on |
26 | entire arrays of bits. It uses \l{implicit sharing} (copy-on-write) |
27 | to reduce memory usage and to avoid the needless copying of data. |
28 | |
29 | The following code constructs a QBitArray containing 200 bits |
30 | initialized to false (0): |
31 | |
32 | \snippet code/src_corelib_tools_qbitarray.cpp 0 |
33 | |
34 | To initialize the bits to true, either pass \c true as second |
35 | argument to the constructor, or call fill() later on. |
36 | |
37 | QBitArray uses 0-based indexes, just like C++ arrays. To access |
38 | the bit at a particular index position, you can use operator[](). |
39 | On non-const bit arrays, operator[]() returns a reference to a |
40 | bit that can be used on the left side of an assignment. For |
41 | example: |
42 | |
43 | \snippet code/src_corelib_tools_qbitarray.cpp 1 |
44 | |
45 | For technical reasons, it is more efficient to use testBit() and |
46 | setBit() to access bits in the array than operator[](). For |
47 | example: |
48 | |
49 | \snippet code/src_corelib_tools_qbitarray.cpp 2 |
50 | |
51 | QBitArray supports \c{&} (\l{operator&()}{AND}), \c{|} |
52 | (\l{operator|()}{OR}), \c{^} (\l{operator^()}{XOR}), |
53 | \c{~} (\l{operator~()}{NOT}), as well as |
54 | \c{&=}, \c{|=}, and \c{^=}. These operators work in the same way |
55 | as the built-in C++ bitwise operators of the same name. For |
56 | example: |
57 | |
58 | \snippet code/src_corelib_tools_qbitarray.cpp 3 |
59 | |
60 | For historical reasons, QBitArray distinguishes between a null |
61 | bit array and an empty bit array. A \e null bit array is a bit |
62 | array that is initialized using QBitArray's default constructor. |
63 | An \e empty bit array is any bit array with size 0. A null bit |
64 | array is always empty, but an empty bit array isn't necessarily |
65 | null: |
66 | |
67 | \snippet code/src_corelib_tools_qbitarray.cpp 4 |
68 | |
69 | All functions except isNull() treat null bit arrays the same as |
70 | empty bit arrays; for example, QBitArray() compares equal to |
71 | QBitArray(0). We recommend that you always use isEmpty() and |
72 | avoid isNull(). |
73 | |
74 | \sa QByteArray, QList |
75 | */ |
76 | |
77 | /*! |
78 | \fn QBitArray::QBitArray(QBitArray &&other) |
79 | |
80 | Move-constructs a QBitArray instance, making it point at the same |
81 | object that \a other was pointing to. |
82 | |
83 | \since 5.2 |
84 | */ |
85 | |
86 | /*! \fn QBitArray::QBitArray() |
87 | |
88 | Constructs an empty bit array. |
89 | |
90 | \sa isEmpty() |
91 | */ |
92 | |
93 | /* |
94 | * QBitArray construction note: |
95 | * |
96 | * We overallocate the byte array by 1 byte. The first user bit is at |
97 | * d.data()[1]. On the extra first byte, we store the difference between the |
98 | * number of bits in the byte array (including this byte) and the number of |
99 | * bits in the bit array. Therefore, for a non-empty QBitArray, it's always a |
100 | * number between 8 and 15. For the empty one, d is the an empty QByteArray and |
101 | * *d.constData() is the QByteArray's terminating NUL (0) byte. |
102 | * |
103 | * This allows for fast calculation of the bit array size: |
104 | * inline qsizetype size() const { return (d.size() << 3) - *d.constData(); } |
105 | */ |
106 | |
107 | /*! |
108 | Constructs a bit array containing \a size bits. The bits are |
109 | initialized with \a value, which defaults to false (0). |
110 | */ |
111 | QBitArray::QBitArray(qsizetype size, bool value) |
112 | : d(size <= 0 ? 0 : 1 + (size + 7) / 8, Qt::Uninitialized) |
113 | { |
114 | Q_ASSERT_X(size >= 0, "QBitArray::QBitArray" , "Size must be greater than or equal to 0." ); |
115 | if (size <= 0) |
116 | return; |
117 | |
118 | uchar *c = reinterpret_cast<uchar *>(d.data()); |
119 | memset(s: c + 1, c: value ? 0xff : 0, n: d.size() - 1); |
120 | *c = d.size() * 8 - size; |
121 | if (value && size && size & 7) |
122 | *(c + 1 + size / 8) &= (1 << (size & 7)) - 1; |
123 | } |
124 | |
125 | /*! \fn qsizetype QBitArray::size() const |
126 | |
127 | Returns the number of bits stored in the bit array. |
128 | |
129 | \sa resize() |
130 | */ |
131 | |
132 | /*! \fn qsizetype QBitArray::count() const |
133 | |
134 | Same as size(). |
135 | */ |
136 | |
137 | /*! |
138 | If \a on is true, this function returns the number of |
139 | 1-bits stored in the bit array; otherwise the number |
140 | of 0-bits is returned. |
141 | */ |
142 | qsizetype QBitArray::count(bool on) const |
143 | { |
144 | qsizetype numBits = 0; |
145 | const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1; |
146 | |
147 | // the loops below will try to read from *end |
148 | // it's the QByteArray implicit NUL, so it will not change the bit count |
149 | const quint8 *const end = reinterpret_cast<const quint8 *>(d.end()); |
150 | |
151 | while (bits + 7 <= end) { |
152 | quint64 v = qFromUnaligned<quint64>(src: bits); |
153 | bits += 8; |
154 | numBits += qsizetype(qPopulationCount(v)); |
155 | } |
156 | if (bits + 3 <= end) { |
157 | quint32 v = qFromUnaligned<quint32>(src: bits); |
158 | bits += 4; |
159 | numBits += qsizetype(qPopulationCount(v)); |
160 | } |
161 | if (bits + 1 < end) { |
162 | quint16 v = qFromUnaligned<quint16>(src: bits); |
163 | bits += 2; |
164 | numBits += qsizetype(qPopulationCount(v)); |
165 | } |
166 | if (bits < end) |
167 | numBits += qsizetype(qPopulationCount(v: bits[0])); |
168 | |
169 | return on ? numBits : size() - numBits; |
170 | } |
171 | |
172 | /*! |
173 | Resizes the bit array to \a size bits. |
174 | |
175 | If \a size is greater than the current size, the bit array is |
176 | extended to make it \a size bits with the extra bits added to the |
177 | end. The new bits are initialized to false (0). |
178 | |
179 | If \a size is less than the current size, bits are removed from |
180 | the end. |
181 | |
182 | \sa size() |
183 | */ |
184 | void QBitArray::resize(qsizetype size) |
185 | { |
186 | if (!size) { |
187 | d.resize(size: 0); |
188 | } else { |
189 | qsizetype s = d.size(); |
190 | d.resize(size: 1 + (size + 7) / 8); |
191 | uchar *c = reinterpret_cast<uchar *>(d.data()); |
192 | if (size > (s << 3)) |
193 | memset(s: c + s, c: 0, n: d.size() - s); |
194 | else if (size & 7) |
195 | *(c + 1 + size / 8) &= (1 << (size & 7)) - 1; |
196 | *c = d.size() * 8 - size; |
197 | } |
198 | } |
199 | |
200 | /*! \fn bool QBitArray::isEmpty() const |
201 | |
202 | Returns \c true if this bit array has size 0; otherwise returns |
203 | false. |
204 | |
205 | \sa size() |
206 | */ |
207 | |
208 | /*! \fn bool QBitArray::isNull() const |
209 | |
210 | Returns \c true if this bit array is null; otherwise returns \c false. |
211 | |
212 | Example: |
213 | \snippet code/src_corelib_tools_qbitarray.cpp 5 |
214 | |
215 | Qt makes a distinction between null bit arrays and empty bit |
216 | arrays for historical reasons. For most applications, what |
217 | matters is whether or not a bit array contains any data, |
218 | and this can be determined using isEmpty(). |
219 | |
220 | \sa isEmpty() |
221 | */ |
222 | |
223 | /*! \fn bool QBitArray::fill(bool value, qsizetype size = -1) |
224 | |
225 | Sets every bit in the bit array to \a value, returning true if successful; |
226 | otherwise returns \c false. If \a size is different from -1 (the default), |
227 | the bit array is resized to \a size beforehand. |
228 | |
229 | Example: |
230 | \snippet code/src_corelib_tools_qbitarray.cpp 6 |
231 | |
232 | \sa resize() |
233 | */ |
234 | |
235 | /*! |
236 | \overload |
237 | |
238 | Sets bits at index positions \a begin up to (but not including) \a end |
239 | to \a value. |
240 | |
241 | \a begin must be a valid index position in the bit array |
242 | (0 <= \a begin < size()). |
243 | |
244 | \a end must be either a valid index position or equal to size(), in |
245 | which case the fill operation runs until the end of the array |
246 | (0 <= \a end <= size()). |
247 | |
248 | Example: |
249 | \snippet code/src_corelib_tools_qbitarray.cpp 15 |
250 | */ |
251 | |
252 | void QBitArray::fill(bool value, qsizetype begin, qsizetype end) |
253 | { |
254 | while (begin < end && begin & 0x7) |
255 | setBit(i: begin++, val: value); |
256 | qsizetype len = end - begin; |
257 | if (len <= 0) |
258 | return; |
259 | qsizetype s = len & ~qsizetype(0x7); |
260 | uchar *c = reinterpret_cast<uchar *>(d.data()); |
261 | memset(s: c + (begin >> 3) + 1, c: value ? 0xff : 0, n: s >> 3); |
262 | begin += s; |
263 | while (begin < end) |
264 | setBit(i: begin++, val: value); |
265 | } |
266 | |
267 | /*! |
268 | \fn const char *QBitArray::bits() const |
269 | \since 5.11 |
270 | |
271 | Returns a pointer to a dense bit array for this QBitArray. Bits are counted |
272 | upwards from the least significant bit in each byte. The number of bits |
273 | relevant in the last byte is given by \c{size() % 8}. |
274 | |
275 | \sa fromBits(), size() |
276 | */ |
277 | |
278 | /*! |
279 | \since 5.11 |
280 | |
281 | Creates a QBitArray with the dense bit array located at \a data, with \a |
282 | size bits. The byte array at \a data must be at least \a size / 8 (rounded up) |
283 | bytes long. |
284 | |
285 | If \a size is not a multiple of 8, this function will include the lowest |
286 | \a size % 8 bits from the last byte in \a data. |
287 | |
288 | \sa bits() |
289 | */ |
290 | QBitArray QBitArray::fromBits(const char *data, qsizetype size) |
291 | { |
292 | QBitArray result; |
293 | if (size == 0) |
294 | return result; |
295 | qsizetype nbytes = (size + 7) / 8; |
296 | |
297 | result.d = QByteArray(nbytes + 1, Qt::Uninitialized); |
298 | char *bits = result.d.data(); |
299 | memcpy(dest: bits + 1, src: data, n: nbytes); |
300 | |
301 | // clear any unused bits from the last byte |
302 | if (size & 7) |
303 | bits[nbytes] &= 0xffU >> (8 - (size & 7)); |
304 | |
305 | *bits = result.d.size() * 8 - size; |
306 | return result; |
307 | } |
308 | |
309 | /*! |
310 | \since 6.0 |
311 | |
312 | Returns the array of bit converted to an int. The conversion is based on \a endianness. |
313 | Converts up to the first 32 bits of the array to \c quint32 and returns it, |
314 | obeying \a endianness. If \a ok is not a null pointer, and the array has more |
315 | than 32 bits, \a ok is set to false and this function returns zero; otherwise, |
316 | it's set to true. |
317 | */ |
318 | quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcept |
319 | { |
320 | const qsizetype _size = size(); |
321 | if (_size > 32) { |
322 | if (ok) |
323 | *ok = false; |
324 | return 0; |
325 | } |
326 | |
327 | if (ok) |
328 | *ok = true; |
329 | |
330 | quint32 factor = 1; |
331 | quint32 total = 0; |
332 | for (qsizetype i = 0; i < _size; ++i, factor *= 2) { |
333 | const auto index = endianness == QSysInfo::Endian::LittleEndian ? i : (_size - i - 1); |
334 | if (testBit(i: index)) |
335 | total += factor; |
336 | } |
337 | |
338 | return total; |
339 | } |
340 | |
341 | /*! \fn bool QBitArray::isDetached() const |
342 | |
343 | \internal |
344 | */ |
345 | |
346 | /*! \fn void QBitArray::detach() |
347 | |
348 | \internal |
349 | */ |
350 | |
351 | /*! \fn void QBitArray::clear() |
352 | |
353 | Clears the contents of the bit array and makes it empty. |
354 | |
355 | \sa resize(), isEmpty() |
356 | */ |
357 | |
358 | /*! \fn void QBitArray::truncate(qsizetype pos) |
359 | |
360 | Truncates the bit array at index position \a pos. |
361 | |
362 | If \a pos is beyond the end of the array, nothing happens. |
363 | |
364 | \sa resize() |
365 | */ |
366 | |
367 | /*! \fn bool QBitArray::toggleBit(qsizetype i) |
368 | |
369 | Inverts the value of the bit at index position \a i, returning the |
370 | previous value of that bit as either true (if it was set) or false (if |
371 | it was unset). |
372 | |
373 | If the previous value was 0, the new value will be 1. If the |
374 | previous value was 1, the new value will be 0. |
375 | |
376 | \a i must be a valid index position in the bit array (i.e., 0 <= |
377 | \a i < size()). |
378 | |
379 | \sa setBit(), clearBit() |
380 | */ |
381 | |
382 | /*! \fn bool QBitArray::testBit(qsizetype i) const |
383 | |
384 | Returns \c true if the bit at index position \a i is 1; otherwise |
385 | returns \c false. |
386 | |
387 | \a i must be a valid index position in the bit array (i.e., 0 <= |
388 | \a i < size()). |
389 | |
390 | \sa setBit(), clearBit() |
391 | */ |
392 | |
393 | /*! \fn bool QBitArray::setBit(qsizetype i) |
394 | |
395 | Sets the bit at index position \a i to 1. |
396 | |
397 | \a i must be a valid index position in the bit array (i.e., 0 <= |
398 | \a i < size()). |
399 | |
400 | \sa clearBit(), toggleBit() |
401 | */ |
402 | |
403 | /*! \fn void QBitArray::setBit(qsizetype i, bool value) |
404 | |
405 | \overload |
406 | |
407 | Sets the bit at index position \a i to \a value. |
408 | */ |
409 | |
410 | /*! \fn void QBitArray::clearBit(qsizetype i) |
411 | |
412 | Sets the bit at index position \a i to 0. |
413 | |
414 | \a i must be a valid index position in the bit array (i.e., 0 <= |
415 | \a i < size()). |
416 | |
417 | \sa setBit(), toggleBit() |
418 | */ |
419 | |
420 | /*! \fn bool QBitArray::at(qsizetype i) const |
421 | |
422 | Returns the value of the bit at index position \a i. |
423 | |
424 | \a i must be a valid index position in the bit array (i.e., 0 <= |
425 | \a i < size()). |
426 | |
427 | \sa operator[]() |
428 | */ |
429 | |
430 | /*! \fn QBitRef QBitArray::operator[](qsizetype i) |
431 | |
432 | Returns the bit at index position \a i as a modifiable reference. |
433 | |
434 | \a i must be a valid index position in the bit array (i.e., 0 <= |
435 | \a i < size()). |
436 | |
437 | Example: |
438 | \snippet code/src_corelib_tools_qbitarray.cpp 7 |
439 | |
440 | The return value is of type QBitRef, a helper class for QBitArray. |
441 | When you get an object of type QBitRef, you can assign to |
442 | it, and the assignment will apply to the bit in the QBitArray |
443 | from which you got the reference. |
444 | |
445 | The functions testBit(), setBit(), and clearBit() are slightly |
446 | faster. |
447 | |
448 | \sa at(), testBit(), setBit(), clearBit() |
449 | */ |
450 | |
451 | /*! \fn bool QBitArray::operator[](qsizetype i) const |
452 | |
453 | \overload |
454 | */ |
455 | |
456 | /*! \fn QBitArray::QBitArray(const QBitArray &other) noexcept |
457 | |
458 | Constructs a copy of \a other. |
459 | |
460 | This operation takes \l{constant time}, because QBitArray is |
461 | \l{implicitly shared}. This makes returning a QBitArray from a |
462 | function very fast. If a shared instance is modified, it will be |
463 | copied (copy-on-write), and that takes \l{linear time}. |
464 | |
465 | \sa operator=() |
466 | */ |
467 | |
468 | /*! \fn QBitArray &QBitArray::operator=(const QBitArray &other) noexcept |
469 | |
470 | Assigns \a other to this bit array and returns a reference to |
471 | this bit array. |
472 | */ |
473 | |
474 | /*! \fn QBitArray &QBitArray::operator=(QBitArray &&other) |
475 | \since 5.2 |
476 | |
477 | Moves \a other to this bit array and returns a reference to |
478 | this bit array. |
479 | */ |
480 | |
481 | /*! \fn void QBitArray::swap(QBitArray &other) |
482 | \since 4.8 |
483 | |
484 | Swaps bit array \a other with this bit array. This operation is very |
485 | fast and never fails. |
486 | */ |
487 | |
488 | /*! \fn bool QBitArray::operator==(const QBitArray &other) const |
489 | |
490 | Returns \c true if \a other is equal to this bit array; otherwise |
491 | returns \c false. |
492 | |
493 | \sa operator!=() |
494 | */ |
495 | |
496 | /*! \fn bool QBitArray::operator!=(const QBitArray &other) const |
497 | |
498 | Returns \c true if \a other is not equal to this bit array; |
499 | otherwise returns \c false. |
500 | |
501 | \sa operator==() |
502 | */ |
503 | |
504 | /*! |
505 | Performs the AND operation between all bits in this bit array and |
506 | \a other. Assigns the result to this bit array, and returns a |
507 | reference to it. |
508 | |
509 | The result has the length of the longest of the two bit arrays, |
510 | with any missing bits (if one array is shorter than the other) |
511 | taken to be 0. |
512 | |
513 | Example: |
514 | \snippet code/src_corelib_tools_qbitarray.cpp 8 |
515 | |
516 | \sa operator&(), operator|=(), operator^=(), operator~() |
517 | */ |
518 | |
519 | QBitArray &QBitArray::operator&=(const QBitArray &other) |
520 | { |
521 | resize(size: qMax(a: size(), b: other.size())); |
522 | uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1; |
523 | const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; |
524 | qsizetype n = other.d.size() - 1; |
525 | qsizetype p = d.size() - 1 - n; |
526 | while (n-- > 0) |
527 | *a1++ &= *a2++; |
528 | while (p-- > 0) |
529 | *a1++ = 0; |
530 | return *this; |
531 | } |
532 | |
533 | /*! |
534 | Performs the OR operation between all bits in this bit array and |
535 | \a other. Assigns the result to this bit array, and returns a |
536 | reference to it. |
537 | |
538 | The result has the length of the longest of the two bit arrays, |
539 | with any missing bits (if one array is shorter than the other) |
540 | taken to be 0. |
541 | |
542 | Example: |
543 | \snippet code/src_corelib_tools_qbitarray.cpp 9 |
544 | |
545 | \sa operator|(), operator&=(), operator^=(), operator~() |
546 | */ |
547 | |
548 | QBitArray &QBitArray::operator|=(const QBitArray &other) |
549 | { |
550 | resize(size: qMax(a: size(), b: other.size())); |
551 | uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1; |
552 | const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; |
553 | qsizetype n = other.d.size() - 1; |
554 | while (n-- > 0) |
555 | *a1++ |= *a2++; |
556 | return *this; |
557 | } |
558 | |
559 | /*! |
560 | Performs the XOR operation between all bits in this bit array and |
561 | \a other. Assigns the result to this bit array, and returns a |
562 | reference to it. |
563 | |
564 | The result has the length of the longest of the two bit arrays, |
565 | with any missing bits (if one array is shorter than the other) |
566 | taken to be 0. |
567 | |
568 | Example: |
569 | \snippet code/src_corelib_tools_qbitarray.cpp 10 |
570 | |
571 | \sa operator^(), operator&=(), operator|=(), operator~() |
572 | */ |
573 | |
574 | QBitArray &QBitArray::operator^=(const QBitArray &other) |
575 | { |
576 | resize(size: qMax(a: size(), b: other.size())); |
577 | uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1; |
578 | const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; |
579 | qsizetype n = other.d.size() - 1; |
580 | while (n-- > 0) |
581 | *a1++ ^= *a2++; |
582 | return *this; |
583 | } |
584 | |
585 | /*! |
586 | Returns a bit array that contains the inverted bits of this bit |
587 | array. |
588 | |
589 | Example: |
590 | \snippet code/src_corelib_tools_qbitarray.cpp 11 |
591 | |
592 | \sa operator&(), operator|(), operator^() |
593 | */ |
594 | |
595 | QBitArray QBitArray::operator~() const |
596 | { |
597 | qsizetype sz = size(); |
598 | QBitArray a(sz); |
599 | const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1; |
600 | uchar *a2 = reinterpret_cast<uchar *>(a.d.data()) + 1; |
601 | qsizetype n = d.size() - 1; |
602 | |
603 | while (n-- > 0) |
604 | *a2++ = ~*a1++; |
605 | |
606 | if (sz && sz % 8) |
607 | *(a2 - 1) &= (1 << (sz % 8)) - 1; |
608 | return a; |
609 | } |
610 | |
611 | /*! |
612 | \relates QBitArray |
613 | |
614 | Returns a bit array that is the AND of the bit arrays \a a1 and \a |
615 | a2. |
616 | |
617 | The result has the length of the longest of the two bit arrays, |
618 | with any missing bits (if one array is shorter than the other) |
619 | taken to be 0. |
620 | |
621 | Example: |
622 | \snippet code/src_corelib_tools_qbitarray.cpp 12 |
623 | |
624 | \sa {QBitArray::}{operator&=()}, {QBitArray::}{operator|()}, {QBitArray::}{operator^()} |
625 | */ |
626 | |
627 | QBitArray operator&(const QBitArray &a1, const QBitArray &a2) |
628 | { |
629 | QBitArray tmp = a1; |
630 | tmp &= a2; |
631 | return tmp; |
632 | } |
633 | |
634 | /*! |
635 | \relates QBitArray |
636 | |
637 | Returns a bit array that is the OR of the bit arrays \a a1 and \a |
638 | a2. |
639 | |
640 | The result has the length of the longest of the two bit arrays, |
641 | with any missing bits (if one array is shorter than the other) |
642 | taken to be 0. |
643 | |
644 | Example: |
645 | \snippet code/src_corelib_tools_qbitarray.cpp 13 |
646 | |
647 | \sa QBitArray::operator|=(), operator&(), operator^() |
648 | */ |
649 | |
650 | QBitArray operator|(const QBitArray &a1, const QBitArray &a2) |
651 | { |
652 | QBitArray tmp = a1; |
653 | tmp |= a2; |
654 | return tmp; |
655 | } |
656 | |
657 | /*! |
658 | \relates QBitArray |
659 | |
660 | Returns a bit array that is the XOR of the bit arrays \a a1 and \a |
661 | a2. |
662 | |
663 | The result has the length of the longest of the two bit arrays, |
664 | with any missing bits (if one array is shorter than the other) |
665 | taken to be 0. |
666 | |
667 | Example: |
668 | \snippet code/src_corelib_tools_qbitarray.cpp 14 |
669 | |
670 | \sa {QBitArray}{operator^=()}, {QBitArray}{operator&()}, {QBitArray}{operator|()} |
671 | */ |
672 | |
673 | QBitArray operator^(const QBitArray &a1, const QBitArray &a2) |
674 | { |
675 | QBitArray tmp = a1; |
676 | tmp ^= a2; |
677 | return tmp; |
678 | } |
679 | |
680 | /*! |
681 | \class QBitRef |
682 | \inmodule QtCore |
683 | \reentrant |
684 | \brief The QBitRef class is an internal class, used with QBitArray. |
685 | |
686 | \internal |
687 | |
688 | The QBitRef is required by the indexing [] operator on bit arrays. |
689 | It is not for use in any other context. |
690 | */ |
691 | |
692 | /*! \fn QBitRef::QBitRef (QBitArray& a, qsizetype i) |
693 | |
694 | Constructs a reference to element \a i in the QBitArray \a a. |
695 | This is what QBitArray::operator[] constructs its return value |
696 | with. |
697 | */ |
698 | |
699 | /*! \fn QBitRef::operator bool() const |
700 | |
701 | Returns the value referenced by the QBitRef. |
702 | */ |
703 | |
704 | /*! \fn bool QBitRef::operator!() const |
705 | |
706 | \internal |
707 | */ |
708 | |
709 | /*! \fn QBitRef& QBitRef::operator= (const QBitRef& v) |
710 | |
711 | Sets the value referenced by the QBitRef to that referenced by |
712 | QBitRef \a v. |
713 | */ |
714 | |
715 | /*! \fn QBitRef& QBitRef::operator= (bool v) |
716 | \overload |
717 | |
718 | Sets the value referenced by the QBitRef to \a v. |
719 | */ |
720 | |
721 | /***************************************************************************** |
722 | QBitArray stream functions |
723 | *****************************************************************************/ |
724 | |
725 | #ifndef QT_NO_DATASTREAM |
726 | /*! |
727 | \relates QBitArray |
728 | |
729 | Writes bit array \a ba to stream \a out. |
730 | |
731 | \sa {Serializing Qt Data Types}{Format of the QDataStream operators} |
732 | */ |
733 | |
734 | QDataStream &operator<<(QDataStream &out, const QBitArray &ba) |
735 | { |
736 | if (out.version() < QDataStream::Qt_6_0) { |
737 | quint32 len = ba.size(); |
738 | out << len; |
739 | if (len > 0) |
740 | out.writeRawData(ba.d.constData() + 1, len: ba.d.size() - 1); |
741 | return out; |
742 | } else { |
743 | quint64 len = ba.size(); |
744 | out << len; |
745 | if (len > 0) |
746 | out.writeRawData(ba.d.constData() + 1, len: ba.d.size() - 1); |
747 | return out; |
748 | } |
749 | } |
750 | |
751 | /*! |
752 | \relates QBitArray |
753 | |
754 | Reads a bit array into \a ba from stream \a in. |
755 | |
756 | \sa {Serializing Qt Data Types}{Format of the QDataStream operators} |
757 | */ |
758 | |
759 | QDataStream &operator>>(QDataStream &in, QBitArray &ba) |
760 | { |
761 | ba.clear(); |
762 | qsizetype len; |
763 | if (in.version() < QDataStream::Qt_6_0) { |
764 | quint32 tmp; |
765 | in >> tmp; |
766 | len = tmp; |
767 | } else { |
768 | quint64 tmp; |
769 | in >> tmp; |
770 | len = tmp; |
771 | } |
772 | if (len == 0) { |
773 | ba.clear(); |
774 | return in; |
775 | } |
776 | |
777 | const qsizetype Step = 8 * 1024 * 1024; |
778 | qsizetype totalBytes = (len + 7) / 8; |
779 | qsizetype allocated = 0; |
780 | |
781 | while (allocated < totalBytes) { |
782 | qsizetype blockSize = qMin(a: Step, b: totalBytes - allocated); |
783 | ba.d.resize(size: allocated + blockSize + 1); |
784 | if (in.readRawData(ba.d.data() + 1 + allocated, len: blockSize) != blockSize) { |
785 | ba.clear(); |
786 | in.setStatus(QDataStream::ReadPastEnd); |
787 | return in; |
788 | } |
789 | allocated += blockSize; |
790 | } |
791 | |
792 | qsizetype paddingMask = ~((0x1 << (len & 0x7)) - 1); |
793 | if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) { |
794 | ba.clear(); |
795 | in.setStatus(QDataStream::ReadCorruptData); |
796 | return in; |
797 | } |
798 | |
799 | *ba.d.data() = ba.d.size() * 8 - len; |
800 | return in; |
801 | } |
802 | #endif // QT_NO_DATASTREAM |
803 | |
804 | #ifndef QT_NO_DEBUG_STREAM |
805 | QDebug operator<<(QDebug dbg, const QBitArray &array) |
806 | { |
807 | QDebugStateSaver saver(dbg); |
808 | dbg.nospace() << "QBitArray(" ; |
809 | for (qsizetype i = 0; i < array.size();) { |
810 | if (array.testBit(i)) |
811 | dbg << '1'; |
812 | else |
813 | dbg << '0'; |
814 | i += 1; |
815 | if (!(i % 4) && (i < array.size())) |
816 | dbg << ' '; |
817 | } |
818 | dbg << ')'; |
819 | return dbg; |
820 | } |
821 | #endif |
822 | |
823 | /*! |
824 | \fn DataPtr &QBitArray::data_ptr() |
825 | \internal |
826 | */ |
827 | |
828 | /*! |
829 | \typedef QBitArray::DataPtr |
830 | \internal |
831 | */ |
832 | |
833 | QT_END_NAMESPACE |
834 | |