TBits.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TBits.h 25695 2008-10-03 14:06:16Z brun $
00002 // Author: Philippe Canal 05/02/01
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TBits
00013 #define ROOT_TBits
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //                                                                      //
00017 // TBits                                                                //
00018 //                                                                      //
00019 // Container of bits.                                                   //
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;         // Highest bit set + 1
00038    UInt_t   fNbytes;        // Number of UChars in fAllBits
00039    UChar_t *fAllBits;       //[fNbytes] array of UChars
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(); // left undefined
00062 
00063 public:
00064       TReference(TBits& bit, UInt_t pos) : fBits(bit),fPos(pos) { }
00065       ~TReference() { }
00066 
00067        // For b[i] = val;
00068       TReference& operator=(Bool_t val);
00069 
00070       // For b[i] = b[__j];
00071       TReference& operator=(const TReference& rhs);
00072 
00073 #ifndef __CINT__
00074       // Flips the bit
00075       Bool_t operator~() const;
00076 #endif
00077 
00078       // For val = b[i];
00079       operator Bool_t() const;
00080    };
00081 
00082    //----- bit manipulation
00083    //----- (note the difference with TObject's bit manipulations)
00084    void   ResetAllBits(Bool_t value=kFALSE);  // if value=1 set all bits to 1
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    //----- Accessors and operator
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    //----- Optimized setters
00103    // Each of these will replace the contents of the receiver with the bitvector
00104    // in the parameter array.  The number of bits is changed to nbits.  If nbits
00105    // is smaller than fNbits, the receiver will NOT be compacted.
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    //----- Optimized getters
00116    // Each of these will replace the contents of the parameter array with the
00117    // bits in the receiver.  The parameter array must be large enough to hold
00118    // all of the bits in the receiver.
00119    // Note on semantics: any bits in the parameter array that go beyond the
00120    // number of the bits in the receiver will have an unspecified value.  For
00121    // example, if you call Get(Int*) with an array of one integer and the TBits
00122    // object has less than 32 bits, then the remaining bits in the integer will
00123    // have an unspecified value.
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    //----- Utilities
00134    void    Clear(Option_t *option="");
00135    void    Compact();               // Reduce the space used.
00136    UInt_t  CountBits(UInt_t startBit=0)     const ;  // return number of bits set to 1
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="");        // to visualize the bits array as an histogram, etc
00146    void    Print(Option_t *option="") const;  // to show the list of active bits
00147    void    Output(ostream &) const;
00148 
00149    ClassDef(TBits,1)        // Bit container
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 // inline functions...
00195 
00196 inline void TBits::SetBitNumber(UInt_t bitnumber, Bool_t value)
00197 {
00198    // Set bit number 'bitnumber' to be value
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    // Return the current value of the bit
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    // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
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    // For b[i] = val.
00247 
00248    fBits.SetBitNumber(fPos,val); return *this;
00249 }
00250 
00251 inline TBits::TReference& TBits::TReference::operator=(const TReference& rhs)
00252 {
00253    // For b[i] = b[__j].
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    // Flips the bit.
00262 
00263    return !fBits.TestBitNumber(fPos);
00264 }
00265 #endif
00266 
00267 inline TBits::TReference::operator Bool_t() const
00268 {
00269    // For val = b[i].
00270 
00271    return fBits.TestBitNumber(fPos);
00272 }
00273 
00274 #endif
00275 
00276 

Generated on Tue Jul 5 14:11:25 2011 for ROOT_528-00b_version by  doxygen 1.5.1