Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { verbose } from "./main.ts";

export function measureTime() {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const original: (...args: Array<unknown>) => unknown = descriptor.value;

const timeLabel: string = `${target.constructor.name}.${propertyKey}`;
descriptor.value = function (...args: Array<unknown>) {
verbose && console.time(timeLabel);
const value: unknown = original.apply(this, args);
verbose && console.timeEnd(timeLabel);

return value;
};
};
}
12 changes: 9 additions & 3 deletions graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { format } from "duration";
import { colors } from "cliffy";
import { measureTime } from "./decorators.ts";


export class Graph {
size: number;
nodes: number[][];
Expand Down Expand Up @@ -32,18 +35,16 @@ export class Graph {
this.verbose && this.logTime("Graph built in", start, end);
}


get subGraphs() {
const visited = new Uint8Array(this.size);
const subgraphs: number[][] = [];
const start = performance.now();
for (let i = 0; i < this.size; i++) {
if (!visited[i]) {
visited[i] = 1;
subgraphs.push(this.dfs(i, visited));
}
}
const end = performance.now();
this.verbose && this.logTime("Subgraphs created in", start, end);
return subgraphs;
}

Expand All @@ -70,4 +71,9 @@ export class Graph {
)}`
);
}

@measureTime()
printTimeAfterFunction(){
return this.subGraphs;
}
}
3 changes: 3 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ while (true) {
Select.separator("---------"),
{ name: "Size of subgraphs", value: "subgraphsSize" },
{ name: "List of subgraphs", value: "subgraphsNodes" },
{ name: "Measure Time with decorator", value: "measureTime"}
],
});
switch (command) {
Expand All @@ -43,5 +44,7 @@ while (true) {
case "subgraphsNodes":
console.log(graph.subGraphs);
break;
case "measureTime":
graph.printTimeAfterFunction();
}
}