Source Code (Use browser search to find items of interest.)

Class Index

qt'QBitArray (./qt-2.1.0/src/tools/qbitarray.h:57)

class Q_EXPORT QBitArray : public QByteArray
{
public:
    QBitArray();
    QBitArray( uint size );
    QBitArray( const QBitArray &a ) : QByteArray( a ) {}

    QBitArray &operator=( const QBitArray & );

    uint    size() const;
    bool    resize( uint size );

    bool    fill( bool v, int size = -1 );

    void    detach();
    QBitArray copy() const;

    bool    testBit( uint index ) const;
    void    setBit( uint index );
    void    setBit( uint index, bool value );
    void    clearBit( uint index );
    bool    toggleBit( uint index );

    bool    at( uint index ) const;
    QBitVal operator[]( int index );

    QBitArray &operator&=( const QBitArray & );
    QBitArray &operator|=( const QBitArray & );
    QBitArray &operator^=( const QBitArray & );
    QBitArray  operator~() const;

protected:
    struct bitarr_data : public QGArray::array_data {
	uint   nbits;
    };
    array_data *newData()		    { return new bitarr_data; }
    void	deleteData( array_data *d ) { delete (bitarr_data*)d; }
private:
    void    pad0();
};


inline QBitArray &QBitArray::operator=( const QBitArray &a )
{ return (QBitArray&)assign( a ); }

inline uint QBitArray::size() const
{ return ((bitarr_data*)sharedBlock())->nbits; }

inline void QBitArray::setBit( uint index, bool value )
{ if ( value ) setBit(index); else clearBit(index); }

inline bool QBitArray::at( uint index ) const
{ return testBit(index); }

inline QBitVal QBitArray::operator[]( int index )
{ return QBitVal( (QBitArray*)this, index ); }


/*****************************************************************************
  Misc. QBitArray operator functions
 *****************************************************************************/


qt'QBitArray::QBitArray() (./qt-2.1.0/src/tools/qbitarray.cpp:110)

QBitArray::QBitArray() : QByteArray( 0, 0 )
{
    bitarr_data *x = new bitarr_data;
    CHECK_PTR( x );
    x->nbits = 0;
    setSharedBlock( x );
}

/*!
  Constructs a bit array of \e size bits. The bits are uninitialized.
*/


qt'QBitArray::QBitArray() (./qt-2.1.0/src/tools/qbitarray.cpp:122)

QBitArray::QBitArray( uint size ) : QByteArray( 0, 0 )
{
    bitarr_data *x = new bitarr_data;
    CHECK_PTR( x );
    x->nbits = 0;
    setSharedBlock( x );
    resize( size );
}

/*!
  \fn QBitArray::QBitArray( const QBitArray &a )
  Constructs a shallow copy of \e a.
*/

/*!
  \fn QBitArray &QBitArray::operator=( const QBitArray &a )
  Assigns a shallow copy of \e a to this bit array and returns a reference
  to this array.
*/


/*!
  Pad last byte with 0-bits.
 */

qt'QBitArray::pad0() (./qt-2.1.0/src/tools/qbitarray.cpp:146)

void QBitArray::pad0()
{
    uint sz = size();
    if ( sz && sz%8 )
	*(data()+sz/8) &= (1 << (sz%8)) - 1;
}


/*!
  \fn uint QBitArray::size() const
  Returns the size (number of bits) of the bit array.
  \sa resize()
*/

/*!
  Resizes the bit array to \e size bits.
  Returns TRUE if the bit array could be resized.

  When expanding the bit array, the new bits will be uninitialized.

  \sa size()
*/


qt'QBitArray::resize() (./qt-2.1.0/src/tools/qbitarray.cpp:169)

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 \e v (1's if \e v is TRUE, or 0's if \e v is FALSE).

  Will resize the bit array to \e size bits if \e size is nonnegative.

  Returns FALSE if a nonnegative \e size was specified and if the bit array
  could not be resized, otherwise returns TRUE.

  \sa resize()
*/


qt'QBitArray::fill() (./qt-2.1.0/src/tools/qbitarray.cpp:195)

bool QBitArray::fill( bool v, int size )
{
    if ( size >= 0 ) {				// resize first
	if ( !resize( size ) )
	    return FALSE;			// cannot resize
    } else {
	size = this->size();
    }
    memset( data(), v ? 0xff : 0, (size+7)/8 ); // set many bytes, fast
    if ( v )
	pad0();
    return TRUE;
}


