-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
Copy pathindex.js
154 lines (115 loc) · 2.44 KB
/
index.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
/* eslint-disable array-bracket-spacing, object-curly-newline, object-curly-spacing */
'use strict';
var flattenArray = require( './../lib' );
var flatten;
var opts;
var arr;
var out;
// Basic usage:
console.log( '\nBasic:\n' );
arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
out = flattenArray( arr );
console.log( out );
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
// Limiting depth:
console.log( '\nLimiting Depth:\n' );
opts = {
'depth': 2
};
out = flattenArray( arr, opts );
console.log( out );
// => [ 1, 2, 3, [4, [ 5 ], 6], 7, 8, 9 ]
// Deep copy:
console.log( '\nDeep Copy:\n' );
opts = {
'depth': 2,
'copy': true
};
out = flattenArray( arr, opts );
console.log( out );
// => [ 1, 2, 3, [4, [ 5 ], 6], 7, 8, 9 ]
console.log( arr[1][1][1] === out[3] );
// => false
// Equal dimensions:
console.log( '\nEqual Dimensions:\n' );
arr = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ],
[ 10, 11, 12 ]
];
out = flattenArray( arr, {
'equalDims': true
});
console.log( out );
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
// Custom flatten function:
console.log( '\nCustom Flatten:\n' );
// Create a flatten function customized for flattening 3x3 arrays:
flatten = flattenArray.factory( [3, 3] );
arr = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
];
out = flatten( arr );
console.log( out );
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
// Custom flatten function and deep copy:
console.log( '\nCustom Flatten and Deep Copy:\n' );
opts = {
'copy': true
};
flatten = flattenArray.factory( [3, 3], opts );
arr = [
[ 1, 2, 3 ],
[ 4, {'x': 5}, 6 ],
[ 7, 8, 9 ]
];
out = flatten( arr );
console.log( out );
// => [ 1, 2, 3, 4, {'x':5}, 6, 7, 8, 9 ]
console.log( arr[1][1] === out[4] );
// => false
// Strided arrays:
console.log( '\nStrided Arrays:\n' );
var xStride;
var yStride;
var zStride;
var data;
var tmp1;
var tmp2;
var val;
var N;
var M;
var L;
var i;
var j;
var k;
N = 1000;
M = 100;
L = 10;
// Create an NxMxL (3D) array...
data = new Array( N );
for ( i = 0; i < N; i++ ) {
tmp1 = new Array( M );
for ( j = 0; j < M; j++ ) {
tmp2 = new Array( L );
for ( k = 0; k < L; k++ ) {
tmp2[ k ] = (M*L*i) + (j*L) + k + 1;
}
tmp1[ j ] = tmp2;
}
data[ i ] = tmp1;
}
// Create a flattened (strided) array:
arr = flattenArray( data );
// To access the data[4][20][2] element...
xStride = M * L;
yStride = L;
zStride = 1;
val = arr[ (4*xStride) + (20*yStride) + (2*zStride) ];
console.log( val );
// => 4203
console.log( data[4][20][2] === val );
// => true