Skip to content

Commit

Permalink
typo import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev committed Aug 30, 2024
1 parent c5a10ed commit b31fd1d
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { doesNotMatch } from "node:assert";
import * as crypto from "node:crypto";
import { BitSet } from "./bitset.js";

Expand Down Expand Up @@ -179,7 +178,10 @@ export class HashGraph<T> {
return result;
}

linearizeOperations(): Operation<T>[] {
linearizeOperations(
areCausallyRelatedFn: (v1: Hash, v2: Hash) => boolean = this
.areCausallyRelated,
): Operation<T>[] {
const order = this.topologicalSort();
const result: Operation<T>[] = [];
let i = 0;
Expand All @@ -192,7 +194,7 @@ export class HashGraph<T> {
while (j < order.length) {
const moving = order[j];

if (!this.areCausallyRelated(anchor, moving)) {
if (!areCausallyRelatedFn(anchor, moving)) {
const v1 = this.vertices.get(anchor);
const v2 = this.vertices.get(moving);
let action: ActionType;
Expand Down Expand Up @@ -251,6 +253,46 @@ export class HashGraph<T> {
return test1 || test2;
}

areCausallyRelatedOld(hash1: Hash, hash2: Hash): boolean {
const visited = new Set<Hash>();
const stack = [hash1];

while (stack.length > 0) {
const current = stack.pop();
if (current === hash2) return true;
if (current === undefined) continue;
visited.add(current);

const vertex = this.vertices.get(current);
if (!vertex) continue;
for (const dep of vertex.dependencies) {
if (!visited.has(dep)) {
stack.push(dep);
}
}
}

visited.clear();
stack.push(hash2);

while (stack.length > 0) {
const current = stack.pop();
if (current === hash1) return true;
if (current === undefined) continue;
visited.add(current);

const vertex = this.vertices.get(current);
if (!vertex) continue;
for (const dep of vertex.dependencies) {
if (!visited.has(dep)) {
stack.push(dep);
}
}
}

return false;
}

// Time complexity: O(1), Space complexity: O(1)
getFrontier(): Hash[] {
return Array.from(this.frontier);
Expand Down

0 comments on commit b31fd1d

Please sign in to comment.