HEBI C++ API  3.9.0
bit_set.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
5 namespace hebi {
6 
7 static constexpr size_t BitsInDataWord = sizeof(int) * 8;
8 
9 static inline bool extractBit(unsigned int index, int word) {
10  return static_cast<bool>((word >> index) & 1);
11 }
12 
13 static inline int setBit(unsigned int index, int word) {
14  const auto val = 1 << index;
15  return word | val;
16 }
17 
18 static inline int clearBit(unsigned int index, int word) {
19  const auto val = 1 << index;
20  return word & ~val;
21 }
22 
24 public:
25  MutableProxyBitSet(int* data, size_t bits)
26  : data_(data), bit_count_(bits), data_word_count_((bits / BitsInDataWord) + 1 * ( (bits % BitsInDataWord) != 0 )) {}
27 
28  bool get(size_t index) const {
29  const auto wordIdx = index / BitsInDataWord;
30  const auto word = data_[wordIdx];
31  const auto relIdx = index % BitsInDataWord;
32  return extractBit(static_cast<unsigned int>(relIdx), word);
33  }
34 
35  void set(size_t index) {
36  const auto wordIdx = index / BitsInDataWord;
37  const auto word = data_[wordIdx];
38  const auto relIdx = index % BitsInDataWord;
39  data_[wordIdx] = setBit(static_cast<unsigned int>(relIdx), word);
40  }
41 
42  void reset(size_t index) {
43  const auto wordIdx = index / BitsInDataWord;
44  const auto word = data_[wordIdx];
45  const auto relIdx = index % BitsInDataWord;
46  data_[wordIdx] = clearBit(static_cast<unsigned int>(relIdx), word);
47  }
48 
49  void reset() {
50  for (size_t i = 0; i < data_word_count_; i++) {
51  data_[i] = 0;
52  }
53  }
54 
55  int* data() {
56  return data_;
57  }
58 
59  const int* data() const {
60  return data_;
61  }
62 
63 private:
64  int* data_;
65  const size_t bit_count_;
66  const size_t data_word_count_;
67 };
68 
69 class ProxyBitSet {
70 public:
71  ProxyBitSet(const int* data, size_t bits)
72  : data_(data), bit_count_(bits), data_word_count_((bits / BitsInDataWord) + 1 * ( (bits % BitsInDataWord) != 0 )) {}
73 
74  bool get(size_t index) const {
75  const auto wordIdx = index / BitsInDataWord;
76  const auto word = data_[wordIdx];
77  const auto relIdx = index % BitsInDataWord;
78  return extractBit(static_cast<unsigned int>(relIdx), word);
79  }
80 
81  const int* data() const {
82  return data_;
83  }
84 
85 private:
86  const int* data_;
87  const size_t bit_count_;
88  const size_t data_word_count_;
89 };
90 
91 }
int * data()
Definition: bit_set.hpp:55
bool get(size_t index) const
Definition: bit_set.hpp:74
Definition: bit_set.hpp:23
const int * data() const
Definition: bit_set.hpp:81
Definition: arm.cpp:8
void reset(size_t index)
Definition: bit_set.hpp:42
ProxyBitSet(const int *data, size_t bits)
Definition: bit_set.hpp:71
const int * data() const
Definition: bit_set.hpp:59
static int setBit(unsigned int index, int word)
Definition: bit_set.hpp:13
void reset()
Definition: bit_set.hpp:49
Definition: bit_set.hpp:69
static constexpr size_t BitsInDataWord
Definition: bit_set.hpp:7
static bool extractBit(unsigned int index, int word)
Definition: bit_set.hpp:9
bool get(size_t index) const
Definition: bit_set.hpp:28
MutableProxyBitSet(int *data, size_t bits)
Definition: bit_set.hpp:25
void set(size_t index)
Definition: bit_set.hpp:35
static int clearBit(unsigned int index, int word)
Definition: bit_set.hpp:18