|
| 1 | +import { UndirectedGraph } from "graphology"; |
| 2 | +import { bidirectional } from "graphology-shortest-path/unweighted"; |
| 3 | +import { starBoard, Graph as StarTopology, starFrequencyFromWidth, STAR_DEFAULT_FREQUENCY } from "../star"; |
| 4 | +import { IGraph } from "./IGraph"; |
| 5 | + |
| 6 | +const columnLabels = "abcdefghijklmnopqrstuvwxyz".split(""); |
| 7 | + |
| 8 | +const ringLetter = (ring: number): string => { |
| 9 | + const letter = columnLabels[ring]; |
| 10 | + if (letter === undefined) { |
| 11 | + throw new Error(`Ring index out of range: ${ring}`); |
| 12 | + } |
| 13 | + return letter; |
| 14 | +}; |
| 15 | + |
| 16 | +const parseAlgebraic = (cell: string): [number, number] => { |
| 17 | + const match = cell.match(/^([a-z]+)(\d+)$/); |
| 18 | + if (match === null) { |
| 19 | + throw new Error(`Invalid algebraic notation: ${cell}`); |
| 20 | + } |
| 21 | + const ring = columnLabels.indexOf(match[1]); |
| 22 | + if (ring < 0) { |
| 23 | + throw new Error(`Invalid ring label: ${match[1]}`); |
| 24 | + } |
| 25 | + const pos = parseInt(match[2], 10) - 1; |
| 26 | + if (isNaN(pos) || pos < 0) { |
| 27 | + throw new Error(`Invalid position: ${match[2]}`); |
| 28 | + } |
| 29 | + return [ring, pos]; |
| 30 | +}; |
| 31 | + |
| 32 | +export type StarNodeData = { |
| 33 | + id: number; |
| 34 | + ring: number; |
| 35 | + pos: number; |
| 36 | + isOuter: boolean; |
| 37 | + isBridge: boolean; |
| 38 | + isQuark: boolean; |
| 39 | + isPericell: boolean; |
| 40 | +}; |
| 41 | + |
| 42 | +export { starFrequencyFromWidth, STAR_DEFAULT_FREQUENCY }; |
| 43 | + |
| 44 | +export class StarGraph implements IGraph { |
| 45 | + public readonly frequency: number; |
| 46 | + public readonly topo: StarTopology; |
| 47 | + public graph: UndirectedGraph; |
| 48 | + private readonly vidToLabel = new Map<number, string>(); |
| 49 | + |
| 50 | + constructor(frequency: number) { |
| 51 | + this.frequency = frequency; |
| 52 | + this.topo = starBoard(frequency); |
| 53 | + this.buildLabelMaps(); |
| 54 | + this.graph = this.buildGraph(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Rings outside-in (a, b, c, …); position 1-based clockwise from the top quark. */ |
| 58 | + private buildLabelMaps(): void { |
| 59 | + for (let ring = 0; ring < this.topo.gridLayers.length; ring++) { |
| 60 | + const layer = this.topo.gridLayers[ring]; |
| 61 | + const letter = ringLetter(ring); |
| 62 | + for (let pos = 0; pos < layer.length; pos++) { |
| 63 | + const label = letter + (pos + 1).toString(); |
| 64 | + const vid = layer[pos].id; |
| 65 | + this.vidToLabel.set(vid, label); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private buildGraph(): UndirectedGraph { |
| 71 | + const g = new UndirectedGraph(); |
| 72 | + for (const vertex of this.topo.vertices) { |
| 73 | + const nodeId = this.vidToLabel.get(vertex.id); |
| 74 | + if (nodeId === undefined) { |
| 75 | + throw new Error(`Missing algebraic label for vertex ${vertex.id}`); |
| 76 | + } |
| 77 | + const [ring, pos] = parseAlgebraic(nodeId); |
| 78 | + g.addNode(nodeId, { |
| 79 | + id: vertex.id, |
| 80 | + ring, |
| 81 | + pos, |
| 82 | + isOuter: vertex.isOuter, |
| 83 | + isBridge: this.topo.bridgeIds.has(vertex.id), |
| 84 | + isQuark: this.topo.quarkIds.has(vertex.id), |
| 85 | + isPericell: this.topo.pericellIds.has(vertex.id), |
| 86 | + } as StarNodeData); |
| 87 | + } |
| 88 | + for (const edge of this.topo.edges) { |
| 89 | + const a = this.vidToLabel.get(edge.vidA); |
| 90 | + const b = this.vidToLabel.get(edge.vidB); |
| 91 | + if (a === undefined || b === undefined) { |
| 92 | + throw new Error(`Could not map edge endpoints ${edge.vidA} or ${edge.vidB} to labels.`); |
| 93 | + } |
| 94 | + g.addUndirectedEdgeWithKey(`${a}>${b}`, a, b); |
| 95 | + } |
| 96 | + return g; |
| 97 | + } |
| 98 | + |
| 99 | + /** x = ring index (0 = outer), y = clockwise position index within the ring. */ |
| 100 | + public coords2algebraic(x: number, y: number): string { |
| 101 | + return ringLetter(x) + (y + 1).toString(); |
| 102 | + } |
| 103 | + |
| 104 | + public algebraic2coords(cell: string): [number, number] { |
| 105 | + return parseAlgebraic(cell); |
| 106 | + } |
| 107 | + |
| 108 | + public listCells(ordered = false): string[] | string[][] { |
| 109 | + if (!ordered) { |
| 110 | + return this.graph.nodes(); |
| 111 | + } |
| 112 | + return this.topo.gridLayers.map((layer, ring) => |
| 113 | + layer.map((_, pos) => this.coords2algebraic(ring, pos)), |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + public neighbours(node: string): string[] { |
| 118 | + return this.graph.neighbors(node); |
| 119 | + } |
| 120 | + |
| 121 | + public path(from: string, to: string): string[] | null { |
| 122 | + return bidirectional(this.graph, from, to); |
| 123 | + } |
| 124 | +} |
0 commit comments