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 #ifndef BIT_SET_H_
00026 #define BIT_SET_H_
00027
00028 namespace Lamp{
00029
00030
00031
00032
00033
00034
00035
00036
00037 template <typename Type>
00038 class BitSet{
00039 public:
00040
00041
00042
00043
00044 BitSet() : bits_(0){}
00045
00046
00047
00048
00049
00050 BitSet(const Type& bits) : bits_(bits){}
00051
00052
00053
00054
00055 ~BitSet(){}
00056
00057
00058
00059
00060
00061
00062 int getLength() const{ return sizeof(Type) * 8; }
00063
00064
00065
00066
00067 void clear(){ bits_ = 0; }
00068
00069
00070
00071
00072
00073
00074 void setBits(Type bits){ bits_ = bits; }
00075
00076
00077
00078
00079
00080 Type getBits() const{ return bits_; }
00081
00082
00083
00084
00085
00086
00087
00088
00089 const BitSet& not(){
00090 bits_ = ~bits_;
00091 return (*this);
00092 }
00093
00094
00095
00096
00097
00098 const BitSet& onAllBits(){
00099 bits_ = 0;
00100 bits_ = ~bits_;
00101 return (*this);
00102 }
00103
00104
00105
00106
00107
00108 const BitSet& offAllBits(){
00109 bits_ = 0;
00110 return (*this);
00111 }
00112
00113
00114
00115
00116
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
00130
00131
00132 const BitSet& and(const BitSet& bitSet){
00133 bits_ &= bitSet.bits_;
00134 return (*this);
00135 }
00136
00137
00138
00139
00140
00141
00142 const BitSet& or(const BitSet& bitSet){
00143 bits_ |= bitSet.bits_;
00144 return (*this);
00145 }
00146
00147
00148
00149
00150
00151
00152 const BitSet& xor(const BitSet& bitSet){
00153 bits_ ^= bitSet.bits_;
00154 return (*this);
00155 }
00156
00157
00158
00159
00160
00161
00162 const BitSet& onBit(const BitSet& bitSet){
00163 bits_ |= bitSet.bits_;
00164 return (*this);
00165 }
00166
00167
00168
00169
00170
00171
00172 const BitSet& offBit(const BitSet& bitSet){
00173 bits_ &= (~bitSet.bits_);
00174 return (*this);
00175 }
00176
00177
00178
00179
00180
00181
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
00192
00193
00194 bool getBit(const BitSet& bitSet) const{
00195 return ((bits_ & bitSet.bits_) == bitSet.bits_);
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 const BitSet& and(Type bits){
00207 bits_ &= bits;
00208 return (*this);
00209 }
00210
00211
00212
00213
00214
00215
00216 const BitSet& or(Type bits){
00217 bits_ |= bits;
00218 return (*this);
00219 }
00220
00221
00222
00223
00224
00225
00226 const BitSet& xor(Type bits){
00227 bits_ ^= bits;
00228 return (*this);
00229 }
00230
00231
00232
00233
00234
00235
00236 const BitSet& onBit(Type bits){
00237 bits_ |= bits;
00238 return (*this);
00239 }
00240
00241
00242
00243
00244
00245
00246 const BitSet& offBit(Type bits){
00247 bits_ &= (~bits);
00248 return (*this);
00249 }
00250
00251
00252
00253
00254
00255
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
00266
00267
00268 bool getBit(Type bits) const{
00269 return ((bits_ & bits) == bits);
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279 const BitSet& onIndexedBit(int index){
00280 Assert((index >= 0) && (index < getLength()));
00281 bits_ |= (0x1 << index);
00282 return (*this);
00283 }
00284
00285
00286
00287
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
00298
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
00309
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
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 }
00339 #endif // End of BIT_SET_H_
00340
00341