ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/BitMask.h
Revision: 1.2
Committed: Fri Mar 11 04:02:38 2011 UTC (14 years, 1 month ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, HEAD
Branch point for: Mit_025c_branch
Changes since 1.1: +6 -1 lines
Log Message:
unspeakable hacks to workaround unspeakable cint bugs

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: BitMask.h,v 1.1 2009/03/08 12:08:31 loizides Exp $
3 //
4 // BitMask
5 //
6 // Helper class implementing a Nx8 bit bitmask where N is a template parameter.
7 //
8 // Authors: J.Bendavid, C.Loizides
9 //--------------------------------------------------------------------------------------------------
10
11 #ifndef MITANA_DATACONT_BITMASK_H
12 #define MITANA_DATACONT_BITMASK_H
13
14 #include <Rtypes.h>
15 #include <TError.h>
16
17 typedef UInt_t unsignedint;
18 typedef UChar_t unsignedchar;
19 typedef UShort_t unsignedshort;
20 typedef ULong64_t unsignedlonglong;
21
22 namespace mithep
23 {
24 template<UInt_t N>
25 class BitMask
26 {
27 public:
28 BitMask() { Clear(); }
29 BitMask(const char *bits) { SetBits(bits); }
30 BitMask(const BitMask<N> &copy) { SetBits(copy.Bits()); }
31 BitMask(Long64_t bits) { SetBits(bits); }
32 virtual ~BitMask() {}
33
34 const char *Bits() const { return fBitMask; }
35 void Clear(Option_t */*opt*/="") { memset(fBitMask,0,N); }
36 void ClearBit(UInt_t n) { SetBit(n,0); }
37 UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
38 void Print(Option_t *opt="") const;
39 void SetBit(UInt_t n, Bool_t b=1);
40 void SetBits(const BitMask<N> &b) { memcpy(fBitMask,b.Bits(),N); }
41 void SetBits(const Char_t *bits) { memcpy(fBitMask,bits,N); }
42 void SetBits(Long64_t bits);
43 UInt_t Size() const { return N*8; }
44 Bool_t TestBit(UInt_t n) const;
45
46 BitMask &operator&=(const BitMask<N> &rhs)
47 { for (UInt_t n=0; n<N; ++n) fBitMask[n] &= rhs.fBitMask[n]; return *this; }
48 BitMask &operator|=(const BitMask<N> &rhs)
49 { for (UInt_t n=0; n<N; ++n) fBitMask[n] |= rhs.fBitMask[n]; return *this; }
50 BitMask &operator^=(const BitMask<N> &rhs)
51 { for (UInt_t n=0; n<N; ++n) fBitMask[n] ^= rhs.fBitMask[n]; return *this; }
52 Bool_t operator!=(const BitMask<N> &other) const;
53 Bool_t operator==(const BitMask<N> &other) const;
54 BitMask operator& (const BitMask<N> &other) const
55 { return BitMask<N>(*this) &= other; }
56 BitMask operator| (const BitMask<N> &other) const
57 { return BitMask<N>(*this) |= other; }
58 BitMask operator^ (const BitMask<N> &other) const
59 { return BitMask<N>(*this) ^= other; }
60 BitMask operator~ () const;
61
62 protected:
63 Char_t fBitMask[N]; //the actual bitmask
64
65 ClassDef(BitMask, 1) // Generic templated bitmask
66 };
67 }
68
69 namespace mithep
70 {
71 template<class T>
72 class BitMaskT
73 {
74 public:
75 BitMaskT() : fBitMask(0) {}
76 BitMaskT(const char *bits) : fBitMask(0) { SetBits(bits); }
77 BitMaskT(const BitMaskT<T> &copy) : fBitMask(0) { SetBits(copy.Mask()); }
78 BitMaskT(T bits) : fBitMask(0) { SetBits(bits); }
79 virtual ~BitMaskT() {}
80
81 const char *Bits() const
82 { return reinterpret_cast<const char*>(&fBitMask); }
83 void Clear(Option_t */*opt*/="") { fBitMask = 0; }
84 void ClearBit(UInt_t n) { SetBit(n,0); }
85 T Mask() const { return fBitMask; }
86 UInt_t NBitsSet(UInt_t first=0, UInt_t last=sizeof(T)*8) const;
87 void Print(Option_t *opt="") const
88 { printf("%llX\n", ULong64_t(fBitMask)); }
89
90 void SetBit(UInt_t n, Bool_t b=1)
91 { if ((n>=sizeof(T)*8) || (TestBit(n)==b)) return; fBitMask = fBitMask ^ (1 << n); }
92 void SetBits(const BitMaskT<T> &b) { fBitMask = b.Mask(); }
93 void SetBits(const Char_t *bits) { fBitMask = T(*bits); }
94 void SetBits(T mask) { fBitMask = mask; }
95 UInt_t Size() const { return sizeof(T)*8; }
96 Bool_t TestBit(UInt_t n) const;
97
98 BitMaskT &operator&=(const BitMaskT<T> &rhs)
99 { fBitMask &= rhs.fBitMask; return *this; }
100 BitMaskT &operator|=(const BitMaskT<T> &rhs)
101 { fBitMask |= rhs.fBitMask; return *this; }
102 BitMaskT &operator^=(const BitMaskT<T> &rhs)
103 { fBitMask ^= rhs.fBitMask; return *this; }
104 Bool_t operator!=(const BitMaskT<T> &other) const
105 { return fBitMask != other.fBitMask; }
106 Bool_t operator==(const BitMaskT<T> &other) const
107 { return fBitMask == other.fBitMask; }
108 BitMaskT operator& (const BitMaskT<T> &other) const
109 { return BitMaskT<T>(*this) &= other; }
110 BitMaskT operator| (const BitMaskT<T> &other) const
111 { return BitMaskT<T>(*this) |= other; }
112 BitMaskT operator^ (const BitMaskT<T> &other) const
113 { return BitMaskT<T>(*this) ^= other; }
114 BitMaskT operator~ () const
115 { return BitMaskT<T>(~fBitMask); }
116
117 protected:
118 T fBitMask; //the actual bitmask
119
120 ClassDef(BitMaskT, 1) // Generic templated bitmask
121 };
122 }
123
124 //--------------------------------------------------------------------------------------------------
125 template<UInt_t N>
126 inline UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
127 {
128 // Count number of bits which are set.
129
130 UInt_t numBits = 0;
131 for (UInt_t i=first; i<=last; i++)
132 numBits += TestBit(i);
133 return numBits;
134 }
135
136 //--------------------------------------------------------------------------------------------------
137 template<UInt_t N>
138 inline void mithep::BitMask<N>::Print(Option_t */*opt*/) const
139 {
140 // Print bitmask.
141
142 for (UInt_t i=0;i<N;++i)
143 printf("%X",fBitMask[i]);
144 printf("\n");
145 }
146
147 //--------------------------------------------------------------------------------------------------
148 template<UInt_t N>
149 inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
150 {
151 // Set nth bit to given value.
152
153 if(n>=N*8)
154 return;
155
156 UInt_t loc = n/8;
157 UChar_t bit = n%8;
158 if (b)
159 fBitMask[loc] |= (1<<bit);
160 else
161 fBitMask[loc] &= (0xFF ^ (1<<bit));
162 }
163
164 //--------------------------------------------------------------------------------------------------
165 template<UInt_t N>
166 inline void mithep::BitMask<N>::SetBits(Long64_t bits)
167 {
168 // Set bits given by bits.
169
170 R__ASSERT(sizeof(Long64_t)>=N);
171 SetBits(reinterpret_cast<const char*>(&bits));
172 }
173
174 //--------------------------------------------------------------------------------------------------
175 template<UInt_t N>
176 inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
177 {
178 // Return true if nth bit is set.
179
180 if(n>=N*8)
181 return 0;
182
183 UInt_t loc = n/8;
184 UChar_t bit = n%8;
185 Char_t val = fBitMask[loc];
186 Bool_t result = (val & (1<<bit)) != 0;
187 return result;
188 }
189
190 //--------------------------------------------------------------------------------------------------
191 template<UInt_t N>
192 inline Bool_t mithep::BitMask<N>::operator==(const mithep::BitMask<N> &other) const
193 {
194 // Equal operator.
195
196 for (UInt_t n=0; n<N; ++n) {
197 if (fBitMask[n]!=other.fBitMask[n])
198 return kFALSE;
199 }
200 return kTRUE;
201 }
202
203 //--------------------------------------------------------------------------------------------------
204 template<UInt_t N>
205 inline Bool_t mithep::BitMask<N>::operator!=(const mithep::BitMask<N> &other) const
206 {
207 // Unequal operator.
208
209 for (UInt_t n=0; n<N; ++n) {
210 if (fBitMask[n]!=other.fBitMask[n])
211 return kTRUE;
212 }
213 return kFALSE;
214 }
215
216 //--------------------------------------------------------------------------------------------------
217 template<UInt_t N>
218 inline mithep::BitMask<N> mithep::BitMask<N>::operator~() const
219 {
220 // bitwise inversion operator
221 BitMask<N> bits;
222 for (UInt_t n=0; n<N; ++n) {
223 bits.fBitMask[n] = ~fBitMask[n];
224 }
225 return bits;
226 }
227
228
229 //--------------------------------------------------------------------------------------------------
230 template<class T>
231 inline UInt_t mithep::BitMaskT<T>::NBitsSet(UInt_t first, UInt_t last) const
232 {
233 // Count number of bits which are set.
234
235 UInt_t numBits = 0;
236 for (UInt_t i=first; i<=last; i++)
237 numBits += TestBit(i);
238 return numBits;
239 }
240
241 //--------------------------------------------------------------------------------------------------
242 template<class T>
243 inline Bool_t mithep::BitMaskT<T>::TestBit(UInt_t n) const
244 {
245 // Return true if nth bit is set.
246
247 if(n>=sizeof(T)*8)
248 return 0;
249
250 Bool_t result = (fBitMask & (1<<n)) != 0;
251 return result;
252 }
253 #endif