CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Protected Types | Static Private Member Functions | Private Attributes | Static Private Attributes
PhysicsTools::BitSet Class Reference

A compact container for storing single bits. More...

#include <BitSet.h>

Classes

class  Iterator
 Iterates over all set bits of a BitSet. More...
 
class  Manipulator
 Opaque structure for transparent write access to individual bits. More...
 

Public Types

typedef std::size_t size_t
 

Public Member Functions

size_t bits () const
 returns the number of set bits in the container More...
 
 BitSet ()
 
 BitSet (const BitSet &orig)
 
 BitSet (size_t bits)
 construct BitSet with a fixed size of bits bits More...
 
Iterator iter () const
 create iterator over all set bits More...
 
BitSetoperator= (const BitSet &orig)
 
Manipulator operator[] (size_t bit)
 provide read/write access to bit with index bit via reference More...
 
const Manipulator operator[] (size_t bit) const
 provide read access to bit with index bit via reference More...
 
size_t size () const
 returns the number of all bits in the container More...
 
 ~BitSet ()
 

Protected Types

typedef unsigned int Word_t
 

Static Private Member Functions

static size_t bitsToWords (std::size_t bits)
 

Private Attributes

size_t bits_
 
Word_tstore
 

Static Private Attributes

static const unsigned int wordSize = sizeof(Word_t) * 8
 

Detailed Description

A compact container for storing single bits.

BitSet provides a container of boolean values, similar to a std::vector<bool> which only consumes one actual bit for each value. Also an iterator is provided that can iterate over all set bits.

Definition at line 29 of file BitSet.h.

Member Typedef Documentation

◆ size_t

typedef std::size_t PhysicsTools::BitSet::size_t

Definition at line 31 of file BitSet.h.

◆ Word_t

typedef unsigned int PhysicsTools::BitSet::Word_t
protected

Definition at line 34 of file BitSet.h.

Constructor & Destructor Documentation

◆ BitSet() [1/3]

PhysicsTools::BitSet::BitSet ( )
inline

Definition at line 129 of file BitSet.h.

129 : store(nullptr), bits_(0) {}
Word_t * store
Definition: BitSet.h:186

◆ BitSet() [2/3]

PhysicsTools::BitSet::BitSet ( const BitSet orig)
inline

Definition at line 131 of file BitSet.h.

References bits_, bitsToWords(), and store.

131  : 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  }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
unsigned int Word_t
Definition: BitSet.h:34
Word_t * store
Definition: BitSet.h:186

◆ BitSet() [3/3]

PhysicsTools::BitSet::BitSet ( size_t  bits)
inline

construct BitSet with a fixed size of bits bits

Definition at line 141 of file BitSet.h.

References bits(), bitsToWords(), and store.

141  : 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  }
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
unsigned int Word_t
Definition: BitSet.h:34
Word_t * store
Definition: BitSet.h:186

◆ ~BitSet()

PhysicsTools::BitSet::~BitSet ( )
inline

Definition at line 150 of file BitSet.h.

References store.

150 { delete[] store; }
Word_t * store
Definition: BitSet.h:186

Member Function Documentation

◆ bits()

BitSet::size_t PhysicsTools::BitSet::bits ( ) const

returns the number of set bits in the container

Definition at line 6 of file BitSet.cc.

References bits_, bitsToWords(), AlCaHLTBitMon_ParallelJobs::p, and store.

Referenced by BitSet(), and bitsToWords().

6  {
7  static constexpr unsigned char byteBits[256] = {
8  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
9  3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
10  3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
11  6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
12  3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
13  5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
14  6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
15  const unsigned char *begin = reinterpret_cast<const unsigned char *>(store);
16  const unsigned char *end = reinterpret_cast<const unsigned char *>(store + bitsToWords(bits_));
17 
18  size_t bits = 0;
19  for (const unsigned char *p = begin; p < end; p++)
20  bits += byteBits[*p];
21 
22  return bits;
23  }
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
Word_t * store
Definition: BitSet.h:186

◆ bitsToWords()

static size_t PhysicsTools::BitSet::bitsToWords ( std::size_t  bits)
inlinestaticprivate

Definition at line 182 of file BitSet.h.

References bits(), and wordSize.

Referenced by bits(), BitSet(), iter(), and operator=().

182 { return (bits + wordSize - 1) / wordSize; }
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
static const unsigned int wordSize
Definition: BitSet.h:184

◆ iter()

Iterator PhysicsTools::BitSet::iter ( ) const
inline

create iterator over all set bits

Definition at line 179 of file BitSet.h.

References bits_, bitsToWords(), and store.

Referenced by PhysicsTools::VarProcessor::configure(), PhysicsTools::VarProcessor::deriv(), and PhysicsTools::VarProcessor::eval().

179 { return Iterator(store, store + bitsToWords(bits_)); }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
TGeoIterator Iterator
Word_t * store
Definition: BitSet.h:186

◆ operator=()

BitSet& PhysicsTools::BitSet::operator= ( const BitSet orig)
inline

Definition at line 152 of file BitSet.h.

References bits_, bitsToWords(), and store.

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 = nullptr;
161  return *this;
162  }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:182
unsigned int Word_t
Definition: BitSet.h:34
Word_t * store
Definition: BitSet.h:186

◆ operator[]() [1/2]

Manipulator PhysicsTools::BitSet::operator[] ( size_t  bit)
inline

provide read/write access to bit with index bit via reference

Definition at line 165 of file BitSet.h.

References triggerObjects_cff::bit, store, and wordSize.

165 { return Manipulator(&store[bit / wordSize], bit % wordSize); }
Word_t * store
Definition: BitSet.h:186
static const unsigned int wordSize
Definition: BitSet.h:184

◆ operator[]() [2/2]

const Manipulator PhysicsTools::BitSet::operator[] ( size_t  bit) const
inline

provide read access to bit with index bit via reference

Definition at line 168 of file BitSet.h.

References triggerObjects_cff::bit, store, and wordSize.

168  {
169  return Manipulator(&store[bit / wordSize], bit % wordSize);
170  }
Word_t * store
Definition: BitSet.h:186
static const unsigned int wordSize
Definition: BitSet.h:184

◆ size()

size_t PhysicsTools::BitSet::size ( void  ) const
inline

returns the number of all bits in the container

Definition at line 173 of file BitSet.h.

References bits_.

Referenced by ntupleDataFormat._Collection::__iter__(), ntupleDataFormat._Collection::__len__(), PhysicsTools::VarProcessor::configure(), and PhysicsTools::Calibration::convert().

173 { return bits_; }

Member Data Documentation

◆ bits_

size_t PhysicsTools::BitSet::bits_
private

Definition at line 187 of file BitSet.h.

Referenced by bits(), BitSet(), iter(), operator=(), and size().

◆ store

Word_t* PhysicsTools::BitSet::store
private

Definition at line 186 of file BitSet.h.

Referenced by bits(), BitSet(), iter(), operator=(), operator[](), and ~BitSet().

◆ wordSize

const unsigned int PhysicsTools::BitSet::wordSize = sizeof(Word_t) * 8
staticprivate