-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorus.ts
178 lines (151 loc) · 6.14 KB
/
torus.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
export class Torus {
private torus: any[]
private _dimension: number
private _dimensions: number[]
constructor(firstDimension: number, ...otherDimensions: number[]) {
this.validateDimensions(firstDimension, ...otherDimensions)
this._dimension = 1 + otherDimensions.length
this._dimensions = new Array()
this._dimensions.push(firstDimension, ...otherDimensions)
let otherDimensionsProduct = 1
for (let i = 0; i < otherDimensions.length; i++) {
otherDimensionsProduct *= otherDimensions[i]
}
this.torus = new Array(firstDimension * otherDimensionsProduct)
}
public get dimension() {
return this._dimension
}
public get dimensions() {
return this._dimensions
}
private validateDimensions(...dimensions: number[]) {
for (let i = 0; i < dimensions.length; i++) {
if (dimensions[i] <= 0) {
throw TypeError(`Arguments must be larger than 0`);
}
}
}
private validateNumberOfCoordinates(coordinates: number[]) {
if (coordinates.length !== this._dimension) {
throw TypeError(`${this._dimension} arguments required (dimension of torus), but ${coordinates.length} passed`);
}
}
private validateDimensionIndex(indexOfDimension: number) {
if (indexOfDimension >= this.dimension || indexOfDimension < 0) {
throw RangeError(`Index of dimension must be in range: [0 .. ${this._dimension - 1}] (dimension indexes of torus)`);
}
}
private validateDimensionSize(size: number) {
if (size <= 1) {
throw TypeError(`Dimension size must be larger than 1`);
}
}
private normalizeValue(value: number, modulo: number) {
value = ((value % modulo) + modulo) % modulo;
return value
}
private normalizeCoordinates(coordinates: number[]) {
for (let i = 0; i < this._dimension; i++) {
coordinates[i] = this.normalizeValue(coordinates[i], this._dimensions[i])
}
return coordinates
}
private coordinatesToIndex(coordinates: number[]) {
coordinates = this.normalizeCoordinates(coordinates)
let index = 0
for (let i = 0; i < this._dimension; i++) {
let dimensionsProduct = 1
for (let j = 1 + i; j < this._dimension; j++) {
dimensionsProduct *= this.dimensions[j]
}
index += (coordinates[i]) * dimensionsProduct
}
return index
}
private buildArray(values: any[], dimension: number, dimensions: number[]) {
let currentArray = new Array(dimension)
for (let i = 0; i < dimension; i++) {
if (dimensions.length == 0) {
currentArray[i] = values[i]
}
else {
const subValuesLength = values.length / dimension
currentArray[i] = this.buildArray(
values.slice(i * subValuesLength, (i + 1) * subValuesLength),
dimensions[0],
dimensions.slice(1)
)
}
}
return currentArray
}
private indexToCoordinates(index: number) {
let coordinates: number[] = []
for (let i = 0; i < this._dimension; i++) {
let dimensionsProduct = 1
for (let j = 1 + i; j < this._dimension; j++) {
dimensionsProduct *= this._dimensions[j]
}
const quotient = Math.floor(index / dimensionsProduct)
coordinates.push(quotient)
index -= quotient * dimensionsProduct
}
return coordinates
}
at(...coordinates: number[]) {
this.validateNumberOfCoordinates(coordinates)
return this.torus[this.coordinatesToIndex(coordinates)]
}
set(value: any, ...coordinates: number[]) {
this.validateNumberOfCoordinates(coordinates)
this.torus[this.coordinatesToIndex(coordinates)] = value
}
toArray() {
return this.buildArray(this.torus, this.dimensions[0], this._dimensions.slice(1))
}
findCoordinates(predicate: (value: any, index: number, obj: any[]) => unknown, thisArg?: any) {
const findIndexResult = this.torus.findIndex(predicate)
if (findIndexResult === -1) {
return -1
}
return this.indexToCoordinates(findIndexResult)
}
shrink(indexOfDimension: number, position: number) {
this.validateDimensionIndex(indexOfDimension)
this.validateDimensionSize(this._dimensions[indexOfDimension])
const normalizedPosition = this.normalizeValue(position, this._dimensions[indexOfDimension])
let dimensionsProduct = 1
for (let j = indexOfDimension; j < this._dimension; j++) {
dimensionsProduct *= this.dimensions[j]
}
let lowerDimensionsProduct = 1
for (let j = 1 + indexOfDimension; j < this._dimension; j++) {
lowerDimensionsProduct *= this.dimensions[j]
}
let i = normalizedPosition * lowerDimensionsProduct;
while (i < this.torus.length) {
this.torus.splice(i, lowerDimensionsProduct);
i += dimensionsProduct - lowerDimensionsProduct
}
this._dimensions[indexOfDimension]--
}
expand(indexOfDimension: number, position: number, fillValue: any = undefined) {
this.validateDimensionIndex(indexOfDimension)
const normalizedPosition = this.normalizeValue(position, this._dimensions[indexOfDimension])
let dimensionsProduct = 1
for (let j = indexOfDimension; j < this._dimension; j++) {
dimensionsProduct *= this.dimensions[j]
}
let lowerDimensionsProduct = 1
for (let j = 1 + indexOfDimension; j < this._dimension; j++) {
lowerDimensionsProduct *= this.dimensions[j]
}
let i = normalizedPosition * lowerDimensionsProduct
while (i < this.torus.length) {
this.torus.splice(i, 0, ...new Array(lowerDimensionsProduct).fill(fillValue));
i += dimensionsProduct + lowerDimensionsProduct
}
this._dimensions[indexOfDimension]++
}
}