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