Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Bitset.h
Go to the documentation of this file.
1 
10 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_BITSET_H
11 #define INCLUDE_MOLASSEMBLER_TEMPLE_BITSET_H
12 
15 
16 namespace Scine {
17 namespace Molassembler {
18 namespace Temple {
19 
25 template<std::size_t N>
26 class Bitset {
27 public:
30 
34  explicit constexpr Bitset() { zero(); }
36 
39 
43  constexpr void zero() {
44  for(auto& block : storage) {
45  block = 0;
46  }
47  }
48 
53  constexpr void set(const std::size_t i) {
54  std::size_t blockIndex = Math::floor(static_cast<double>(i) / bitsPerBlock);
55  std::size_t bitIndex = i - bitsPerBlock * blockIndex;
56 
57  storage.at(blockIndex) |= (1ULL << bitIndex);
58  }
59 
64  constexpr void unset(const std::size_t i) {
65  std::size_t blockIndex = Math::floor(static_cast<double>(i) / bitsPerBlock);
66  std::size_t bitIndex = i - bitsPerBlock * blockIndex;
67 
68  storage.at(blockIndex) ^= (1ULL << bitIndex);
69  }
70 
75  constexpr void set(const std::size_t i, const bool value) {
76  if(value) {
77  set(i);
78  } else {
79  unset(i);
80  }
81  }
83 
86 
90  constexpr bool test(const std::size_t i) const {
91  std::size_t blockIndex = Math::floor(static_cast<double>(i) / bitsPerBlock);
92  std::size_t bitIndex = i - bitsPerBlock * blockIndex;
93 
94  return (
95  storage.at(blockIndex) & (1ULL << bitIndex)
96  ) != 0;
97  }
99 
100 private:
103  using Block = long long unsigned;
105 
109  static constexpr std::size_t bitsPerBlock = Math::floor(
110  Math::log(
111  static_cast<double>(std::numeric_limits<Block>::max()),
112  2.0
113  )
114  );
115 
117  static constexpr std::size_t B = Math::ceil(
118  static_cast<double>(N) / static_cast<double>(bitsPerBlock)
119  );
121 
124  Array<Block, B> storage;
126 
127 };
128 
129 } // namespace Temple
130 } // namespace Molassembler
131 } // namespace Scine
132 
133 #endif
constexpr Bitset()
Zeroing constructor.
Definition: Bitset.h:34
constexpr void set(const std::size_t i, const bool value)
Sets a specific bit to a specified value.
Definition: Bitset.h:75
constexpr math implementations
constexpr void unset(const std::size_t i)
Unsets a specific bit.
Definition: Bitset.h:64
std::array-like class
constexpr void set(const std::size_t i)
Sets a specific bit.
Definition: Bitset.h:53
static constexpr std::size_t bitsPerBlock
Number of bits per block.
Definition: Bitset.h:109
Constexpr bitset class.
Definition: Bitset.h:26
static constexpr std::size_t B
Number of Block types stored.
Definition: Bitset.h:117
constexpr bool test(const std::size_t i) const
Test the value at a particular bit.
Definition: Bitset.h:90
constexpr void zero()
Zero out all bits.
Definition: Bitset.h:43