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