00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef QBITARRAY_H
00039 #define QBITARRAY_H
00040
00041 #ifndef QT_H
00042 #include "qstring.h"
00043 #endif // QT_H
00044
00045
00046
00047
00048
00049
00050 class QBitArray;
00051
00052 class Q_EXPORT QBitVal
00053 {
00054 private:
00055 QBitArray *array;
00056 uint index;
00057 public:
00058 QBitVal( QBitArray *a, uint i ) : array(a), index(i) {}
00059 operator int();
00060 QBitVal &operator=( const QBitVal &v );
00061 QBitVal &operator=( bool v );
00062 };
00063
00064
00065
00066
00067
00068
00069 class Q_EXPORT QBitArray : public QByteArray
00070 {
00071 public:
00072 QBitArray();
00073 QBitArray( uint size );
00074 QBitArray( const QBitArray &a ) : QByteArray( a ) {}
00075
00076 QBitArray &operator=( const QBitArray & );
00077
00078 uint size() const;
00079 bool resize( uint size );
00080
00081 bool fill( bool v, int size = -1 );
00082
00083 void detach();
00084 QBitArray copy() const;
00085
00086 bool testBit( uint index ) const;
00087 void setBit( uint index );
00088 void setBit( uint index, bool value );
00089 void clearBit( uint index );
00090 bool toggleBit( uint index );
00091
00092 bool at( uint index ) const;
00093 QBitVal operator[]( int index );
00094 bool operator[]( int index ) const;
00095
00096 QBitArray &operator&=( const QBitArray & );
00097 QBitArray &operator|=( const QBitArray & );
00098 QBitArray &operator^=( const QBitArray & );
00099 QBitArray operator~() const;
00100
00101 protected:
00102 struct bitarr_data : public QGArray::array_data {
00103 uint nbits;
00104 };
00105 array_data *newData() { return new bitarr_data; }
00106 void deleteData( array_data *d ) { delete (bitarr_data*)d; }
00107 private:
00108 void pad0();
00109 };
00110
00111
00112 inline QBitArray &QBitArray::operator=( const QBitArray &a )
00113 { return (QBitArray&)assign( a ); }
00114
00115 inline uint QBitArray::size() const
00116 { return ((bitarr_data*)sharedBlock())->nbits; }
00117
00118 inline void QBitArray::setBit( uint index, bool value )
00119 { if ( value ) setBit(index); else clearBit(index); }
00120
00121 inline bool QBitArray::at( uint index ) const
00122 { return testBit(index); }
00123
00124 inline QBitVal QBitArray::operator[]( int index )
00125 { return QBitVal( (QBitArray*)this, index ); }
00126
00127 inline bool QBitArray::operator[]( int index ) const
00128 { return testBit( index ); }
00129
00130
00131
00132
00133
00134
00135 Q_EXPORT QBitArray operator&( const QBitArray &, const QBitArray & );
00136 Q_EXPORT QBitArray operator|( const QBitArray &, const QBitArray & );
00137 Q_EXPORT QBitArray operator^( const QBitArray &, const QBitArray & );
00138
00139
00140 inline QBitVal::operator int()
00141 {
00142 return array->testBit( index );
00143 }
00144
00145 inline QBitVal &QBitVal::operator=( const QBitVal &v )
00146 {
00147 array->setBit( index, v.array->testBit(v.index) );
00148 return *this;
00149 }
00150
00151 inline QBitVal &QBitVal::operator=( bool v )
00152 {
00153 array->setBit( index, v );
00154 return *this;
00155 }
00156
00157
00158
00159
00160
00161 #ifndef QT_NO_DATASTREAM
00162 Q_EXPORT QDataStream &operator<<( QDataStream &, const QBitArray & );
00163 Q_EXPORT QDataStream &operator>>( QDataStream &, QBitArray & );
00164 #endif
00165
00166 #endif // QBITARRAY_H