|
| 1 | +export const VACANT_EDGE_ID = -1; |
| 2 | +export const VACANT_NODE_ID = -1; |
| 3 | +export const VACANT_EDGE_LABEL = '-1'; |
| 4 | +export const VACANT_NODE_LABEL = '-1'; |
| 5 | +export const VACANT_GRAPH_ID = -1; |
| 6 | +export const AUTO_EDGE_ID = '-1'; |
| 7 | + |
| 8 | +export class Edge { |
| 9 | + public id: number; |
| 10 | + public from: number; |
| 11 | + public to: number; |
| 12 | + public label: string; |
| 13 | + |
| 14 | + constructor( |
| 15 | + id = VACANT_EDGE_ID, |
| 16 | + from = VACANT_NODE_ID, |
| 17 | + to = VACANT_NODE_ID, |
| 18 | + label = VACANT_EDGE_LABEL |
| 19 | + ) { |
| 20 | + this.id = id; |
| 21 | + this.from = from; |
| 22 | + this.to = to; |
| 23 | + this.label = label; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export class Node { |
| 28 | + public id: number; |
| 29 | + public from: number; |
| 30 | + public to: number; |
| 31 | + public label: string; |
| 32 | + public edges: Edge[]; |
| 33 | + public edgeMap: { [key: number]: Edge }; |
| 34 | + |
| 35 | + constructor(id = VACANT_NODE_ID, label = VACANT_NODE_LABEL) { |
| 36 | + this.id = id; |
| 37 | + this.label = label; |
| 38 | + this.edges = []; |
| 39 | + this.edgeMap = {}; |
| 40 | + } |
| 41 | + |
| 42 | + addEdge(edge: Edge) { |
| 43 | + this.edges.push(edge); |
| 44 | + this.edgeMap[edge.id] = edge; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export class Graph { |
| 49 | + public id: number; |
| 50 | + public from: number; |
| 51 | + public to: number; |
| 52 | + public label: string; |
| 53 | + public edgeIdAutoIncrease: boolean; |
| 54 | + public nodes: Node[]; |
| 55 | + public edges: Edge[]; |
| 56 | + public nodeMap: { [key: number]: Node }; |
| 57 | + public edgeMap: { [key: number]: Edge }; |
| 58 | + public nodeLabelMap: { [key: string]: number[] }; // key is label,value is the array of nodes' ids |
| 59 | + public edgeLabelMap: { [key: string]: Edge[] }; |
| 60 | + private counter: number; // The ID used for generating the graph, incremented automatically. |
| 61 | + public directed: boolean; |
| 62 | + |
| 63 | + constructor( |
| 64 | + id = VACANT_NODE_ID, |
| 65 | + edgeIdAutoIncrease = true, |
| 66 | + directed = false |
| 67 | + ) { |
| 68 | + this.id = id; |
| 69 | + this.edgeIdAutoIncrease = edgeIdAutoIncrease; |
| 70 | + this.edges = []; |
| 71 | + this.nodes = []; |
| 72 | + this.nodeMap = {}; |
| 73 | + this.edgeMap = {}; |
| 74 | + this.nodeLabelMap = {}; |
| 75 | + this.edgeLabelMap = {}; |
| 76 | + this.counter = 0; |
| 77 | + this.directed = directed; |
| 78 | + } |
| 79 | + |
| 80 | + getNodeNum() { |
| 81 | + return this.nodes.length; |
| 82 | + } |
| 83 | + |
| 84 | + addNode(id: number, label: string) { |
| 85 | + if (this.nodeMap[id]) return; |
| 86 | + const node = new Node(id, label); |
| 87 | + this.nodes.push(node); |
| 88 | + this.nodeMap[id] = node; |
| 89 | + if (!this.nodeLabelMap[label]) this.nodeLabelMap[label] = []; |
| 90 | + this.nodeLabelMap[label].push(id); |
| 91 | + } |
| 92 | + |
| 93 | + addEdge(id: number, from: number, to: number, label: string) { |
| 94 | + let usingId = id; |
| 95 | + if (this.edgeIdAutoIncrease || usingId === undefined) { |
| 96 | + usingId = this.counter++; |
| 97 | + } |
| 98 | + if ( |
| 99 | + this.nodeMap[from] && |
| 100 | + this.nodeMap[to] && |
| 101 | + this.nodeMap[to].edgeMap[usingId] |
| 102 | + ) { |
| 103 | + return; |
| 104 | + } |
| 105 | + const edge = new Edge(usingId, from, to, label); |
| 106 | + this.edges.push(edge); |
| 107 | + this.edgeMap[usingId] = edge; |
| 108 | + |
| 109 | + this.nodeMap[from].addEdge(edge); |
| 110 | + |
| 111 | + if (!this.edgeLabelMap[label]) this.edgeLabelMap[label] = []; |
| 112 | + this.edgeLabelMap[label].push(edge); |
| 113 | + |
| 114 | + if (!this.directed) { |
| 115 | + const rEdge = new Edge(usingId, to, from, label); |
| 116 | + this.nodeMap[to].addEdge(rEdge); |
| 117 | + this.edgeLabelMap[label].push(rEdge); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments