ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.7
Committed: Thu Oct 16 16:12:06 2008 UTC (16 years, 6 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_005
Changes since 1.6: +5 -1 lines
Log Message:
Added XOR operator for BitMask

File Contents

# User Rev Content
1 loizides 1.3 //--------------------------------------------------------------------------------------------------
2 bendavid 1.7 // $Id: BitMask.h,v 1.6 2008/10/13 10:34: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> &copy) { SetBits(copy.Bits()); }
26 loizides 1.3 virtual ~BitMask() {}
27    
28 loizides 1.5 const char *Bits() const { return fBitMask; }
29 loizides 1.3 void Clear() { memset(fBitMask,0,N); }
30     void ClearBit(UInt_t n) { SetBit(n,0); }
31     UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
32     void SetBit(UInt_t n, Bool_t b=1);
33 loizides 1.5 void SetBits(const Char_t *bits) { memcpy(fBitMask,bits,N); }
34 loizides 1.3 void SetBits(Long64_t bits);
35     UInt_t Size() const { return N*8; }
36     Bool_t TestBit(UInt_t n) const;
37 loizides 1.5 BitMask &operator&=(const BitMask<N> &rhs)
38     { for (UInt_t n=0; n<N; ++n) fBitMask[n]&=rhs.fBitMask[n]; return *this; }
39     BitMask &operator|=(const BitMask<N> &rhs)
40     { for (UInt_t n=0; n<N; ++n) fBitMask[n]|=rhs.fBitMask[n]; return *this; }
41 bendavid 1.7 BitMask &operator^=(const BitMask<N> &rhs)
42     { for (UInt_t n=0; n<N; ++n) fBitMask[n]^=rhs.fBitMask[n]; return *this; }
43 loizides 1.5 Bool_t operator!=(const BitMask<N> &other) const;
44     Bool_t operator==(const BitMask<N> &other) const;
45 bendavid 1.6 BitMask operator& (const BitMask &other) const
46     { return BitMask<N>(*this) &= other; }
47     BitMask operator| (const BitMask &other) const
48     { return BitMask<N>(*this) |= other; }
49 bendavid 1.7 BitMask operator^ (const BitMask &other) const
50     { return BitMask<N>(*this) ^= other; }
51 bendavid 1.6 BitMask operator ~ () const;
52    
53    
54 loizides 1.3 protected:
55     Char_t fBitMask[N]; //the actual bitmask
56    
57     ClassDefT(BitMask, 1)
58     };
59     }
60    
61     //--------------------------------------------------------------------------------------------------
62     template<UInt_t N>
63     inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
64     {
65     // Set nth bit to given value.
66    
67     if(n>=N*8) return;
68    
69     UInt_t loc = n/8;
70     UChar_t bit = n%8;
71     if (b)
72     fBitMask[loc] |= (1<<bit);
73     else
74     fBitMask[loc] &= (0xFF ^ (1<<bit));
75     }
76    
77     //--------------------------------------------------------------------------------------------------
78     template<UInt_t N>
79     UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
80     {
81     // Count number of bits which are set.
82    
83     UInt_t numBits = 0;
84     for (UInt_t i=first; i<=last; i++)
85     numBits += TestBit(i);
86    
87     return numBits;
88     }
89    
90     //--------------------------------------------------------------------------------------------------
91     template<UInt_t N>
92     inline void mithep::BitMask<N>::SetBits(Long64_t bits)
93     {
94     // Set bits given by bits.
95    
96 loizides 1.4 assert(sizeof(Long64_t)>=N);
97 loizides 1.3 SetBits(reinterpret_cast<const char*>(&bits));
98     }
99    
100     //--------------------------------------------------------------------------------------------------
101     template<UInt_t N>
102     inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
103     {
104     // Return true if nth bit is set.
105    
106     if(n>=N*8) return 0;
107    
108     UInt_t loc = n/8;
109     UChar_t bit = n%8;
110     Char_t val = fBitMask[loc];
111     Bool_t result = (val & (1<<bit)) != 0;
112     return result;
113     }
114 loizides 1.5
115     //--------------------------------------------------------------------------------------------------
116     template<UInt_t N>
117     Bool_t mithep::BitMask<N>::operator==(const mithep::BitMask<N> &other) const
118     {
119     // Equal operator.
120    
121     for (UInt_t n=0; n<N; ++n) {
122     if (fBitMask[n]!=other.fBitMask[n])
123     return kFALSE;
124     }
125     return kTRUE;
126     }
127    
128     //--------------------------------------------------------------------------------------------------
129     template<UInt_t N>
130     Bool_t mithep::BitMask<N>::operator!=(const mithep::BitMask<N> &other) const
131     {
132     // Unequal operator.
133    
134     for (UInt_t n=0; n<N; ++n) {
135 bendavid 1.6 if (fBitMask[n]!=other.fBitMask[n])
136     return kTRUE;
137     }
138     return kFALSE;
139     }
140    
141     //--------------------------------------------------------------------------------------------------
142     template<UInt_t N>
143     mithep::BitMask<N> mithep::BitMask<N>::operator ~() const
144     {
145     // bitwise inversion operator
146     BitMask<N> bits;
147     for (UInt_t n=0; n<N; ++n) {
148     bits.fBitMask[n] = ~fBitMask[n];
149 loizides 1.5 }
150 bendavid 1.6
151     return bits;
152     }
153 loizides 1.3 #endif