📄 qbitarray.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qbitarray.h"#include "qdatastream.h"#define SHBLOCK ((bitarr_data*)(sharedBlock()))/*! \class QBitVal qbitarray.h \reentrant \brief The QBitVal class is an internal class, used with QBitArray. \ingroup collection The QBitVal is required by the indexing [] operator on bit arrays. It is not for use in any other context.*//*! \fn QBitVal::QBitVal (QBitArray* a, uint i) Constructs a reference to element \a i in the QBitArray \a a. This is what QBitArray::operator[] constructs its return value with.*//*! \fn QBitVal::operator int() Returns the value referenced by the QBitVal.*//*! \fn QBitVal& QBitVal::operator= (const QBitVal& v) Sets the value referenced by the QBitVal to that referenced by QBitVal \a v.*//*! \overload QBitVal& QBitVal::operator= (bool v) Sets the value referenced by the QBitVal to \a v.*//*! \class QBitArray qbitarray.h \reentrant \brief The QBitArray class provides an array of bits. \ingroup collection \ingroup tools \ingroup shared Because QBitArray is a QMemArray, it uses explicit \link shclass.html sharing\endlink with a reference count. A QBitArray is a special byte array that can access individual bits and perform bit-operations (AND, OR, XOR and NOT) on entire arrays or bits. Bits can be manipulated by the setBit() and clearBit() functions, but it is also possible to use the indexing [] operator to test and set individual bits. The [] operator is a little slower than setBit() and clearBit() because some tricks are required to implement single-bit assignments. Example: \code QBitArray a(3); a.setBit( 0 ); a.clearBit( 1 ); a.setBit( 2 ); // a = [1 0 1] QBitArray b(3); b[0] = 1; b[1] = 1; b[2] = 0; // b = [1 1 0] QBitArray c; c = ~a & b; // c = [0 1 0] \endcode When a QBitArray is constructed the bits are uninitialized. Use fill() to set all the bits to 0 or 1. The array can be resized with resize() and copied with copy(). Bits can be set with setBit() and cleared with clearBit(). Bits can be toggled with toggleBit(). A bit's value can be obtained with testBit() and with at(). QBitArray supports the \& (AND), | (OR), ^ (XOR) and ~ (NOT) operators.*//*! \class QBitArray::bitarr_data \brief The QBitArray::bitarr_data class is internal. \internal*//*! Constructs an empty bit array.*/QBitArray::QBitArray() : QByteArray( 0, 0 ){ bitarr_data *x = new bitarr_data; Q_CHECK_PTR( x ); x->nbits = 0; setSharedBlock( x );}/*! Constructs a bit array of \a size bits. The bits are uninitialized. \sa fill()*/QBitArray::QBitArray( uint size ) : QByteArray( 0, 0 ){ bitarr_data *x = new bitarr_data; Q_CHECK_PTR( x ); x->nbits = 0; setSharedBlock( x ); resize( size );}/*! \fn QBitArray::QBitArray( const QBitArray &a ) Constructs a shallow copy of \a a.*//*! \fn QBitArray &QBitArray::operator=( const QBitArray &a ) Assigns a shallow copy of \a a to this bit array and returns a reference to this array.*//*! Pad last byte with 0-bits.*/void QBitArray::pad0(){ uint sz = size(); if ( sz && sz%8 ) *(data()+sz/8) &= (1 << (sz%8)) - 1;}/*! \fn uint QBitArray::size() const Returns the bit array's size (number of bits). \sa resize()*//*! Resizes the bit array to \a size bits and returns TRUE if the bit array could be resized; otherwise returns FALSE. The array becomes a null array if \a size == 0. If the array is expanded, the new bits are set to 0. \sa size()*/bool QBitArray::resize( uint size ){ uint s = this->size(); if ( !QByteArray::resize( (size+7)/8 ) ) return FALSE; // cannot resize SHBLOCK->nbits = size; if ( size != 0 ) { // not null array int ds = (int)(size+7)/8 - (int)(s+7)/8;// number of bytes difference if ( ds > 0 ) // expanding array memset( data() + (s+7)/8, 0, ds ); // reset new data } return TRUE;}/*! Fills the bit array with \a v (1's if \a v is TRUE, or 0's if \a v is FALSE). fill() resizes the bit array to \a size bits if \a size is nonnegative. Returns FALSE if a nonnegative \e size was specified and the bit array could not be resized; otherwise returns TRUE. \sa resize()*/bool QBitArray::fill( bool v, int size ){ if ( size >= 0 ) { // resize first if ( !resize( size ) ) return FALSE; // cannot resize } else { size = this->size(); } if ( size > 0 ) memset( data(), v ? 0xff : 0, (size + 7) / 8 ); if ( v ) pad0(); return TRUE;}/*! Detaches from shared bit array data and makes sure that this bit array is the only one referring to the data. If multiple bit arrays share common data, this bit array dereferences the data and gets a copy of the data. Nothing happens if there is only a single reference. \sa copy()*/void QBitArray::detach(){ int nbits = SHBLOCK->nbits; this->duplicate( *this ); SHBLOCK->nbits = nbits;}/*! Returns a deep copy of the bit array. \sa detach()*/QBitArray QBitArray::copy() const{ QBitArray tmp; tmp.duplicate( *this ); ((bitarr_data*)(tmp.sharedBlock()))->nbits = SHBLOCK->nbits; return tmp;}/*! Returns TRUE if the bit at position \a index is set, i.e. is 1; otherwise returns FALSE. \sa setBit(), clearBit()*/bool QBitArray::testBit( uint index ) const{#if defined(QT_CHECK_RANGE) if ( index >= size() ) { qWarning( "QBitArray::testBit: Index %d out of range", index ); return FALSE; }#endif return (*(data()+(index>>3)) & (1 << (index & 7))) != 0;}/*! \overload Sets the bit at position \a index to 1. \sa clearBit() toggleBit()*/void QBitArray::setBit( uint index ){#if defined(QT_CHECK_RANGE) if ( index >= size() ) { qWarning( "QBitArray::setBit: Index %d out of range", index ); return; }#endif *(data()+(index>>3)) |= (1 << (index & 7));}/*! \fn void QBitArray::setBit( uint index, bool value ) Sets the bit at position \a index to \a value. Equivalent to: \code if ( value ) setBit( index ); else clearBit( index ); \endcode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -