LeviLamina
Loading...
Searching...
No Matches
bitset_base.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4
5namespace brstd::detail {
6
7template <typename Derived, typename UnderlyingContainer>
9public:
10 using underlying_container = UnderlyingContainer;
11 using underlying_type = UnderlyingContainer::value_type;
12 using size_type = UnderlyingContainer::size_type;
13
14protected:
15 static const size_t bits_per_value = sizeof(underlying_type) * 8;
16
17public:
18 constexpr bool test(size_type pos) const {
19 auto idx = pos / bits_per_value;
20 auto offset = pos % bits_per_value;
21 return ((container_[idx] >> offset) & underlying_type(1)) != 0;
22 }
23
24 constexpr Derived& set(size_type pos) { return set(pos, true); }
25
26 constexpr Derived& set(size_type pos, bool value) {
27 auto idx = pos / bits_per_value;
28 auto offset = pos % bits_per_value;
29 if (value) {
30 container_[idx] |= (underlying_type(1) << offset);
31 } else {
32 container_[idx] &= ~(underlying_type(1) << offset);
33 }
34 return static_cast<Derived&>(*this);
35 }
36
37protected:
38 underlying_container container_;
39};
40
41} // namespace brstd::detail
Definition bitset_base.h:8