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