Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

BitSet.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * ビットセットヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef BIT_SET_H_
00026 #define BIT_SET_H_
00027 
00028 namespace Lamp{
00029 
00030 //------------------------------------------------------------------------------
00031 /**
00032  * ビットセット
00033  *
00034  * テンプレートの型にu_char, u_short, u_int等を使用してください。
00035  * このクラスは継承しないで下さい。
00036  */
00037 template <typename Type>
00038 class BitSet{
00039 public:
00040     //--------------------------------------------------------------------------
00041     /**
00042      * コンストラクタ
00043      */
00044     BitSet() : bits_(0){}
00045 
00046     /**
00047      * コンストラクタ
00048      * @param bits ビット列
00049      */
00050     BitSet(const Type& bits) : bits_(bits){}
00051 
00052     /**
00053      * デストラクタ
00054      */
00055     ~BitSet(){}
00056 
00057     //--------------------------------------------------------------------------
00058     /**
00059      * 長さの取得
00060      * @return 長さ
00061      */
00062     int getLength() const{ return sizeof(Type) * 8; }
00063 
00064     /**
00065      * クリア
00066      */
00067     void clear(){ bits_ = 0; }
00068 
00069     //--------------------------------------------------------------------------
00070     /**
00071      * ビット列の設定
00072      * @param bits ビット列
00073      */
00074     void setBits(Type bits){ bits_ = bits; }
00075 
00076     /**
00077      * ビット列の取得
00078      * @return ビット列
00079      */
00080     Type getBits() const{ return bits_; }
00081 
00082     //--------------------------------------------------------------------------
00083     // 全ビットに対する操作
00084     //--------------------------------------------------------------------------
00085     /**
00086      * 補数
00087      * @return ビットセット
00088      */
00089     const BitSet& not(){
00090         bits_ = ~bits_;
00091         return (*this);
00092     }
00093 
00094     /**
00095      * 全てのビットをONにする
00096      * @return ビットセット
00097      */
00098     const BitSet& onAllBits(){
00099         bits_ = 0;
00100         bits_ = ~bits_;
00101         return (*this);
00102     }
00103 
00104     /**
00105      * 全てのビットをOFFにする
00106      * @return ビットセット
00107      */
00108     const BitSet& offAllBits(){
00109         bits_ = 0;
00110         return (*this);
00111     }
00112 
00113     /**
00114      * 全てのビットを設定する
00115      * @param flag trueならビットをONにします
00116      * @return ビットセット
00117      */
00118     const BitSet& setAllBits(bool flag){
00119         if(flag){ onAllBits(); }
00120         else{ offAllBits(); }
00121         return (*this);
00122     }
00123 
00124     //--------------------------------------------------------------------------
00125     // ビットセットによるビット操作
00126     //--------------------------------------------------------------------------
00127     /**
00128      * 論理積
00129      * @param bitSet 論理積をとるビットセット
00130      * @return ビットセット
00131      */
00132     const BitSet& and(const BitSet& bitSet){
00133         bits_ &= bitSet.bits_;
00134         return (*this);
00135     }
00136 
00137     /**
00138      * 論理和
00139      * @param bitSet 論理和をとるビットセット
00140      * @return ビットセット
00141      */
00142     const BitSet& or(const BitSet& bitSet){
00143         bits_ |= bitSet.bits_;
00144         return (*this);
00145     }
00146 
00147     /**
00148      * 排他的論理和
00149      * @param bitSet 排他的論理和をとるビットセット
00150      * @return ビットセット
00151      */
00152     const BitSet& xor(const BitSet& bitSet){
00153         bits_ ^= bitSet.bits_;
00154         return (*this);
00155     }
00156 
00157     /**
00158      * ビットをONにする
00159      * @param bitSet 設定するビットセット
00160      * @return ビットセット
00161      */
00162     const BitSet& onBit(const BitSet& bitSet){
00163         bits_ |= bitSet.bits_;
00164         return (*this);
00165     }
00166 
00167     /**
00168      * ビットをOFFにする
00169      * @param bitSet 設定するビットセット
00170      * @return ビットセット
00171      */
00172     const BitSet& offBit(const BitSet& bitSet){
00173         bits_ &= (~bitSet.bits_);
00174         return (*this);
00175     }
00176 
00177     /**
00178      * ビットの設定
00179      * @param bitSet ビットを設定するビットセット
00180      * @param flag trueならビットをONにします
00181      * @return ビットセット
00182      */
00183     const BitSet& setBit(const BitSet& bitSet, bool flag){
00184         if(flag){ onBit(bitSet); }
00185         else{ offBit(bitSet); }
00186         return (*this);
00187     }
00188 
00189     /**
00190      * ビットの取得
00191      * @param bitSet ビットを取得するビットセット
00192      * @return ビットがすべてONならばtrue
00193      */
00194     bool getBit(const BitSet& bitSet) const{
00195         return ((bits_ & bitSet.bits_) == bitSet.bits_);
00196     }
00197 
00198     //--------------------------------------------------------------------------
00199     // ビット列によるビット操作
00200     //--------------------------------------------------------------------------
00201     /**
00202      * 論理積
00203      * @param bits 論理積をとるビット列
00204      * @return ビットセット
00205      */
00206     const BitSet& and(Type bits){
00207         bits_ &= bits;
00208         return (*this);
00209     }
00210 
00211     /**
00212      * 論理和
00213      * @param bits 論理和をとるビット列
00214      * @return ビットセット
00215      */
00216     const BitSet& or(Type bits){
00217         bits_ |= bits;
00218         return (*this);
00219     }
00220 
00221     /**
00222      * 排他的論理和
00223      * @param bits 排他的論理和をとるビット列
00224      * @return ビットセット
00225      */
00226     const BitSet& xor(Type bits){
00227         bits_ ^= bits;
00228         return (*this);
00229     }
00230 
00231     /**
00232      * ビットをONにする
00233      * @param bits 設定するビット列
00234      * @return ビットセット
00235      */
00236     const BitSet& onBit(Type bits){
00237         bits_ |= bits;
00238         return (*this);
00239     }
00240 
00241     /**
00242      * ビットをOFFにする
00243      * @param bits 設定するビット列
00244      * @return ビットセット
00245      */
00246     const BitSet& offBit(Type bits){
00247         bits_ &= (~bits);
00248         return (*this);
00249     }
00250 
00251     /**
00252      * ビットの設定
00253      * @param bits ビットを設定するビット列
00254      * @param flag trueならビットをONにします
00255      * @return ビットセット
00256      */
00257     const BitSet& setBit(Type bits, bool flag){
00258         if(flag){ onBit(bits); }
00259         else{ offBit(bits); }
00260         return (*this);
00261     }
00262 
00263     /**
00264      * ビットの取得
00265      * @param bits ビットを取得するビット列
00266      * @return ビットがすべてONならばtrue
00267      */
00268     bool getBit(Type bits) const{
00269         return ((bits_ & bits) == bits);
00270     }
00271 
00272     //--------------------------------------------------------------------------
00273     // インデックスによるビット操作
00274     //--------------------------------------------------------------------------
00275     /**
00276      * インデックスビットをONにする
00277      * @param index ビットのインデックス
00278      */
00279     const BitSet& onIndexedBit(int index){
00280         Assert((index >= 0) && (index < getLength()));
00281         bits_ |= (0x1 << index);
00282         return (*this);
00283     }
00284 
00285     /**
00286      * インデックスビットをOFFにする
00287      * @param index ビットのインデックス
00288      */
00289     const BitSet& offIndexedBit(int index){
00290         Assert((index >= 0) && (index < getLength()));
00291         bits_ &= (~(0x1 << index));
00292         return (*this);
00293     }
00294 
00295     /**
00296      * インデックスビットの設定
00297      * @param index ビットのインデックス
00298      * @param flag trueならビットをONにします
00299      */
00300     const BitSet& setIndexedBit(int index, bool flag){
00301         if(flag){ onIndexedBit(index); }
00302         else{ offIndexedBit(index); }
00303         return (*this);
00304     }
00305 
00306     /**
00307      * インデックスビットの取得
00308      * @param index ビットのインデックス
00309      * @return インデックスのビットが立っていればtrue
00310      */
00311     bool getIndexedBit(int index) const{
00312         Assert((index >= 0) && (index < getLength()));
00313         return ((bits_ & (0x1 << index)) != 0);
00314     }
00315 
00316     //--------------------------------------------------------------------------
00317     /**
00318      * 文字列への変換
00319      * @return 文字列
00320      */
00321     String toString() const{
00322         String result;
00323         for(int i = getLength() - 1; i >= 0; i--){
00324             if(getIndexedBit(i)){ result += "1"; }
00325             else{ result += "0"; }
00326         }
00327         return result;
00328     }
00329 
00330 private:
00331     //--------------------------------------------------------------------------
00332     // ビット列
00333     Type bits_;
00334 
00335 };
00336 
00337 //------------------------------------------------------------------------------
00338 } // End of namespace Lamp
00339 #endif // End of BIT_SET_H_
00340 //------------------------------------------------------------------------------
00341 

Generated on Wed Mar 16 10:29:28 2005 for Lamp by doxygen 1.3.2