-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathColumnIndex.ts
More file actions
357 lines (301 loc) · 12.6 KB
/
Copy pathColumnIndex.ts
File metadata and controls
357 lines (301 loc) · 12.6 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {CellError, movedSimpleCellAddress, SimpleCellAddress} from '../Cell'
import {Config} from '../Config'
import {CellValueChange} from '../ContentChanges'
import {DependencyGraph} from '../DependencyGraph'
import {AddRowsTransformer} from '../dependencyTransformers/AddRowsTransformer'
import {RemoveRowsTransformer} from '../dependencyTransformers/RemoveRowsTransformer'
import {FormulaTransformer} from '../dependencyTransformers/Transformer'
import {forceNormalizeString} from '../interpreter/ArithmeticHelper'
import {
EmptyValue,
getRawValue,
RawInterpreterValue,
RawNoErrorScalarValue,
RawScalarValue
} from '../interpreter/InterpreterValue'
import {SimpleRangeValue} from '../SimpleRangeValue'
import {LazilyTransformingAstService} from '../LazilyTransformingAstService'
import {ColumnsSpan, RowsSpan} from '../Span'
import {Statistics, StatType} from '../statistics'
import {ColumnBinarySearch} from './ColumnBinarySearch'
import {AdvancedFindOptions, ColumnSearchStrategy, SearchOptions} from './SearchStrategy'
import {Maybe} from '../Maybe'
import {AbsoluteCellRange} from '../AbsoluteCellRange'
type ColumnMap = Map<RawInterpreterValue, ValueIndex>
interface ValueIndex {
version: number,
index: number[],
}
type SheetIndex = ColumnMap[]
export class ColumnIndex implements ColumnSearchStrategy {
private readonly index: Map<number, SheetIndex> = new Map()
private readonly transformingService: LazilyTransformingAstService
private readonly binarySearchStrategy: ColumnBinarySearch
constructor(
private readonly dependencyGraph: DependencyGraph,
private readonly config: Config,
private readonly stats: Statistics,
) {
this.transformingService = this.dependencyGraph.lazilyTransformingAstService
this.binarySearchStrategy = new ColumnBinarySearch(dependencyGraph)
}
public add(value: RawInterpreterValue, address: SimpleCellAddress) {
if (value === EmptyValue || value instanceof CellError) {
return
} else if (value instanceof SimpleRangeValue) {
for (const [arrayValue, cellAddress] of value.entriesFromTopLeftCorner(address)) {
this.addSingleCellValue(getRawValue(arrayValue), cellAddress)
}
} else {
this.addSingleCellValue(value, address)
}
}
public remove(value: RawInterpreterValue | undefined, address: SimpleCellAddress) {
if (value === undefined) {
return
}
if (value instanceof SimpleRangeValue) {
for (const [arrayValue, cellAddress] of value.entriesFromTopLeftCorner(address)) {
this.removeSingleValue(getRawValue(arrayValue), cellAddress)
}
} else {
this.removeSingleValue(value, address)
}
}
public change(oldValue: RawInterpreterValue | undefined, newValue: RawInterpreterValue, address: SimpleCellAddress) {
if (oldValue === newValue) {
return
}
this.remove(oldValue, address)
this.add(newValue, address)
}
public applyChanges(contentChanges: CellValueChange[]) {
for (const change of contentChanges) {
if (change.oldValue !== undefined) {
this.change(getRawValue(change.oldValue), getRawValue(change.value), change.address)
}
}
}
public moveValues(sourceRange: IterableIterator<[RawScalarValue, SimpleCellAddress]>, toRight: number, toBottom: number, toSheet: number) {
for (const [value, address] of sourceRange) {
const targetAddress = movedSimpleCellAddress(address, toSheet, toRight, toBottom)
this.remove(value, address)
this.add(value, targetAddress)
}
}
public removeValues(range: IterableIterator<[RawScalarValue, SimpleCellAddress]>): void {
for (const [value, address] of range) {
this.remove(value, address)
}
}
public find(searchKey: RawNoErrorScalarValue, rangeValue: SimpleRangeValue, { ordering, ifNoMatch, returnOccurrence }: SearchOptions): number {
if (returnOccurrence == null) {
returnOccurrence = ordering === 'none' ? 'first' : 'last'
}
const resultUsingColumnIndex = this.findUsingColumnIndex(searchKey, rangeValue, returnOccurrence)
return resultUsingColumnIndex !== undefined ? resultUsingColumnIndex : this.binarySearchStrategy.find(searchKey, rangeValue, { ordering, ifNoMatch, returnOccurrence })
}
private findUsingColumnIndex(key: RawNoErrorScalarValue, rangeValue: SimpleRangeValue, returnOccurrence: 'first' | 'last'): Maybe<number> {
const range = rangeValue.range
if (range === undefined) {
return undefined
}
this.ensureRecentData(range.sheet, range.start.col, key)
const columnMap = this.getColumnMap(range.sheet, range.start.col)
if (!columnMap) {
return -1
}
const normalizedKey = typeof key === 'string' ? forceNormalizeString(key) : key
const valueIndexForTheKey = columnMap.get(normalizedKey)
if (!valueIndexForTheKey || !valueIndexForTheKey.index || valueIndexForTheKey.index.length === 0) {
return undefined
}
const rowNumber = ColumnIndex.findRowBelongingToRange(valueIndexForTheKey, range, returnOccurrence)
return rowNumber !== undefined ? rowNumber - range.start.row : undefined
}
private static findRowBelongingToRange(valueIndex: ValueIndex, range: AbsoluteCellRange, returnOccurrence: 'first' | 'last'): Maybe<number> {
const start = range.start.row
const end = range.end.row
const positionInIndex = returnOccurrence === 'first'
? findInOrderedArray(start, valueIndex.index, 'upperBound')
: findInOrderedArray(end, valueIndex.index, 'lowerBound')
if (positionInIndex === -1) {
return undefined
}
const rowNumber = valueIndex.index[positionInIndex]
const isRowNumberBelongingToRange = rowNumber >= start && rowNumber <= end
return isRowNumberBelongingToRange ? rowNumber : undefined
}
public advancedFind(keyMatcher: (arg: RawInterpreterValue) => boolean, range: SimpleRangeValue, options: AdvancedFindOptions = { returnOccurrence: 'first' }): number {
return this.binarySearchStrategy.advancedFind(keyMatcher, range, options)
}
public addColumns(columnsSpan: ColumnsSpan) {
const sheetIndex = this.index.get(columnsSpan.sheet)
if (!sheetIndex) {
return
}
sheetIndex.splice(columnsSpan.columnStart, 0, ...Array(columnsSpan.numberOfColumns))
}
public removeColumns(columnsSpan: ColumnsSpan) {
const sheetIndex = this.index.get(columnsSpan.sheet)
if (!sheetIndex) {
return
}
sheetIndex.splice(columnsSpan.columnStart, columnsSpan.numberOfColumns)
}
public removeSheet(sheetId: number): void {
this.index.delete(sheetId)
}
/**
* Forces all ValueIndex entries to apply any pending lazy transformations,
* bringing every entry up to the current LazilyTransformingAstService version.
* Must be called before compacting LazilyTransformingAstService.
*/
public forceApplyPostponedTransformations(): void {
for (const [sheet, sheetIndex] of this.index) {
sheetIndex.forEach((columnMap, col) => {
if (!columnMap) {
return
}
for (const value of columnMap.keys()) {
this.ensureRecentData(sheet, col, value)
}
})
}
}
public getColumnMap(sheet: number, col: number): ColumnMap {
if (!this.index.has(sheet)) {
this.index.set(sheet, [])
}
const sheetMap = this.index.get(sheet)! // eslint-disable-line @typescript-eslint/no-non-null-assertion
let columnMap = sheetMap[col]
if (!columnMap) {
columnMap = new Map()
sheetMap[col] = columnMap
}
return columnMap
}
public getValueIndex(sheet: number, col: number, value: RawInterpreterValue): ValueIndex {
const columnMap = this.getColumnMap(sheet, col)
let index = this.getColumnMap(sheet, col).get(value)
if (!index) {
index = {
version: this.transformingService.version(),
index: [],
}
columnMap.set(value, index)
}
return index
}
public ensureRecentData(sheet: number, col: number, value: RawInterpreterValue) {
const valueIndex = this.getValueIndex(sheet, col, value)
const actualVersion = this.transformingService.version()
if (valueIndex.version === actualVersion) {
return
}
const relevantTransformations = this.transformingService.getTransformationsFrom(valueIndex.version, (transformation: FormulaTransformer) => {
return transformation.sheet === sheet && (transformation instanceof AddRowsTransformer || transformation instanceof RemoveRowsTransformer)
})
for (const transformation of relevantTransformations) {
if (transformation instanceof AddRowsTransformer) {
this.addRows(col, transformation.rowsSpan, value)
} else if (transformation instanceof RemoveRowsTransformer) {
this.removeRows(col, transformation.rowsSpan, value)
}
}
valueIndex.version = actualVersion
}
private addSingleCellValue(value: RawInterpreterValue, address: SimpleCellAddress) {
this.stats.measure(StatType.BUILD_COLUMN_INDEX, () => {
this.ensureRecentData(address.sheet, address.col, value)
if (typeof value === 'string') {
value = forceNormalizeString(value)
}
const valueIndex = this.getValueIndex(address.sheet, address.col, value)
ColumnIndex.addValue(valueIndex, address.row)
})
}
private removeSingleValue(value: RawInterpreterValue, address: SimpleCellAddress) {
this.stats.measure(StatType.BUILD_COLUMN_INDEX, () => {
this.ensureRecentData(address.sheet, address.col, value)
const columnMap = this.getColumnMap(address.sheet, address.col)
if (typeof value === 'string') {
value = forceNormalizeString(value)
}
const valueIndex = columnMap.get(value)
if (!valueIndex) {
return
}
const positionInIndex = findInOrderedArray(address.row, valueIndex.index)
if (positionInIndex > -1) {
valueIndex.index.splice(positionInIndex, 1)
}
if (valueIndex.index.length === 0) {
columnMap.delete(value)
}
if (columnMap.size === 0) {
delete this.index.get(address.sheet)![address.col] // eslint-disable-line @typescript-eslint/no-non-null-assertion
}
})
}
private addRows(col: number, rowsSpan: RowsSpan, value: RawInterpreterValue) {
const valueIndex = this.getValueIndex(rowsSpan.sheet, col, value)
ColumnIndex.shiftRows(valueIndex, rowsSpan.rowStart, rowsSpan.numberOfRows)
}
private removeRows(col: number, rowsSpan: RowsSpan, value: RawInterpreterValue) {
const valueIndex = this.getValueIndex(rowsSpan.sheet, col, value)
ColumnIndex.removeRowsFromValues(valueIndex, rowsSpan)
ColumnIndex.shiftRows(valueIndex, rowsSpan.rowEnd + 1, -rowsSpan.numberOfRows)
}
private static addValue(valueIndex: ValueIndex, rowNumber: number): void {
const rowIndex = findInOrderedArray(rowNumber, valueIndex.index, 'lowerBound')
const isRowNumberAlreadyInIndex = valueIndex.index[rowIndex] === rowNumber
if (!isRowNumberAlreadyInIndex) {
valueIndex.index.splice(rowIndex + 1, 0, rowNumber)
}
}
private static removeRowsFromValues(valueIndex: ValueIndex, rowsSpan: RowsSpan) {
const start = findInOrderedArray(rowsSpan.rowStart, valueIndex.index, 'upperBound')
const end = findInOrderedArray(rowsSpan.rowEnd, valueIndex.index, 'lowerBound')
const isFoundSpanValid = start > -1 && end > -1 && start <= end && valueIndex.index[start] <= rowsSpan.rowEnd
if (isFoundSpanValid) {
valueIndex.index.splice(start, end - start + 1)
}
}
private static shiftRows(valueIndex: ValueIndex, afterRow: number, numberOfRows: number) {
const positionInIndex = findInOrderedArray(afterRow, valueIndex.index, 'upperBound')
if (positionInIndex === -1) {
return
}
for (let i = positionInIndex; i < valueIndex.index.length; ++i) {
valueIndex.index[i] += numberOfRows
}
}
}
/*
* Returns:
* - index of the key, if the key exists in the array,
* - index of the lower/upper bound (depending on handlingMisses parameter) otherwise.
* Assumption: The array is ordered ascending and contains no repetitions.
*/
export function findInOrderedArray(key: number, values: number[], handlingMisses: 'lowerBound' | 'upperBound' = 'upperBound'): number {
let start = 0
let end = values.length - 1
while (start <= end) {
const center = Math.floor((start + end) / 2)
if (key > values[center]) {
start = center + 1
} else if (key < values[center]) {
end = center - 1
} else {
return center
}
}
const foundIndex = handlingMisses === 'lowerBound' ? end : start
const isIndexInRange = foundIndex >= 0 && foundIndex <= values.length
return isIndexInRange ? foundIndex : -1
}