|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 | +# gmskrev |
| 22 | + |
| 23 | +> Reverse a strided array in-place according to a mask. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var gmskrev = require( '@stdlib/blas/ext/base/gmskrev' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### gmskrev( N, x, strideX, mask, strideMask ) |
| 34 | + |
| 35 | +Reverses a strided array in-place according to a mask. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 39 | +var mask = [ 0, 0, 0, 1, 0, 0, 0, 0 ]; |
| 40 | + |
| 41 | +gmskrev( x.length, x, 1, mask, 1 ); |
| 42 | +// x => [ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ] |
| 43 | +``` |
| 44 | + |
| 45 | +The function has the following parameters: |
| 46 | + |
| 47 | +- **N**: number of indexed elements. |
| 48 | +- **x**: input array. |
| 49 | +- **strideX**: stride length for `x`. |
| 50 | +- **mask**: mask array. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in the reversal. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from the reversal. |
| 51 | +- **strideMask**: stride length for `mask`. |
| 52 | + |
| 53 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to reverse every other element: |
| 54 | + |
| 55 | +```javascript |
| 56 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 57 | +var mask = [ 0, 0, 0, 0 ]; |
| 58 | + |
| 59 | +gmskrev( 4, x, 2, mask, 1 ); |
| 60 | +// x => [ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.0 ] |
| 61 | +``` |
| 62 | + |
| 63 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 64 | + |
| 65 | +```javascript |
| 66 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 67 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 68 | + |
| 69 | +// Initial arrays... |
| 70 | +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 71 | +var m0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0 ] ); |
| 72 | + |
| 73 | +// Create offset views... |
| 74 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 75 | +var m1 = new Uint8Array( m0.buffer, m0.BYTES_PER_ELEMENT*1 ); |
| 76 | + |
| 77 | +// Reverse every other element... |
| 78 | +gmskrev( 3, x1, 2, m1, 1 ); |
| 79 | +// x0 => <Float64Array>[ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] |
| 80 | +``` |
| 81 | + |
| 82 | +#### gmskrev.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) |
| 83 | + |
| 84 | +Reverses a strided array in-place according to a mask and using alternative indexing semantics. |
| 85 | + |
| 86 | +```javascript |
| 87 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 88 | +var mask = [ 0, 0, 0, 1, 0, 0, 0, 0 ]; |
| 89 | + |
| 90 | +gmskrev.ndarray( x.length, x, 1, 0, mask, 1, 0 ); |
| 91 | +// x => [ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ] |
| 92 | +``` |
| 93 | + |
| 94 | +The function has the following additional parameters: |
| 95 | + |
| 96 | +- **offsetX**: starting index for `x`. |
| 97 | +- **offsetMask**: starting index for `mask`. |
| 98 | + |
| 99 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements: |
| 100 | + |
| 101 | +```javascript |
| 102 | +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; |
| 103 | +var mask = [ 0, 0, 0, 0, 0, 0 ]; |
| 104 | + |
| 105 | +gmskrev.ndarray( 3, x, 1, x.length-3, mask, 1, 3 ); |
| 106 | +// x => [ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ] |
| 107 | +``` |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.usage --> |
| 112 | + |
| 113 | +<section class="notes"> |
| 114 | + |
| 115 | +## Notes |
| 116 | + |
| 117 | +- If `N <= 0`, both functions return `x` unchanged. |
| 118 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/complex64`][@stdlib/array/complex64]). |
| 119 | +- Where possible, one should "reverse" a strided array by negating its stride, which is an `O(1)` operation, in contrast to performing an in-place reversal, which is `O(N)`. However, in certain circumstances, this is not tenable, particularly when interfacing with libraries which assume and/or expect a specific memory layout (e.g., strided array elements arranged in memory in ascending order). In general, when working with strided arrays, only perform an in-place reversal when strictly necessary. |
| 120 | +- Depending on the environment, the typed versions ([`dmskrev`][@stdlib/blas/ext/base/dmskrev], [`smskrev`][@stdlib/blas/ext/base/smskrev], etc.) are likely to be significantly more performant. |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.notes --> |
| 125 | + |
| 126 | +<section class="examples"> |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 134 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 135 | +var gmskrev = require( '@stdlib/blas/ext/base/gmskrev' ); |
| 136 | + |
| 137 | +var x = discreteUniform( 10, -100, 100, { |
| 138 | + 'dtype': 'generic' |
| 139 | +}); |
| 140 | +console.log( x ); |
| 141 | + |
| 142 | +var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 ] ); |
| 143 | +console.log( mask ); |
| 144 | + |
| 145 | +gmskrev( x.length, x, 1, mask, 1 ); |
| 146 | +console.log( x ); |
| 147 | +``` |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.examples --> |
| 152 | + |
| 153 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 154 | + |
| 155 | +<section class="related"> |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.related --> |
| 160 | + |
| 161 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 162 | + |
| 163 | +<section class="links"> |
| 164 | + |
| 165 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 166 | + |
| 167 | +[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64 |
| 168 | + |
| 169 | +[@stdlib/blas/ext/base/dmskrev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dmskrev |
| 170 | + |
| 171 | +[@stdlib/blas/ext/base/smskrev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/smskrev |
| 172 | + |
| 173 | +<!-- <related-links> --> |
| 174 | + |
| 175 | +<!-- </related-links> --> |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.links --> |
0 commit comments