Beatmup
bitset.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <cstdint>
21 #include "../basic_types.h"
22 #include <vector>
23 
24 
25 namespace Beatmup {
26 
27  /**
28  A set of boolean flags.
29  */
30  class Bitset {
31  private:
32 #ifdef BEATMUP_PLATFORM_64BIT_
33  typedef uint64_t bits_t;
34  static const bits_t ALL_ONES = 0xffffffffffffffff;
35 #else
36  typedef uint32_t bits_t;
37  static const bits_t ALL_ONES = 0xffffffff;
38 #endif
39 
40  static const bits_t _1_ = (bits_t)1;
41  static const size_t PACK_SIZE = 8 * sizeof(bits_t);
42  std::vector<bits_t> bits;
43  size_t size;
44 
45  inline bool getBit(size_t i) const {
46  return ( bits[i / PACK_SIZE] & (_1_ << (i & (PACK_SIZE - 1))) ) > 0;
47  }
48 
49  public:
50  inline Bitset(): size(0) {}
51 
52  inline Bitset(size_t size, bool value) : bits(ceili(size, PACK_SIZE), value ? ALL_ONES : 0), size(size)
53  {}
54 
55  inline void resize(size_t size) {
56  bits.resize(ceili(size, PACK_SIZE));
57  this->size = size;
58  }
59 
60  inline void setAll(bool value) {
61  if (value)
62  for (auto &_ : bits) _ = ALL_ONES;
63  else
64  for (auto &_ : bits) _ = 0;
65  }
66 
67  inline void set(size_t i, bool value = true) {
69  auto& _ = bits[i / PACK_SIZE];
70  _ = value
71  ? _ | (_1_ << (i & (PACK_SIZE - 1)))
72  : _ & ~(_1_ << (i & (PACK_SIZE - 1)));
73  }
74 
75  inline bool all() const {
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  }
84 
85  inline bool any() const {
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  }
94 
95  inline size_t count() const {
96  size_t n = 0;
97  for (size_t i = 0; i < size; ++i)
98  if (getBit(i))
99  n++;
100  return n;
101  }
102 
103  inline bool operator[](size_t i) const {
105  return getBit(i);
106  }
107  };
108 }
A set of boolean flags.
Definition: bitset.h:30
static const bits_t _1_
Definition: bitset.h:40
bool getBit(size_t i) const
Definition: bitset.h:45
Bitset(size_t size, bool value)
Definition: bitset.h:52
void setAll(bool value)
Definition: bitset.h:60
bool operator[](size_t i) const
Definition: bitset.h:103
size_t count() const
Definition: bitset.h:95
void set(size_t i, bool value=true)
Definition: bitset.h:67
uint32_t bits_t
Definition: bitset.h:36
bool any() const
Definition: bitset.h:85
static const bits_t ALL_ONES
Definition: bitset.h:37
void resize(size_t size)
Definition: bitset.h:55
static const size_t PACK_SIZE
Definition: bitset.h:41
bool all() const
Definition: bitset.h:75
size_t size
Definition: bitset.h:43
std::vector< bits_t > bits
Definition: bitset.h:42
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
#define ceili(x, y)
integer division x/y with ceiling
Definition: utils.hpp:21
int n