Skip to content

Commit 6cd1bad

Browse files
committed
feat: add ndarray/iter/entries
1 parent 905a042 commit 6cd1bad

File tree

10 files changed

+1988
-0
lines changed

10 files changed

+1988
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
# nditerEntries
22+
23+
> Create an iterator which returns `[index, value]` pairs for each element in a provided [`ndarray`][@stdlib/ndarray/ctor].
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 nditerEntries = require( '@stdlib/ndarray/iter/entries' );
41+
```
42+
43+
#### nditerEntries( x\[, options] )
44+
45+
Returns an iterator which returns `[index, value]` pairs for each element in a provided [`ndarray`][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
51+
// returns <ndarray>
52+
53+
var iter = nditerEntries( x );
54+
55+
var v = iter.next().value;
56+
// returns [ [ 0, 0, 0 ], 1 ]
57+
58+
v = iter.next().value;
59+
// returns [ [ 0, 0, 1 ], 2 ]
60+
61+
v = iter.next().value;
62+
// returns [ [ 0, 1, 0 ], 3 ]
63+
64+
// ...
65+
```
66+
67+
The function accepts the following `options`:
68+
69+
- **order**: index iteration order. By default, the returned [iterator][mdn-iterator-protocol] returns values according to the layout order of the provided array. Accordingly, for row-major input arrays, the last dimension indices increment fastest. For column-major input arrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input array's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input array may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`.
70+
71+
By default, the iterator iterates according to the layout order of the input [`ndarray`][@stdlib/ndarray/ctor]. To iterate according to a specified order, set the `order` option.
72+
73+
```javascript
74+
var array = require( '@stdlib/ndarray/array' );
75+
76+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ], {
77+
'order': 'row-major'
78+
});
79+
// returns <ndarray>
80+
81+
var iter = nditerEntries( x, {
82+
'order': 'column-major'
83+
});
84+
85+
var v = iter.next().value;
86+
// returns [ [ 0, 0, 0 ], 1 ]
87+
88+
v = iter.next().value;
89+
// returns [ [ 1, 0, 0 ], 5 ]
90+
91+
v = iter.next().value;
92+
// returns [ [ 0, 1, 0 ], 3 ]
93+
94+
// ...
95+
```
96+
97+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
98+
99+
- **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.
100+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
101+
102+
</section>
103+
104+
<!-- /.usage -->
105+
106+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
107+
108+
<section class="notes">
109+
110+
## Notes
111+
112+
- Each returned index is a Cartesian index (i.e., an array of subscripts/dimension indices).
113+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
114+
- 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.
115+
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined.
116+
117+
</section>
118+
119+
<!-- /.notes -->
120+
121+
<!-- Package usage examples. -->
122+
123+
<section class="examples">
124+
125+
## Examples
126+
127+
<!-- eslint no-undef: "error" -->
128+
129+
```javascript
130+
var array = require( '@stdlib/ndarray/array' );
131+
var zeroTo = require( '@stdlib/array/base/zero-to' );
132+
var nditerEntries = require( '@stdlib/ndarray/iter/entries' );
133+
134+
// Define an input array:
135+
var x = array( zeroTo( 27 ), {
136+
'shape': [ 3, 3, 3 ]
137+
});
138+
139+
// Create an iterator for returning [index, value] pairs:
140+
var it = nditerEntries( x );
141+
142+
// Perform manual iteration...
143+
var v;
144+
while ( true ) {
145+
v = it.next();
146+
if ( v.done ) {
147+
break;
148+
}
149+
console.log( v.value );
150+
}
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
<!-- 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. -->
158+
159+
<section class="references">
160+
161+
</section>
162+
163+
<!-- /.references -->
164+
165+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166+
167+
<section class="related">
168+
169+
</section>
170+
171+
<!-- /.related -->
172+
173+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="links">
176+
177+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
178+
179+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
180+
181+
</section>
182+
183+
<!-- /.links -->
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var array = require( '@stdlib/ndarray/array' );
27+
var zeros = require( '@stdlib/ndarray/zeros' );
28+
var pkg = require( './../package.json' ).name;
29+
var nditerEntries = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var iter;
36+
var x;
37+
var i;
38+
39+
x = array( [ [ 1, 2, 3, 4 ] ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
iter = nditerEntries( x );
44+
if ( typeof iter !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isIteratorLike( iter ) ) {
50+
b.fail( 'should return an iterator protocol-compliant object' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::iteration', function benchmark( b ) {
57+
var iter;
58+
var x;
59+
var z;
60+
var i;
61+
62+
x = zeros( [ b.iterations+1, 1 ], {
63+
'dtype': 'generic'
64+
});
65+
66+
iter = nditerEntries( x );
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = iter.next().value;
71+
if ( typeof z !== 'object' ) {
72+
b.fail( 'should return an array' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isArray( z ) ) {
77+
b.fail( 'should return an array' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
{{alias}}( x[, options] )
3+
Returns an iterator which returns [index, value] pairs for each element in a
4+
provided ndarray.
5+
6+
Each returned index is a Cartesian index (i.e., an array of subscripts/
7+
dimension indices).
8+
9+
If an environment supports Symbol.iterator, the returned iterator is
10+
iterable.
11+
12+
If an environment supports Symbol.iterator, the function explicitly does not
13+
invoke an ndarray's `@@iterator` method, regardless of whether this method
14+
is defined.
15+
16+
Parameters
17+
----------
18+
x: ndarray
19+
Input array.
20+
21+
options: Object (optional)
22+
Options.
23+
24+
options.order: string (optional)
25+
Index iteration order. By default, the returned iterator returns values
26+
according to the layout order of the provided array. Accordingly, for
27+
row-major input arrays, the last dimension indices increment fastest.
28+
For column-major input arrays, the first dimension indices increment
29+
fastest. To override the inferred order and ensure that indices
30+
increment in a specific manor, regardless of the input array's layout
31+
order, explicitly set the iteration order. Note, however, that iterating
32+
according to an order which does not match that of the input array may,
33+
in some circumstances, result in performance degradation due to cache
34+
misses. Must be either 'row-major' or 'column-major'.
35+
36+
Returns
37+
-------
38+
iterator: Object
39+
Iterator.
40+
41+
iterator.next(): Function
42+
Returns an iterator protocol-compliant object containing the next
43+
iterated value (if one exists) and a boolean flag indicating whether the
44+
iterator is finished.
45+
46+
iterator.return( [value] ): Function
47+
Finishes an iterator and returns a provided value.
48+
49+
Examples
50+
--------
51+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
52+
> var it = {{alias}}( x );
53+
> var v = it.next().value
54+
[ [ 0, 0 ], 1 ]
55+
> v = it.next().value
56+
[ [ 0, 1 ], 2 ]
57+
58+
See Also
59+
--------
60+

0 commit comments

Comments
 (0)