Skip to content

Commit 6ab12ad

Browse files
committed
feat: add array/base/flatten4d
1 parent 8dbecd0 commit 6ab12ad

File tree

10 files changed

+847
-0
lines changed

10 files changed

+847
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
# flatten4d
22+
23+
> Flatten a four-dimensional nested array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var flatten4d = require( '@stdlib/array/base/flatten4d' );
31+
```
32+
33+
#### flatten4d( x, shape, colexicographic )
34+
35+
Flattens a four-dimensional nested array.
36+
37+
```javascript
38+
var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
39+
40+
var out = flatten4d( x, [ 2, 1, 1, 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 = flatten4d( x, [ 2, 1, 1, 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 flatten4d = require( '@stdlib/array/base/flatten4d' );
75+
76+
// Define a 2x2x2x2 array:
77+
var x = [
78+
[
79+
[ [ 1, 2 ], [ 3, 4 ] ],
80+
[ [ 5, 6 ], [ 7, 8 ] ]
81+
],
82+
[
83+
[ [ 9, 10 ], [ 11, 12 ] ],
84+
[ [ 13, 14 ], [ 15, 16 ] ]
85+
]
86+
];
87+
88+
var out = flatten4d( x, [ 0, 0, 0, 0 ], false );
89+
// returns []
90+
91+
out = flatten4d( x, [ 0, 0, 0, 0 ], true );
92+
// returns []
93+
94+
out = flatten4d( x, [ 1, 1, 1, 1 ], false );
95+
// returns [ 1 ]
96+
97+
out = flatten4d( x, [ 1, 1, 1, 1 ], true );
98+
// returns [ 1 ]
99+
100+
out = flatten4d( x, [ 1, 2, 2, 2 ], false );
101+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8 ]
102+
103+
out = flatten4d( x, [ 1, 2, 2, 2 ], true );
104+
// returns [ 1, 5, 3, 7, 2, 6, 4, 8 ]
105+
106+
out = flatten4d( x, [ 2, 2, 2, 2 ], false );
107+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
108+
109+
out = flatten4d( x, [ 2, 2, 2, 2 ], true );
110+
// returns [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ]
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
</section>
130+
131+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 flatten4d = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':size=144,lexicographic', function benchmark( b ) {
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
v = flatten4d( x, [ 4, 3, 4, 3 ], 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+':size=144,colexicographic', function benchmark( b ) {
56+
var x;
57+
var i;
58+
var v;
59+
60+
x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = flatten4d( x, [ 4, 3, 4, 3 ], 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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x, shape, colexicographic )
3+
Flattens a four-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: ArrayLikeObject
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, 1, 1, 2 ], false )
28+
[ 1, 2, 3, 4 ]
29+
> out = {{alias}}( x, [ 2, 1, 1, 2 ], true )
30+
[ 1, 3, 2, 4 ]
31+
32+
See Also
33+
--------
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Four-dimensional nested array.
27+
*/
28+
type Array4D<T> = Array<Array<Array<Collection<T>>>>;
29+
30+
/**
31+
* Flattens a four-dimensional nested array.
32+
*
33+
* ## Notes
34+
*
35+
* - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array).
36+
*
37+
* @param x - input array
38+
* @param shape - array shape
39+
* @param colexicographic - specifies whether to flatten array values in colexicographic order
40+
* @returns flattened array
41+
*
42+
* @example
43+
* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
44+
*
45+
* var out = flatten4d( x, [ 2, 1, 1, 2 ], false );
46+
* // returns [ 1, 2, 3, 4 ]
47+
*
48+
* @example
49+
* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
50+
*
51+
* var out = flatten4d( x, [ 2, 1, 1, 2 ], true );
52+
* // returns [ 1, 3, 2, 4 ]
53+
*/
54+
declare function flatten4d<T = unknown>( x: Array4D<T>, shape: Collection<number>, colexicographic: boolean ): Array<T>;
55+
56+
57+
// EXPORTS //
58+
59+
export = flatten4d;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
import flatten4d = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
27+
28+
flatten4d( x, [ 2, 1, 1, 2 ], false ); // $ExpectType number[]
29+
flatten4d( x, [ 2, 1, 1, 2 ], true ); // $ExpectType number[]
30+
31+
flatten4d<number>( x, [ 2, 1, 1, 2 ], false ); // $ExpectType number[]
32+
flatten4d<number>( x, [ 2, 1, 1, 2 ], true ); // $ExpectType number[]
33+
34+
flatten4d<string>( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], false ); // $ExpectType string[]
35+
flatten4d<string>( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], true ); // $ExpectType string[]
36+
}
37+
38+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
39+
{
40+
flatten4d( 1, [ 2, 1, 1, 2 ], false ); // $ExpectError
41+
flatten4d( true, [ 2, 1, 1, 2 ], false ); // $ExpectError
42+
flatten4d( false, [ 2, 1, 1, 2 ], false ); // $ExpectError
43+
flatten4d( null, [ 2, 1, 1, 2 ], false ); // $ExpectError
44+
flatten4d( void 0, [ 2, 1, 1, 2 ], false ); // $ExpectError
45+
flatten4d( {}, [ 2, 1, 1, 2 ], false ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers...
49+
{
50+
const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
51+
52+
flatten4d( x, '', false ); // $ExpectError
53+
flatten4d( x, 1, false ); // $ExpectError
54+
flatten4d( x, true, false ); // $ExpectError
55+
flatten4d( x, false, false ); // $ExpectError
56+
flatten4d( x, null, false ); // $ExpectError
57+
flatten4d( x, void 0, false ); // $ExpectError
58+
flatten4d( x, {}, false ); // $ExpectError
59+
flatten4d( x, [ 1, '2', 3 ], false ); // $ExpectError
60+
flatten4d( x, ( x: number ): number => x, false ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided a third argument which is not a boolean...
64+
{
65+
const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
66+
67+
flatten4d( x, [ 2, 1, 1, 2 ], '' ); // $ExpectError
68+
flatten4d( x, [ 2, 1, 1, 2 ], 1 ); // $ExpectError
69+
flatten4d( x, [ 2, 1, 1, 2 ], null ); // $ExpectError
70+
flatten4d( x, [ 2, 1, 1, 2 ], void 0 ); // $ExpectError
71+
flatten4d( x, [ 2, 1, 1, 2 ], {} ); // $ExpectError
72+
flatten4d( x, [ 2, 1, 1, 2 ], [] ); // $ExpectError
73+
flatten4d( x, [ 2, 1, 1, 2 ], ( x: number ): number => x ); // $ExpectError
74+
}
75+
76+
// The compiler throws an error if the function is provided an unsupported number of arguments...
77+
{
78+
const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ];
79+
80+
flatten4d(); // $ExpectError
81+
flatten4d( x ); // $ExpectError
82+
flatten4d( x, [ 2, 1, 1, 2 ] ); // $ExpectError
83+
flatten4d( x, [ 2, 1, 1, 2 ], false, {} ); // $ExpectError
84+
}

0 commit comments

Comments
 (0)