forked from handsontable/hyperformula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.ts
304 lines (256 loc) · 7.27 KB
/
Vertex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {CellValue, CellError, SimpleCellAddress} from '../Cell'
import {CriterionLambda} from '../interpreter/Criterion'
import {Matrix} from '../Matrix'
import {Ast} from '../parser'
/**
* Represents vertex which keeps values of one or more cells
*/
export type CellVertex = FormulaCellVertex | ValueCellVertex | EmptyCellVertex | MatrixVertex
/**
* Represents any vertex
*/
export type Vertex = CellVertex | RangeVertex
export class MatrixVertex {
public static fromRange(range: AbsoluteCellRange, formula?: Ast): MatrixVertex {
return new MatrixVertex(range.start, range.width(), range.height(), formula)
}
public readonly width: number
public readonly height: number
private formula: Ast | null
private cellAddress: SimpleCellAddress
private matrix: Matrix | CellError
constructor(cellAddress: SimpleCellAddress, width: number, height: number, formula?: Ast) {
this.cellAddress = cellAddress
this.width = width
this.height = height
this.formula = formula || null
this.matrix = new Matrix([])
}
public setCellValue(matrix: CellValue) {
this.matrix = matrix as (Matrix | CellError)
}
public getCellValue() {
return this.matrix
}
public getMatrixCellValue(address: SimpleCellAddress): number | CellError {
const col = address.col - this.cellAddress.col
const row = address.row - this.cellAddress.row
if (this.matrix instanceof Matrix) {
return this.matrix.get(col, row)
} else {
return this.matrix
}
}
public setMatrixCellValue(address: SimpleCellAddress, value: number): void {
const col = address.col - this.cellAddress.col
const row = address.row - this.cellAddress.row
if (this.matrix instanceof Matrix) {
this.matrix.set(col, row, value)
}
}
public getAddress(): SimpleCellAddress {
return this.cellAddress
}
public getFormula(): Ast | null {
return this.formula
}
public isFormula(): boolean {
return this.formula !== null
}
public spansThroughSheetRow(sheet: number, row: number): boolean {
return (this.cellAddress.sheet === sheet) &&
(this.cellAddress.row <= row) &&
(row < this.cellAddress.row + this.height)
}
public addRows(sheet: number, row: number, numberOfRows: number): void {
if (this.matrix instanceof Matrix) {
this.matrix.addRows(row - this.getAddress().row, numberOfRows)
}
}
}
/**
* Represents vertex which keeps formula
*/
export class FormulaCellVertex {
/** Most recently computed value of this formula. */
private cachedCellValue: CellValue | null
/** Formula in AST format */
private formula: Ast
/** Address which this vertex represents */
private cellAddress: SimpleCellAddress
constructor(formula: Ast, cellAddress: SimpleCellAddress) {
this.formula = formula
this.cellAddress = cellAddress
this.cachedCellValue = null
}
/**
* Returns formula stored in this vertex
*/
public getFormula(): Ast {
return this.formula
}
public setFormula(formula: Ast) {
this.formula = formula
this.cachedCellValue = null
}
/**
* Returns address of the cell associated with vertex
*/
public getAddress(): SimpleCellAddress {
return this.cellAddress
}
public setAddress(address: SimpleCellAddress) {
this.cellAddress = address
}
/**
* Sets computed cell value stored in this vertex
*/
public setCellValue(cellValue: CellValue) {
this.cachedCellValue = cellValue
}
/**
* Returns cell value stored in vertex
*/
public getCellValue() {
if (this.cachedCellValue !== null) {
return this.cachedCellValue
} else {
throw Error('Value of the formula cell is not computed.')
}
}
}
/**
* Represents vertex which keeps static cell value
*/
export class ValueCellVertex {
/** Static cell value. */
private cellValue: CellValue
constructor(cellValue: CellValue) {
this.cellValue = cellValue
}
/**
* Returns cell value stored in vertex
*/
public getCellValue() {
return this.cellValue
}
/**
* Sets computed cell value stored in this vertex
*/
public setCellValue(cellValue: CellValue) {
this.cellValue = cellValue
}
}
/**
* Represents singleton vertex bound to all empty cells
*/
export class EmptyCellVertex {
/**
* Retrieves singleton
*/
public static getSingletonInstance() {
if (!EmptyCellVertex.instance) {
EmptyCellVertex.instance = new EmptyCellVertex()
}
return EmptyCellVertex.instance
}
/** Singleton instance. */
private static instance: EmptyCellVertex
/**
* Retrieves cell value bound to that singleton
*/
public getCellValue() {
return 0
}
}
/**
* Represents cache structure for one criterion
*/
export type CriterionCache = Map<string, [CellValue, CriterionLambda]>
/**
* Represents vertex bound to range
*/
export class RangeVertex {
/** Cache for associative aggregate functions. */
private functionCache: Map<string, CellValue>
/** Cache for criterion-based functions. */
private criterionFuncitonCache: Map<string, CriterionCache>
constructor(public range: AbsoluteCellRange) {
this.functionCache = new Map()
this.criterionFuncitonCache = new Map()
}
public get start() {
return this.range.start
}
public get end() {
return this.range.end
}
public get sheet() {
return this.range.start.sheet
}
/**
* Returns cached value stored for given function
*
* @param functionName - name of the function
*/
public getFunctionValue(functionName: string): CellValue | null {
return this.functionCache.get(functionName) || null
}
/**
* Stores cached value for given function
*
* @param functionName - name of the function
* @param value - cached value
*/
public setFunctionValue(functionName: string, value: CellValue) {
this.functionCache.set(functionName, value)
}
/**
* Returns cached value for given cache key and criterion text representation
*
* @param cacheKey - key to retrieve from the cache
* @param criterionString - criterion text (ex. '<=5')
*/
public getCriterionFunctionValue(cacheKey: string, criterionString: string): CellValue | null {
const values = this.getCriterionFunctionValues(cacheKey)
const value = values.get(criterionString)
return value ? value[0] : null
}
/**
* Returns all cached values stored for given criterion function
*
* @param cacheKey - key to retrieve from the cache
*/
public getCriterionFunctionValues(cacheKey: string): Map<string, [CellValue, CriterionLambda]> {
return this.criterionFuncitonCache.get(cacheKey) || new Map()
}
/**
* Stores all values for given criterion function
*
* @param cacheKey - key to store in the cache
* @param values - map with values
*/
public setCriterionFunctionValues(cacheKey: string, values: CriterionCache) {
this.criterionFuncitonCache.set(cacheKey, values)
}
/**
* Clears function cache
*/
public clearCache() {
this.functionCache.clear()
this.criterionFuncitonCache.clear()
}
/**
* Returns start of the range (it's top-left corner)
*/
public getStart(): SimpleCellAddress {
return this.start
}
/**
* Returns end of the range (it's bottom-right corner)
*/
public getEnd(): SimpleCellAddress {
return this.end
}
}