forked from infiniflow/infinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmask.cpp
339 lines (284 loc) · 8.71 KB
/
bitmask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
module;
#include <sstream>
import bitmask_buffer;
import global_resource_usage;
import infinity_exception;
import serialize;
import stl;
import serialize;
module bitmask;
namespace infinity {
Bitmask::Bitmask() : data_ptr_(nullptr), buffer_ptr(nullptr), count_(0) {
#ifdef INFINITY_DEBUG
GlobalResourceUsage::IncrObjectCount("Bitmask");
#endif
}
Bitmask::Bitmask(Bitmask &&right) : data_ptr_(right.data_ptr_), buffer_ptr(std::move(right.buffer_ptr)), count_(right.count_) {
right.data_ptr_ = nullptr;
right.count_ = 0;
#ifdef INFINITY_DEBUG
GlobalResourceUsage::IncrObjectCount("Bitmask");
#endif
}
Bitmask::~Bitmask() {
#ifdef INFINITY_DEBUG
GlobalResourceUsage::DecrObjectCount("Bitmask");
#endif
// Reset();
}
void Bitmask::Reset() {
buffer_ptr.reset();
data_ptr_ = nullptr;
count_ = 0;
}
void Bitmask::Initialize(SizeT count) {
if (count_ != 0) {
UnrecoverableError("Bitmask is already initialized.");
}
if ((count & (count - 1)) != 0) {
UnrecoverableError("Capacity need to be N power of 2.");
}
count_ = count;
}
void Bitmask::DeepCopy(const Bitmask &ref) {
count_ = ref.count_;
if (ref.IsAllTrue()) {
buffer_ptr = nullptr;
data_ptr_ = nullptr;
} else {
buffer_ptr = BitmaskBuffer::Make(ref.data_ptr_, count_);
data_ptr_ = buffer_ptr->data_ptr_.get();
}
}
void Bitmask::ShallowCopy(const Bitmask &ref) {
count_ = ref.count_;
if (ref.IsAllTrue()) {
buffer_ptr = nullptr;
data_ptr_ = nullptr;
} else {
buffer_ptr = ref.buffer_ptr;
data_ptr_ = ref.data_ptr_;
}
}
void Bitmask::Resize(SizeT new_count) {
u64 bit_count = new_count & (new_count - 1);
if (bit_count != 0) {
UnrecoverableError("New capacity need to be N power of 2.");
}
if (new_count < count_) {
UnrecoverableError("New capacity < old capacity.");
}
if (buffer_ptr) {
SharedPtr<BitmaskBuffer> new_buffer_ptr = BitmaskBuffer::Make(new_count);
u64 *new_data_ptr = new_buffer_ptr->data_ptr_.get();
// TODO: use std::memcpy but not assignment
void *source_ptr = (void *)data_ptr_;
void *target_ptr = (void *)new_data_ptr;
std::memcpy(target_ptr, source_ptr, count_ / BitmaskBuffer::BYTE_BITS);
// Reset part of new buffer was already initialized as true in BitmaskBuffer::Initialize before.
buffer_ptr = new_buffer_ptr;
data_ptr_ = new_data_ptr;
}
// Don't forget the count;
count_ = new_count;
}
String Bitmask::ToString(SizeT from, SizeT to) {
std::stringstream ss;
ss << "BITMASK(" << to - from << "): ";
if (data_ptr_ == nullptr) {
for (SizeT i = from; i <= to; ++i) {
ss << 1;
}
} else {
for (SizeT i = from; i < to; ++i) {
ss << (IsTrue(i) ? 1 : 0);
}
}
return ss.str();
}
bool Bitmask::IsAllTrue() const {
if (data_ptr_ == nullptr)
return true;
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
for (SizeT i = 0; i < u64_count; ++i) {
if (data_ptr_[i] != BitmaskBuffer::UNIT_MAX)
return false;
}
return true;
}
bool Bitmask::IsTrue(SizeT row_index) const {
if (data_ptr_ == nullptr) {
// All is true
return true;
}
SizeT u64_index = row_index / BitmaskBuffer::UNIT_BITS;
SizeT index_in_u64 = row_index - u64_index * BitmaskBuffer::UNIT_BITS;
return data_ptr_[u64_index] & ((u64(1)) << index_in_u64);
}
void Bitmask::SetTrue(SizeT row_index) {
if (data_ptr_ == nullptr)
return;
SizeT u64_index = row_index / BitmaskBuffer::UNIT_BITS;
SizeT index_in_u64 = row_index - u64_index * BitmaskBuffer::UNIT_BITS;
data_ptr_[u64_index] |= ((u64(1)) << index_in_u64);
}
void Bitmask::SetFalse(SizeT row_index) {
if (buffer_ptr.get() == nullptr) {
buffer_ptr = BitmaskBuffer::Make(count_);
// Set raw pointer;
data_ptr_ = buffer_ptr->data_ptr_.get();
}
SizeT u64_index = row_index / BitmaskBuffer::UNIT_BITS;
SizeT index_in_u64 = row_index - u64_index * BitmaskBuffer::UNIT_BITS;
data_ptr_[u64_index] &= ~(((u64(1))) << index_in_u64);
}
void Bitmask::Set(SizeT row_index, bool valid) {
if (valid) {
SetTrue(row_index);
} else {
SetFalse(row_index);
}
}
void Bitmask::SetAllTrue() {
if (data_ptr_ == nullptr)
return;
buffer_ptr.reset();
data_ptr_ = nullptr;
}
void Bitmask::SetAllFalse() {
if (buffer_ptr.get() == nullptr) {
buffer_ptr = BitmaskBuffer::Make(count_);
// Set raw pointer;
data_ptr_ = buffer_ptr->data_ptr_.get();
}
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
for (SizeT i = 0; i < u64_count; ++i) {
data_ptr_[i] = 0;
}
}
SizeT Bitmask::CountTrue() const {
if (data_ptr_ == nullptr)
return count_;
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
SizeT count_true = 0;
for (SizeT i = 0; i < u64_count; ++i) {
if (data_ptr_[i] == BitmaskBuffer::UNIT_MAX) {
// All bits of u64 variable are 1.
count_true += BitmaskBuffer::UNIT_BITS;
} else {
u64 elem = data_ptr_[i];
// Count 1 of an u64 variable.
while (elem) {
elem &= (elem - 1);
++count_true;
}
}
}
return count_true;
}
void Bitmask::Merge(const Bitmask &other) {
if (other.IsAllTrue()) {
return;
}
if (this->IsAllTrue()) {
// Share the same bitmask with other.
ShallowCopy(other);
}
if (data_ptr_ == other.data_ptr_) {
// Totally same bitmask
return;
}
if (count() != other.count()) {
UnrecoverableError("Attempt to merge two bitmasks with different size.");
}
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
for (SizeT i = 0; i < u64_count; ++i) {
data_ptr_[i] &= other.data_ptr_[i];
}
}
void Bitmask::MergeOr(const Bitmask &other) {
if (this->IsAllTrue()) {
return;
}
if (other.IsAllTrue()) {
SetAllTrue();
return;
}
if (data_ptr_ == other.data_ptr_) {
// Totally same bitmask
return;
}
if (count() != other.count()) {
UnrecoverableError("Attempt to merge two bitmasks with different size.");
}
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
for (SizeT i = 0; i < u64_count; ++i) {
data_ptr_[i] |= other.data_ptr_[i];
}
}
bool Bitmask::operator==(const Bitmask &other) const {
if (count_ != other.count_)
return false;
if (data_ptr_ == other.data_ptr_)
return true;
if (data_ptr_ == nullptr || other.data_ptr_ == nullptr)
return false;
SizeT u64_count = BitmaskBuffer::UnitCount(count_);
for (SizeT i = 0; i < u64_count; ++i) {
if (data_ptr_[i] != other.data_ptr_[i])
return false;
}
return true;
}
i32 Bitmask::GetSizeInBytes() const {
// count_, IsAllTrue(), buffer_content (only if !IsAllTrue)
i32 size = IsAllTrue() ? 0 : BitmaskBuffer::UnitCount(count_) * sizeof(u64);
size += sizeof(i32) + 1;
return size;
}
void Bitmask::WriteAdv(char *&ptr) const {
bool all_true = IsAllTrue();
WriteBufAdv(ptr, (i32)count_);
WriteBufAdv(ptr, (i8)all_true);
if (!all_true) {
i32 bytes = BitmaskBuffer::UnitCount(count_) * sizeof(u64);
std::memcpy(ptr, data_ptr_, bytes);
ptr += bytes;
}
}
SharedPtr<Bitmask> Bitmask::ReadAdv(char *&ptr, i32) {
i32 count = ReadBufAdv<i32>(ptr);
auto bitmask = Bitmask::Make(count);
i8 all_true = ReadBufAdv<i8>(ptr);
if (!all_true) {
i32 bytes = BitmaskBuffer::UnitCount(count) * sizeof(u64);
bitmask->SetAllFalse();
std::memcpy(bitmask->data_ptr_, ptr, bytes);
ptr += bytes;
}
return bitmask;
}
Bitmask &Bitmask::operator=(Bitmask &&right) {
if (this != &right) {
data_ptr_ = right.data_ptr_;
buffer_ptr = std::move(right.buffer_ptr);
count_ = right.count_;
right.data_ptr_ = nullptr;
right.count_ = 0;
}
return *this;
}
} // namespace infinity