Beatmup
Beatmup::Bitset Class Reference

A set of boolean flags. More...

#include <bitset.h>

Public Member Functions

 Bitset ()
 
 Bitset (size_t size, bool value)
 
void resize (size_t size)
 
void setAll (bool value)
 
void set (size_t i, bool value=true)
 
bool all () const
 
bool any () const
 
size_t count () const
 
bool operator[] (size_t i) const
 

Private Types

typedef uint32_t bits_t
 

Private Member Functions

bool getBit (size_t i) const
 

Private Attributes

std::vector< bits_tbits
 
size_t size
 

Static Private Attributes

static const bits_t ALL_ONES = 0xffffffff
 
static const bits_t _1_ = (bits_t)1
 
static const size_t PACK_SIZE = 8 * sizeof(bits_t)
 

Detailed Description

A set of boolean flags.

Definition at line 30 of file bitset.h.

Member Typedef Documentation

◆ bits_t

typedef uint32_t Beatmup::Bitset::bits_t
private

Definition at line 36 of file bitset.h.

Constructor & Destructor Documentation

◆ Bitset() [1/2]

Beatmup::Bitset::Bitset ( )
inline

Definition at line 50 of file bitset.h.

50 : size(0) {}
size_t size
Definition: bitset.h:43

◆ Bitset() [2/2]

Beatmup::Bitset::Bitset ( size_t  size,
bool  value 
)
inline

Definition at line 52 of file bitset.h.

52  : bits(ceili(size, PACK_SIZE), value ? ALL_ONES : 0), size(size)
53  {}
static const bits_t ALL_ONES
Definition: bitset.h:37
static const size_t PACK_SIZE
Definition: bitset.h:41
std::vector< bits_t > bits
Definition: bitset.h:42
#define ceili(x, y)
integer division x/y with ceiling
Definition: utils.hpp:21

Member Function Documentation

◆ getBit()

bool Beatmup::Bitset::getBit ( size_t  i) const
inlineprivate

Definition at line 45 of file bitset.h.

45  {
46  return ( bits[i / PACK_SIZE] & (_1_ << (i & (PACK_SIZE - 1))) ) > 0;
47  }
static const bits_t _1_
Definition: bitset.h:40

◆ resize()

void Beatmup::Bitset::resize ( size_t  size)
inline

Definition at line 55 of file bitset.h.

55  {
56  bits.resize(ceili(size, PACK_SIZE));
57  this->size = size;
58  }

◆ setAll()

void Beatmup::Bitset::setAll ( bool  value)
inline

Definition at line 60 of file bitset.h.

60  {
61  if (value)
62  for (auto &_ : bits) _ = ALL_ONES;
63  else
64  for (auto &_ : bits) _ = 0;
65  }

◆ set()

void Beatmup::Bitset::set ( size_t  i,
bool  value = true 
)
inline

Definition at line 67 of file bitset.h.

67  {
69  auto& _ = bits[i / PACK_SIZE];
70  _ = value
71  ? _ | (_1_ << (i & (PACK_SIZE - 1)))
72  : _ & ~(_1_ << (i & (PACK_SIZE - 1)));
73  }
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163

◆ all()

bool Beatmup::Bitset::all ( ) const
inline

Definition at line 75 of file bitset.h.

75  {
76  for (size_t i = 0; i < size / PACK_SIZE; ++i)
77  if (bits[i] != ALL_ONES)
78  return false;
79  const size_t rem = size & (PACK_SIZE - 1);
80  if (rem > 0)
81  return (~(bits.back() & ((_1_ << rem) - 1))) == 0;
82  return true;
83  }

◆ any()

bool Beatmup::Bitset::any ( ) const
inline

Definition at line 85 of file bitset.h.

85  {
86  for (size_t i = 0; i < size / PACK_SIZE; ++i)
87  if (bits[i] != 0)
88  return false;
89  const size_t rem = size & (PACK_SIZE - 1);
90  if (rem > 0)
91  return (bits.back() & ((_1_ << rem) - 1)) > 0;
92  return true;
93  }

◆ count()

size_t Beatmup::Bitset::count ( ) const
inline

Definition at line 95 of file bitset.h.

95  {
96  size_t n = 0;
97  for (size_t i = 0; i < size; ++i)
98  if (getBit(i))
99  n++;
100  return n;
101  }
bool getBit(size_t i) const
Definition: bitset.h:45
int n

◆ operator[]()

bool Beatmup::Bitset::operator[] ( size_t  i) const
inline

Definition at line 103 of file bitset.h.

103  {
105  return getBit(i);
106  }

Member Data Documentation

◆ ALL_ONES

const bits_t Beatmup::Bitset::ALL_ONES = 0xffffffff
staticprivate

Definition at line 37 of file bitset.h.

◆ _1_

const bits_t Beatmup::Bitset::_1_ = (bits_t)1
staticprivate

Definition at line 40 of file bitset.h.

◆ PACK_SIZE

const size_t Beatmup::Bitset::PACK_SIZE = 8 * sizeof(bits_t)
staticprivate

Definition at line 41 of file bitset.h.

◆ bits

std::vector<bits_t> Beatmup::Bitset::bits
private

Definition at line 42 of file bitset.h.

◆ size

size_t Beatmup::Bitset::size
private

Definition at line 43 of file bitset.h.


The documentation for this class was generated from the following file: