Skip to content

Commit

Permalink
impr: use object as a set (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev authored Dec 9, 2024
1 parent 392479d commit 65a7983
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
15 changes: 8 additions & 7 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -195,11 +196,11 @@ export class HashGraph {

depthFirstSearch(
origin: Hash,
subgraph: Set<Hash>,
subgraph: ObjectSet<Hash>,
visited: Map<Hash, number> = 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) => {
Expand Down Expand Up @@ -234,7 +235,7 @@ export class HashGraph {
topologicalSort(
updateBitsets = false,
origin: Hash = HashGraph.rootHash,
subgraph: Set<Hash> = new Set(this.vertices.keys()),
subgraph: ObjectSet<Hash> = new ObjectSet(this.vertices.keys()),
): Hash[] {
const result = this.depthFirstSearch(origin, subgraph);
result.reverse();
Expand Down Expand Up @@ -270,7 +271,7 @@ export class HashGraph {

linearizeOperations(
origin: Hash = HashGraph.rootHash,
subgraph: Set<string> = new Set(this.vertices.keys()),
subgraph: ObjectSet<string> = new ObjectSet(this.vertices.keys()),
): Operation[] {
switch (this.semanticsType) {
case SemanticsType.pair:
Expand All @@ -284,7 +285,7 @@ export class HashGraph {

lowestCommonAncestorMultipleVertices(
hashes: Hash[],
visited: Set<Hash>,
visited: ObjectSet<Hash>,
): Hash {
if (hashes.length === 0) {
throw new Error("Vertex dependencies are empty");
Expand Down Expand Up @@ -317,7 +318,7 @@ export class HashGraph {
private lowestCommonAncestorPairVertices(
hash1: Hash,
hash2: Hash,
visited: Set<Hash>,
visited: ObjectSet<Hash>,
targetVertices: Hash[],
): Hash | undefined {
let currentHash1 = hash1;
Expand Down Expand Up @@ -445,7 +446,7 @@ export class HashGraph {
const visited = new Map<Hash, number>();
this.depthFirstSearch(
HashGraph.rootHash,
new Set(this.vertices.keys()),
new ObjectSet(this.vertices.keys()),
visited,
);
for (const vertex of this.getAllVertices()) {
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -178,7 +179,7 @@ export class DRPObject implements IDRPObject {
vertexOperation?: Operation | undefined,
// biome-ignore lint: values can be anything
): Map<string, any> {
const subgraph: Set<Hash> = new Set();
const subgraph: ObjectSet<Hash> = new ObjectSet();
const lca =
vertexDependencies.length === 1
? vertexDependencies[0]
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/linearize/multipleSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
subgraph: ObjectSet<string>,
): Operation[] {
const order = hashGraph.topologicalSort(true, origin, subgraph);
const dropped = new Array(order.length).fill(false);
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
subgraph: ObjectSet<string>,
): Operation[] {
const order: Hash[] = hashGraph.topologicalSort(true, origin, subgraph);
const dropped = new Array(order.length).fill(false);
Expand Down
26 changes: 26 additions & 0 deletions packages/object/src/utils/objectSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export class ObjectSet<T extends string | number | symbol> {
set: { [key in T]: boolean };

constructor(iterable: Iterable<T> = []) {
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<T> {
return Object.keys(this.set) as Array<T>;
}
}

0 comments on commit 65a7983

Please sign in to comment.