ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.14
Committed: Tue Mar 3 17:04:09 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre1
Changes since 1.13: +10 -10 lines
Log Message:
Cleanup and double32.

File Contents

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