tools/qbitarray.cpp

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

Generated by Squish Coco Non-Commercial