|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# gfindIndex |
| 22 | + |
| 23 | +> Return the index of the first element which passes a test implemented by a predicate function. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var gfindIndex = require( '@stdlib/blas/ext/base/gfind-index' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### gfindIndex( N, x, strideX, clbk\[, thisArg] ) |
| 34 | + |
| 35 | +Returns the index of the first element which passes a test implemented by a predicate function. |
| 36 | + |
| 37 | +```javascript |
| 38 | +function isEven( v ) { |
| 39 | + return v % 2.0 === 0.0; |
| 40 | +} |
| 41 | + |
| 42 | +var x = [ 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 43 | + |
| 44 | +var idx = gfindIndex( x.length, x, 1, isEven ); |
| 45 | +// returns 3 |
| 46 | +``` |
| 47 | + |
| 48 | +If no element passes a test implemented by a predicate function, the function returns `-1`. |
| 49 | + |
| 50 | +```javascript |
| 51 | +function isEven( v ) { |
| 52 | + return v % 2.0 === 0.0; |
| 53 | +} |
| 54 | + |
| 55 | +var x = [ 1.0, 3.0, 5.0, 7.0, 9.0 ]; |
| 56 | + |
| 57 | +var idx = gfindIndex( x.length, x, 1, isEven ); |
| 58 | +// returns -1 |
| 59 | +``` |
| 60 | + |
| 61 | +The function has the following parameters: |
| 62 | + |
| 63 | +- **N**: number of indexed elements. |
| 64 | +- **x**: input array. |
| 65 | +- **strideX**: stride length. |
| 66 | +- **clbk**: callback function. |
| 67 | +- **thisArg**: execution context (_optional_). |
| 68 | + |
| 69 | +The callback function is provided the following arguments: |
| 70 | + |
| 71 | +- **value**: current array element. |
| 72 | +- **aidx**: array index. |
| 73 | +- **sidx**: strided index (`offset + aidx*stride`). |
| 74 | +- **array**: the input array. |
| 75 | + |
| 76 | +To set the callback execution context, provide a `thisArg`. |
| 77 | + |
| 78 | +```javascript |
| 79 | +function isEven( v ) { |
| 80 | + this.count += 1; |
| 81 | + return v % 2.0 === 0.0; |
| 82 | +} |
| 83 | + |
| 84 | +var x = [ 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 85 | + |
| 86 | +var ctx = { |
| 87 | + 'count': 0 |
| 88 | +}; |
| 89 | + |
| 90 | +var idx = gfindIndex( x.length, x, 1, isEven, ctx ); |
| 91 | +// returns 3 |
| 92 | + |
| 93 | +var count = ctx.count; |
| 94 | +// returns 4 |
| 95 | +``` |
| 96 | + |
| 97 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to test every other element: |
| 98 | + |
| 99 | +```javascript |
| 100 | +function isEven( v ) { |
| 101 | + return v % 2.0 === 0.0; |
| 102 | +} |
| 103 | + |
| 104 | +var x = [ 1.0, 3.0, -5.0, 0.0, 4.0, -1.0, -3.0 ]; |
| 105 | + |
| 106 | +var idx = gfindIndex( 4, x, 2, isEven ); |
| 107 | +// returns 2 |
| 108 | +``` |
| 109 | + |
| 110 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 111 | + |
| 112 | +```javascript |
| 113 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 114 | + |
| 115 | +function isEven( v ) { |
| 116 | + return v % 2.0 === 0.0; |
| 117 | +} |
| 118 | + |
| 119 | +// Initial array: |
| 120 | +var x0 = new Float64Array( [ 1.0, 3.0, 4.0, -5.0, 7.0, 6.0 ] ); |
| 121 | + |
| 122 | +// Create an offset view: |
| 123 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 124 | + |
| 125 | +var idx = gfindIndex( 3, x1, 2, isEven ); |
| 126 | +// returns 2 |
| 127 | +``` |
| 128 | + |
| 129 | +#### gfindIndex.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) |
| 130 | + |
| 131 | +Returns the index of the first element which passes a test implemented by a predicate function using alternative indexing semantics. |
| 132 | + |
| 133 | +```javascript |
| 134 | +function isEven( v ) { |
| 135 | + return v % 2.0 === 0.0; |
| 136 | +} |
| 137 | + |
| 138 | +var x = [ 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; |
| 139 | + |
| 140 | +var idx = gfindIndex.ndarray( x.length, x, 1, 0, isEven ); |
| 141 | +// returns 3 |
| 142 | +``` |
| 143 | + |
| 144 | +The function has the following additional parameters: |
| 145 | + |
| 146 | +- **offsetX**: starting index. |
| 147 | + |
| 148 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements: |
| 149 | + |
| 150 | +```javascript |
| 151 | +function isEven( v ) { |
| 152 | + return v % 2.0 === 0.0; |
| 153 | +} |
| 154 | + |
| 155 | +var x = [ 1.0, -2.0, -4.0, 5.0, -7.0, 6.0 ]; |
| 156 | + |
| 157 | +var idx = gfindIndex.ndarray( 3, x, 1, x.length-3, isEven ); |
| 158 | +// returns 2 |
| 159 | +``` |
| 160 | + |
| 161 | +</section> |
| 162 | + |
| 163 | +<!-- /.usage --> |
| 164 | + |
| 165 | +<section class="notes"> |
| 166 | + |
| 167 | +## Notes |
| 168 | + |
| 169 | +- If `N <= 0`, both functions return `-1`. |
| 170 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 171 | + |
| 172 | +</section> |
| 173 | + |
| 174 | +<!-- /.notes --> |
| 175 | + |
| 176 | +<section class="examples"> |
| 177 | + |
| 178 | +## Examples |
| 179 | + |
| 180 | +<!-- eslint no-undef: "error" --> |
| 181 | + |
| 182 | +```javascript |
| 183 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 184 | +var gfindIndex = require( '@stdlib/blas/ext/base/gfind-index' ); |
| 185 | + |
| 186 | +function isEven( v ) { |
| 187 | + return v % 2.0 === 0.0; |
| 188 | +} |
| 189 | + |
| 190 | +var x = discreteUniform( 10, -100, 100, { |
| 191 | + 'dtype': 'generic' |
| 192 | +}); |
| 193 | +console.log( x ); |
| 194 | + |
| 195 | +var idx = gfindIndex( x.length, x, 1, isEven ); |
| 196 | +console.log( idx ); |
| 197 | +``` |
| 198 | + |
| 199 | +</section> |
| 200 | + |
| 201 | +<!-- /.examples --> |
| 202 | + |
| 203 | +<!-- 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. --> |
| 204 | + |
| 205 | +<section class="references"> |
| 206 | + |
| 207 | +</section> |
| 208 | + |
| 209 | +<!-- /.references --> |
| 210 | + |
| 211 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 212 | + |
| 213 | +<section class="related"> |
| 214 | + |
| 215 | +</section> |
| 216 | + |
| 217 | +<!-- /.related --> |
| 218 | + |
| 219 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 220 | + |
| 221 | +<section class="links"> |
| 222 | + |
| 223 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 224 | + |
| 225 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 226 | + |
| 227 | +</section> |
| 228 | + |
| 229 | +<!-- /.links --> |
0 commit comments