Logo ROOT  
Reference Guide
TBits.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Philippe Canal 05/02/01
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TBits
13 #define ROOT_TBits
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TBits //
18 // //
19 // Container of bits. //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 
23 #include "TObject.h"
24 #include <string.h>
25 
26 class TBits : public TObject {
27 
28 protected:
29 
30  UInt_t fNbits; // Highest bit set + 1
31  UInt_t fNbytes; // Number of UChars in fAllBits
32  UChar_t *fAllBits; //[fNbytes] array of UChars
33 
34  void ReserveBytes(UInt_t nbytes);
35  void DoAndEqual(const TBits& rhs);
36  void DoOrEqual (const TBits& rhs);
37  void DoXorEqual(const TBits& rhs);
38  void DoLeftShift(UInt_t shift);
39  void DoRightShift(UInt_t shift);
40  void DoFlip();
41  void Resize(UInt_t newlen);
42 
43 public:
44  TBits(UInt_t nbits = 8);
45  TBits(const TBits&);
46  TBits& operator=(const TBits&);
47  virtual ~TBits();
48 
49  class TReference {
50  friend class TBits;
51 
52  TBits &fBits; //!
53  UInt_t fPos; //!
54 
55  TReference(); // left undefined
56 
57 public:
58  TReference(TBits& bit, UInt_t pos) : fBits(bit),fPos(pos) { }
60 
61  // For b[i] = val;
63 
64  // For b[i] = b[__j];
66 
67  // Flips the bit
68  Bool_t operator~() const;
69 
70  // For val = b[i];
71  operator Bool_t() const;
72  };
73 
74  //----- bit manipulation
75  //----- (note the difference with TObject's bit manipulations)
76  void ResetAllBits(Bool_t value=kFALSE); // if value=1 set all bits to 1
77  void ResetBitNumber(UInt_t bitnumber);
78  void SetBitNumber(UInt_t bitnumber, Bool_t value = kTRUE);
79  Bool_t TestBitNumber(UInt_t bitnumber) const;
80 
81  //----- Accessors and operator
82  TBits::TReference operator[](UInt_t bitnumber) { return TReference(*this,bitnumber); }
83  Bool_t operator[](UInt_t bitnumber) const;
84 
85  TBits& operator&=(const TBits& rhs) { DoAndEqual(rhs); return *this; }
86  TBits& operator|=(const TBits& rhs) { DoOrEqual(rhs); return *this; }
87  TBits& operator^=(const TBits& rhs) { DoXorEqual(rhs); return *this; }
88  TBits& operator<<=(UInt_t rhs) { DoLeftShift(rhs); return *this; }
89  TBits& operator>>=(UInt_t rhs) { DoRightShift(rhs); return *this; }
90  TBits operator<<(UInt_t rhs) { return TBits(*this)<<= rhs; }
91  TBits operator>>(UInt_t rhs) { return TBits(*this)>>= rhs; }
92  TBits operator~() const { TBits res(*this); res.DoFlip(); return res; }
93 
94  //----- Optimized setters
95  // Each of these will replace the contents of the receiver with the bitvector
96  // in the parameter array. The number of bits is changed to nbits. If nbits
97  // is smaller than fNbits, the receiver will NOT be compacted.
98  void Set(UInt_t nbits, const Char_t *array);
99  void Set(UInt_t nbits, const UChar_t *array) { Set(nbits, (const Char_t*)array); }
100  void Set(UInt_t nbits, const Short_t *array);
101  void Set(UInt_t nbits, const UShort_t *array) { Set(nbits, (const Short_t*)array); }
102  void Set(UInt_t nbits, const Int_t *array);
103  void Set(UInt_t nbits, const UInt_t *array) { Set(nbits, (const Int_t*)array); }
104  void Set(UInt_t nbits, const Long64_t *array);
105  void Set(UInt_t nbits, const ULong64_t *array) { Set(nbits, (const Long64_t*)array); }
106 
107  //----- Optimized getters
108  // Each of these will replace the contents of the parameter array with the
109  // bits in the receiver. The parameter array must be large enough to hold
110  // all of the bits in the receiver.
111  // Note on semantics: any bits in the parameter array that go beyond the
112  // number of the bits in the receiver will have an unspecified value. For
113  // example, if you call Get(Int*) with an array of one integer and the TBits
114  // object has less than 32 bits, then the remaining bits in the integer will
115  // have an unspecified value.
116  void Get(Char_t *array) const;
117  void Get(UChar_t *array) const { Get((Char_t*)array); }
118  void Get(Short_t *array) const;
119  void Get(UShort_t *array) const { Get((Short_t*)array); }
120  void Get(Int_t *array) const;
121  void Get(UInt_t *array) const { Get((Int_t*)array); }
122  void Get(Long64_t *array) const;
123  void Get(ULong64_t *array) const { Get((Long64_t*)array); }
124 
125  //----- Utilities
126  void Clear(Option_t *option="");
127  void Compact(); // Reduce the space used.
128  UInt_t CountBits(UInt_t startBit=0) const ; // return number of bits set to 1
129  UInt_t FirstNullBit(UInt_t startBit=0) const;
130  UInt_t FirstSetBit(UInt_t startBit=0) const;
131  UInt_t LastNullBit(UInt_t startBit=999999999) const;
132  UInt_t LastSetBit(UInt_t startBit=999999999) const;
133  UInt_t GetNbits() const { return fNbits; }
134  UInt_t GetNbytes() const { return fNbytes; }
135 
136  Bool_t operator==(const TBits &other) const;
137  Bool_t operator!=(const TBits &other) const { return !(*this==other); }
138 
139  void Paint(Option_t *option=""); // to visualize the bits array as an histogram, etc
140  void Print(Option_t *option="") const; // to show the list of active bits
141  void Output(std::ostream &) const;
142 
143  ClassDef(TBits,1) // Bit container
144 };
145 
146 
147 inline Bool_t operator&(const TBits::TReference& lhs, const TBits::TReference& rhs)
148 {
149  return (Bool_t)lhs & rhs;
150 }
151 
152 inline Bool_t operator|(const TBits::TReference& lhs, const TBits::TReference& rhs)
153 {
154  return (Bool_t)lhs | rhs;
155 }
156 
157 inline Bool_t operator^(const TBits::TReference& lhs, const TBits::TReference& rhs)
158 {
159  return (Bool_t)lhs ^ rhs;
160 }
161 
162 inline TBits operator&(const TBits& lhs, const TBits& rhs)
163 {
164  TBits result(lhs);
165  result &= rhs;
166  return result;
167 }
168 
169 inline TBits operator|(const TBits& lhs, const TBits& rhs)
170 {
171  TBits result(lhs);
172  result |= rhs;
173  return result;
174 }
175 
176 inline TBits operator^(const TBits& lhs, const TBits& rhs)
177 {
178  TBits result(lhs);
179  result ^= rhs;
180  return result;
181 }
182 
183 inline std::ostream &operator<<(std::ostream& os, const TBits& rhs)
184 {
185  rhs.Output(os); return os;
186 }
187 
188 // inline functions...
189 
190 inline void TBits::Resize(UInt_t newbitnumber)
191 {
192  // Update the allocated size.
193  UInt_t new_size = (newbitnumber / 8) + 1;
194  if (new_size > fNbytes) {
195  new_size *= 2;
196  UChar_t *old_location = fAllBits;
197  fAllBits = new UChar_t[new_size];
198  memcpy(fAllBits, old_location, fNbytes);
199  memset(fAllBits + fNbytes, 0, new_size - fNbytes);
200  fNbytes = new_size;
201  delete[] old_location;
202  }
203 }
204 
205 inline void TBits::SetBitNumber(UInt_t bitnumber, Bool_t value)
206 {
207  // Set bit number 'bitnumber' to be value
208 
209  if (bitnumber >= fNbits) {
210  Resize(bitnumber);
211  fNbits = bitnumber+1;
212  }
213  UInt_t loc = bitnumber/8;
214  UChar_t bit = bitnumber%8;
215  if (value)
216  fAllBits[loc] |= (1<<bit);
217  else
218  fAllBits[loc] &= (0xFF ^ (1<<bit));
219 }
220 
221 inline Bool_t TBits::TestBitNumber(UInt_t bitnumber) const
222 {
223  // Return the current value of the bit
224 
225  if (bitnumber >= fNbits) return kFALSE;
226  UInt_t loc = bitnumber/8;
227  UChar_t value = fAllBits[loc];
228  UChar_t bit = bitnumber%8;
229  Bool_t result = (value & (1<<bit)) != 0;
230  return result;
231  // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
232 }
233 
234 inline void TBits::ResetBitNumber(UInt_t bitnumber)
235 {
236  SetBitNumber(bitnumber,kFALSE);
237 }
238 
239 inline Bool_t TBits::operator[](UInt_t bitnumber) const
240 {
241  return TestBitNumber(bitnumber);
242 }
243 
245 {
246  // For b[i] = val.
247 
248  fBits.SetBitNumber(fPos,val); return *this;
249 }
250 
252 {
253  // For b[i] = b[__j].
254 
255  fBits.SetBitNumber(fPos,rhs.fBits.TestBitNumber(rhs.fPos)); return *this;
256 }
257 
259 {
260  // Flips the bit.
261 
262  return !fBits.TestBitNumber(fPos);
263 }
264 
265 inline TBits::TReference::operator Bool_t() const
266 {
267  // For val = b[i].
268 
269  return fBits.TestBitNumber(fPos);
270 }
271 
272 #endif
273 
274 
operator|
Bool_t operator|(const TBits::TReference &lhs, const TBits::TReference &rhs)
Definition: TBits.h:152
TBits::DoFlip
void DoFlip()
Execute ~(*this)
Definition: TBits.cxx:205
TBits::FirstNullBit
UInt_t FirstNullBit(UInt_t startBit=0) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:275
TBits::Set
void Set(UInt_t nbits, const Char_t *array)
Set all the bytes.
Definition: TBits.cxx:503
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TBits::Resize
void Resize(UInt_t newlen)
Definition: TBits.h:190
TBits::Output
void Output(std::ostream &) const
Print the value to the std::ostream.
Definition: TBits.cxx:443
TBits::GetNbits
UInt_t GetNbits() const
Definition: TBits.h:139
operator<<
std::ostream & operator<<(std::ostream &os, const TBits &rhs)
Definition: TBits.h:183
UShort_t
unsigned short UShort_t
Definition: RtypesCore.h:40
operator^
Bool_t operator^(const TBits::TReference &lhs, const TBits::TReference &rhs)
Definition: TBits.h:157
TBits::TReference::operator~
Bool_t operator~() const
Definition: TBits.h:258
TBits::operator[]
TBits::TReference operator[](UInt_t bitnumber)
Definition: TBits.h:88
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBits::Print
void Print(Option_t *option="") const
Print the list of active bits.
Definition: TBits.cxx:465
TBits::DoRightShift
void DoRightShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:242
TBits::ResetAllBits
void ResetAllBits(Bool_t value=kFALSE)
Reset all bits to 0 (false).
Definition: TBits.cxx:481
TBits::Clear
void Clear(Option_t *option="")
Clear the value.
Definition: TBits.cxx:84
TBits::operator==
Bool_t operator==(const TBits &other) const
Definition: TBits.cxx:699
Int_t
int Int_t
Definition: RtypesCore.h:45
TBits::fAllBits
UChar_t * fAllBits
Definition: TBits.h:38
TBits::TReference::TReference
TReference()
TBits::DoAndEqual
void DoAndEqual(const TBits &rhs)
Execute (*this) &= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:165
TBits::operator<<=
TBits & operator<<=(UInt_t rhs)
Definition: TBits.h:94
TBits::operator>>=
TBits & operator>>=(UInt_t rhs)
Definition: TBits.h:95
TBits::TestBitNumber
Bool_t TestBitNumber(UInt_t bitnumber) const
Definition: TBits.h:221
TBits::Get
void Get(Char_t *array) const
Copy all the byes.
Definition: TBits.cxx:516
TBits
Definition: TBits.h:26
TBits::operator!=
Bool_t operator!=(const TBits &other) const
Definition: TBits.h:143
TBits::TReference::~TReference
~TReference()
Definition: TBits.h:65
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
bool
TBits::LastNullBit
UInt_t LastNullBit(UInt_t startBit=999999999) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:320
operator&
Bool_t operator&(const TBits::TReference &lhs, const TBits::TReference &rhs)
Definition: TBits.h:147
TBits::operator=
TBits & operator=(const TBits &)
TBits assignment operator.
Definition: TBits.cxx:56
TBits::Compact
void Compact()
Reduce the storage used by the object to a minimun.
Definition: TBits.cxx:95
TBits::operator~
TBits operator~() const
Definition: TBits.h:98
TBits::TReference::operator=
TReference & operator=(Bool_t val)
Definition: TBits.h:244
TBits::~TBits
virtual ~TBits()
TBits destructor.
Definition: TBits.cxx:76
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TBits::DoOrEqual
void DoOrEqual(const TBits &rhs)
Execute (*this) &= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:181
TBits::DoLeftShift
void DoLeftShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:217
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TBits::LastSetBit
UInt_t LastSetBit(UInt_t startBit=999999999) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:404
TBits::FirstSetBit
UInt_t FirstSetBit(UInt_t startBit=0) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:359
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
Short_t
short Short_t
Definition: RtypesCore.h:39
TBits::Get
void Get(UChar_t *array) const
Definition: TBits.h:123
TBits::fNbytes
UInt_t fNbytes
Definition: TBits.h:37
TBits::Paint
void Paint(Option_t *option="")
Once implemented, it will draw the bit field as an histogram.
Definition: TBits.cxx:458
TBits::operator|=
TBits & operator|=(const TBits &rhs)
Definition: TBits.h:92
unsigned int
ULong64_t
unsigned long long ULong64_t
Definition: RtypesCore.h:74
TObject::fBits
UInt_t fBits
bit field status word
Definition: TObject.h:41
TBits::operator<<
TBits operator<<(UInt_t rhs)
Definition: TBits.h:96
TBits::Set
void Set(UInt_t nbits, const UChar_t *array)
Definition: TBits.h:105
TBits::DoXorEqual
void DoXorEqual(const TBits &rhs)
Execute (*this) ^= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:194
TBits::TReference
Definition: TBits.h:55
TObject.h
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TBits::operator>>
TBits operator>>(UInt_t rhs)
Definition: TBits.h:97
TObject
Definition: TObject.h:37
TBits::SetBitNumber
void SetBitNumber(UInt_t bitnumber, Bool_t value=kTRUE)
Definition: TBits.h:205
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TBits::TBits
TBits(UInt_t nbits=8)
TBits constructor. All bits set to 0.
Definition: TBits.cxx:33
TBits::TReference::fBits
TBits & fBits
Definition: TBits.h:58
TBits::CountBits
UInt_t CountBits(UInt_t startBit=0) const
Return number of bits set to 1 starting at bit startBit.
Definition: TBits.cxx:118
TBits::fNbits
UInt_t fNbits
Definition: TBits.h:36
TBits::ResetBitNumber
void ResetBitNumber(UInt_t bitnumber)
Definition: TBits.h:234
TBits::GetNbytes
UInt_t GetNbytes() const
Definition: TBits.h:140
Char_t
char Char_t
Definition: RtypesCore.h:33
TBits::operator^=
TBits & operator^=(const TBits &rhs)
Definition: TBits.h:93
TBits::operator&=
TBits & operator&=(const TBits &rhs)
Definition: TBits.h:91
TBits::ReserveBytes
void ReserveBytes(UInt_t nbytes)
Reverse each bytes.
Definition: TBits.cxx:489
TBits::TReference::fPos
UInt_t fPos
Definition: TBits.h:59