Skip to content

Commit 25e87e4

Browse files
committed
feat: add ndarray/iter/interleave-subarrays
1 parent 2939f56 commit 25e87e4

File tree

10 files changed

+1357
-0
lines changed

10 files changed

+1357
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# nditerInterleaveSubarrays
22+
23+
> Create an iterator which iterates over interleaved subarrays.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var nditerInterleaveSubarrays = require( '@stdlib/ndarray/iter/interleave-subarrays' );
41+
```
42+
43+
#### nditerInterleaveSubarrays( arr, ndims )
44+
45+
Returns an iterator which iterates over interleaved subarrays.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
52+
// returns <ndarray>
53+
54+
var iter = nditerInterleaveSubarrays( [ x, x ], 2 );
55+
56+
var v = iter.next().value;
57+
// returns <ndarray>
58+
59+
var arr = ndarray2array( v );
60+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
61+
62+
v = iter.next().value;
63+
// returns <ndarray>
64+
65+
arr = ndarray2array( v );
66+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
67+
68+
// ...
69+
```
70+
71+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
72+
73+
- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished.
74+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
81+
82+
<section class="notes">
83+
84+
## Notes
85+
86+
- All provided [`ndarrays`][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes].
87+
- After broadcasting, each broadcasted input [`ndarray`][@stdlib/ndarray/ctor] must have at least `ndims+1` dimensions.
88+
- For input [`ndarrays`][@stdlib/ndarray/ctor] supporting read-only views, the function returns **read-only** views of interleaved subarrays. As input [`ndarrays`][@stdlib/ndarray/ctor] may be broadcasted, a view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to a view may affect multiple elements. If you need to write to a subarray, copy the subarray **before** attempting mutation.
89+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
90+
- A returned iterator does **not** copy a provided [`ndarray`][@stdlib/ndarray/ctor]. To ensure iterable reproducibility, copy the input [`ndarray`][@stdlib/ndarray/ctor] **before** creating an iterator. Otherwise, any changes to the contents of input [`ndarray`][@stdlib/ndarray/ctor] will be reflected in the returned iterator.
91+
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined.
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var array = require( '@stdlib/ndarray/array' );
107+
var zeroTo = require( '@stdlib/array/base/zero-to' );
108+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
109+
var nditerInterleaveSubarrays = require( '@stdlib/ndarray/iter/interleave-subarrays' );
110+
111+
// Define input arrays:
112+
var x = array( zeroTo( 27 ), {
113+
'shape': [ 3, 3, 3 ]
114+
});
115+
var y = array( zeroTo( 9 ), {
116+
'shape': [ 3, 3 ]
117+
});
118+
119+
// Create an iterator for iterating over interleaved matrices:
120+
var it = nditerInterleaveSubarrays( [ x, y ], 2 );
121+
122+
// Perform manual iteration...
123+
var v;
124+
while ( true ) {
125+
v = it.next();
126+
if ( v.done ) {
127+
break;
128+
}
129+
console.log( ndarray2array( v.value ) );
130+
}
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="references">
140+
141+
</section>
142+
143+
<!-- /.references -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
158+
159+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
160+
161+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
162+
163+
<!-- <related-links> -->
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.links -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var array = require( '@stdlib/ndarray/array' );
27+
var pkg = require( './../package.json' ).name;
28+
var nditerInterleaveSubarrays = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var iter;
35+
var x;
36+
var i;
37+
38+
x = array( [ [ [ 1, 2, 3, 4 ] ] ] );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = nditerInterleaveSubarrays( [ x, x ], 2 );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var xbuf;
57+
var iter;
58+
var x;
59+
var z;
60+
var i;
61+
62+
xbuf = [];
63+
xbuf.length = b.iterations + 1;
64+
x = array( xbuf, {
65+
'shape': [ xbuf.length, 1, 1 ],
66+
'dtype': 'generic',
67+
'copy': false
68+
});
69+
70+
iter = nditerInterleaveSubarrays( [ x, x ], 2 );
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
z = iter.next().value;
75+
if ( typeof z !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isndarrayLike( z ) ) {
81+
b.fail( 'should return an ndarray' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
{{alias}}( arr, ndims )
3+
Returns an iterator which iterates over interleaved subarrays.
4+
5+
For input ndarrays supporting read-only views, the function returns
6+
*read-only* views of interleaved subarrays. As input ndarrays may be
7+
broadcasted, a view is typically *not* contiguous. As more than one element
8+
of a returned view may refer to the same memory location, writing to a view
9+
may affect multiple elements. If you need to write to a subarray, copy the
10+
subarray before attempting mutation.
11+
12+
The function throws an error if a provided broadcast-incompatible ndarrays.
13+
14+
If an environment supports Symbol.iterator, the returned iterator is
15+
iterable.
16+
17+
If an environment supports Symbol.iterator, the function explicitly does not
18+
invoke an ndarray's `@@iterator` method, regardless of whether this method
19+
is defined.
20+
21+
Parameters
22+
----------
23+
arr: ArrayLike<ndarray>
24+
Input ndarrays. All ndarrays must be broadcast-compatible. After
25+
broadcasting, each broadcasted input ndarray must have at least
26+
`ndims+1` dimensions.
27+
28+
ndims: integer
29+
Number of dimensions to stack after broadcasting.
30+
31+
Returns
32+
-------
33+
iterator: Object
34+
Iterator.
35+
36+
iterator.next(): Function
37+
Returns an iterator protocol-compliant object containing the next
38+
iterated value (if one exists) and a boolean flag indicating whether the
39+
iterator is finished.
40+
41+
iterator.return( [value] ): Function
42+
Finishes an iterator and returns a provided value.
43+
44+
Examples
45+
--------
46+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] );
47+
> var it = {{alias}}( [ x, x ], 2 );
48+
> var v = it.next().value;
49+
> {{alias:@stdlib/ndarray/to-array}}( v )
50+
[ [ 1, 2 ], [ 3, 4 ] ]
51+
52+
See Also
53+
--------
54+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 { TypedIterator, TypedIterableIterator } from '@stdlib/types/iter';
24+
import { typedndarray } from '@stdlib/types/ndarray';
25+
import { ArrayLike } from '@stdlib/types/array';
26+
27+
// Define a union type representing both iterable and non-iterable iterators:
28+
type Iterator<T> = TypedIterator<T> | TypedIterableIterator<T>;
29+
30+
/**
31+
* Returns an iterator which iterates over interleaved subarrays.
32+
*
33+
* ## Notes
34+
*
35+
* - The function throws an error if a provided broadcast-incompatible ndarrays.
36+
* - For input ndarrays supporting read-only views, the function returns *read-only* views of interleaved subarrays. As input ndarrays may be broadcasted, a view is typically *not* contiguous. As more than one element of a returned view may refer to the same memory location, writing to a view may affect multiple elements. If you need to write to a subarray, copy the subarray before attempting mutation.
37+
*
38+
* @param arr - input ndarrays
39+
* @param ndims - number of dimensions to stack
40+
* @returns iterator
41+
*
42+
* @example
43+
* var array = require( '@stdlib/ndarray/array' );
44+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
45+
*
46+
* var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ], {
47+
* 'dtype': 'float64'
48+
* });
49+
* // returns <ndarray>
50+
*
51+
* var iter = nditerInterleaveSubarrays( [ x, x ], 2 );
52+
*
53+
* var v = iter.next().value;
54+
* // returns <ndarray>
55+
*
56+
* var arr = ndarray2array( v );
57+
* // returns [ [ 1, 2 ], [ 3, 4 ] ]
58+
*
59+
* v = iter.next().value;
60+
* // returns <ndarray>
61+
*
62+
* arr = ndarray2array( v );
63+
* // returns [ [ 1, 2 ], [ 3, 4 ] ]
64+
*
65+
* // ...
66+
*/
67+
declare function nditerInterleaveSubarrays<T = unknown>( arr: ArrayLike<typedndarray<T>>, ndims: number ): Iterator<typedndarray<T>>;
68+
69+
70+
// EXPORTS //
71+
72+
export = nditerInterleaveSubarrays;

0 commit comments

Comments
 (0)