From 65a79835ccee663b24f3d04e44ce80452ed5678e Mon Sep 17 00:00:00 2001 From: Jan Lewandowski <62848215+JanLewDev@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:42:00 +0100 Subject: [PATCH] impr: use object as a set (#280) --- packages/object/src/hashgraph/index.ts | 15 ++++++----- packages/object/src/index.ts | 3 ++- .../object/src/linearize/multipleSemantics.ts | 3 ++- .../object/src/linearize/pairSemantics.ts | 3 ++- packages/object/src/utils/objectSet.ts | 26 +++++++++++++++++++ 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 packages/object/src/utils/objectSet.ts diff --git a/packages/object/src/hashgraph/index.ts b/packages/object/src/hashgraph/index.ts index 0dd3ad97..1f3fbc7f 100644 --- a/packages/object/src/hashgraph/index.ts +++ b/packages/object/src/hashgraph/index.ts @@ -6,6 +6,7 @@ import type { Vertex_Operation as Operation, Vertex, } from "../proto/drp/object/v1/object_pb.js"; +import { ObjectSet } from "../utils/objectSet.js"; import { BitSet } from "./bitset.js"; // Reexporting the Vertex and Operation types from the protobuf file @@ -195,11 +196,11 @@ export class HashGraph { depthFirstSearch( origin: Hash, - subgraph: Set, + subgraph: ObjectSet, visited: Map = new Map(), ): Hash[] { const result: Hash[] = []; - for (const hash of subgraph) { + for (const hash of subgraph.entries()) { visited.set(hash, DepthFirstSearchState.UNVISITED); } const visit = (hash: Hash) => { @@ -234,7 +235,7 @@ export class HashGraph { topologicalSort( updateBitsets = false, origin: Hash = HashGraph.rootHash, - subgraph: Set = new Set(this.vertices.keys()), + subgraph: ObjectSet = new ObjectSet(this.vertices.keys()), ): Hash[] { const result = this.depthFirstSearch(origin, subgraph); result.reverse(); @@ -270,7 +271,7 @@ export class HashGraph { linearizeOperations( origin: Hash = HashGraph.rootHash, - subgraph: Set = new Set(this.vertices.keys()), + subgraph: ObjectSet = new ObjectSet(this.vertices.keys()), ): Operation[] { switch (this.semanticsType) { case SemanticsType.pair: @@ -284,7 +285,7 @@ export class HashGraph { lowestCommonAncestorMultipleVertices( hashes: Hash[], - visited: Set, + visited: ObjectSet, ): Hash { if (hashes.length === 0) { throw new Error("Vertex dependencies are empty"); @@ -317,7 +318,7 @@ export class HashGraph { private lowestCommonAncestorPairVertices( hash1: Hash, hash2: Hash, - visited: Set, + visited: ObjectSet, targetVertices: Hash[], ): Hash | undefined { let currentHash1 = hash1; @@ -445,7 +446,7 @@ export class HashGraph { const visited = new Map(); this.depthFirstSearch( HashGraph.rootHash, - new Set(this.vertices.keys()), + new ObjectSet(this.vertices.keys()), visited, ); for (const vertex of this.getAllVertices()) { diff --git a/packages/object/src/index.ts b/packages/object/src/index.ts index 5361caa2..436215e8 100644 --- a/packages/object/src/index.ts +++ b/packages/object/src/index.ts @@ -9,6 +9,7 @@ import { type Vertex, } from "./hashgraph/index.js"; import * as ObjectPb from "./proto/drp/object/v1/object_pb.js"; +import { ObjectSet } from "./utils/objectSet.js"; export * as ObjectPb from "./proto/drp/object/v1/object_pb.js"; export * from "./hashgraph/index.js"; @@ -178,7 +179,7 @@ export class DRPObject implements IDRPObject { vertexOperation?: Operation | undefined, // biome-ignore lint: values can be anything ): Map { - const subgraph: Set = new Set(); + const subgraph: ObjectSet = new ObjectSet(); const lca = vertexDependencies.length === 1 ? vertexDependencies[0] diff --git a/packages/object/src/linearize/multipleSemantics.ts b/packages/object/src/linearize/multipleSemantics.ts index 818bed3c..b0b58fb1 100644 --- a/packages/object/src/linearize/multipleSemantics.ts +++ b/packages/object/src/linearize/multipleSemantics.ts @@ -5,11 +5,12 @@ import { type Operation, type Vertex, } from "../hashgraph/index.js"; +import type { ObjectSet } from "../utils/objectSet.js"; export function linearizeMultipleSemantics( hashGraph: HashGraph, origin: Hash, - subgraph: Set, + subgraph: ObjectSet, ): Operation[] { const order = hashGraph.topologicalSort(true, origin, subgraph); const dropped = new Array(order.length).fill(false); diff --git a/packages/object/src/linearize/pairSemantics.ts b/packages/object/src/linearize/pairSemantics.ts index e1ea5c4b..5d7892c5 100644 --- a/packages/object/src/linearize/pairSemantics.ts +++ b/packages/object/src/linearize/pairSemantics.ts @@ -4,11 +4,12 @@ import { type HashGraph, type Operation, } from "../hashgraph/index.js"; +import type { ObjectSet } from "../utils/objectSet.js"; export function linearizePairSemantics( hashGraph: HashGraph, origin: Hash, - subgraph: Set, + subgraph: ObjectSet, ): Operation[] { const order: Hash[] = hashGraph.topologicalSort(true, origin, subgraph); const dropped = new Array(order.length).fill(false); diff --git a/packages/object/src/utils/objectSet.ts b/packages/object/src/utils/objectSet.ts new file mode 100644 index 00000000..cd11095b --- /dev/null +++ b/packages/object/src/utils/objectSet.ts @@ -0,0 +1,26 @@ +export class ObjectSet { + set: { [key in T]: boolean }; + + constructor(iterable: Iterable = []) { + this.set = {} as { [key in T]: boolean }; + for (const item of iterable) { + this.set[item] = true; + } + } + + add(item: T): void { + this.set[item] = true; + } + + delete(item: T): void { + delete this.set[item]; + } + + has(item: T): boolean { + return this.set[item] === true; + } + + entries(): Array { + return Object.keys(this.set) as Array; + } +}