Files
grokking_algorithms/09_dijkstras_algorithm/ts/iterable_graph.ts
2024-12-07 07:34:16 -06:00

27 lines
553 B
TypeScript

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 };
}
},
};
}
}