1 | 'use strict'
|
2 |
|
3 | import number from './number'
|
4 | import string from './string'
|
5 |
|
6 | import DimensionError from '../error/DimensionError'
|
7 | import IndexError from '../error/IndexError'
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function size (x) {
|
17 | let s = []
|
18 |
|
19 | while (Array.isArray(x)) {
|
20 | s.push(x.length)
|
21 | x = x[0]
|
22 | }
|
23 |
|
24 | return s
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function _validate (array, size, dim) {
|
37 | let i
|
38 | const len = array.length
|
39 |
|
40 | if (len !== size[dim]) {
|
41 | throw new DimensionError(len, size[dim])
|
42 | }
|
43 |
|
44 | if (dim < size.length - 1) {
|
45 |
|
46 | const dimNext = dim + 1
|
47 | for (i = 0; i < len; i++) {
|
48 | const child = array[i]
|
49 | if (!Array.isArray(child)) {
|
50 | throw new DimensionError(size.length - 1, size.length, '<')
|
51 | }
|
52 | _validate(array[i], size, dimNext)
|
53 | }
|
54 | } else {
|
55 |
|
56 | for (i = 0; i < len; i++) {
|
57 | if (Array.isArray(array[i])) {
|
58 | throw new DimensionError(size.length + 1, size.length, '>')
|
59 | }
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | export function validate (array, size) {
|
72 | const isScalar = (size.length === 0)
|
73 | if (isScalar) {
|
74 |
|
75 | if (Array.isArray(array)) {
|
76 | throw new DimensionError(array.length, 0)
|
77 | }
|
78 | } else {
|
79 |
|
80 | _validate(array, size, 0)
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export function validateIndex (index, length) {
|
91 | if (!number.isNumber(index) || !number.isInteger(index)) {
|
92 | throw new TypeError('Index must be an integer (value: ' + index + ')')
|
93 | }
|
94 | if (index < 0 || (typeof length === 'number' && index >= length)) {
|
95 | throw new IndexError(index, length)
|
96 | }
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export function resize (array, size, defaultValue) {
|
110 |
|
111 |
|
112 |
|
113 | if (!Array.isArray(array) || !Array.isArray(size)) {
|
114 | throw new TypeError('Array expected')
|
115 | }
|
116 | if (size.length === 0) {
|
117 | throw new Error('Resizing to scalar is not supported')
|
118 | }
|
119 |
|
120 |
|
121 | size.forEach(function (value) {
|
122 | if (!number.isNumber(value) || !number.isInteger(value) || value < 0) {
|
123 | throw new TypeError('Invalid size, must contain positive integers ' +
|
124 | '(size: ' + string.format(size) + ')')
|
125 | }
|
126 | })
|
127 |
|
128 |
|
129 | const _defaultValue = (defaultValue !== undefined) ? defaultValue : 0
|
130 | _resize(array, size, 0, _defaultValue)
|
131 |
|
132 | return array
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function _resize (array, size, dim, defaultValue) {
|
145 | let i
|
146 | let elem
|
147 | const oldLen = array.length
|
148 | const newLen = size[dim]
|
149 | const minLen = Math.min(oldLen, newLen)
|
150 |
|
151 |
|
152 | array.length = newLen
|
153 |
|
154 | if (dim < size.length - 1) {
|
155 |
|
156 | const dimNext = dim + 1
|
157 |
|
158 |
|
159 | for (i = 0; i < minLen; i++) {
|
160 |
|
161 | elem = array[i]
|
162 | if (!Array.isArray(elem)) {
|
163 | elem = [elem]
|
164 | array[i] = elem
|
165 | }
|
166 | _resize(elem, size, dimNext, defaultValue)
|
167 | }
|
168 |
|
169 |
|
170 | for (i = minLen; i < newLen; i++) {
|
171 |
|
172 | elem = []
|
173 | array[i] = elem
|
174 |
|
175 |
|
176 | _resize(elem, size, dimNext, defaultValue)
|
177 | }
|
178 | } else {
|
179 |
|
180 |
|
181 |
|
182 | for (i = 0; i < minLen; i++) {
|
183 | while (Array.isArray(array[i])) {
|
184 | array[i] = array[i][0]
|
185 | }
|
186 | }
|
187 |
|
188 |
|
189 | for (i = minLen; i < newLen; i++) {
|
190 | array[i] = defaultValue
|
191 | }
|
192 | }
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | export function reshape (array, sizes) {
|
206 | const flatArray = flatten(array)
|
207 | let newArray
|
208 |
|
209 | function product (arr) {
|
210 | return arr.reduce((prev, curr) => prev * curr)
|
211 | }
|
212 |
|
213 | if (!Array.isArray(array) || !Array.isArray(sizes)) {
|
214 | throw new TypeError('Array expected')
|
215 | }
|
216 |
|
217 | if (sizes.length === 0) {
|
218 | throw new DimensionError(0, product(size(array)), '!=')
|
219 | }
|
220 |
|
221 | var totalSize = 1
|
222 | for (var sizeIndex = 0; sizeIndex < sizes.length; sizeIndex++) {
|
223 | totalSize *= sizes[sizeIndex]
|
224 | }
|
225 |
|
226 | if (flatArray.length !== totalSize) {
|
227 | throw new DimensionError(
|
228 | product(sizes),
|
229 | product(size(array)),
|
230 | '!='
|
231 | )
|
232 | }
|
233 |
|
234 | try {
|
235 | newArray = _reshape(flatArray, sizes)
|
236 | } catch (e) {
|
237 | if (e instanceof DimensionError) {
|
238 | throw new DimensionError(
|
239 | product(sizes),
|
240 | product(size(array)),
|
241 | '!='
|
242 | )
|
243 | }
|
244 | throw e
|
245 | }
|
246 |
|
247 | return newArray
|
248 | }
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | function _reshape (array, sizes) {
|
259 |
|
260 | var tmpArray = array
|
261 | var tmpArray2
|
262 |
|
263 | for (var sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
|
264 | var size = sizes[sizeIndex]
|
265 | tmpArray2 = []
|
266 |
|
267 |
|
268 | var length = tmpArray.length / size
|
269 | for (var i = 0; i < length; i++) {
|
270 | tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size))
|
271 | }
|
272 |
|
273 | tmpArray = tmpArray2
|
274 | }
|
275 |
|
276 | return tmpArray
|
277 | }
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | export function squeeze (array, arraySize) {
|
286 | let s = arraySize || size(array)
|
287 |
|
288 |
|
289 | while (Array.isArray(array) && array.length === 1) {
|
290 | array = array[0]
|
291 | s.shift()
|
292 | }
|
293 |
|
294 |
|
295 | let dims = s.length
|
296 | while (s[dims - 1] === 1) {
|
297 | dims--
|
298 | }
|
299 |
|
300 |
|
301 | if (dims < s.length) {
|
302 | array = _squeeze(array, dims, 0)
|
303 | s.length = dims
|
304 | }
|
305 |
|
306 | return array
|
307 | }
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | function _squeeze (array, dims, dim) {
|
318 | let i, ii
|
319 |
|
320 | if (dim < dims) {
|
321 | const next = dim + 1
|
322 | for (i = 0, ii = array.length; i < ii; i++) {
|
323 | array[i] = _squeeze(array[i], dims, next)
|
324 | }
|
325 | } else {
|
326 | while (Array.isArray(array)) {
|
327 | array = array[0]
|
328 | }
|
329 | }
|
330 |
|
331 | return array
|
332 | }
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | export function unsqueeze (array, dims, outer, arraySize) {
|
347 | let s = arraySize || size(array)
|
348 |
|
349 |
|
350 | if (outer) {
|
351 | for (let i = 0; i < outer; i++) {
|
352 | array = [array]
|
353 | s.unshift(1)
|
354 | }
|
355 | }
|
356 |
|
357 |
|
358 | array = _unsqueeze(array, dims, 0)
|
359 | while (s.length < dims) {
|
360 | s.push(1)
|
361 | }
|
362 |
|
363 | return array
|
364 | }
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | function _unsqueeze (array, dims, dim) {
|
375 | let i, ii
|
376 |
|
377 | if (Array.isArray(array)) {
|
378 | const next = dim + 1
|
379 | for (i = 0, ii = array.length; i < ii; i++) {
|
380 | array[i] = _unsqueeze(array[i], dims, next)
|
381 | }
|
382 | } else {
|
383 | for (let d = dim; d < dims; d++) {
|
384 | array = [array]
|
385 | }
|
386 | }
|
387 |
|
388 | return array
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | export function flatten (array) {
|
397 | if (!Array.isArray(array)) {
|
398 |
|
399 | return array
|
400 | }
|
401 | let flat = []
|
402 |
|
403 | array.forEach(function callback (value) {
|
404 | if (Array.isArray(value)) {
|
405 | value.forEach(callback)
|
406 | } else {
|
407 | flat.push(value)
|
408 | }
|
409 | })
|
410 |
|
411 | return flat
|
412 | }
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | export function map (array, callback) {
|
420 | return Array.prototype.map.call(array, callback)
|
421 | }
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | export function forEach (array, callback) {
|
429 | Array.prototype.forEach.call(array, callback)
|
430 | }
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | export function filter (array, callback) {
|
438 | if (size(array).length !== 1) {
|
439 | throw new Error('Only one dimensional matrices supported')
|
440 | }
|
441 |
|
442 | return Array.prototype.filter.call(array, callback)
|
443 | }
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 | export function filterRegExp (array, regexp) {
|
453 | if (size(array).length !== 1) {
|
454 | throw new Error('Only one dimensional matrices supported')
|
455 | }
|
456 |
|
457 | return Array.prototype.filter.call(array, (entry) => regexp.test(entry))
|
458 | }
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | export function join (array, separator) {
|
466 | return Array.prototype.join.call(array, separator)
|
467 | }
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | export function identify (a) {
|
475 | if (!Array.isArray(a)) {
|
476 | throw new TypeError('Array input expected')
|
477 | }
|
478 |
|
479 | if (a.length === 0) {
|
480 | return a
|
481 | }
|
482 |
|
483 | let b = []
|
484 | let count = 0
|
485 | b[0] = { value: a[0], identifier: 0 }
|
486 | for (let i = 1; i < a.length; i++) {
|
487 | if (a[i] === a[i - 1]) {
|
488 | count++
|
489 | } else {
|
490 | count = 0
|
491 | }
|
492 | b.push({ value: a[i], identifier: count })
|
493 | }
|
494 | return b
|
495 | }
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 | export function generalize (a) {
|
503 | if (!Array.isArray(a)) {
|
504 | throw new TypeError('Array input expected')
|
505 | }
|
506 |
|
507 | if (a.length === 0) {
|
508 | return a
|
509 | }
|
510 |
|
511 | let b = []
|
512 | for (let i = 0; i < a.length; i++) {
|
513 | b.push(a[i].value)
|
514 | }
|
515 | return b
|
516 | }
|