ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/BitMask.h
Revision: 1.1
Committed: Sun Mar 8 12:08:31 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008, Mit_008pre2
Log Message:
Added BitMask and CacheFlag. Also have type-based BitMaskT for 8, 16, 32 and 64 bit masks. Use more generic BitMask with char ptr for every other case. Solves weird ROOT problem with re-skimming.

File Contents

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