ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.12
Committed: Tue Dec 9 17:46:59 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006b, Mit_006a
Changes since 1.11: +11 -7 lines
Log Message:
Added ObjType to retrieve type of object.

File Contents

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