1 |
loizides |
1.3 |
//--------------------------------------------------------------------------------------------------
|
2 |
loizides |
1.9 |
// $Id: BitMask.h,v 1.8 2008/10/29 17:03:30 bendavid Exp $
|
3 |
loizides |
1.3 |
//
|
4 |
|
|
// BitMask
|
5 |
|
|
//
|
6 |
|
|
// Helper class implementing a Nx8 bit bitmask.
|
7 |
|
|
//
|
8 |
|
|
// Authors: J.Bendavid, C.Loizides
|
9 |
|
|
//--------------------------------------------------------------------------------------------------
|
10 |
|
|
|
11 |
|
|
#ifndef MITANA_DATATREE_BITMASK_H
|
12 |
|
|
#define MITANA_DATATREE_BITMASK_H
|
13 |
|
|
|
14 |
|
|
#include <assert.h>
|
15 |
|
|
#include <Rtypes.h>
|
16 |
|
|
|
17 |
|
|
namespace mithep
|
18 |
|
|
{
|
19 |
|
|
template<UInt_t N>
|
20 |
|
|
class BitMask
|
21 |
|
|
{
|
22 |
|
|
public:
|
23 |
|
|
BitMask() { Clear(); }
|
24 |
loizides |
1.5 |
BitMask(const char *bits) { SetBits(bits); }
|
25 |
|
|
BitMask(const BitMask<N> ©) { SetBits(copy.Bits()); }
|
26 |
bendavid |
1.8 |
BitMask(Long64_t bits) { SetBits(bits); }
|
27 |
loizides |
1.3 |
virtual ~BitMask() {}
|
28 |
|
|
|
29 |
loizides |
1.9 |
const char *Bits() const { return fBitMask; }
|
30 |
|
|
void Clear() { memset(fBitMask,0,N); }
|
31 |
|
|
void ClearBit(UInt_t n) { SetBit(n,0); }
|
32 |
loizides |
1.3 |
UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
|
33 |
loizides |
1.9 |
void Print(Option_t *opt="") const;
|
34 |
loizides |
1.3 |
void SetBit(UInt_t n, Bool_t b=1);
|
35 |
loizides |
1.5 |
void SetBits(const Char_t *bits) { memcpy(fBitMask,bits,N); }
|
36 |
loizides |
1.3 |
void SetBits(Long64_t bits);
|
37 |
loizides |
1.9 |
UInt_t Size() const { return N*8; }
|
38 |
|
|
Bool_t TestBit(UInt_t n) const;
|
39 |
loizides |
1.5 |
BitMask &operator&=(const BitMask<N> &rhs)
|
40 |
|
|
{ for (UInt_t n=0; n<N; ++n) fBitMask[n]&=rhs.fBitMask[n]; return *this; }
|
41 |
|
|
BitMask &operator|=(const BitMask<N> &rhs)
|
42 |
|
|
{ for (UInt_t n=0; n<N; ++n) fBitMask[n]|=rhs.fBitMask[n]; return *this; }
|
43 |
bendavid |
1.7 |
BitMask &operator^=(const BitMask<N> &rhs)
|
44 |
|
|
{ for (UInt_t n=0; n<N; ++n) fBitMask[n]^=rhs.fBitMask[n]; return *this; }
|
45 |
loizides |
1.5 |
Bool_t operator!=(const BitMask<N> &other) const;
|
46 |
|
|
Bool_t operator==(const BitMask<N> &other) const;
|
47 |
loizides |
1.9 |
BitMask operator& (const BitMask &other) const
|
48 |
bendavid |
1.6 |
{ return BitMask<N>(*this) &= other; }
|
49 |
loizides |
1.9 |
BitMask operator| (const BitMask &other) const
|
50 |
bendavid |
1.6 |
{ return BitMask<N>(*this) |= other; }
|
51 |
loizides |
1.9 |
BitMask operator^ (const BitMask &other) const
|
52 |
bendavid |
1.7 |
{ return BitMask<N>(*this) ^= other; }
|
53 |
loizides |
1.9 |
BitMask operator~ () const;
|
54 |
bendavid |
1.6 |
|
55 |
loizides |
1.3 |
protected:
|
56 |
|
|
Char_t fBitMask[N]; //the actual bitmask
|
57 |
|
|
|
58 |
|
|
ClassDefT(BitMask, 1)
|
59 |
|
|
};
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
//--------------------------------------------------------------------------------------------------
|
63 |
|
|
template<UInt_t N>
|
64 |
|
|
inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
|
65 |
|
|
{
|
66 |
|
|
// Set nth bit to given value.
|
67 |
|
|
|
68 |
|
|
if(n>=N*8) return;
|
69 |
|
|
|
70 |
|
|
UInt_t loc = n/8;
|
71 |
|
|
UChar_t bit = n%8;
|
72 |
|
|
if (b)
|
73 |
|
|
fBitMask[loc] |= (1<<bit);
|
74 |
|
|
else
|
75 |
|
|
fBitMask[loc] &= (0xFF ^ (1<<bit));
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
//--------------------------------------------------------------------------------------------------
|
79 |
|
|
template<UInt_t N>
|
80 |
|
|
UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
|
81 |
|
|
{
|
82 |
|
|
// Count number of bits which are set.
|
83 |
|
|
|
84 |
|
|
UInt_t numBits = 0;
|
85 |
|
|
for (UInt_t i=first; i<=last; i++)
|
86 |
|
|
numBits += TestBit(i);
|
87 |
|
|
|
88 |
|
|
return numBits;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
//--------------------------------------------------------------------------------------------------
|
92 |
|
|
template<UInt_t N>
|
93 |
loizides |
1.9 |
void mithep::BitMask<N>::Print(Option_t *opt) const
|
94 |
|
|
{
|
95 |
|
|
// Print bitmask.
|
96 |
|
|
|
97 |
|
|
for (UInt_t i=0;i<N;++i)
|
98 |
|
|
printf("%X",i);
|
99 |
|
|
printf("\n");
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
//--------------------------------------------------------------------------------------------------
|
103 |
|
|
template<UInt_t N>
|
104 |
loizides |
1.3 |
inline void mithep::BitMask<N>::SetBits(Long64_t bits)
|
105 |
|
|
{
|
106 |
|
|
// Set bits given by bits.
|
107 |
|
|
|
108 |
loizides |
1.4 |
assert(sizeof(Long64_t)>=N);
|
109 |
loizides |
1.3 |
SetBits(reinterpret_cast<const char*>(&bits));
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
//--------------------------------------------------------------------------------------------------
|
113 |
|
|
template<UInt_t N>
|
114 |
|
|
inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
|
115 |
|
|
{
|
116 |
|
|
// Return true if nth bit is set.
|
117 |
|
|
|
118 |
|
|
if(n>=N*8) return 0;
|
119 |
|
|
|
120 |
|
|
UInt_t loc = n/8;
|
121 |
|
|
UChar_t bit = n%8;
|
122 |
|
|
Char_t val = fBitMask[loc];
|
123 |
|
|
Bool_t result = (val & (1<<bit)) != 0;
|
124 |
|
|
return result;
|
125 |
|
|
}
|
126 |
loizides |
1.5 |
|
127 |
|
|
//--------------------------------------------------------------------------------------------------
|
128 |
|
|
template<UInt_t N>
|
129 |
|
|
Bool_t mithep::BitMask<N>::operator==(const mithep::BitMask<N> &other) const
|
130 |
|
|
{
|
131 |
|
|
// Equal operator.
|
132 |
|
|
|
133 |
|
|
for (UInt_t n=0; n<N; ++n) {
|
134 |
|
|
if (fBitMask[n]!=other.fBitMask[n])
|
135 |
|
|
return kFALSE;
|
136 |
|
|
}
|
137 |
|
|
return kTRUE;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
//--------------------------------------------------------------------------------------------------
|
141 |
|
|
template<UInt_t N>
|
142 |
|
|
Bool_t mithep::BitMask<N>::operator!=(const mithep::BitMask<N> &other) const
|
143 |
|
|
{
|
144 |
|
|
// Unequal operator.
|
145 |
|
|
|
146 |
|
|
for (UInt_t n=0; n<N; ++n) {
|
147 |
bendavid |
1.6 |
if (fBitMask[n]!=other.fBitMask[n])
|
148 |
|
|
return kTRUE;
|
149 |
|
|
}
|
150 |
|
|
return kFALSE;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
//--------------------------------------------------------------------------------------------------
|
154 |
|
|
template<UInt_t N>
|
155 |
loizides |
1.9 |
mithep::BitMask<N> mithep::BitMask<N>::operator~() const
|
156 |
bendavid |
1.6 |
{
|
157 |
|
|
// bitwise inversion operator
|
158 |
|
|
BitMask<N> bits;
|
159 |
|
|
for (UInt_t n=0; n<N; ++n) {
|
160 |
|
|
bits.fBitMask[n] = ~fBitMask[n];
|
161 |
loizides |
1.5 |
}
|
162 |
bendavid |
1.6 |
|
163 |
|
|
return bits;
|
164 |
|
|
}
|
165 |
loizides |
1.3 |
#endif
|