CMS 3D CMS Logo

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 
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
PhysicsTools::Calibration::convert
PhysicsTools::BitSet convert(const BitSet &bitSet)
constructs BitSet container from persistent representation
Definition: BitSet.cc:25
PhysicsTools::BitSet::bits_
size_t bits_
Definition: BitSet.h:187
electrons_cff.bool
bool
Definition: electrons_cff.py:393
PhysicsTools::BitSet::Manipulator::Manipulator
Manipulator(Word_t *word, unsigned int bit)
Definition: BitSet.h:63
PhysicsTools::BitSet::Manipulator::Manipulator
Manipulator(const Manipulator &orig)
Definition: BitSet.h:48
PhysicsTools::BitSet::Iterator::operator()
size_t operator()() const
returns the index of the currently pointed-at bit
Definition: BitSet.h:83
pos
Definition: PixelAliasList.h:18
PhysicsTools::BitSet::size
size_t size() const
returns the number of all bits in the container
Definition: BitSet.h:173
PhysicsTools::BitSet::operator[]
const Manipulator operator[](size_t bit) const
provide read access to bit with index bit via reference
Definition: BitSet.h:168
PhysicsTools::BitSet::BitSet
BitSet(const BitSet &orig)
Definition: BitSet.h:131
PhysicsTools::BitSet::size_t
std::size_t size_t
Definition: BitSet.h:31
PhysicsTools::BitSet::Manipulator::operator=
bool operator=(bool bit)
bit assignment operator
Definition: BitSet.h:55
word
uint64_t word
Definition: CTPPSTotemDataFormatter.cc:29
cms::Iterator
TGeoIterator Iterator
Definition: DDFilteredView.h:54
PhysicsTools::BitSet::Iterator::store
Word_t * store
Definition: BitSet.h:125
PhysicsTools::BitSet::BitSet
BitSet()
Definition: BitSet.h:129
PhysicsTools::BitSet::iter
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
PhysicsTools::BitSet::Iterator::end
Word_t * end
Definition: BitSet.h:125
PhysicsTools::BitSet::Iterator::Iterator
Iterator(Word_t *begin, Word_t *end)
Definition: BitSet.h:119
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
PhysicsTools::BitSet::store
Word_t * store
Definition: BitSet.h:186
PhysicsTools::BitSet::~BitSet
~BitSet()
Definition: BitSet.h:150
PhysicsTools::BitSet::Iterator::pos
unsigned int pos
Definition: BitSet.h:126
PhysicsTools::BitSet::Iterator::operator++
Iterator & operator++()
increment iterator to point at the next set bit
Definition: BitSet.h:86
PhysicsTools::BitSet::Iterator::operator++
Iterator operator++(int dummy)
increment iterator to point at the next set bit
Definition: BitSet.h:110
PhysicsTools::BitSet::Manipulator
Opaque structure for transparent write access to individual bits.
Definition: BitSet.h:46
PhysicsTools
Definition: Histogram.h:13
PhysicsTools::BitSet::wordSize
static const unsigned int wordSize
Definition: BitSet.h:184
PhysicsTools::BitSet::operator=
BitSet & operator=(const BitSet &orig)
Definition: BitSet.h:152
PhysicsTools::BitSet::bits
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
PhysicsTools::BitSet::bitsToWords
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
PhysicsTools::BitSet
A compact container for storing single bits.
Definition: BitSet.h:29
PhysicsTools::BitSet::BitSet
BitSet(size_t bits)
construct BitSet with a fixed size of bits bits
Definition: BitSet.h:141
PhysicsTools::BitSet::Manipulator::word
Word_t * word
Definition: BitSet.h:66
PhysicsTools::BitSet::operator[]
Manipulator operator[](size_t bit)
provide read/write access to bit with index bit via reference
Definition: BitSet.h:165
PhysicsTools::BitSet::Manipulator::mask
Word_t mask
Definition: BitSet.h:67
PhysicsTools::BitSet::Iterator::begin
Word_t * begin
Definition: BitSet.h:125
HLT_FULL_cff.Calibration
Calibration
Definition: HLT_FULL_cff.py:3579
dummy
Definition: DummySelector.h:38
PhysicsTools::BitSet::Iterator
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
PhysicsTools::BitSet::Word_t
unsigned int Word_t
Definition: BitSet.h:34
PhysicsTools::BitSet::Manipulator::~Manipulator
~Manipulator()
Definition: BitSet.h:49