/*!
  Detaches from shared bit array data and makes sure that this bit array
  is the only one referring the data.

  If multiple bit arrays share common data, this bit array dereferences the
  data and gets a copy of the data. Nothing will be done if there is just
  a single reference.

  \sa copy()
*/


qt'QBitArray::detach() (./qt-2.1.0/src/tools/qbitarray.cpp:221)

void QBitArray::detach()
{
    int nbits = SHBLOCK->nbits;
    this->duplicate( *this );
    SHBLOCK->nbits = nbits;
}

/*!
  Returns a deep copy of the bit array.
  \sa detach()
*/


qt'QBitArray::copy() (./qt-2.1.0/src/tools/qbitarray.cpp:233)

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 \e index is set.
  \sa setBit(), clearBit()
*/


qt'QBitArray::testBit() (./qt-2.1.0/src/tools/qbitarray.cpp:247)

bool QBitArray::testBit( uint index ) const
{
#if defined(CHECK_RANGE)
    if ( index >= size() ) {
	qWarning( "QBitArray::testBit: Index %d out of range", index );
	return FALSE;
    }
#endif
    return *(data()+(index>>3)) & (1 << (index & 7));
}

/*!
  Sets the bit at position \e index (sets it to 1).
  \sa clearBit(), toggleBit()
*/


qt'QBitArray::setBit() (./qt-2.1.0/src/tools/qbitarray.cpp:263)

void QBitArray::setBit( uint index )
{
#if defined(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 \e index to \e value.

  Equivalent to:
  \code
    if ( value )
	setBit( index );
    else
	clearBit( index );
  \endcode

  \sa clearBit(), toggleBit()
*/

/*!
  Clears the bit at position \e index (sets it to 0).
  \sa setBit(), toggleBit()
*/


qt'QBitArray::clearBit() (./qt-2.1.0/src/tools/qbitarray.cpp:294)

void QBitArray::clearBit( uint index )
{
#if defined(CHECK_RANGE)
    if ( index >= size() ) {
	qWarning( "QBitArray::clearBit: Index %d out of range", index );
	return;
    }
#endif
    *(data()+(index>>3)) &= ~(1 << (index & 7));
}

/*!
  Toggles the bit at position \e index.

  If the previous value was 0, the new value will be 1.	 If the previous
  value was 1, the new value will be 0.

  \sa setBit(), clearBit()
*/


qt'QBitArray::toggleBit() (./qt-2.1.0/src/tools/qbitarray.cpp:314)

bool QBitArray::toggleBit( uint index )
{
#if defined(CHECK_RANGE)
    if ( index >= size() ) {
	qWarning( "QBitArray::toggleBit: Index %d out of range", index );
	return FALSE;
    }
#endif
    register uchar *p = (uchar *)data() + (index>>3);
    uchar b = (1 << (index & 7));		// bit position
    uchar c = *p & b;				// read bit
    *p ^= b;					// toggle bit
    return c;
}


/*!
  \fn bool QBitArray::at( uint index ) const
  Returns the value (0 or 1) of the bit at position \e index.
  \sa operator[]()
*/

/*!
  \fn QBitVal QBitArray::operator[]( int index )
  Implements the [] operator for bit arrays.

  The returned QBitVal is a context object. It makes it possible to get
  and set a single bit value.

  Example:
  \code
    QBitArray a( 3 );
    a[0] = 0;
    a[1] = 1;
    a[2] = a[0] ^ a[1];
  \endcode

  The functions testBit(), setBit() and clearBit() are faster.

  \sa at()
*/


/*!
  Performs the AND operation between all bits in this bit array and \e a.
  Returns a reference to this bit array.

  The two bit arrays must have the same size.  The debug library will
  warn you if they aren't, the production library blithely ignores the
  problem.

  Example:
  \code
    QBitArray a(3), b(3);
    a[0] = 1;  a[1] = 0;	a[2] = 1;	// a = [1 0 1]
    b[0] = 0;  b[1] = 0;	b[2] = 1;	// b = [0 0 1]
    a &= b;					// a = [0 0 1]
  \endcode

  \sa operator|=(), operator^=(), operator~()
*/


qt'QBitArray::~() (./qt-2.1.0/src/tools/qbitarray.cpp:473)

QBitArray QBitArray::operator~() const
{
    QBitArray a( size() );
    register uchar *a1 = (uchar *)data();
    register uchar *a2 = (uchar *)a.data();
    int n = QByteArray::size();
    while ( n-- )
	*a2++ = ~*a1++;
    a.pad0();
    return a;
}


/*!
  \relates QBitArray
  Returns the AND result between the bit arrays \e a1 and \e a2.
  \sa QBitArray::operator&=()
*/