-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathArrayValue.ts
179 lines (146 loc) · 4.62 KB
/
ArrayValue.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
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {ArraySize} from './ArraySize'
import {CellError} from './Cell'
import {EmptyValue, InternalScalarValue, InterpreterValue} from './interpreter/InterpreterValue'
import {SimpleRangeValue} from './SimpleRangeValue'
export interface CellArray {
size: ArraySize,
width(): number,
height(): number,
get(col: number, row: number): InternalScalarValue,
simpleRangeValue(): SimpleRangeValue | CellError,
}
export class NotComputedArray implements CellArray {
constructor(public readonly size: ArraySize) {
}
public width(): number {
return this.size.width
}
public height(): number {
return this.size.height
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public get(col: number, row: number): number {
throw Error('Array not computed yet.')
}
simpleRangeValue(): SimpleRangeValue {
throw Error('Array not computed yet.')
}
}
export class ArrayValue implements CellArray {
public size: ArraySize
private readonly array: InternalScalarValue[][]
constructor(array: InternalScalarValue[][]) {
this.size = new ArraySize(array.length > 0 ? array[0].length : 0, array.length)
this.array = array
if (this.size.width <= 0 || this.size.height <= 0) {
throw Error('Incorrect array size')
}
}
static fromInterpreterValue(value: InterpreterValue) {
if (value instanceof SimpleRangeValue) {
return new ArrayValue(value.data)
} else {
return new ArrayValue([[value]])
}
}
simpleRangeValue(): SimpleRangeValue {
return SimpleRangeValue.onlyValues(this.array)
}
public addRows(aboveRow: number, numberOfRows: number) {
this.array.splice(aboveRow, 0, ...this.nullArrays(numberOfRows, this.width()))
this.size.height += numberOfRows
}
public addColumns(aboveColumn: number, numberOfColumns: number) {
for (let i = 0; i < this.height(); i++) {
this.array[i].splice(aboveColumn, 0, ...new Array(numberOfColumns).fill(EmptyValue))
}
this.size.width += numberOfColumns
}
public removeRows(startRow: number, endRow: number) {
if (this.outOfBound(0, startRow) || this.outOfBound(0, endRow)) {
throw Error('Array index out of bound')
}
const numberOfRows = endRow - startRow + 1
this.array.splice(startRow, numberOfRows)
this.size.height -= numberOfRows
}
public removeColumns(leftmostColumn: number, rightmostColumn: number) {
if (this.outOfBound(leftmostColumn, 0) || this.outOfBound(rightmostColumn, 0)) {
throw Error('Array index out of bound')
}
const numberOfColumns = rightmostColumn - leftmostColumn + 1
for (const row of this.array) {
row.splice(leftmostColumn, numberOfColumns)
}
this.size.width -= numberOfColumns
}
public nullArrays(count: number, size: number) {
const result = []
for (let i = 0; i < count; ++i) {
result.push(new Array(size).fill(EmptyValue))
}
return result
}
public get(col: number, row: number): InternalScalarValue {
if (this.outOfBound(col, row)) {
throw Error('Array index out of bound')
}
return this.array[row][col]
}
public set(col: number, row: number, value: number): void {
if (this.outOfBound(col, row)) {
throw Error('Array index out of bound')
}
this.array[row][col] = value
}
public width(): number {
return this.size.width
}
public height(): number {
return this.size.height
}
public raw(): InternalScalarValue[][] {
return this.array
}
public resize(newSize: ArraySize) {
if (this.height() < newSize.height && isFinite(newSize.height)) {
this.addRows(this.height(), newSize.height - this.height())
}
if (this.height() > newSize.height) {
throw Error('Resizing to smaller array')
}
if (this.width() < newSize.width && isFinite(newSize.width)) {
this.addColumns(this.width(), newSize.width - this.width())
}
if (this.width() > newSize.width) {
throw Error('Resizing to smaller array')
}
}
private outOfBound(col: number, row: number): boolean {
return col < 0 || row < 0 || row > this.size.height - 1 || col > this.size.width - 1
}
}
export class ErroredArray implements CellArray {
constructor(
private readonly error: CellError,
public readonly size: ArraySize,
) {
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public get(col: number, row: number): CellError {
return this.error
}
public width(): number {
return this.size.width
}
public height(): number {
return this.size.height
}
simpleRangeValue(): CellError {
return this.error
}
}