23
23
24
24
#pragma once
25
25
26
- #include < bits/ranges_algo.h>
27
26
#include < algorithm>
28
- #include < bitset>
29
27
#include " basic/exception.h"
30
28
#include " basic/mem.h"
31
- #include " basic/vec.h"
32
29
33
30
namespace lps ::basic {
34
- template <size_t N, typename T = void >
31
+ template <size_t N>
35
32
class Bitset {
36
33
public:
34
+ using ele_type = bool ;
35
+ using bitset = std::array<ele_type, N>;
37
36
constexpr static mem::TraceTag::tag_type kTag = " Bitset" ;
38
37
template <std::size_t R, std::size_t L>
39
- static std::bitset<N > range (std:: bitset<N> b) {
38
+ static std::array<ele_type, L - R > range (const bitset& b) {
40
39
static_assert (R <= L && L <= N, " Not valid." );
41
- b >>= R;
42
- b <<= (N - L + R);
43
- b >>= (N - L);
44
- return b;
40
+ std::array<ele_type, L - R> new_b;
41
+ size_t idx = 0 ;
42
+ for (int i = R; i < L; i++) {
43
+ new_b[idx++] = b[i];
44
+ }
45
+ return new_b;
45
46
}
46
47
47
- static std:: bitset<N> range (std:: bitset<N> b, std::size_t R, std::size_t L) {
48
+ static bitset range (const bitset& b, std::size_t R, std::size_t L) {
48
49
lps_assert (kTag , R <= L && L <= N);
49
- b >>= R;
50
- b <<= (N - L + R);
51
- b >>= (N - L);
52
- return b;
50
+ bitset new_b;
51
+ size_t idx = 0 ;
52
+ for (int i = R; i < L; i++) {
53
+ new_b[idx++] = b[i];
54
+ }
55
+ return new_b;
53
56
}
54
57
55
58
static constexpr size_t kN = N;
56
- Bitset () : len_(N) { value_.reset (); }
59
+
60
+ explicit Bitset () { reset (); }
61
+
57
62
template <size_t N1>
58
- explicit Bitset (const std::array<bool , N1>& other, size_t start_idx = 0 )
63
+ explicit Bitset (const std::array<ele_type , N1>& other, size_t start_idx = 0 )
59
64
: start_idx_(start_idx) {
60
- size_t m = std::min (N, N1);
61
- for (size_t i = start_idx; i < m; i++) {
62
- value_[i] = other[i];
63
- }
64
- len_ = m;
65
+ set (other, start_idx);
65
66
}
66
67
67
68
template <size_t N1>
68
- explicit Bitset (const std::bitset< N1>& other, size_t start_idx = 0 )
69
- : start_idx_( start_idx) {
69
+ void set (const std::array<ele_type, N1>& other, size_t start_idx = 0 ) {
70
+ start_idx_ = start_idx;
70
71
size_t m = std::min (N, N1);
71
72
if (start_idx > 0 ) {
72
73
lps_assert (kTag , N1 < N);
73
74
value_ = value_.to_ullong () || (other.to_ullong () << start_idx);
74
75
} else {
75
- value_ = other.to_ullong ();
76
+
77
+ for (int i = 0 ; i < m; i++) {
78
+ value_[i] = other[i];
79
+ }
76
80
}
77
81
len_ = m;
82
+ lps_assert (kTag , len_ + start_idx_ <= N);
78
83
}
79
84
80
- void reset () { value_ = value (start_idx_).reset ().to_ullong (); }
81
- void set () { value_ = value (start_idx_).set ().to_ullong (); }
85
+ void reset () {
86
+ for (int i = start_idx_; i < start_idx_ + len_; i++) {
87
+ value_[i] = false ;
88
+ }
89
+ }
90
+ void set () {
91
+ for (int i = start_idx_; i < start_idx_ + len_; i++) {
92
+ value_[i] = true ;
93
+ }
94
+ }
82
95
void set (size_t pos) {
83
96
lps_assert (kTag , (pos + start_idx_) < len_);
84
97
value_[pos + start_idx_] = true ;
@@ -87,73 +100,29 @@ class Bitset {
87
100
lps_assert (kTag , (pos + start_idx_) < len_);
88
101
return value_[pos + start_idx_];
89
102
}
90
- std::bitset<N> value (size_t start_idx = 0 ) {
91
- return std::bitset<N>(
92
- range (value_, start_idx, start_idx + len_).to_ullong ());
93
- }
94
-
95
- template <size_t N1>
96
- std::bitset<N1> value (size_t start_idx = 0 ) {
97
- return std::bitset<N1>(
98
- range (value_, start_idx, start_idx + len_).to_ullong ());
103
+ bitset value (size_t start_idx = 0 ) {
104
+ if (start_idx == 0 ) {
105
+ return range (value_, 0 , len_);
106
+ }
107
+ auto a = range (value_, start_idx, len_);
108
+ return a;
99
109
}
100
110
101
111
bool all () {
102
- return range (value_, start_idx_, start_idx_ + len_).count () == len_;
112
+ size_t cnt = 0 ;
113
+ for (int i = start_idx_; i < start_idx_ + len_; i++) {
114
+ if (value_[i]) {
115
+ cnt++;
116
+ }
117
+ }
118
+ return cnt == len_;
103
119
}
104
120
[[nodiscard]] size_t start_idx () const { return start_idx_; }
105
121
106
122
private:
107
- std:: bitset<N> value_;
108
- size_t len_{0 };
123
+ bitset value_;
124
+ size_t len_{N };
109
125
size_t start_idx_{0 };
110
126
};
111
127
112
- template <size_t N>
113
- class Bitset <N, typename std::enable_if<(N > (8 * sizeof (unsigned long long ))),
114
- bool >::type> {
115
- public:
116
- constexpr static mem::TraceTag::tag_type kTag = " Bitset" ;
117
- static constexpr size_t kN = N;
118
- Bitset () : len_ (N) { LPS_ERROR (kTag , " Unsupport yet." ); }
119
- template <size_t N1>
120
- explicit Bitset (const std::array<bool , N1>& other, size_t start_idx = 0 ) {
121
- constexpr size_t kM = std::min (N, N1);
122
- for (size_t i = start_idx; i < kM ; i++) {
123
- value_[i] = other[i];
124
- }
125
- len_ = kM ;
126
- }
127
- template <size_t N1>
128
- explicit Bitset (const std::bitset<N1>& other, size_t start_idx = 0 ) {
129
- static_assert (N1 < N, " not valid size" );
130
- for (size_t i = start_idx; i < N1; i++) {
131
- value_[i] = other[i];
132
- }
133
- len_ = N1;
134
- }
135
- void reset () { value_.fill (false ); }
136
- void set () { value_.fill (true ); }
137
- void set (size_t pos) {
138
- lps_assert (kTag , pos < len_);
139
- value_[pos] = true ;
140
- }
141
- bool at (size_t pos) {
142
- lps_assert (kTag , pos < len_);
143
- return value_[pos];
144
- }
145
- std::array<bool , N> value (size_t start_idx = 0 ) {
146
- lps_assert (kTag , start_idx == 0 );
147
- return value_;
148
- }
149
- bool all () {
150
- lps_assert (kTag , len_ <= N);
151
- return std::ranges::all_of (value_.begin (), value_.begin () + len_,
152
- [](bool a) { return a; });
153
- }
154
-
155
- private:
156
- basic::Vector<N, bool > value_;
157
- size_t len_{0 };
158
- };
159
128
} // namespace lps::basic
0 commit comments