Skip to content

Commit

Permalink
fix: wrong initial bitset size (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 authored Jan 13, 2025
1 parent eb33885 commit ff657ad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.5.1";
export const VERSION = "0.5.2";
31 changes: 5 additions & 26 deletions packages/object/src/hashgraph/bitset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
export class BitSet {
private data: Uint32Array;

constructor(size = 1) {
// Always start with size 32
constructor(bits: number) {
const size = Math.ceil(bits / 32);
this.data = new Uint32Array(size);
}

Expand Down Expand Up @@ -42,7 +42,7 @@ export class BitSet {

// AND two bitsets of the same size
and(other: BitSet): BitSet {
const result = new BitSet(this.data.length);
const result = new BitSet(this.data.length * 32);
for (let i = 0; i < this.data.length; i++) {
result.data[i] = this.data[i] & other.data[i];
}
Expand All @@ -51,7 +51,7 @@ export class BitSet {

// OR two bitsets of the same size
or(other: BitSet): BitSet {
const result = new BitSet(this.data.length);
const result = new BitSet(this.data.length * 32);
for (let i = 0; i < this.data.length; i++) {
result.data[i] = this.data[i] | other.data[i];
}
Expand All @@ -60,7 +60,7 @@ export class BitSet {

// XOR two bitsets of the same size
xor(other: BitSet): BitSet {
const result = new BitSet(this.data.length);
const result = new BitSet(this.data.length * 32);
for (let i = 0; i < this.data.length; i++) {
result.data[i] = this.data[i] ^ other.data[i];
}
Expand All @@ -81,25 +81,4 @@ export class BitSet {
.map((int) => int.toString(2).padStart(32, "0"))
.join("");
}

findNext(index: number, bit: number): number {
let wordIndex = Math.floor((index + 1) / 32);
const bitIndex = (index + 1) % 32;
let mask = ~((1 << bitIndex) - 1);

while (wordIndex < this.data.length) {
let currentWord = this.data[wordIndex];
if (bit === 0) currentWord = ~currentWord;
currentWord &= mask;

if (currentWord !== 0) {
return wordIndex * 32 + 31 - Math.clz32(currentWord & -currentWord);
}

wordIndex++;
mask = ~0;
}

return this.data.length * 32;
}
}
4 changes: 0 additions & 4 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,6 @@ export class HashGraph {
return true;
}

findNextCausallyUnrelated(hash: Hash, start: number): number | undefined {
return this.reachablePredecessors.get(hash)?.findNext(start, 0);
}

areCausallyRelatedUsingBFS(hash1: Hash, hash2: Hash): boolean {
return (
this._areCausallyRelatedUsingBFS(hash1, hash2) ||
Expand Down
55 changes: 18 additions & 37 deletions packages/object/tests/bitset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@ describe("BitSet Test", () => {
let bitset: BitSet;

beforeEach(() => {
// Bitset of size 64
bitset = new BitSet(2);
// Bitset of size 65
bitset = new BitSet(65);
});

test("Test: Bitset data", () => {
for (let i = 0; i < 65; i++) {
bitset.set(i, true);
}
for (let i = 0; i < 65; i++) {
expect(bitset.get(i)).toBe(true);
}
for (let i = 0; i < 65; i++) {
bitset.set(i, false);
}
for (let i = 0; i < 65; i++) {
expect(bitset.get(i)).toBe(false);
}
});

test("Test: BitSet", () => {
Expand All @@ -24,7 +39,7 @@ describe("BitSet Test", () => {

bitset.clear();

let other: BitSet = new BitSet(2);
let other: BitSet = new BitSet(65);
other.set(0, true);
other = other.or(bitset);
expect(other.get(0)).toBe(true);
Expand All @@ -35,38 +50,4 @@ describe("BitSet Test", () => {
other = other.and(bitset);
expect(other.get(0)).toBe(false);
});

test("find next index of one-bit", () => {
bitset.set(5, true);
bitset.set(10, true);
bitset.set(20, true);
bitset.set(30, true);
bitset.set(40, true);

expect(bitset.findNext(0, 1)).toBe(5);
expect(bitset.findNext(5, 1)).toBe(10);
expect(bitset.findNext(10, 1)).toBe(20);
expect(bitset.findNext(20, 1)).toBe(30);
expect(bitset.findNext(30, 1)).toBe(40);
expect(bitset.findNext(40, 1)).toBe(64);
});

test("find next index of zero-bit", () => {
for (let i = 0; i < 64; i++) {
bitset.set(i, true);
}

bitset.set(5, false);
bitset.set(10, false);
bitset.set(20, false);
bitset.set(30, false);
bitset.set(40, false);

expect(bitset.findNext(0, 0)).toBe(5);
expect(bitset.findNext(5, 0)).toBe(10);
expect(bitset.findNext(10, 0)).toBe(20);
expect(bitset.findNext(20, 0)).toBe(30);
expect(bitset.findNext(30, 0)).toBe(40);
expect(bitset.findNext(40, 0)).toBe(64);
});
});
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff657ad

Please sign in to comment.