|
| 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 | +# nditerStacks |
| 22 | + |
| 23 | +> Create an iterator which iterates over each subarray in a stack of subarrays according to a list of specified stack dimensions. |
| 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 nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### nditerStacks( x, dims\[, options] ) |
| 44 | + |
| 45 | +Returns an iterator which iterates over each subarray in a stack of subarrays according to a list of specified stack dimensions. |
| 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 = nditerStacks( x, [ 1, 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 = nditerStacks( x, [ 1, 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 `dims.length+1` dimensions. |
| 116 | +- The list of provided index dimensions must be unique and resolve to index dimensions sorted in ascending order (i.e., the function does **not** allow rearranging [`ndarray`][@stdlib/ndarray/ctor] dimensions by specifying an arbitrary index order). |
| 117 | +- If an environment supports `Symbol.iterator`, the returned iterator is iterable. |
| 118 | +- 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. |
| 119 | +- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined. |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.notes --> |
| 124 | + |
| 125 | +<!-- Package usage examples. --> |
| 126 | + |
| 127 | +<section class="examples"> |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +<!-- eslint no-undef: "error" --> |
| 132 | + |
| 133 | +```javascript |
| 134 | +var array = require( '@stdlib/ndarray/array' ); |
| 135 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 136 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 137 | +var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); |
| 138 | + |
| 139 | +// Define an input array: |
| 140 | +var x = array( zeroTo( 27 ), { |
| 141 | + 'shape': [ 3, 3, 3 ] |
| 142 | +}); |
| 143 | + |
| 144 | +// Create an iterator for iterating over matrices: |
| 145 | +var it = nditerStacks( x, [ 1, 2 ] ); |
| 146 | + |
| 147 | +// Perform manual iteration... |
| 148 | +var v; |
| 149 | +while ( true ) { |
| 150 | + v = it.next(); |
| 151 | + if ( v.done ) { |
| 152 | + break; |
| 153 | + } |
| 154 | + console.log( ndarray2array( v.value ) ); |
| 155 | +} |
| 156 | + |
| 157 | +// Create an iterator for iterating over matrices: |
| 158 | +it = nditerStacks( x, [ 0, 2 ] ); |
| 159 | + |
| 160 | +// Perform manual iteration... |
| 161 | +while ( true ) { |
| 162 | + v = it.next(); |
| 163 | + if ( v.done ) { |
| 164 | + break; |
| 165 | + } |
| 166 | + console.log( ndarray2array( v.value ) ); |
| 167 | +} |
| 168 | + |
| 169 | +// Create an iterator for iterating over matrices: |
| 170 | +it = nditerStacks( x, [ 0, 1 ] ); |
| 171 | + |
| 172 | +// Perform manual iteration... |
| 173 | +while ( true ) { |
| 174 | + v = it.next(); |
| 175 | + if ( v.done ) { |
| 176 | + break; |
| 177 | + } |
| 178 | + console.log( ndarray2array( v.value ) ); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.examples --> |
| 185 | + |
| 186 | +<!-- 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. --> |
| 187 | + |
| 188 | +<section class="references"> |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.references --> |
| 193 | + |
| 194 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 195 | + |
| 196 | +<section class="related"> |
| 197 | + |
| 198 | +</section> |
| 199 | + |
| 200 | +<!-- /.related --> |
| 201 | + |
| 202 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 203 | + |
| 204 | +<section class="links"> |
| 205 | + |
| 206 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 207 | + |
| 208 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 209 | + |
| 210 | +[@stdlib/ndarray/slice]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/slice |
| 211 | + |
| 212 | +</section> |
| 213 | + |
| 214 | +<!-- /.links --> |
0 commit comments