CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BitSet.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_BitSet_h
2 #define PhysicsTools_MVAComputer_BitSet_h
3 // -*- C++ -*-
4 //
5 // Package: MVAComputer
6 // Class : BitSet
7 //
8 
9 //
10 // Author: Christophe Saout <christophe.saout@cern.ch>
11 // Created: Sat Apr 24 15:18 CEST 2007
12 // $Id: BitSet.h,v 1.5 2009/05/09 12:23:46 saout Exp $
13 //
14 
15 #include <string.h>
16 #include <cstddef>
17 #include <cstring>
18 
19 namespace PhysicsTools {
20 
30 class BitSet {
31  public:
32  typedef std::size_t size_t;
33 
34  protected:
35  typedef unsigned int Word_t;
36 
37  public:
47  struct Manipulator {
48  public:
49  inline Manipulator(const Manipulator &orig) :
50  word(orig.word), mask(orig.mask) {}
51  inline ~Manipulator() {}
52 
54  inline operator bool() const { return *word & mask; }
55 
57  inline bool operator = (bool bit)
58  { *word = (*word & ~mask) | (bit ? mask : 0); return bit; }
59 
60  protected:
61  friend class BitSet;
62 
63  inline Manipulator(Word_t *word, unsigned int bit) :
64  word(word), mask((Word_t)1 << bit) {}
65 
66  private:
69  };
70 
78  struct Iterator {
79  public:
81  inline operator bool() const { return store < end; }
82 
84  inline size_t operator () () const
85  { return (store - begin) * wordSize + pos; }
86 
89  {
90  if (++pos < wordSize) {
91  Word_t word = *store & -(1 << pos);
92  if (word) {
93  pos = ffs(word) - 1;
94  return *this;
95  }
96  }
97 
98  pos = 0;
99  for(;;) {
100  if (++store >= end)
101  break;
102  else if (*store) {
103  pos = ffs(*store) - 1;
104  break;
105  }
106  }
107 
108  return *this;
109  }
110 
112  inline Iterator operator ++ (int dummy)
113  { Iterator orig = *this; ++*this; return orig; }
114 
115  protected:
116  friend class BitSet;
117 
119  begin(begin), store(begin), end(end), pos(0)
120  { if (store < end && !(*store & 1)) ++*this; }
121 
122  private:
124  unsigned int pos;
125  };
126 
127  BitSet() : store(0), bits_(0) {}
128 
129  BitSet(const BitSet &orig) : bits_(orig.bits_)
130  {
131  std::size_t words = bitsToWords(bits_);
132  if (words) {
133  store = new Word_t[words];
134  std::memcpy(store, orig.store, words * sizeof(Word_t));
135  } else
136  store = 0;
137  }
138 
140  BitSet(size_t bits) : bits_(bits)
141  {
142  std::size_t words = bitsToWords(bits);
143  if (words) {
144  store = new Word_t[words];
145  std::memset(store, 0, sizeof(Word_t) * words);
146  } else
147  store = 0;
148  }
149 
150  inline ~BitSet() { delete[] store; }
151 
152  BitSet &operator = (const BitSet &orig)
153  {
154  delete[] store;
155  bits_ = orig.bits_;
156  std::size_t words = bitsToWords(bits_);
157  if (words) {
158  store = new Word_t[words];
159  std::memcpy(store, orig.store, words * sizeof(Word_t));
160  } else
161  store = 0;
162  return *this;
163  }
164 
166  inline Manipulator operator [] (size_t bit)
167  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
168 
170  inline const Manipulator operator [] (size_t bit) const
171  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
172 
174  inline size_t size() const { return bits_; }
175 
177  size_t bits() const;
178 
180  inline Iterator iter() const
181  { return Iterator(store, store + bitsToWords(bits_)); }
182 
183  private:
184  static inline size_t bitsToWords(std::size_t bits)
185  { return (bits + wordSize - 1) / wordSize; }
186 
187  static const unsigned int wordSize = sizeof(Word_t) * 8;
188 
190  size_t bits_;
191 };
192 
193 namespace Calibration {
194  class BitSet;
195 
197  PhysicsTools::BitSet convert(const BitSet &bitSet);
199  BitSet convert(const PhysicsTools::BitSet &bitSet);
200 }
201 
202 } // namespace PhysicsTools
203 
204 #endif // PhysicsTools_MVAComputer_BitSet_h
Manipulator operator[](size_t bit)
provide read/write access to bit with index bit via reference
Definition: BitSet.h:166
Manipulator(Word_t *word, unsigned int bit)
Definition: BitSet.h:63
BitSet(size_t bits)
construct BitSet with a fixed size of bits bits
Definition: BitSet.h:140
Opaque structure for transparent write access to individual bits.
Definition: BitSet.h:47
size_t operator()() const
returns the index of the currently pointed-at bit
Definition: BitSet.h:84
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:184
BitSet & operator=(const BitSet &orig)
Definition: BitSet.h:152
bool operator=(bool bit)
bit assignment operator
Definition: BitSet.h:57
Iterator(Word_t *begin, Word_t *end)
Definition: BitSet.h:118
A compact container for storing single bits.
Definition: BitSet.h:30
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
unsigned int Word_t
Definition: BitSet.h:35
Iterator & operator++()
increment iterator to point at the next set bit
Definition: BitSet.h:88
size_t size() const
returns the number of all bits in the container
Definition: BitSet.h:174
Manipulator(const Manipulator &orig)
Definition: BitSet.h:49
std::size_t size_t
Definition: BitSet.h:32
BitSet(const BitSet &orig)
Definition: BitSet.h:129
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:180
PhysicsTools::BitSet convert(const BitSet &bitSet)
constructs BitSet container from persistent representation
Definition: BitSet.cc:38
Iterates over all set bits of a BitSet.
Definition: BitSet.h:78
Word_t * store
Definition: BitSet.h:189
static const unsigned int wordSize
Definition: BitSet.h:187