Add Dijkstra's algorithm to Typescript (#291)
This commit is contained in:
committed by
GitHub
parent
1d5567fe9a
commit
add60334fb
26
09_dijkstras_algorithm/ts/iterable_graph.ts
Normal file
26
09_dijkstras_algorithm/ts/iterable_graph.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export interface Graph<T> {
|
||||
[key: string]: T;
|
||||
}
|
||||
|
||||
export class GraphIterable<T> implements Iterable<string> {
|
||||
private graph: Graph<T>;
|
||||
|
||||
constructor(graph: Graph<T>) {
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
[Symbol.iterator](): Iterator<string> {
|
||||
const keys = Object.keys(this.graph);
|
||||
let index = 0;
|
||||
|
||||
return {
|
||||
next: (): IteratorResult<string> => {
|
||||
if (index < keys.length) {
|
||||
return { value: keys[index++], done: false };
|
||||
} else {
|
||||
return { value: undefined, done: true };
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user