00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef ROOT_TBits
00013 #define ROOT_TBits
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef ROOT_TObject
00024 #include "TObject.h"
00025 #endif
00026 #ifndef ROOT_Riosfwd
00027 #include "Riosfwd.h"
00028 #endif
00029 #ifndef __CINT__
00030 #include <string.h>
00031 #endif
00032
00033 class TBits : public TObject {
00034
00035 protected:
00036
00037 UInt_t fNbits;
00038 UInt_t fNbytes;
00039 UChar_t *fAllBits;
00040
00041 void ReserveBytes(UInt_t nbytes);
00042 void DoAndEqual(const TBits& rhs);
00043 void DoOrEqual (const TBits& rhs);
00044 void DoXorEqual(const TBits& rhs);
00045 void DoLeftShift(UInt_t shift);
00046 void DoRightShift(UInt_t shift);
00047 void DoFlip();
00048
00049 public:
00050 TBits(UInt_t nbits = 8);
00051 TBits(const TBits&);
00052 TBits& operator=(const TBits&);
00053 virtual ~TBits();
00054
00055 class TReference {
00056 friend class TBits;
00057
00058 TBits &fBits;
00059 UInt_t fPos;
00060
00061 TReference();
00062
00063 public:
00064 TReference(TBits& bit, UInt_t pos) : fBits(bit),fPos(pos) { }
00065 ~TReference() { }
00066
00067
00068 TReference& operator=(Bool_t val);
00069
00070
00071 TReference& operator=(const TReference& rhs);
00072
00073 #ifndef __CINT__
00074
00075 Bool_t operator~() const;
00076 #endif
00077
00078
00079 operator Bool_t() const;
00080 };
00081
00082
00083
00084 void ResetAllBits(Bool_t value=kFALSE);
00085 void ResetBitNumber(UInt_t bitnumber);
00086 void SetBitNumber(UInt_t bitnumber, Bool_t value = kTRUE);
00087 Bool_t TestBitNumber(UInt_t bitnumber) const;
00088
00089
00090 TBits::TReference operator[](UInt_t bitnumber) { return TReference(*this,bitnumber); }
00091 Bool_t operator[](UInt_t bitnumber) const;
00092
00093 TBits& operator&=(const TBits& rhs) { DoAndEqual(rhs); return *this; }
00094 TBits& operator|=(const TBits& rhs) { DoOrEqual(rhs); return *this; }
00095 TBits& operator^=(const TBits& rhs) { DoXorEqual(rhs); return *this; }
00096 TBits& operator<<=(UInt_t rhs) { DoLeftShift(rhs); return *this; }
00097 TBits& operator>>=(UInt_t rhs) { DoRightShift(rhs); return *this; }
00098 TBits operator<<(UInt_t rhs) { return TBits(*this)<<= rhs; }
00099 TBits operator>>(UInt_t rhs) { return TBits(*this)>>= rhs; }
00100 TBits operator~() { TBits res(*this); res.DoFlip(); return res; }
00101
00102
00103
00104
00105
00106 void Set(UInt_t nbits, const Char_t *array);
00107 void Set(UInt_t nbits, const UChar_t *array) { Set(nbits, (const Char_t*)array); }
00108 void Set(UInt_t nbits, const Short_t *array);
00109 void Set(UInt_t nbits, const UShort_t *array) { Set(nbits, (const Short_t*)array); }
00110 void Set(UInt_t nbits, const Int_t *array);
00111 void Set(UInt_t nbits, const UInt_t *array) { Set(nbits, (const Int_t*)array); }
00112 void Set(UInt_t nbits, const Long64_t *array);
00113 void Set(UInt_t nbits, const ULong64_t *array) { Set(nbits, (const Long64_t*)array); }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 void Get(Char_t *array) const;
00125 void Get(UChar_t *array) const { Get((Char_t*)array); }
00126 void Get(Short_t *array) const;
00127 void Get(UShort_t *array) const { Get((Short_t*)array); }
00128 void Get(Int_t *array) const;
00129 void Get(UInt_t *array) const { Get((Int_t*)array); }
00130 void Get(Long64_t *array) const;
00131 void Get(ULong64_t *array) const { Get((Long64_t*)array); }
00132
00133
00134 void Clear(Option_t *option="");
00135 void Compact();
00136 UInt_t CountBits(UInt_t startBit=0) const ;
00137 UInt_t FirstNullBit(UInt_t startBit=0) const;
00138 UInt_t FirstSetBit(UInt_t startBit=0) const;
00139 UInt_t GetNbits() const { return fNbits; }
00140 UInt_t GetNbytes() const { return fNbytes; }
00141
00142 Bool_t operator==(const TBits &other) const;
00143 Bool_t operator!=(const TBits &other) const { return !(*this==other); }
00144
00145 void Paint(Option_t *option="");
00146 void Print(Option_t *option="") const;
00147 void Output(ostream &) const;
00148
00149 ClassDef(TBits,1)
00150 };
00151
00152
00153 inline Bool_t operator&(const TBits::TReference& lhs, const TBits::TReference& rhs)
00154 {
00155 return (bool)lhs & rhs;
00156 }
00157
00158 inline Bool_t operator|(const TBits::TReference& lhs, const TBits::TReference& rhs)
00159 {
00160 return (bool)lhs | rhs;
00161 }
00162
00163 inline Bool_t operator^(const TBits::TReference& lhs, const TBits::TReference& rhs)
00164 {
00165 return (bool)lhs ^ rhs;
00166 }
00167
00168 inline TBits operator&(const TBits& lhs, const TBits& rhs)
00169 {
00170 TBits result(lhs);
00171 result &= rhs;
00172 return result;
00173 }
00174
00175 inline TBits operator|(const TBits& lhs, const TBits& rhs)
00176 {
00177 TBits result(lhs);
00178 result |= rhs;
00179 return result;
00180 }
00181
00182 inline TBits operator^(const TBits& lhs, const TBits& rhs)
00183 {
00184 TBits result(lhs);
00185 result ^= rhs;
00186 return result;
00187 }
00188
00189 inline ostream &operator<<(ostream& os, const TBits& rhs)
00190 {
00191 rhs.Output(os); return os;
00192 }
00193
00194
00195
00196 inline void TBits::SetBitNumber(UInt_t bitnumber, Bool_t value)
00197 {
00198
00199
00200 if (bitnumber >= fNbits) {
00201 UInt_t new_size = (bitnumber/8) + 1;
00202 if (new_size > fNbytes) {
00203 new_size *= 2;
00204 UChar_t *old_location = fAllBits;
00205 fAllBits = new UChar_t[new_size];
00206 memcpy(fAllBits,old_location,fNbytes);
00207 memset(fAllBits+fNbytes ,0, new_size-fNbytes);
00208 fNbytes = new_size;
00209 delete [] old_location;
00210 }
00211 fNbits = bitnumber+1;
00212 }
00213 UInt_t loc = bitnumber/8;
00214 UChar_t bit = bitnumber%8;
00215 if (value)
00216 fAllBits[loc] |= (1<<bit);
00217 else
00218 fAllBits[loc] &= (0xFF ^ (1<<bit));
00219 }
00220
00221 inline Bool_t TBits::TestBitNumber(UInt_t bitnumber) const
00222 {
00223
00224
00225 if (bitnumber >= fNbits) return kFALSE;
00226 UInt_t loc = bitnumber/8;
00227 UChar_t value = fAllBits[loc];
00228 UChar_t bit = bitnumber%8;
00229 Bool_t result = (value & (1<<bit)) != 0;
00230 return result;
00231
00232 }
00233
00234 inline void TBits::ResetBitNumber(UInt_t bitnumber)
00235 {
00236 SetBitNumber(bitnumber,kFALSE);
00237 }
00238
00239 inline Bool_t TBits::operator[](UInt_t bitnumber) const
00240 {
00241 return TestBitNumber(bitnumber);
00242 }
00243
00244 inline TBits::TReference& TBits::TReference::operator=(Bool_t val)
00245 {
00246
00247
00248 fBits.SetBitNumber(fPos,val); return *this;
00249 }
00250
00251 inline TBits::TReference& TBits::TReference::operator=(const TReference& rhs)
00252 {
00253
00254
00255 fBits.SetBitNumber(fPos,rhs.fBits.TestBitNumber(rhs.fPos)); return *this;
00256 }
00257
00258 #ifndef __CINT__
00259 inline Bool_t TBits::TReference::operator~() const
00260 {
00261
00262
00263 return !fBits.TestBitNumber(fPos);
00264 }
00265 #endif
00266
00267 inline TBits::TReference::operator Bool_t() const
00268 {
00269
00270
00271 return fBits.TestBitNumber(fPos);
00272 }
00273
00274 #endif
00275
00276