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