summaryrefslogtreecommitdiff
path: root/qmake/tools/qbitarray.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qbitarray.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qbitarray.cpp661
1 files changed, 661 insertions, 0 deletions
diff --git a/qmake/tools/qbitarray.cpp b/qmake/tools/qbitarray.cpp
new file mode 100644
index 0000000..4f4e14b
--- a/dev/null
+++ b/qmake/tools/qbitarray.cpp
@@ -0,0 +1,661 @@
1/****************************************************************************
2** $Id$
3**
4** Implementation of QBitArray class
5**
6** Created : 940118
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qbitarray.h"
39#include "qdatastream.h"
40
41 #define SHBLOCK ((bitarr_data*)(sharedBlock()))
42
43
44/*!
45 \class QBitVal qbitarray.h
46 \reentrant
47 \brief The QBitVal class is an internal class, used with QBitArray.
48
49 \ingroup collection
50
51 The QBitVal is required by the indexing [] operator on bit arrays.
52 It is not for use in any other context.
53*/
54
55/*!
56 \fn QBitVal::QBitVal (QBitArray* a, uint i)
57
58 Constructs a reference to element \a i in the QBitArray \a a.
59 This is what QBitArray::operator[] constructs its return value
60 with.
61*/
62
63/*!
64 \fn QBitVal::operator int()
65
66 Returns the value referenced by the QBitVal.
67*/
68
69/*!
70 \fn QBitVal& QBitVal::operator= (const QBitVal& v)
71
72 Sets the value referenced by the QBitVal to that referenced by
73 QBitVal \a v.
74*/
75
76/*!
77 \overload QBitVal& QBitVal::operator= (bool v)
78
79 Sets the value referenced by the QBitVal to \a v.
80*/
81
82
83/*!
84 \class QBitArray qbitarray.h
85 \reentrant
86 \brief The QBitArray class provides an array of bits.
87
88 \ingroup collection
89 \ingroup tools
90 \ingroup shared
91
92 Because QBitArray is a QMemArray, it uses explicit \link
93 shclass.html sharing\endlink with a reference count.
94
95 A QBitArray is a special byte array that can access individual
96 bits and perform bit-operations (AND, OR, XOR and NOT) on entire
97 arrays or bits.
98
99 Bits can be manipulated by the setBit() and clearBit() functions,
100 but it is also possible to use the indexing [] operator to test
101 and set individual bits. The [] operator is a little slower than
102 setBit() and clearBit() because some tricks are required to
103 implement single-bit assignments.
104
105 Example:
106 \code
107 QBitArray a(3);
108 a.setBit( 0 );
109 a.clearBit( 1 );
110 a.setBit( 2 ); // a = [1 0 1]
111
112 QBitArray b(3);
113 b[0] = 1;
114 b[1] = 1;
115 b[2] = 0; // b = [1 1 0]
116
117 QBitArray c;
118 c = ~a & b; // c = [0 1 0]
119 \endcode
120
121 When a QBitArray is constructed the bits are uninitialized. Use
122 fill() to set all the bits to 0 or 1. The array can be resized
123 with resize() and copied with copy(). Bits can be set with
124 setBit() and cleared with clearBit(). Bits can be toggled with
125 toggleBit(). A bit's value can be obtained with testBit() and with
126 at().
127
128 QBitArray supports the \& (AND), | (OR), ^ (XOR) and ~ (NOT)
129 operators.
130*/
131
132/*! \class QBitArray::bitarr_data
133 \brief The QBitArray::bitarr_data class is internal.
134 \internal
135*/
136
137
138/*!
139 Constructs an empty bit array.
140*/
141
142QBitArray::QBitArray() : QByteArray( 0, 0 )
143{
144 bitarr_data *x = new bitarr_data;
145 Q_CHECK_PTR( x );
146 x->nbits = 0;
147 setSharedBlock( x );
148}
149
150/*!
151 Constructs a bit array of \a size bits. The bits are uninitialized.
152
153 \sa fill()
154*/
155
156QBitArray::QBitArray( uint size ) : QByteArray( 0, 0 )
157{
158 bitarr_data *x = new bitarr_data;
159 Q_CHECK_PTR( x );
160 x->nbits = 0;
161 setSharedBlock( x );
162 resize( size );
163}
164
165/*!
166 \fn QBitArray::QBitArray( const QBitArray &a )
167
168 Constructs a shallow copy of \a a.
169*/
170
171/*!
172 \fn QBitArray &QBitArray::operator=( const QBitArray &a )
173
174 Assigns a shallow copy of \a a to this bit array and returns a
175 reference to this array.
176*/
177
178
179/*!
180 Pad last byte with 0-bits.
181*/
182void QBitArray::pad0()
183{
184 uint sz = size();
185 if ( sz && sz%8 )
186 *(data()+sz/8) &= (1 << (sz%8)) - 1;
187}
188
189
190/*!
191 \fn uint QBitArray::size() const
192
193 Returns the bit array's size (number of bits).
194
195 \sa resize()
196*/
197
198/*!
199 Resizes the bit array to \a size bits and returns TRUE if the bit
200 array could be resized; otherwise returns FALSE.
201
202 If the array is expanded, the new bits are set to 0.
203
204 \sa size()
205*/
206
207bool QBitArray::resize( uint size )
208{
209 uint s = this->size();
210 if ( !QByteArray::resize( (size+7)/8 ) )
211 return FALSE; // cannot resize
212 SHBLOCK->nbits = size;
213 if ( size != 0 ) { // not null array
214 int ds = (int)(size+7)/8 - (int)(s+7)/8;// number of bytes difference
215 if ( ds > 0 ) // expanding array
216 memset( data() + (s+7)/8, 0, ds );// reset new data
217 }
218 return TRUE;
219}
220
221
222/*!
223 Fills the bit array with \a v (1's if \a v is TRUE, or 0's if \a v
224 is FALSE).
225
226 fill() resizes the bit array to \a size bits if \a size is
227 nonnegative.
228
229 Returns FALSE if a nonnegative \e size was specified and the bit
230 array could not be resized; otherwise returns TRUE.
231
232 \sa resize()
233*/
234
235bool QBitArray::fill( bool v, int size )
236{
237 if ( size >= 0 ) { // resize first
238 if ( !resize( size ) )
239 return FALSE; // cannot resize
240 } else {
241 size = this->size();
242 }
243 if ( size > 0 )
244 memset( data(), v ? 0xff : 0, (size + 7) / 8 );
245 if ( v )
246 pad0();
247 return TRUE;
248}
249
250
251/*!
252 Detaches from shared bit array data and makes sure that this bit
253 array is the only one referring to the data.
254
255 If multiple bit arrays share common data, this bit array
256 dereferences the data and gets a copy of the data. Nothing happens
257 if there is only a single reference.
258
259 \sa copy()
260*/
261
262void QBitArray::detach()
263{
264 int nbits = SHBLOCK->nbits;
265 this->duplicate( *this );
266 SHBLOCK->nbits = nbits;
267}
268
269/*!
270 Returns a deep copy of the bit array.
271
272 \sa detach()
273*/
274
275QBitArray QBitArray::copy() const
276{
277 QBitArray tmp;
278 tmp.duplicate( *this );
279 ((bitarr_data*)(tmp.sharedBlock()))->nbits = SHBLOCK->nbits;
280 return tmp;
281}
282
283
284/*!
285 Returns TRUE if the bit at position \a index is set, i.e. is 1;
286 otherwise returns FALSE.
287
288 \sa setBit(), clearBit()
289*/
290
291bool QBitArray::testBit( uint index ) const
292{
293#if defined(QT_CHECK_RANGE)
294 if ( index >= size() ) {
295 qWarning( "QBitArray::testBit: Index %d out of range", index );
296 return FALSE;
297 }
298#endif
299 return (*(data()+(index>>3)) & (1 << (index & 7))) != 0;
300}
301
302/*!
303 \overload
304
305 Sets the bit at position \a index to 1.
306
307 \sa clearBit() toggleBit()
308*/
309
310void QBitArray::setBit( uint index )
311{
312#if defined(QT_CHECK_RANGE)
313 if ( index >= size() ) {
314 qWarning( "QBitArray::setBit: Index %d out of range", index );
315 return;
316 }
317#endif
318 *(data()+(index>>3)) |= (1 << (index & 7));
319}
320
321/*!
322 \fn void QBitArray::setBit( uint index, bool value )
323
324 Sets the bit at position \a index to \a value.
325
326 Equivalent to:
327 \code
328 if ( value )
329 setBit( index );
330 else
331 clearBit( index );
332 \endcode
333
334 \sa clearBit() toggleBit()
335*/
336
337/*!
338 Clears the bit at position \a index, i.e. sets it to 0.
339
340 \sa setBit(), toggleBit()
341*/
342
343void QBitArray::clearBit( uint index )
344{
345#if defined(QT_CHECK_RANGE)
346 if ( index >= size() ) {
347 qWarning( "QBitArray::clearBit: Index %d out of range", index );
348 return;
349 }
350#endif
351 *(data()+(index>>3)) &= ~(1 << (index & 7));
352}
353
354/*!
355 Toggles the bit at position \a index.
356
357 If the previous value was 0, the new value will be 1. If the
358 previous value was 1, the new value will be 0.
359
360 \sa setBit(), clearBit()
361*/
362
363bool QBitArray::toggleBit( uint index )
364{
365#if defined(QT_CHECK_RANGE)
366 if ( index >= size() ) {
367 qWarning( "QBitArray::toggleBit: Index %d out of range", index );
368 return FALSE;
369 }
370#endif
371 register uchar *p = (uchar *)data() + (index>>3);
372 uchar b = (1 << (index & 7)); // bit position
373 uchar c = *p & b; // read bit
374 *p ^= b; // toggle bit
375 return c;
376}
377
378
379/*!
380 \fn bool QBitArray::at( uint index ) const
381
382 Returns the value (0 or 1) of the bit at position \a index.
383
384 \sa operator[]()
385*/
386
387/*!
388 \fn QBitVal QBitArray::operator[]( int index )
389
390 Implements the [] operator for bit arrays.
391
392 The returned QBitVal is a context object. It makes it possible to
393 get and set a single bit value by its \a index position.
394
395 Example:
396 \code
397 QBitArray a( 3 );
398 a[0] = 0;
399 a[1] = 1;
400 a[2] = a[0] ^ a[1];
401 \endcode
402
403 The functions testBit(), setBit() and clearBit() are faster.
404
405 \sa at()
406*/
407
408/*!
409 \overload bool QBitArray::operator[]( int index ) const
410
411 Implements the [] operator for constant bit arrays.
412*/
413
414
415/*!
416 Performs the AND operation between all bits in this bit array and
417 \a a. Returns a reference to this bit array.
418
419 The result has the length of the longest of the two bit arrays,
420 with any missing bits (i.e. if one array is shorter than the
421 other), taken to be 0.
422 \code
423 QBitArray a( 3 ), b( 2 );
424 a[0] = 1; a[1] = 0; a[2] = 1; // a = [1 0 1]
425 b[0] = 1; b[1] = 0; // b = [1 0]
426 a &= b; // a = [1 0 0]
427 \endcode
428
429 \sa operator|=(), operator^=(), operator~()
430*/
431
432QBitArray &QBitArray::operator&=( const QBitArray &a )
433{
434 resize( QMAX(size(), a.size()) );
435 register uchar *a1 = (uchar *)data();
436 register uchar *a2 = (uchar *)a.data();
437 int n = QMIN( QByteArray::size(), a.QByteArray::size() );
438 int p = QMAX( QByteArray::size(), a.QByteArray::size() ) - n;
439 while ( n-- > 0 )
440 *a1++ &= *a2++;
441 while ( p-- > 0 )
442 *a1++ = 0;
443 return *this;
444}
445
446/*!
447 Performs the OR operation between all bits in this bit array and
448 \a a. Returns a reference to this bit array.
449
450 The result has the length of the longest of the two bit arrays,
451 with any missing bits (i.e. if one array is shorter than the
452 other), taken to be 0.
453 \code
454 QBitArray a( 3 ), b( 2 );
455 a[0] = 1; a[1] = 0; a[2] = 1; // a = [1 0 1]
456 b[0] = 1; b[1] = 0; // b = [1 0]
457 a |= b; // a = [1 0 1]
458 \endcode
459
460 \sa operator&=(), operator^=(), operator~()
461*/
462
463QBitArray &QBitArray::operator|=( const QBitArray &a )
464{
465 resize( QMAX(size(), a.size()) );
466 register uchar *a1 = (uchar *)data();
467 register uchar *a2 = (uchar *)a.data();
468 int n = QMIN( QByteArray::size(), a.QByteArray::size() );
469 while ( n-- > 0 )
470 *a1++ |= *a2++;
471 return *this;
472}
473
474/*!
475 Performs the XOR operation between all bits in this bit array and
476 \a a. Returns a reference to this bit array.
477
478 The result has the length of the longest of the two bit arrays,
479 with any missing bits (i.e. if one array is shorter than the
480 other), taken to be 0.
481 \code
482 QBitArray a( 3 ), b( 2 );
483 a[0] = 1; a[1] = 0; a[2] = 1; // a = [1 0 1]
484 b[0] = 1; b[1] = 0; // b = [1 0]
485 a ^= b; // a = [0 0 1]
486 \endcode
487
488 \sa operator&=(), operator|=(), operator~()
489*/
490
491QBitArray &QBitArray::operator^=( const QBitArray &a )
492{
493 resize( QMAX(size(), a.size()) );
494 register uchar *a1 = (uchar *)data();
495 register uchar *a2 = (uchar *)a.data();
496 int n = QMIN( QByteArray::size(), a.QByteArray::size() );
497 while ( n-- > 0 )
498 *a1++ ^= *a2++;
499 return *this;
500}
501
502/*!
503 Returns a bit array that contains the inverted bits of this bit array.
504
505 Example:
506 \code
507 QBitArray a( 3 ), b;
508 a[0] = 1; a[1] = 0; a[2] = 1;// a = [1 0 1]
509 b = ~a; // b = [0 1 0]
510 \endcode
511*/
512
513QBitArray QBitArray::operator~() const
514{
515 QBitArray a( size() );
516 register uchar *a1 = (uchar *)data();
517 register uchar *a2 = (uchar *)a.data();
518 int n = QByteArray::size();
519 while ( n-- )
520 *a2++ = ~*a1++;
521 a.pad0();
522 return a;
523}
524
525
526/*!
527 \relates QBitArray
528
529 Returns the AND result between the bit arrays \a a1 and \a a2.
530
531 The result has the length of the longest of the two bit arrays,
532 with any missing bits (i.e. if one array is shorter than the
533 other), taken to be 0.
534
535 \sa QBitArray::operator&=()
536*/
537
538QBitArray operator&( const QBitArray &a1, const QBitArray &a2 )
539{
540 QBitArray tmp = a1.copy();
541 tmp &= a2;
542 return tmp;
543}
544
545/*!
546 \relates QBitArray
547
548 Returns the OR result between the bit arrays \a a1 and \a a2.
549
550 The result has the length of the longest of the two bit arrays,
551 with any missing bits (i.e. if one array is shorter than the
552 other), taken to be 0.
553
554 \sa QBitArray::operator|=()
555*/
556
557QBitArray operator|( const QBitArray &a1, const QBitArray &a2 )
558{
559 QBitArray tmp = a1.copy();
560 tmp |= a2;
561 return tmp;
562}
563
564/*!
565 \relates QBitArray
566
567 Returns the XOR result between the bit arrays \a a1 and \a a2.
568
569 The result has the length of the longest of the two bit arrays,
570 with any missing bits (i.e. if one array is shorter than the
571 other), taken to be 0.
572
573 \sa QBitArray::operator^()
574*/
575
576QBitArray operator^( const QBitArray &a1, const QBitArray &a2 )
577{
578 QBitArray tmp = a1.copy();
579 tmp ^= a2;
580 return tmp;
581}
582
583
584/* \enum QGArray::array_data
585
586 \warning This will be renamed in the next major release of Qt. Until
587 then it is undocumented and we recommend against its use.
588
589 \internal
590
591 ### 3.0 rename ###
592 ### 3.0 move it to QGArray? ###
593*/
594
595
596/*!
597 \fn QBitArray::array_data * QBitArray::newData()
598
599 \internal
600
601 Returns data specific to QBitArray that extends what QGArray provides.
602 QPtrCollection mechanism for allowing extra/different data.
603*/
604
605
606/*!
607 \fn void QBitArray::deleteData ( array_data * d )
608
609 \internal
610
611 Deletes data specific to QBitArray that extended what QGArray provided.
612
613 QPtrCollection mechanism for allowing extra/different data.
614*/
615
616
617/*****************************************************************************
618 QBitArray stream functions
619 *****************************************************************************/
620
621/*!
622 \relates QBitArray
623
624 Writes bit array \a a to stream \a s.
625
626 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
627*/
628#ifndef QT_NO_DATASTREAM
629QDataStream &operator<<( QDataStream &s, const QBitArray &a )
630{
631 Q_UINT32 len = a.size();
632 s << len; // write size of array
633 if ( len > 0 ) // write data
634 s.writeRawBytes( a.data(), a.QByteArray::size() );
635 return s;
636}
637
638/*!
639 \relates QBitArray
640
641 Reads a bit array into \a a from stream \a s.
642
643 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
644*/
645
646QDataStream &operator>>( QDataStream &s, QBitArray &a )
647{
648 Q_UINT32 len;
649 s >> len; // read size of array
650 if ( !a.resize( (uint)len ) ) { // resize array
651#if defined(QT_CHECK_NULL)
652 qWarning( "QDataStream: Not enough memory to read QBitArray" );
653#endif
654 len = 0;
655 }
656 if ( len > 0 ) // read data
657 s.readRawBytes( a.data(), a.QByteArray::size() );
658 return s;
659}
660
661#endif // QT_NO_DATASTREAM