Skip to content

Commit 57e7a8e

Browse files
committed
make bitset auto-adjust
1 parent 7cf32d7 commit 57e7a8e

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

packages/node-sdk/src/summary-event.ts

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,72 @@ export enum EventType {
77
}
88

99
/**
10-
* A simple bitset implementation for efficient boolean storage
10+
* A dynamic bitset implementation for efficient boolean storage
11+
* that automatically resizes as needed
1112
*/
1213
class BitSet {
1314
private bits: Uint32Array;
15+
private _size: number;
1416

15-
constructor(size: number = 32) {
16-
// Initialize with enough 32-bit integers to store the requested bits
17-
const arraySize = Math.ceil(size / 32);
17+
constructor(initialCapacity: number = 32) {
18+
const arraySize = Math.ceil(initialCapacity / 32);
1819
this.bits = new Uint32Array(arraySize);
20+
this._size = 0;
21+
}
22+
23+
/**
24+
* Ensures the bitset has capacity for the specified position
25+
*/
26+
private ensureCapacity(position: number): void {
27+
const requiredIndex = Math.floor(position / 32);
28+
29+
if (requiredIndex >= this.bits.length) {
30+
// Calculate new size with growth factor for better amortized performance
31+
const newSize = Math.max(this.bits.length * 2, requiredIndex + 1);
32+
const newBits = new Uint32Array(newSize);
33+
newBits.set(this.bits);
34+
this.bits = newBits;
35+
}
36+
37+
// Update size if we're setting a bit beyond our current size
38+
this._size = Math.max(this._size, position + 1);
1939
}
2040

2141
/**
2242
* Sets a bit at the specified position
2343
*/
2444
set(position: number, value: boolean): void {
45+
if (position < 0) {
46+
throw new Error("Bit position cannot be negative");
47+
}
48+
2549
const index = Math.floor(position / 32);
2650
const offset = position % 32;
2751

2852
// Ensure we have enough space
29-
if (index >= this.bits.length) {
30-
const newSize = index + 1;
31-
const newBits = new Uint32Array(newSize);
32-
newBits.set(this.bits);
33-
this.bits = newBits;
53+
if (value || index < this.bits.length) {
54+
// Only resize if setting true or within current capacity
55+
this.ensureCapacity(position);
3456
}
3557

3658
if (value) {
3759
// Set the bit
3860
this.bits[index] |= 1 << offset;
39-
} else {
40-
// Clear the bit
61+
} else if (index < this.bits.length) {
62+
// Clear the bit if within range
4163
this.bits[index] &= ~(1 << offset);
4264
}
65+
// If out of range and setting to false, we do nothing as bits default to 0
4366
}
4467

4568
/**
4669
* Gets the bit at the specified position
4770
*/
4871
get(position: number): boolean {
72+
if (position < 0 || position >= this._size) {
73+
return false;
74+
}
75+
4976
const index = Math.floor(position / 32);
5077
const offset = position % 32;
5178

@@ -62,6 +89,28 @@ class BitSet {
6289
isSet(position: number): boolean {
6390
return this.get(position);
6491
}
92+
93+
/**
94+
* Gets the current capacity of the bitset
95+
*/
96+
get capacity(): number {
97+
return this.bits.length * 32;
98+
}
99+
100+
/**
101+
* Gets the current logical size of the bitset
102+
*/
103+
get size(): number {
104+
return this._size;
105+
}
106+
107+
/**
108+
* Clear all bits in the bitset
109+
*/
110+
clear(): void {
111+
this.bits.fill(0);
112+
this._size = 0;
113+
}
65114
}
66115

67116
/**

0 commit comments

Comments
 (0)