Skip to content

Commit fcb29f0

Browse files
Krizzsequba
andauthored
Feat/remove nodesids map (#1597)
* remove nodeids map and store node id directly * add _graphId property --------- Co-authored-by: Kuba Sekowski <[email protected]>
1 parent e8f338c commit fcb29f0

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

src/DependencyGraph/EmptyCellVertex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {EmptyValue, EmptyValueType} from '../interpreter/InterpreterValue'
99
* Represents singleton vertex bound to all empty cells
1010
*/
1111
export class EmptyCellVertex {
12+
public _graphId?: number
13+
1214
constructor() {}
1315

1416
/**

src/DependencyGraph/FormulaVertex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {Ast} from '../parser'
1616
import {ColumnsSpan, RowsSpan} from '../Span'
1717

1818
export abstract class FormulaVertex {
19+
public _graphId?: number
20+
1921
protected constructor(
2022
protected formula: Ast,
2123
protected cellAddress: SimpleCellAddress,

src/DependencyGraph/Graph.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/DependencyGraph/ParsingErrorVertex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {ParsingError} from '../parser/Ast'
1010
* Represents a cell that contains a parsing error.
1111
*/
1212
export class ParsingErrorVertex {
13+
public _graphId?: number
14+
1315
/**
1416
* Constructor
1517
*/

src/DependencyGraph/RangeVertex.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type CriterionCache = Map<string, [any, CriterionLambda[]]>
1616
* Represents vertex bound to range
1717
*/
1818
export class RangeVertex {
19+
public _graphId?: number
1920
public bruteForce: boolean
2021
/** Cache for associative aggregate functions. */
2122
private functionCache: Map<string, any>

src/DependencyGraph/ValueCellVertex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface RawAndParsedValue {
1818
* Represents vertex which keeps static cell value
1919
*/
2020
export class ValueCellVertex {
21+
public _graphId?: number
22+
2123
/** Static cell value. */
2224
constructor(private parsedValue: ValueCellVertexValue, private rawValue: RawCellContent) {
2325
}

0 commit comments

Comments
 (0)