Skip to content

Commit d79d03e

Browse files
committed
Auto-generated commit
1 parent eba1910 commit d79d03e

File tree

12 files changed

+1521
-0
lines changed

12 files changed

+1521
-0
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ This release closes the following issue:
562562

563563
##### Features
564564

565+
- [`873b085`](https://github.com/stdlib-js/stdlib/commit/873b085ae0183426f3e8e831a50a42e2df3ba13d) - add `nditerSubarrays` to namespace
565566
- [`46aec25`](https://github.com/stdlib-js/stdlib/commit/46aec25aac5d4a0c8a3fd7b719dd6a080e59beb8) - add `nditerSelectDimension` to namespace
566567

567568
</section>
@@ -596,6 +597,28 @@ This release closes the following issue:
596597

597598
<!-- /.package -->
598599

600+
<section class="package" id="ndarray-iter-subarrays-unreleased">
601+
602+
#### [@stdlib/ndarray/iter/subarrays](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/iter/subarrays)
603+
604+
<details>
605+
606+
<section class="features">
607+
608+
##### Features
609+
610+
- [`fd9a5c2`](https://github.com/stdlib-js/stdlib/commit/fd9a5c2e29508ab5b393e2273ddb7463be6affb3) - add `ndarray/iter/subarrays`
611+
612+
</section>
613+
614+
<!-- /.features -->
615+
616+
</details>
617+
618+
</section>
619+
620+
<!-- /.package -->
621+
599622
<section class="package" id="ndarray-min-dtype-unreleased">
600623

601624
#### [@stdlib/ndarray/min-dtype](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/min-dtype)
@@ -860,6 +883,8 @@ A total of 4 people contributed to this release. Thank you to the following cont
860883

861884
<details>
862885

886+
- [`873b085`](https://github.com/stdlib-js/stdlib/commit/873b085ae0183426f3e8e831a50a42e2df3ba13d) - **feat:** add `nditerSubarrays` to namespace _(by Athan Reines)_
887+
- [`fd9a5c2`](https://github.com/stdlib-js/stdlib/commit/fd9a5c2e29508ab5b393e2273ddb7463be6affb3) - **feat:** add `ndarray/iter/subarrays` _(by Athan Reines)_
863888
- [`46aec25`](https://github.com/stdlib-js/stdlib/commit/46aec25aac5d4a0c8a3fd7b719dd6a080e59beb8) - **feat:** add `nditerSelectDimension` to namespace _(by Athan Reines)_
864889
- [`6dce19b`](https://github.com/stdlib-js/stdlib/commit/6dce19b2a2dfae6159257dab0c52a8421e0861d2) - **feat:** add `ndarray/iter/select-dimension` _(by Athan Reines)_
865890
- [`4ec7a03`](https://github.com/stdlib-js/stdlib/commit/4ec7a031214836b442e5bce57b57b20e166ef8a4) - **refactor:** improve type specificity _(by Athan Reines)_

iter/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ setReadOnly( ns, 'nditerRows', require( './../../iter/rows' ) );
117117
*/
118118
setReadOnly( ns, 'nditerSelectDimension', require( './../../iter/select-dimension' ) );
119119

120+
/**
121+
* @name nditerSubarrays
122+
* @memberof ns
123+
* @readonly
124+
* @type {Function}
125+
* @see {@link module:@stdlib/ndarray/iter/subarrays}
126+
*/
127+
setReadOnly( ns, 'nditerSubarrays', require( './../../iter/subarrays' ) );
128+
120129
/**
121130
* @name nditer2arrayEach
122131
* @memberof ns

iter/subarrays/README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
# nditerSubarrays
22+
23+
> Create an iterator which iterates over each subarray in a stack of 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 nditerSubarrays = require( '@stdlib/ndarray/iter/subarrays' );
41+
```
42+
43+
#### nditerSubarrays( x, ndims\[, options] )
44+
45+
Returns an iterator which iterates over each subarray in a stack of 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 = nditerSubarrays( 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 [ [ 5, 6 ], [ 7, 8 ] ]
67+
68+
// ...
69+
```
70+
71+
The function accepts the following `options`:
72+
73+
- **readonly**: boolean indicating whether returned [`ndarray`][@stdlib/ndarray/ctor] views should be read-only. In order to return writable [`ndarray`][@stdlib/ndarray/ctor] views, the input [`ndarray`][@stdlib/ndarray/ctor] must be writable. If the input [`ndarray`][@stdlib/ndarray/ctor] is read-only, setting this option to `false` raises an exception. Default: `true`.
74+
75+
By default, the iterator returns [`ndarray`][@stdlib/ndarray/ctor] views which are **read-only**. To return writable [views][@stdlib/ndarray/slice], set the `readonly` option to `false`.
76+
77+
```javascript
78+
var array = require( '@stdlib/ndarray/array' );
79+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
80+
81+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
82+
// returns <ndarray>
83+
84+
var iter = nditerSubarrays( x, 2, {
85+
'readonly': false
86+
});
87+
88+
var v = iter.next().value;
89+
// returns <ndarray>
90+
91+
var arr = ndarray2array( v );
92+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
93+
94+
v.set( 0, 0, 10 );
95+
96+
arr = ndarray2array( v );
97+
// returns [ [ 10, 2 ], [ 3, 4 ] ]
98+
```
99+
100+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
101+
102+
- **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.
103+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
104+
105+
</section>
106+
107+
<!-- /.usage -->
108+
109+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
110+
111+
<section class="notes">
112+
113+
## Notes
114+
115+
- The input [`ndarray`][@stdlib/ndarray/ctor] must have at least `ndims+1` dimensions.
116+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
117+
- 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.
118+
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined.
119+
120+
</section>
121+
122+
<!-- /.notes -->
123+
124+
<!-- Package usage examples. -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var array = require( '@stdlib/ndarray/array' );
134+
var zeroTo = require( '@stdlib/array/base/zero-to' );
135+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
136+
var nditerSubarrays = require( '@stdlib/ndarray/iter/subarrays' );
137+
138+
// Define an input array:
139+
var x = array( zeroTo( 27 ), {
140+
'shape': [ 3, 3, 3 ]
141+
});
142+
143+
// Create an iterator for iterating over matrices:
144+
var it = nditerSubarrays( x, 2 );
145+
146+
// Perform manual iteration...
147+
var v;
148+
while ( true ) {
149+
v = it.next();
150+
if ( v.done ) {
151+
break;
152+
}
153+
console.log( ndarray2array( v.value ) );
154+
}
155+
```
156+
157+
</section>
158+
159+
<!-- /.examples -->
160+
161+
<!-- 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. -->
162+
163+
<section class="references">
164+
165+
</section>
166+
167+
<!-- /.references -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
182+
183+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor
184+
185+
[@stdlib/ndarray/slice]: https://github.com/stdlib-js/ndarray/tree/main/slice
186+
187+
</section>
188+
189+
<!-- /.links -->

iter/subarrays/benchmark/benchmark.js

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( './../../../array' );
27+
var pkg = require( './../package.json' ).name;
28+
var nditerSubarrays = 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 = nditerSubarrays( 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 = nditerSubarrays( 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+
});

iter/subarrays/docs/repl.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
{{alias}}( x, ndims[, options] )
3+
Returns an iterator which iterates over each subarray in a stack of
4+
subarrays.
5+
6+
If an environment supports Symbol.iterator, the returned iterator is
7+
iterable.
8+
9+
If an environment supports Symbol.iterator, the function explicitly does not
10+
invoke an ndarray's `@@iterator` method, regardless of whether this method
11+
is defined.
12+
13+
Parameters
14+
----------
15+
x: ndarray
16+
Input ndarray for which to create the iterator. Must have at least
17+
`ndims+1` dimensions.
18+
19+
ndims: integer
20+
Number of dimensions to stack.
21+
22+
options: Object (optional)
23+
Options.
24+
25+
options.readonly: boolean (optional)
26+
Boolean indicating whether returned ndarray views should be read-only.
27+
If the input ndarray is read-only, setting this option to `false` raises
28+
an exception. Default: true.
29+
30+
Returns
31+
-------
32+
iterator: Object
33+
Iterator.
34+
35+
iterator.next(): Function
36+
Returns an iterator protocol-compliant object containing the next
37+
iterated value (if one exists) and a boolean flag indicating whether the
38+
iterator is finished.
39+
40+
iterator.return( [value] ): Function
41+
Finishes an iterator and returns a provided value.
42+
43+
Examples
44+
--------
45+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] );
46+
> var it = {{alias}}( x, 2 );
47+
> var v = it.next().value;
48+
> {{alias:@stdlib/ndarray/to-array}}( v )
49+
[ [ 1, 2 ], [ 3, 4 ] ]
50+
51+
See Also
52+
--------
53+

0 commit comments

Comments
 (0)