CrossFramework Library
|
00001 00006 #if defined(XBASE_INCLUDED_BITSET_HPP) 00007 #else 00008 #define XBASE_INCLUDED_BITSET_HPP 00009 00010 //------------------------------------------------------------ 00011 #include <XBase/BuiltInTypes.hpp> 00012 #include <XBase/RuntimeAssert.hpp> 00013 00014 //------------------------------------------------------------ 00015 namespace XBase { 00017 00018 00024 template < uint BIT_COUNT , uint ALIGNMENT = 4 > 00025 struct BitSetPOD 00026 { 00027 //============================================================ 00028 // private 00029 enum 00030 { 00031 ByteSizeRaw_ = ( BIT_COUNT + 7 ) / 8 , 00032 ByteSize_ = ByteSizeRaw_ + ( ALIGNMENT - 1 ) / ALIGNMENT 00033 }; 00034 byte_t bits_[ ByteSize_ ]; 00035 00036 //============================================================ 00037 // public 00038 00040 00041 enum { BitCount = BIT_COUNT }; 00042 typedef BitSetPOD< BitCount > MyType; 00043 00044 00046 00047 00048 void clear() 00049 { 00050 for ( uint i = 0; i < MyType::ByteSize_; ++i ) 00051 { 00052 bits_[i] = 0; 00053 } 00054 } 00056 00058 00059 00060 void set( uint aIndex , bool aFlag ) 00061 { 00062 // チェック 00063 if ( BitCount <= aIndex ) 00064 { 00065 XBASE_INVALID_VALUE_ERROR( aIndex ); 00066 return; 00067 } 00068 00069 // 設定 00070 byte_t& val = bits_[ aIndex >> 3 ]; 00071 const byte_t mask = byte_t( 1 << ( aIndex & 0x7 ) ); 00072 val = ( val & ~mask ) | ( aFlag ? mask : 0 ); 00073 } 00074 00076 void on( uint aIndex ) 00077 { 00078 set( aIndex , true ); 00079 } 00080 00082 void off( uint aIndex ) 00083 { 00084 set( aIndex , false ); 00085 } 00086 00088 bool get( uint aIndex )const 00089 { 00090 // チェック 00091 if ( BitCount <= aIndex ) 00092 { 00093 XBASE_INVALID_VALUE_ERROR( aIndex ); 00094 return bool(); 00095 } 00096 00097 // 取得 00098 const byte_t& val = bits_[ aIndex >> 3 ]; 00099 const byte_t mask = byte_t( 1 << ( aIndex & 0x7 ) ); 00100 return ( val & mask ) != 0 ? true : false; 00101 } 00103 bool isAnyOn()const 00104 { 00105 for ( uint i = 0; i < ByteSize_; ++i ) 00106 { 00107 if ( bits_[i] != 0 ) 00108 { 00109 return true; 00110 } 00111 } 00112 return false; 00113 } 00115 bool isAllOff()const 00116 { 00117 return !isAnyOn(); 00118 } 00120 bool isAllOn()const 00121 { 00122 for ( uint i = 0; i < BitCount; ++i ) 00123 { 00124 if ( !get( i ) ) 00125 { 00126 return false; 00127 } 00128 } 00129 return true; 00130 } 00132 00134 00135 00136 const MyType operator ~()const 00137 { 00138 MyType obj = *this; 00139 for ( uint i = 0; i < ByteSize_; ++i ) 00140 { 00141 obj.bits_[ i ] = ~obj.bits_[ i ]; 00142 } 00143 return obj; 00144 } 00145 00147 const MyType operator &( const MyType& aRHS )const 00148 { 00149 MyType obj = *this; 00150 for ( uint i = 0; i < ByteSize_; ++i ) 00151 { 00152 obj.bits_[ i ] = bits_[i] & aRHS.bits_[i]; 00153 } 00154 return obj; 00155 } 00156 00158 const MyType operator |( const MyType& aRHS )const 00159 { 00160 MyType obj = *this; 00161 for ( uint i = 0; i < ByteSize_; ++i ) 00162 { 00163 obj.bits_[ i ] = bits_[i] | aRHS.bits_[i]; 00164 } 00165 return obj; 00166 } 00167 00169 const MyType operator ^( const MyType& aRHS )const 00170 { 00171 MyType obj = *this; 00172 for ( uint i = 0; i < ByteSize_; ++i ) 00173 { 00174 obj.bits_[ i ] = bits_[i] ^ aRHS.bits_[i]; 00175 } 00176 return obj; 00177 } 00179 }; 00180 00182 template < uint BIT_COUNT , uint ALIGNMENT = 4 > 00183 class BitSet : public BitSetPOD< BIT_COUNT , ALIGNMENT > 00184 { 00185 public: 00187 00188 00189 BitSet() 00190 { 00191 BitSetPOD< BIT_COUNT , ALIGNMENT >::clear(); 00192 } 00193 00195 BitSet( const BitSetPOD< BIT_COUNT , ALIGNMENT >& aObj ) 00196 { 00197 static_cast< BitSetPOD< BIT_COUNT , ALIGNMENT >& >( *this ) = aObj; 00198 } 00199 00201 }; 00203 } 00204 //------------------------------------------------------------ 00205 #endif 00206 // EOF