@@ -18,7 +18,7 @@ export type DependencyQuery<Node> = (vertex: Node) => [(SimpleCellAddress | Simp
1818 * Idea for performance improvement:
1919 * - use Set<Node>[] instead of NodeId[][] for edgesSparseArray
2020 */
21- export class Graph < Node > {
21+ export class Graph < Node extends { _graphId ?: NodeId } > {
2222 /**
2323 * A sparse array. The value nodesSparseArray[n] exists if and only if node n is in the graph.
2424 * @private
@@ -32,12 +32,6 @@ export class Graph<Node> {
3232 */
3333 private edgesSparseArray : NodeId [ ] [ ] = [ ]
3434
35- /**
36- * A mapping from node to its id. The value nodesIds.get(node) exists if and only if node is in the graph.
37- * @private
38- */
39- private nodesIds : Map < Node , NodeId > = new Map ( )
40-
4135 /**
4236 * A ProcessableValue object.
4337 * @private
@@ -78,7 +72,7 @@ export class Graph<Node> {
7872 * @param node - node to check
7973 */
8074 public hasNode ( node : Node ) : boolean {
81- return this . nodesIds . has ( node )
75+ return node . _graphId !== undefined
8276 }
8377
8478 /**
@@ -137,7 +131,7 @@ export class Graph<Node> {
137131 * @param node - a node to be added
138132 */
139133 public addNodeAndReturnId ( node : Node ) : NodeId {
140- const idOfExistingNode = this . nodesIds . get ( node )
134+ const idOfExistingNode = node . _graphId
141135
142136 if ( idOfExistingNode !== undefined ) {
143137 return idOfExistingNode
@@ -148,7 +142,7 @@ export class Graph<Node> {
148142
149143 this . nodesSparseArray [ newId ] = node
150144 this . edgesSparseArray [ newId ] = [ ]
151- this . nodesIds . set ( node , newId )
145+ node . _graphId = newId
152146 return newId
153147 }
154148
@@ -183,7 +177,7 @@ export class Graph<Node> {
183177 * Removes node from graph
184178 */
185179 public removeNode ( node : Node ) : [ ( SimpleCellAddress | SimpleCellRange ) , Node ] [ ] {
186- const id = this . getNodeId ( node )
180+ const id = node . _graphId
187181
188182 if ( id === undefined ) {
189183 throw this . missingNodeError ( node )
@@ -199,7 +193,7 @@ export class Graph<Node> {
199193 delete this . nodesSparseArray [ id ]
200194 delete this . edgesSparseArray [ id ]
201195 this . infiniteRangeIds . delete ( id )
202- this . nodesIds . delete ( node )
196+ node . _graphId = undefined
203197
204198 return dependencies
205199 }
@@ -368,14 +362,14 @@ export class Graph<Node> {
368362 * Returns the internal id of a node.
369363 */
370364 public getNodeId ( node : Node ) : NodeId | undefined {
371- return this . nodesIds . get ( node )
365+ return node . _graphId
372366 }
373367
374368 /**
375369 *
376370 */
377371 private getNodeIdIfNotNumber ( node : Node | NodeId ) : NodeId | undefined {
378- return typeof node === 'number' ? node : this . nodesIds . get ( node )
372+ return typeof node === 'number' ? node : node . _graphId
379373 }
380374
381375 /**
0 commit comments