-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.ts
81 lines (65 loc) · 1.87 KB
/
bitmap.ts
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
// u8 -> number of 1s LUT
const bitCountLUT = [...Array(256)].map((r = 0, a) => {
for (; a; a >>= 1) r += 1 & a;
return r;
});
function countOnes(array: Uint8Array) {
let count = 0;
for (let i = 0; i < array.length; i++) {
count += bitCountLUT[array[i]];
}
return count;
}
type BitmapChangeCallback = (min: number, max: number) => void;
export class Bitmap {
checkedCount = 0;
public bytes: Uint8Array;
private subscribers: Set<BitmapChangeCallback> = new Set();
constructor(public bitCount: number) {
const byteCount = Math.ceil(bitCount / 8);
this.bytes = new Uint8Array(byteCount);
}
get(index: number) {
const byteIndex = index >> 3;
const bitIndex = index & 7;
return (this.bytes[byteIndex] & (1 << bitIndex)) !== 0;
}
set(index: number, value: boolean) {
const byteIndex = index >> 3;
const bitIndex = index & 7;
let b = this.bytes[byteIndex];
this.checkedCount -= bitCountLUT[b];
b &= ~(1 << bitIndex);
if (value) {
b |= 1 << bitIndex;
}
this.bytes[byteIndex] = b;
this.checkedCount += bitCountLUT[b];
}
fullStateUpdate(bitmap: Uint8Array) {
this.bytes.set(bitmap);
this.checkedCount = countOnes(bitmap);
this.fireChange();
}
partialStateUpdate(offset: number, chunk: Uint8Array) {
for (let i = 0; i < chunk.length; i++) {
const byteIndex = offset + i;
const b = this.bytes[byteIndex];
this.checkedCount -= bitCountLUT[b];
this.bytes[byteIndex] = chunk[i];
this.checkedCount += bitCountLUT[chunk[i]];
}
this.fireChange(offset * 8, (offset + chunk.length) * 8);
}
fireChange(rangeMin: number = 0, rangeMax: number = this.bitCount) {
for (const subscriber of this.subscribers) {
subscriber(rangeMin, rangeMax);
}
}
subscribeToChanges(callback: BitmapChangeCallback) {
this.subscribers.add(callback);
}
unsubscribeFromChanges(callback: BitmapChangeCallback) {
this.subscribers.delete(callback);
}
}