| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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, QVector |
| 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 int 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(int 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(s: c + 1, c: value ? 0xff : 0, n: 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 int QBitArray::size() const |
| 162 | |
| 163 | Returns the number of bits stored in the bit array. |
| 164 | |
| 165 | \sa resize() |
| 166 | */ |
| 167 | |
| 168 | /*! \fn int 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 | int QBitArray::count(bool on) const |
| 179 | { |
| 180 | int 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>(src: bits); |
| 189 | bits += 8; |
| 190 | numBits += int(qPopulationCount(v)); |
| 191 | } |
| 192 | if (bits + 3 <= end) { |
| 193 | quint32 v = qFromUnaligned<quint32>(src: bits); |
| 194 | bits += 4; |
| 195 | numBits += int(qPopulationCount(v)); |
| 196 | } |
| 197 | if (bits + 1 < end) { |
| 198 | quint16 v = qFromUnaligned<quint16>(src: bits); |
| 199 | bits += 2; |
| 200 | numBits += int(qPopulationCount(v)); |
| 201 | } |
| 202 | if (bits < end) |
| 203 | numBits += int(qPopulationCount(v: 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(int size) |
| 221 | { |
| 222 | if (!size) { |
| 223 | d.resize(size: 0); |
| 224 | } else { |
| 225 | int s = d.size(); |
| 226 | d.resize(size: 1 + (size+7)/8); |
| 227 | uchar* c = reinterpret_cast<uchar*>(d.data()); |
| 228 | if (size > (s << 3)) |
| 229 | memset(s: c + s, c: 0, n: 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, int 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, int begin, int end) |
| 289 | { |
| 290 | while (begin < end && begin & 0x7) |
| 291 | setBit(i: begin++, val: value); |
| 292 | int len = end - begin; |
| 293 | if (len <= 0) |
| 294 | return; |
| 295 | int s = len & ~0x7; |
| 296 | uchar *c = reinterpret_cast<uchar*>(d.data()); |
| 297 | memset(s: c + (begin >> 3) + 1, c: value ? 0xff : 0, n: s >> 3); |
| 298 | begin += s; |
| 299 | while (begin < end) |
| 300 | setBit(i: begin++, val: 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(dest: bits + 1, src: data, n: 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 | /*! \fn bool QBitArray::isDetached() const |
| 346 | |
| 347 | \internal |
| 348 | */ |
| 349 | |
| 350 | /*! \fn void QBitArray::detach() |
| 351 | |
| 352 | \internal |
| 353 | */ |
| 354 | |
| 355 | /*! \fn void QBitArray::clear() |
| 356 | |
| 357 | Clears the contents of the bit array and makes it empty. |
| 358 | |
| 359 | \sa resize(), isEmpty() |
| 360 | */ |
| 361 | |
| 362 | /*! \fn void QBitArray::truncate(int pos) |
| 363 | |
| 364 | Truncates the bit array at index position \a pos. |
| 365 | |
| 366 | If \a pos is beyond the end of the array, nothing happens. |
| 367 | |
| 368 | \sa resize() |
| 369 | */ |
| 370 | |
| 371 | /*! \fn bool QBitArray::toggleBit(int i) |
| 372 | |
| 373 | Inverts the value of the bit at index position \a i, returning the |
| 374 | previous value of that bit as either true (if it was set) or false (if |
| 375 | it was unset). |
| 376 | |
| 377 | If the previous value was 0, the new value will be 1. If the |
| 378 | previous value was 1, the new value will be 0. |
| 379 | |
| 380 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 381 | \a i < size()). |
| 382 | |
| 383 | \sa setBit(), clearBit() |
| 384 | */ |
| 385 | |
| 386 | /*! \fn bool QBitArray::testBit(int i) const |
| 387 | |
| 388 | Returns \c true if the bit at index position \a i is 1; otherwise |
| 389 | returns \c false. |
| 390 | |
| 391 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 392 | \a i < size()). |
| 393 | |
| 394 | \sa setBit(), clearBit() |
| 395 | */ |
| 396 | |
| 397 | /*! \fn bool QBitArray::setBit(int i) |
| 398 | |
| 399 | Sets the bit at index position \a i to 1. |
| 400 | |
| 401 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 402 | \a i < size()). |
| 403 | |
| 404 | \sa clearBit(), toggleBit() |
| 405 | */ |
| 406 | |
| 407 | /*! \fn void QBitArray::setBit(int i, bool value) |
| 408 | |
| 409 | \overload |
| 410 | |
| 411 | Sets the bit at index position \a i to \a value. |
| 412 | */ |
| 413 | |
| 414 | /*! \fn void QBitArray::clearBit(int i) |
| 415 | |
| 416 | Sets the bit at index position \a i to 0. |
| 417 | |
| 418 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 419 | \a i < size()). |
| 420 | |
| 421 | \sa setBit(), toggleBit() |
| 422 | */ |
| 423 | |
| 424 | /*! \fn bool QBitArray::at(int i) const |
| 425 | |
| 426 | Returns the value of the bit at index position \a i. |
| 427 | |
| 428 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 429 | \a i < size()). |
| 430 | |
| 431 | \sa operator[]() |
| 432 | */ |
| 433 | |
| 434 | /*! \fn QBitRef QBitArray::operator[](int i) |
| 435 | |
| 436 | Returns the bit at index position \a i as a modifiable reference. |
| 437 | |
| 438 | \a i must be a valid index position in the bit array (i.e., 0 <= |
| 439 | \a i < size()). |
| 440 | |
| 441 | Example: |
| 442 | \snippet code/src_corelib_tools_qbitarray.cpp 7 |
| 443 | |
| 444 | The return value is of type QBitRef, a helper class for QBitArray. |
| 445 | When you get an object of type QBitRef, you can assign to |
| 446 | it, and the assignment will apply to the bit in the QBitArray |
| 447 | from which you got the reference. |
| 448 | |
| 449 | The functions testBit(), setBit(), and clearBit() are slightly |
| 450 | faster. |
| 451 | |
| 452 | \sa at(), testBit(), setBit(), clearBit() |
| 453 | */ |
| 454 | |
| 455 | /*! \fn bool QBitArray::operator[](int i) const |
| 456 | |
| 457 | \overload |
| 458 | */ |
| 459 | |
| 460 | /*! \fn QBitRef QBitArray::operator[](uint i) |
| 461 | |
| 462 | \overload |
| 463 | */ |
| 464 | |
| 465 | /*! \fn bool QBitArray::operator[](uint i) const |
| 466 | |
| 467 | \overload |
| 468 | */ |
| 469 | |
| 470 | /*! \fn QBitArray::QBitArray(const QBitArray &other) |
| 471 | |
| 472 | Constructs a copy of \a other. |
| 473 | |
| 474 | This operation takes \l{constant time}, because QBitArray is |
| 475 | \l{implicitly shared}. This makes returning a QBitArray from a |
| 476 | function very fast. If a shared instance is modified, it will be |
| 477 | copied (copy-on-write), and that takes \l{linear time}. |
| 478 | |
| 479 | \sa operator=() |
| 480 | */ |
| 481 | |
| 482 | /*! \fn QBitArray &QBitArray::operator=(const QBitArray &other) |
| 483 | |
| 484 | Assigns \a other to this bit array and returns a reference to |
| 485 | this bit array. |
| 486 | */ |
| 487 | |
| 488 | /*! \fn QBitArray &QBitArray::operator=(QBitArray &&other) |
| 489 | \since 5.2 |
| 490 | |
| 491 | Moves \a other to this bit array and returns a reference to |
| 492 | this bit array. |
| 493 | */ |
| 494 | |
| 495 | /*! \fn void QBitArray::swap(QBitArray &other) |
| 496 | \since 4.8 |
| 497 | |
| 498 | Swaps bit array \a other with this bit array. This operation is very |
| 499 | fast and never fails. |
| 500 | */ |
| 501 | |
| 502 | /*! \fn bool QBitArray::operator==(const QBitArray &other) const |
| 503 | |
| 504 | Returns \c true if \a other is equal to this bit array; otherwise |
| 505 | returns \c false. |
| 506 | |
| 507 | \sa operator!=() |
| 508 | */ |
| 509 | |
| 510 | /*! \fn bool QBitArray::operator!=(const QBitArray &other) const |
| 511 | |
| 512 | Returns \c true if \a other is not equal to this bit array; |
| 513 | otherwise returns \c false. |
| 514 | |
| 515 | \sa operator==() |
| 516 | */ |
| 517 | |
| 518 | /*! |
| 519 | Performs the AND operation between all bits in this bit array and |
| 520 | \a other. Assigns the result to this bit array, and returns a |
| 521 | reference to it. |
| 522 | |
| 523 | The result has the length of the longest of the two bit arrays, |
| 524 | with any missing bits (if one array is shorter than the other) |
| 525 | taken to be 0. |
| 526 | |
| 527 | Example: |
| 528 | \snippet code/src_corelib_tools_qbitarray.cpp 8 |
| 529 | |
| 530 | \sa operator&(), operator|=(), operator^=(), operator~() |
| 531 | */ |
| 532 | |
| 533 | QBitArray &QBitArray::operator&=(const QBitArray &other) |
| 534 | { |
| 535 | resize(size: qMax(a: size(), b: other.size())); |
| 536 | uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; |
| 537 | const uchar *a2 = reinterpret_cast<const uchar*>(other.d.constData()) + 1; |
| 538 | int n = other.d.size() -1 ; |
| 539 | int p = d.size() - 1 - n; |
| 540 | while (n-- > 0) |
| 541 | *a1++ &= *a2++; |
| 542 | while (p-- > 0) |
| 543 | *a1++ = 0; |
| 544 | return *this; |
| 545 | } |
| 546 | |
| 547 | /*! |
| 548 | Performs the OR operation between all bits in this bit array and |
| 549 | \a other. Assigns the result to this bit array, and returns a |
| 550 | reference to it. |
| 551 | |
| 552 | The result has the length of the longest of the two bit arrays, |
| 553 | with any missing bits (if one array is shorter than the other) |
| 554 | taken to be 0. |
| 555 | |
| 556 | Example: |
| 557 | \snippet code/src_corelib_tools_qbitarray.cpp 9 |
| 558 | |
| 559 | \sa operator|(), operator&=(), operator^=(), operator~() |
| 560 | */ |
| 561 | |
| 562 | QBitArray &QBitArray::operator|=(const QBitArray &other) |
| 563 | { |
| 564 | resize(size: qMax(a: size(), b: other.size())); |
| 565 | uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; |
| 566 | const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; |
| 567 | int n = other.d.size() - 1; |
| 568 | while (n-- > 0) |
| 569 | *a1++ |= *a2++; |
| 570 | return *this; |
| 571 | } |
| 572 | |
| 573 | /*! |
| 574 | Performs the XOR operation between all bits in this bit array and |
| 575 | \a other. Assigns the result to this bit array, and returns a |
| 576 | reference to it. |
| 577 | |
| 578 | The result has the length of the longest of the two bit arrays, |
| 579 | with any missing bits (if one array is shorter than the other) |
| 580 | taken to be 0. |
| 581 | |
| 582 | Example: |
| 583 | \snippet code/src_corelib_tools_qbitarray.cpp 10 |
| 584 | |
| 585 | \sa operator^(), operator&=(), operator|=(), operator~() |
| 586 | */ |
| 587 | |
| 588 | QBitArray &QBitArray::operator^=(const QBitArray &other) |
| 589 | { |
| 590 | resize(size: qMax(a: size(), b: other.size())); |
| 591 | uchar *a1 = reinterpret_cast<uchar*>(d.data()) + 1; |
| 592 | const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1; |
| 593 | int n = other.d.size() - 1; |
| 594 | while (n-- > 0) |
| 595 | *a1++ ^= *a2++; |
| 596 | return *this; |
| 597 | } |
| 598 | |
| 599 | /*! |
| 600 | Returns a bit array that contains the inverted bits of this bit |
| 601 | array. |
| 602 | |
| 603 | Example: |
| 604 | \snippet code/src_corelib_tools_qbitarray.cpp 11 |
| 605 | |
| 606 | \sa operator&(), operator|(), operator^() |
| 607 | */ |
| 608 | |
| 609 | QBitArray QBitArray::operator~() const |
| 610 | { |
| 611 | int sz = size(); |
| 612 | QBitArray a(sz); |
| 613 | const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1; |
| 614 | uchar *a2 = reinterpret_cast<uchar*>(a.d.data()) + 1; |
| 615 | int n = d.size() - 1; |
| 616 | |
| 617 | while (n-- > 0) |
| 618 | *a2++ = ~*a1++; |
| 619 | |
| 620 | if (sz && sz%8) |
| 621 | *(a2-1) &= (1 << (sz%8)) - 1; |
| 622 | return a; |
| 623 | } |
| 624 | |
| 625 | /*! |
| 626 | \relates QBitArray |
| 627 | |
| 628 | Returns a bit array that is the AND of the bit arrays \a a1 and \a |
| 629 | a2. |
| 630 | |
| 631 | The result has the length of the longest of the two bit arrays, |
| 632 | with any missing bits (if one array is shorter than the other) |
| 633 | taken to be 0. |
| 634 | |
| 635 | Example: |
| 636 | \snippet code/src_corelib_tools_qbitarray.cpp 12 |
| 637 | |
| 638 | \sa {QBitArray::}{operator&=()}, {QBitArray::}{operator|()}, {QBitArray::}{operator^()} |
| 639 | */ |
| 640 | |
| 641 | QBitArray operator&(const QBitArray &a1, const QBitArray &a2) |
| 642 | { |
| 643 | QBitArray tmp = a1; |
| 644 | tmp &= a2; |
| 645 | return tmp; |
| 646 | } |
| 647 | |
| 648 | /*! |
| 649 | \relates QBitArray |
| 650 | |
| 651 | Returns a bit array that is the OR of the bit arrays \a a1 and \a |
| 652 | a2. |
| 653 | |
| 654 | The result has the length of the longest of the two bit arrays, |
| 655 | with any missing bits (if one array is shorter than the other) |
| 656 | taken to be 0. |
| 657 | |
| 658 | Example: |
| 659 | \snippet code/src_corelib_tools_qbitarray.cpp 13 |
| 660 | |
| 661 | \sa QBitArray::operator|=(), operator&(), operator^() |
| 662 | */ |
| 663 | |
| 664 | QBitArray operator|(const QBitArray &a1, const QBitArray &a2) |
| 665 | { |
| 666 | QBitArray tmp = a1; |
| 667 | tmp |= a2; |
| 668 | return tmp; |
| 669 | } |
| 670 | |
| 671 | /*! |
| 672 | \relates QBitArray |
| 673 | |
| 674 | Returns a bit array that is the XOR of the bit arrays \a a1 and \a |
| 675 | a2. |
| 676 | |
| 677 | The result has the length of the longest of the two bit arrays, |
| 678 | with any missing bits (if one array is shorter than the other) |
| 679 | taken to be 0. |
| 680 | |
| 681 | Example: |
| 682 | \snippet code/src_corelib_tools_qbitarray.cpp 14 |
| 683 | |
| 684 | \sa {QBitArray}{operator^=()}, {QBitArray}{operator&()}, {QBitArray}{operator|()} |
| 685 | */ |
| 686 | |
| 687 | QBitArray operator^(const QBitArray &a1, const QBitArray &a2) |
| 688 | { |
| 689 | QBitArray tmp = a1; |
| 690 | tmp ^= a2; |
| 691 | return tmp; |
| 692 | } |
| 693 | |
| 694 | /*! |
| 695 | \class QBitRef |
| 696 | \inmodule QtCore |
| 697 | \reentrant |
| 698 | \brief The QBitRef class is an internal class, used with QBitArray. |
| 699 | |
| 700 | \internal |
| 701 | |
| 702 | The QBitRef is required by the indexing [] operator on bit arrays. |
| 703 | It is not for use in any other context. |
| 704 | */ |
| 705 | |
| 706 | /*! \fn QBitRef::QBitRef (QBitArray& a, int i) |
| 707 | |
| 708 | Constructs a reference to element \a i in the QBitArray \a a. |
| 709 | This is what QBitArray::operator[] constructs its return value |
| 710 | with. |
| 711 | */ |
| 712 | |
| 713 | /*! \fn QBitRef::operator bool() const |
| 714 | |
| 715 | Returns the value referenced by the QBitRef. |
| 716 | */ |
| 717 | |
| 718 | /*! \fn bool QBitRef::operator!() const |
| 719 | |
| 720 | \internal |
| 721 | */ |
| 722 | |
| 723 | /*! \fn QBitRef& QBitRef::operator= (const QBitRef& v) |
| 724 | |
| 725 | Sets the value referenced by the QBitRef to that referenced by |
| 726 | QBitRef \a v. |
| 727 | */ |
| 728 | |
| 729 | /*! \fn QBitRef& QBitRef::operator= (bool v) |
| 730 | \overload |
| 731 | |
| 732 | Sets the value referenced by the QBitRef to \a v. |
| 733 | */ |
| 734 | |
| 735 | |
| 736 | /***************************************************************************** |
| 737 | QBitArray stream functions |
| 738 | *****************************************************************************/ |
| 739 | |
| 740 | #ifndef QT_NO_DATASTREAM |
| 741 | /*! |
| 742 | \relates QBitArray |
| 743 | |
| 744 | Writes bit array \a ba to stream \a out. |
| 745 | |
| 746 | \sa {Serializing Qt Data Types}{Format of the QDataStream operators} |
| 747 | */ |
| 748 | |
| 749 | QDataStream &operator<<(QDataStream &out, const QBitArray &ba) |
| 750 | { |
| 751 | quint32 len = ba.size(); |
| 752 | out << len; |
| 753 | if (len > 0) |
| 754 | out.writeRawData(ba.d.constData() + 1, len: ba.d.size() - 1); |
| 755 | return out; |
| 756 | } |
| 757 | |
| 758 | /*! |
| 759 | \relates QBitArray |
| 760 | |
| 761 | Reads a bit array into \a ba from stream \a in. |
| 762 | |
| 763 | \sa {Serializing Qt Data Types}{Format of the QDataStream operators} |
| 764 | */ |
| 765 | |
| 766 | QDataStream &operator>>(QDataStream &in, QBitArray &ba) |
| 767 | { |
| 768 | ba.clear(); |
| 769 | quint32 len; |
| 770 | in >> len; |
| 771 | if (len == 0) { |
| 772 | ba.clear(); |
| 773 | return in; |
| 774 | } |
| 775 | |
| 776 | const quint32 Step = 8 * 1024 * 1024; |
| 777 | quint32 totalBytes = (len + 7) / 8; |
| 778 | quint32 allocated = 0; |
| 779 | |
| 780 | while (allocated < totalBytes) { |
| 781 | int blockSize = qMin(a: Step, b: totalBytes - allocated); |
| 782 | ba.d.resize(size: allocated + blockSize + 1); |
| 783 | if (in.readRawData(ba.d.data() + 1 + allocated, len: blockSize) != blockSize) { |
| 784 | ba.clear(); |
| 785 | in.setStatus(QDataStream::ReadPastEnd); |
| 786 | return in; |
| 787 | } |
| 788 | allocated += blockSize; |
| 789 | } |
| 790 | |
| 791 | int paddingMask = ~((0x1 << (len & 0x7)) - 1); |
| 792 | if (paddingMask != ~0x0 && (ba.d.constData()[ba.d.size() - 1] & paddingMask)) { |
| 793 | ba.clear(); |
| 794 | in.setStatus(QDataStream::ReadCorruptData); |
| 795 | return in; |
| 796 | } |
| 797 | |
| 798 | *ba.d.data() = ba.d.size() * 8 - len; |
| 799 | return in; |
| 800 | } |
| 801 | #endif // QT_NO_DATASTREAM |
| 802 | |
| 803 | #ifndef QT_NO_DEBUG_STREAM |
| 804 | QDebug operator<<(QDebug dbg, const QBitArray &array) |
| 805 | { |
| 806 | QDebugStateSaver saver(dbg); |
| 807 | dbg.nospace() << "QBitArray(" ; |
| 808 | for (int i = 0; i < array.size();) { |
| 809 | if (array.testBit(i)) |
| 810 | dbg << '1'; |
| 811 | else |
| 812 | dbg << '0'; |
| 813 | i += 1; |
| 814 | if (!(i % 4) && (i < array.size())) |
| 815 | dbg << ' '; |
| 816 | } |
| 817 | dbg << ')'; |
| 818 | return dbg; |
| 819 | } |
| 820 | #endif |
| 821 | |
| 822 | /*! |
| 823 | \fn DataPtr &QBitArray::data_ptr() |
| 824 | \internal |
| 825 | */ |
| 826 | |
| 827 | /*! |
| 828 | \typedef QBitArray::DataPtr |
| 829 | \internal |
| 830 | */ |
| 831 | |
| 832 | QT_END_NAMESPACE |
| 833 | |