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 //
13 
14 #include <string.h>
15 #include <cstddef>
16 #include <cstring>
17 
18 namespace PhysicsTools {
19 
29 class BitSet {
30  public:
31  typedef std::size_t size_t;
32 
33  protected:
34  typedef unsigned int Word_t;
35 
36  public:
46  struct Manipulator {
47  public:
48  inline Manipulator(const Manipulator &orig) :
49  word(orig.word), mask(orig.mask) {}
50  inline ~Manipulator() {}
51 
53  inline operator bool() const { return *word & mask; }
54 
56  inline bool operator = (bool bit)
57  { *word = (*word & ~mask) | (bit ? mask : 0); return bit; }
58 
59  protected:
60  friend class BitSet;
61 
62  inline Manipulator(Word_t *word, unsigned int bit) :
63  word(word), mask((Word_t)1 << bit) {}
64 
65  private:
68  };
69 
77  struct Iterator {
78  public:
80  inline operator bool() const { return store < end; }
81 
83  inline size_t operator () () const
84  { return (store - begin) * wordSize + pos; }
85 
88  {
89  if (++pos < wordSize) {
90  Word_t word = *store & -(1 << pos);
91  if (word) {
92  pos = ffs(word) - 1;
93  return *this;
94  }
95  }
96 
97  pos = 0;
98  for(;;) {
99  if (++store >= end)
100  break;
101  else if (*store) {
102  pos = ffs(*store) - 1;
103  break;
104  }
105  }
106 
107  return *this;
108  }
109 
111  inline Iterator operator ++ (int dummy)
112  { Iterator orig = *this; ++*this; return orig; }
113 
114  protected:
115  friend class BitSet;
116 
118  begin(begin), store(begin), end(end), pos(0)
119  { if (store < end && !(*store & 1)) ++*this; }
120 
121  private:
123  unsigned int pos;
124  };
125 
126  BitSet() : store(0), bits_(0) {}
127 
128  BitSet(const BitSet &orig) : bits_(orig.bits_)
129  {
130  std::size_t words = bitsToWords(bits_);
131  if (words) {
132  store = new Word_t[words];
133  std::memcpy(store, orig.store, words * sizeof(Word_t));
134  } else
135  store = 0;
136  }
137 
139  BitSet(size_t bits) : bits_(bits)
140  {
141  std::size_t words = bitsToWords(bits);
142  if (words) {
143  store = new Word_t[words];
144  std::memset(store, 0, sizeof(Word_t) * words);
145  } else
146  store = 0;
147  }
148 
149  inline ~BitSet() { delete[] store; }
150 
151  BitSet &operator = (const BitSet &orig)
152  {
153  delete[] store;
154  bits_ = orig.bits_;
155  std::size_t words = bitsToWords(bits_);
156  if (words) {
157  store = new Word_t[words];
158  std::memcpy(store, orig.store, words * sizeof(Word_t));
159  } else
160  store = 0;
161  return *this;
162  }
163 
165  inline Manipulator operator [] (size_t bit)
166  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
167 
169  inline const Manipulator operator [] (size_t bit) const
170  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
171 
173  inline size_t size() const { return bits_; }
174 
176  size_t bits() const;
177 
179  inline Iterator iter() const
180  { return Iterator(store, store + bitsToWords(bits_)); }
181 
182  private:
183  static inline size_t bitsToWords(std::size_t bits)
184  { return (bits + wordSize - 1) / wordSize; }
185 
186  static const unsigned int wordSize = sizeof(Word_t) * 8;
187 
189  size_t bits_;
190 };
191 
192 namespace Calibration {
193  class BitSet;
194 
196  PhysicsTools::BitSet convert(const BitSet &bitSet);
198  BitSet convert(const PhysicsTools::BitSet &bitSet);
199 }
200 
201 } // namespace PhysicsTools
202 
203 #endif // PhysicsTools_MVAComputer_BitSet_h
Manipulator operator[](size_t bit)
provide read/write access to bit with index bit via reference
Definition: BitSet.h:165
Manipulator(Word_t *word, unsigned int bit)
Definition: BitSet.h:62
BitSet(size_t bits)
construct BitSet with a fixed size of bits bits
Definition: BitSet.h:139
Opaque structure for transparent write access to individual bits.
Definition: BitSet.h:46
size_t operator()() const
returns the index of the currently pointed-at bit
Definition: BitSet.h:83
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
BitSet & operator=(const BitSet &orig)
Definition: BitSet.h:151
bool operator=(bool bit)
bit assignment operator
Definition: BitSet.h:56
Iterator(Word_t *begin, Word_t *end)
Definition: BitSet.h:117
A compact container for storing single bits.
Definition: BitSet.h:29
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
unsigned int Word_t
Definition: BitSet.h:34
Iterator & operator++()
increment iterator to point at the next set bit
Definition: BitSet.h:87
size_t size() const
returns the number of all bits in the container
Definition: BitSet.h:173
Manipulator(const Manipulator &orig)
Definition: BitSet.h:48
std::size_t size_t
Definition: BitSet.h:31
BitSet(const BitSet &orig)
Definition: BitSet.h:128
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
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:77
Word_t * store
Definition: BitSet.h:188
static const unsigned int wordSize
Definition: BitSet.h:186