CrossFramework Library

XBase/BitSet.hpp

説明を見る。
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 
00021     template < uint BIT_COUNT >
00022     struct BitSet
00023     {
00024         //============================================================
00025         // private
00026         enum  { ByteSize_ = ( BIT_COUNT + 7 ) / 8 };
00027         byte_t bits_[ ByteSize_ ];
00028         
00029         //============================================================
00030         // public
00031         
00033 
00034         enum { BitCount = BIT_COUNT }; 
00035         typedef BitSet< BitCount > MyType; 
00036 
00037 
00039 
00040 
00041         void clear()
00042         {
00043             for ( uint i = 0; i < MyType::ByteSize_; ++i )
00044             {
00045                 bits_[i] = 0;
00046             }
00047         }
00049 
00051 
00052 
00053         void set( uint aIndex , bool aFlag )
00054         {
00055             // チェック
00056             if ( BitCount <= aIndex )
00057             {
00058                 XBASE_INVALID_VALUE_ERROR( aIndex );
00059                 return;
00060             }
00061 
00062             // 設定
00063             byte_t& val = bits_[ aIndex >> 3 ];
00064             const byte_t mask = byte_t( 1 << ( aIndex & 0x7 ) );
00065             val = ( val & ~mask ) | ( aFlag ? mask : 0 );
00066         }
00067 
00069         void on( uint aIndex )
00070         {
00071             set( aIndex , true );
00072         }
00073                 
00075         void off( uint aIndex )
00076         {
00077             set( aIndex , false );
00078         }
00079 
00081         bool get( uint aIndex )const
00082         {
00083             // チェック
00084             if ( BitCount <= aIndex )
00085             {
00086                 XBASE_INVALID_VALUE_ERROR( aIndex );
00087                 return bool();
00088             }
00089             
00090             // 取得
00091             const byte_t& val = bits_[ aIndex >> 3 ];
00092             const byte_t mask = byte_t( 1 << ( aIndex & 0x7 ) );
00093             return ( val & mask ) != 0 ? true : false;
00094         }
00096         bool isAnyOn()const
00097         {
00098             for ( uint i = 0; i < ByteSize_; ++i )
00099             {
00100                 if ( bits_[i] != 0 )
00101                 {
00102                     return true;
00103                 }
00104             }
00105             return false;
00106         }
00108         bool isAllOff()const
00109         {
00110             return !isAnyOn();
00111         }
00113         bool isAllOn()const
00114         {
00115             for ( uint i = 0; i < BitCount; ++i )
00116             {
00117                 if ( !get( i ) )
00118                 {
00119                     return false;
00120                 }
00121             }
00122             return true;
00123         }
00125         
00127 
00128 
00129         const MyType operator ~()const
00130         {
00131             MyType obj = *this;
00132             for ( uint i = 0; i < ByteSize_; ++i )
00133             {
00134                 obj.bits_[ i ] = ~obj.bits_[ i ];
00135             }
00136             return obj;
00137         }
00138         
00140         const MyType operator &( const MyType& aRHS )const
00141         {
00142             MyType obj = *this;
00143             for ( uint i = 0; i < ByteSize_; ++i )
00144             {
00145                  obj.bits_[ i ] = bits_[i] & aRHS.bits_[i];
00146             }
00147             return obj;
00148         }
00149         
00151         const MyType operator |( const MyType& aRHS )const
00152         {
00153             MyType obj = *this;
00154             for ( uint i = 0; i < ByteSize_; ++i )
00155             {
00156                  obj.bits_[ i ] = bits_[i] | aRHS.bits_[i];
00157             }
00158             return obj;
00159         }
00160         
00162         const MyType operator ^( const MyType& aRHS )const
00163         {
00164             MyType obj = *this;
00165             for ( uint i = 0; i < ByteSize_; ++i )
00166             {
00167                 obj.bits_[ i ] = bits_[i] ^ aRHS.bits_[i];
00168             }
00169             return obj;
00170         }
00172     };
00174 }
00175 //------------------------------------------------------------
00176 #endif
00177 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義