HEBI C++ API  3.12.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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), 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 data_word_count_;
66 };
67 
68 class ProxyBitSet {
69 public:
70  ProxyBitSet(const int* data, size_t /*bits*/)
71  : data_(data) {}
72 
73  bool get(size_t index) const {
74  const auto wordIdx = index / BitsInDataWord;
75  const auto word = data_[wordIdx];
76  const auto relIdx = index % BitsInDataWord;
77  return extractBit(static_cast<unsigned int>(relIdx), word);
78  }
79 
80  const int* data() const {
81  return data_;
82  }
83 
84 private:
85  const int* data_;
86 };
87 
88 }
int * data()
Definition: bit_set.hpp:55
bool get(size_t index) const
Definition: bit_set.hpp:73
Definition: bit_set.hpp:23
const int * data() const
Definition: bit_set.hpp:80
Definition: arm.cpp:10
void reset(size_t index)
Definition: bit_set.hpp:42
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:68
static constexpr size_t BitsInDataWord
Definition: bit_set.hpp:7
static bool extractBit(unsigned int index, int word)
Definition: bit_set.hpp:9
ProxyBitSet(const int *data, size_t)
Definition: bit_set.hpp:70
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