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

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

Definition at line 31 of file BitSet.h.

typedef unsigned int PhysicsTools::BitSet::Word_t
protected

Definition at line 34 of file BitSet.h.

Constructor & Destructor Documentation

PhysicsTools::BitSet::BitSet ( )
inline

Definition at line 126 of file BitSet.h.

126 : store(0), bits_(0) {}
Word_t * store
Definition: BitSet.h:188
PhysicsTools::BitSet::BitSet ( const BitSet orig)
inline

Definition at line 128 of file BitSet.h.

References bits_, bitsToWords(), and store.

128  : 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  }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
unsigned int Word_t
Definition: BitSet.h:34
Word_t * store
Definition: BitSet.h:188
PhysicsTools::BitSet::BitSet ( size_t  bits)
inline

construct BitSet with a fixed size of bits bits

Definition at line 139 of file BitSet.h.

References bitsToWords(), and store.

139  : 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  }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
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
Word_t * store
Definition: BitSet.h:188
PhysicsTools::BitSet::~BitSet ( )
inline

Definition at line 149 of file BitSet.h.

References store.

149 { delete[] store; }
Word_t * store
Definition: BitSet.h:188

Member Function Documentation

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 begin, bits_, bitsToWords(), constexpr, end, AlCaHLTBitMon_ParallelJobs::p, and store.

Referenced by size().

7 {
8  static constexpr unsigned char byteBits[256] = {
9  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3,
10  4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4,
11  4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3,
12  4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,
13  4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3,
14  4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4,
15  4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3,
16  4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
17  4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3,
18  4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4,
19  4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3,
20  4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,
21  4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3,
22  4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4,
23  4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3,
24  4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
25  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7,
26  8
27  };
28  const unsigned char *begin = reinterpret_cast<const unsigned char*>(store);
29  const unsigned char *end = reinterpret_cast<const unsigned char*>(store + bitsToWords(bits_));
30 
31  size_t bits = 0;
32  for(const unsigned char *p = begin; p < end; p++)
33  bits += byteBits[*p];
34 
35  return bits;
36 }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
#define constexpr
#define end
Definition: vmac.h:37
size_t bits() const
returns the number of set bits in the container
Definition: BitSet.cc:6
#define begin
Definition: vmac.h:30
Word_t * store
Definition: BitSet.h:188
static size_t PhysicsTools::BitSet::bitsToWords ( std::size_t  bits)
inlinestaticprivate

Definition at line 183 of file BitSet.h.

References wordSize.

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

184  { 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:186
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().

180  { return Iterator(store, store + bitsToWords(bits_)); }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
Word_t * store
Definition: BitSet.h:188
BitSet& PhysicsTools::BitSet::operator= ( const BitSet orig)
inline

Definition at line 151 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 = 0;
161  return *this;
162  }
static size_t bitsToWords(std::size_t bits)
Definition: BitSet.h:183
unsigned int Word_t
Definition: BitSet.h:34
Word_t * store
Definition: BitSet.h:188
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 PhysicsTools::BitSet::Manipulator::Manipulator(), store, and wordSize.

166  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
Word_t * store
Definition: BitSet.h:188
static const unsigned int wordSize
Definition: BitSet.h:186
const Manipulator PhysicsTools::BitSet::operator[] ( size_t  bit) const
inline

provide read access to bit with index bit via reference

Definition at line 169 of file BitSet.h.

References PhysicsTools::BitSet::Manipulator::Manipulator(), store, and wordSize.

170  { return Manipulator(&store[bit / wordSize], bit % wordSize); }
Word_t * store
Definition: BitSet.h:188
static const unsigned int wordSize
Definition: BitSet.h:186
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(), and bits_.

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

173 { return bits_; }

Member Data Documentation

size_t PhysicsTools::BitSet::bits_
private

Definition at line 189 of file BitSet.h.

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

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