Skip to content

Commit

Permalink
impr: code readibility
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev committed Aug 30, 2024
1 parent 85d5836 commit f32fc47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
35 changes: 11 additions & 24 deletions packages/object/src/hashgraph/BitSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
export class BitSet {
private data: Uint32Array;

// (index / 32) | 0 is equivalent to Math.floor(index / 32)

constructor(size: number) {
if ((size >> 5) << 5 !== size) {
this.data = new Uint32Array((size >> 5) + 1);
} else {
this.data = new Uint32Array(size >> 5);
}
this.data = new Uint32Array(Math.ceil(size / 32));
}

set(index: number): void {
const byteIndex = index >> 5;
const bitIndex = index & 31;
const byteIndex = ~~(index / 32);
const bitIndex = index % 32;
this.data[byteIndex] |= 1 << bitIndex;
}

Expand All @@ -27,14 +25,14 @@ export class BitSet {
}

get(index: number): boolean {
const byteIndex = index >> 5;
const bitIndex = index & 31;
const byteIndex = (index / 32) | 0;
const bitIndex = index % 32;
return (this.data[byteIndex] & (1 << bitIndex)) !== 0;
}

flip(index: number): void {
const byteIndex = index >> 5;
const bitIndex = index & 31;
const byteIndex = (index / 32) | 0;
const bitIndex = index % 32;
this.data[byteIndex] ^= 1 << bitIndex;
}

Expand All @@ -56,12 +54,6 @@ export class BitSet {
return result;
}

_or(other: BitSet): void {
for (let i = 0; i < this.data.length; i++) {
this.data[i] |= other.data[i];
}
}

// XOR two bitsets of the same size
xor(other: BitSet): BitSet {
const result = new BitSet(this.size());
Expand All @@ -80,12 +72,7 @@ export class BitSet {
}

resize(size: number): void {
let newData: Uint32Array;
if ((size >> 5) << 5 !== size) {
newData = new Uint32Array((size >> 5) + 1);
} else {
newData = new Uint32Array(size >> 5);
}
const newData: Uint32Array = new Uint32Array(Math.ceil(size / 32));
const length = Math.min(this.data.length, newData.length);
for (let i = 0; i < length; i++) {
newData[i] = this.data[i];
Expand All @@ -94,7 +81,7 @@ export class BitSet {
}

size(): number {
return this.data.length << 5;
return this.data.length * 32;
}

toString(): string {
Expand Down
8 changes: 6 additions & 2 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as crypto from "node:crypto";
import { BitSet } from "./BitSet.js";
import { BitSet } from "./bitset.js";

type Hash = string;
const maxN = 1 << 16;
Expand Down Expand Up @@ -160,7 +160,11 @@ export class HashGraph<T> {
);
depReachable?.set(this.topoSortedIndex.get(dep) || 0);
if (depReachable) {
this.reachablePredecessors.get(i)?._or(depReachable);
const reachable = this.reachablePredecessors.get(i);
this.reachablePredecessors.set(
i,
reachable?.or(depReachable) || depReachable,
);
}
}
}
Expand Down

0 comments on commit f32fc47

Please sign in to comment.