-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return graph from collect function
- Loading branch information
1 parent
b8262e7
commit e97b74c
Showing
3 changed files
with
84 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export class Graph { | ||
vertexes = new Map<string, { to: Set<string>; from: Set<string> }>(); | ||
|
||
private addVertex(vertex: string) { | ||
const selected = this.vertexes.get(vertex); | ||
if (selected) { | ||
return selected; | ||
} | ||
|
||
const created = { to: new Set<string>(), from: new Set<string>() }; | ||
|
||
this.vertexes.set(vertex, created); | ||
|
||
return created; | ||
} | ||
|
||
deleteVertex(vertex: string) { | ||
const selected = this.vertexes.get(vertex); | ||
|
||
if (!selected) { | ||
return; | ||
} | ||
|
||
for (const v of selected.to) { | ||
const target = this.vertexes.get(v); | ||
|
||
if (!target) { | ||
continue; | ||
} | ||
|
||
target.from.delete(vertex); | ||
|
||
if (target.from.size === 0 && target.to.size === 0) { | ||
this.vertexes.delete(v); | ||
} | ||
} | ||
|
||
for (const v of selected.from) { | ||
const target = this.vertexes.get(v); | ||
|
||
if (!target) { | ||
continue; | ||
} | ||
|
||
target.to.delete(vertex); | ||
|
||
if (target.from.size === 0 && target.to.size === 0) { | ||
this.vertexes.delete(v); | ||
} | ||
} | ||
|
||
this.vertexes.delete(vertex); | ||
} | ||
|
||
addEdge(source: string, destination: string): void { | ||
const s = this.addVertex(source); | ||
const d = this.addVertex(destination); | ||
s.to.add(destination); | ||
d.from.add(source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters