-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdim.js
214 lines (155 loc) · 4.11 KB
/
mdim.js
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
/*
Created by: Samu Németh
Creation date: 2021
License: MIT
Contact:
E-mail: samunemeth.hu@gmail.com
Website: http://www.samunemeth.hu/
*/
/* --- 1.0 Standalone functions --- */
/*
1.1 2D array creator
fast but it can only handle 2 dimensions
*/
function make2DArray(x, y) {
arr = new Array(x);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(y);
};
return (arr);
}
/*
1.2 Multi dimensional array creator
slow but it can handle any number of dimensions
*/
function makeArray(dim) {
// check for invalid list of dimensions
if (!dim || typeof dim != 'object' || dim.constructor != Array) return undefined;
if (dim.length < 1) return undefined;
if (!dim[0] || dim[0] % 1 != 0) return undefined;
// if only 1 dimension then create simple array, else do recursion
if (dim.length == 1) {
return new Array(dim[0]);
} else {
const old = dim[0];
dim.shift();
return makeArrayHelp(new Array(old), dim);
}
}
function makeArrayHelp(arr, dim) {
// check for invalid list of dimensions
if (!dim[0] || dim[0] % 1 != 0) return undefined;
// add sub-arrays
for (let k = 0; k < arr.length; k++) {
arr[k] = new Array(dim[0]);
}
// do recursion if there are more dimensions
if (dim.length > 1) {
dim.shift();
for (let k = 0; k < arr.length; k++) {
arr[k] = makeArrayHelp(arr[k], dim);
}
}
return arr;
}
/* --- 2.0 Array operations --- */
/*
2.1 Remove element (removed)
*/
/*
2.2 Copy multidimensional array
copies a multidimensional array with the spread operator
*/
Array.prototype.copy = function () {
if (!this.length) return [];
if (this[0] && this[0].constructor == Array) {
let arr = [];
for (let i = 0; i < this.length; i++) {
arr.push(this[i].copy());
}
return arr;
}
return [...this];
}
/*
2.3 Multidimensional forEach
forEach for multidimensional arrays
*/
Array.prototype.forEachM = function (f, from = []) {
if (!this.length) return;
const evaluate = !this[0] || this[0].constructor != Array;
for (let i = 0; i < this.length; i++) {
const pos = [...from];
pos.push(i);
if (evaluate) {
f(this[i], pos);
continue;
}
this[i].forEachM(f, pos);
}
}
/*
2.4 Fill multidimensional array
for filling a multidimensional array with anything
*/
Array.prototype.fillM = function (f, from = []) {
if (!this.length) return;
const evaluate = !this[0] || this[0].constructor != Array;
const func = f && f.constructor == Function;
for (let i = 0; i < this.length; i++) {
const pos = [...from];
pos.push(i);
if (evaluate) {
if (func) {
this[i] = f(pos, this[i]);
continue;
}
this[i] = f;
continue;
}
this[i].fillM(f, pos);
}
}
/*
2.5 Dimensions of multidimensional array
for getting the dimensions of a multidimensional array
*/
Array.prototype.dim = function (prev = []) {
prev.push(this.length);
if (this[0] && this[0].constructor == Array) {
return this[0].dim(prev);
}
return prev;
}
/*
2.6 Last element of an array
for getting the last element of an array
*/
Object.defineProperty(Array.prototype, 'last', {
get: function() {
return this[this.length - 1];
}
});
// Array.prototype.last = function () {
// return this[this.length - 1];
// }
/*
2.6 Last element of a multidimensional array
for getting the last element of a multidimensional array
*/
Object.defineProperty(Array.prototype, 'lastM', {
get: function() {
const last = this.last;
if (last && last.constructor == Array) {
return last.lastM;
}
return last;
}
});
// Array.prototype.lastM = function () {
// const last = this.last();
// if (last && last.constructor == Array) {
// return last.lastM();
// }
// return last;
// }