CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 <cstring>
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) : word(orig.word), mask(orig.mask) {}
49  inline ~Manipulator() {}
50 
52  inline operator bool() const { return *word & mask; }
53 
55  inline bool operator=(bool bit) {
56  *word = (*word & ~mask) | (bit ? mask : 0);
57  return bit;
58  }
59 
60  protected:
61  friend class BitSet;
62 
63  inline Manipulator(Word_t *word, unsigned int bit) : 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 { return (store - begin) * wordSize + pos; }
84 
87  if (++pos < wordSize) {
88  //Only look at bits at and beyond pos
89  Word_t word = *store & ((~(1U << pos)) + 1);
90  if (word) {
91  pos = ffs(word) - 1;
92  return *this;
93  }
94  }
95 
96  pos = 0;
97  for (;;) {
98  if (++store >= end)
99  break;
100  else if (*store) {
101  pos = ffs(*store) - 1;
102  break;
103  }
104  }
105 
106  return *this;
107  }
108 
110  inline Iterator operator++(int dummy) {
111  Iterator orig = *this;
112  ++*this;
113  return orig;
114  }
115 
116  protected:
117  friend class BitSet;
118 
119  Iterator(Word_t *begin, Word_t *end) : begin(begin), store(begin), end(end), pos(0) {
120  if (store < end && !(*store & 1))
121  ++*this;
122  }
123 
124  private:
126  unsigned int pos;
127  };
128 
129  BitSet() : store(nullptr), bits_(0) {}
130 
131  BitSet(const BitSet &orig) : bits_(orig.bits_) {
132  std::size_t words = bitsToWords(bits_);
133  if (words) {
134  store = new Word_t[words];
135  std::memcpy(store, orig.store, words * sizeof(Word_t));
136  } else
137  store = nullptr;
138  }
139 
141  BitSet(size_t bits) : bits_(bits) {
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 = nullptr;
148  }
149 
150  inline ~BitSet() { delete[] store; }
151 
152  BitSet &operator=(const BitSet &orig) {
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 = nullptr;
161  return *this;
162  }
163 
165  inline Manipulator operator[](size_t bit) { return Manipulator(&store[bit / wordSize], bit % wordSize); }
166 
168  inline const Manipulator operator[](size_t bit) const {
169  return Manipulator(&store[bit / wordSize], bit % wordSize);
170  }
171 
173  inline size_t size() const { return bits_; }
174 
176  size_t bits() const;
177 
179  inline Iterator iter() const { return Iterator(store, store + bitsToWords(bits_)); }
180 
181  private:
182  static inline size_t bitsToWords(std::size_t bits) { return (bits + wordSize - 1) / wordSize; }
183 
184  static const unsigned int wordSize = sizeof(Word_t) * 8;
185 
187  size_t bits_;
188  };
189 
190  namespace Calibration {
191  class BitSet;
192 
194  PhysicsTools::BitSet convert(const BitSet &bitSet);
196  BitSet convert(const PhysicsTools::BitSet &bitSet);
197  } // namespace Calibration
198 
199 } // namespace PhysicsTools
200 
201 #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:63
BitSet(size_t bits)
construct BitSet with a fixed size of bits bits
Definition: BitSet.h:141
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:182
BitSet & operator=(const BitSet &orig)
Definition: BitSet.h:152
const Manipulator operator[](size_t bit) const
provide read access to bit with index bit via reference
Definition: BitSet.h:168
TGeoIterator Iterator
bool operator=(bool bit)
bit assignment operator
Definition: BitSet.h:55
uint64_t word
Iterator(Word_t *begin, Word_t *end)
Definition: BitSet.h:119
Iterator operator++(int dummy)
increment iterator to point at the next set bit
Definition: BitSet.h:110
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:86
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:131
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:25
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
Word_t * store
Definition: BitSet.h:186
static const unsigned int wordSize
Definition: BitSet.h:184