Skip to content

Commit 7031fea

Browse files
committed
feat: add array/base/flatten
1 parent cea3602 commit 7031fea

10 files changed

Lines changed: 1627 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# flatten
22+
23+
> Flatten an n-dimensional nested array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var flatten = require( '@stdlib/array/base/flatten' );
31+
```
32+
33+
#### flatten( x, shape, colexicographic )
34+
35+
Flattens an n-dimensional nested array.
36+
37+
```javascript
38+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
39+
40+
var out = flatten( x, [ 2, 2 ], false );
41+
// returns [ 1, 2, 3, 4 ]
42+
```
43+
44+
To flatten in colexicographic order, provide a third argument equal to `true`.
45+
46+
```javascript
47+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
48+
49+
var out = flatten( x, [ 2, 2 ], true );
50+
// returns [ 1, 3, 2, 4 ]
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array).
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var flatten = require( '@stdlib/array/base/flatten' );
75+
76+
// Define a 2x2x1x2x2 array:
77+
var x = [
78+
[
79+
[
80+
[
81+
[ 1, 2 ], [ 3, 4 ]
82+
]
83+
],
84+
[
85+
[
86+
[ 5, 6 ], [ 7, 8 ]
87+
]
88+
]
89+
],
90+
[
91+
[
92+
[
93+
[ 9, 10 ], [ 11, 12 ]
94+
]
95+
],
96+
[
97+
[
98+
[ 13, 14 ], [ 15, 16 ]
99+
]
100+
]
101+
]
102+
];
103+
104+
var out = flatten( x, [ 0, 0, 0, 0, 0 ], false );
105+
// returns []
106+
107+
out = flatten( x, [ 0, 0, 0, 0, 0 ], true );
108+
// returns []
109+
110+
out = flatten( x, [ 1, 1, 1, 1, 1 ], false );
111+
// returns [ 1 ]
112+
113+
out = flatten( x, [ 1, 1, 1, 1, 1 ], true );
114+
// returns [ 1 ]
115+
116+
out = flatten( x, [ 1, 2, 1, 2, 2 ], false );
117+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8 ]
118+
119+
out = flatten( x, [ 1, 2, 1, 2, 2 ], true );
120+
// returns [ 1, 5, 3, 7, 2, 6, 4, 8 ]
121+
122+
out = flatten( x, [ 2, 2, 1, 2, 2 ], false );
123+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
124+
125+
out = flatten( x, [ 2, 2, 1, 2, 2 ], true );
126+
// returns [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ]
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
137+
</section>
138+
139+
<!-- /.related -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
</section>
146+
147+
<!-- /.links -->
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var filled = require( '@stdlib/array/base/filled' );
27+
var pkg = require( './../package.json' ).name;
28+
var flatten = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':ndims=2,size=100,lexicographic', function benchmark( b ) {
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = filled( zeroTo( 10 ), 10 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
v = flatten( x, [ 10, 10 ], false );
43+
if ( typeof v !== 'object' ) {
44+
b.fail( 'should return an array' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isArray( v ) ) {
49+
b.fail( 'should return an array' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+':ndims=2,size=100,colexicographic', function benchmark( b ) {
56+
var x;
57+
var i;
58+
var v;
59+
60+
x = filled( zeroTo( 10 ), 10 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = flatten( x, [ 10, 10 ], true );
65+
if ( typeof v !== 'object' ) {
66+
b.fail( 'should return an array' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isArray( v ) ) {
71+
b.fail( 'should return an array' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
76+
77+
bench( pkg+':ndims=3,size=125,lexicographic', function benchmark( b ) {
78+
var x;
79+
var i;
80+
var v;
81+
82+
x = filled( filled( zeroTo( 5 ), 5 ), 5 );
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
v = flatten( x, [ 5, 5, 5 ], false );
87+
if ( typeof v !== 'object' ) {
88+
b.fail( 'should return an array' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isArray( v ) ) {
93+
b.fail( 'should return an array' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+':ndims=3,size=125,colexicographic', function benchmark( b ) {
100+
var x;
101+
var i;
102+
var v;
103+
104+
x = filled( filled( zeroTo( 5 ), 5 ), 5 );
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
v = flatten( x, [ 5, 5, 5 ], true );
109+
if ( typeof v !== 'object' ) {
110+
b.fail( 'should return an array' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isArray( v ) ) {
115+
b.fail( 'should return an array' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( pkg+':ndims=4,size=144,lexicographic', function benchmark( b ) {
122+
var x;
123+
var i;
124+
var v;
125+
126+
x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 );
127+
128+
b.tic();
129+
for ( i = 0; i < b.iterations; i++ ) {
130+
v = flatten( x, [ 4, 3, 4, 3 ], false );
131+
if ( typeof v !== 'object' ) {
132+
b.fail( 'should return an array' );
133+
}
134+
}
135+
b.toc();
136+
if ( !isArray( v ) ) {
137+
b.fail( 'should return an array' );
138+
}
139+
b.pass( 'benchmark finished' );
140+
b.end();
141+
});
142+
143+
bench( pkg+':ndims=4,size=144,colexicographic', function benchmark( b ) {
144+
var x;
145+
var i;
146+
var v;
147+
148+
x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 );
149+
150+
b.tic();
151+
for ( i = 0; i < b.iterations; i++ ) {
152+
v = flatten( x, [ 4, 3, 4, 3 ], true );
153+
if ( typeof v !== 'object' ) {
154+
b.fail( 'should return an array' );
155+
}
156+
}
157+
b.toc();
158+
if ( !isArray( v ) ) {
159+
b.fail( 'should return an array' );
160+
}
161+
b.pass( 'benchmark finished' );
162+
b.end();
163+
});
164+
165+
bench( pkg+':ndims=5,size=108,lexicographic', function benchmark( b ) {
166+
var x;
167+
var i;
168+
var v;
169+
170+
x = filled( filled( filled( filled( zeroTo( 3 ), 3 ), 3 ), 2 ), 2 );
171+
172+
b.tic();
173+
for ( i = 0; i < b.iterations; i++ ) {
174+
v = flatten( x, [ 2, 2, 3, 3, 3 ], false );
175+
if ( typeof v !== 'object' ) {
176+
b.fail( 'should return an array' );
177+
}
178+
}
179+
b.toc();
180+
if ( !isArray( v ) ) {
181+
b.fail( 'should return an array' );
182+
}
183+
b.pass( 'benchmark finished' );
184+
b.end();
185+
});
186+
187+
bench( pkg+':ndims=5,size=108,colexicographic', function benchmark( b ) {
188+
var x;
189+
var i;
190+
var v;
191+
192+
x = filled( filled( filled( filled( zeroTo( 3 ), 3 ), 3 ), 2 ), 2 );
193+
194+
b.tic();
195+
for ( i = 0; i < b.iterations; i++ ) {
196+
v = flatten( x, [ 2, 2, 3, 3, 3 ], true );
197+
if ( typeof v !== 'object' ) {
198+
b.fail( 'should return an array' );
199+
}
200+
}
201+
b.toc();
202+
if ( !isArray( v ) ) {
203+
b.fail( 'should return an array' );
204+
}
205+
b.pass( 'benchmark finished' );
206+
b.end();
207+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x, shape, colexicographic )
3+
Flattens an n-dimensional nested array.
4+
5+
The function assumes that all nested arrays have the same length (i.e., the
6+
input array is *not* a ragged array).
7+
8+
Parameters
9+
----------
10+
x: Array
11+
Input array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
colexicographic: boolean
17+
Specifies whether to flatten array values in colexicographic order.
18+
19+
Returns
20+
-------
21+
out: Array
22+
Flattened array.
23+
24+
Examples
25+
--------
26+
> var x = [ [ 1, 2 ], [ 3, 4 ] ];
27+
> var out = {{alias}}( x, [ 2, 2 ], false )
28+
[ 1, 2, 3, 4 ]
29+
> out = {{alias}}( x, [ 2, 2 ], true )
30+
[ 1, 3, 2, 4 ]
31+
32+
See Also
33+
--------
34+

0 commit comments

Comments
 (0)