|
| 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 | +# sindexOf |
| 22 | + |
| 23 | +> Return the first index of a specified search element in a single-precision floating-point strided array. |
| 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 sindexOf = require( '@stdlib/blas/ext/base/sindex-of' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### sindexOf( N, searchElement, x, strideX ) |
| 44 | + |
| 45 | +Returns the first index of a specified search element in a single-precision floating-point strided array. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 49 | + |
| 50 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] ); |
| 51 | + |
| 52 | +var idx = sindexOf( x.length, 3.0, x, 1 ); |
| 53 | +// returns 2 |
| 54 | +``` |
| 55 | + |
| 56 | +The function has the following parameters: |
| 57 | + |
| 58 | +- **N**: number of indexed elements. |
| 59 | +- **searchElement**: search element. |
| 60 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 61 | +- **strideX**: stride length. |
| 62 | + |
| 63 | +If the function is unable to find a search element, the function returns `-1`. |
| 64 | + |
| 65 | +```javascript |
| 66 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 67 | + |
| 68 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] ); |
| 69 | + |
| 70 | +var idx = sindexOf( x.length, 8.0, x, 1 ); |
| 71 | +// returns -1 |
| 72 | +``` |
| 73 | + |
| 74 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to search every other element: |
| 75 | + |
| 76 | +```javascript |
| 77 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 78 | + |
| 79 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] ); |
| 80 | + |
| 81 | +var idx = sindexOf( 4, -1.0, x, 2 ); |
| 82 | +// returns 3 |
| 83 | +``` |
| 84 | + |
| 85 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 86 | + |
| 87 | +```javascript |
| 88 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 89 | + |
| 90 | +// Initial array... |
| 91 | +var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 92 | + |
| 93 | +// Create an offset view... |
| 94 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 95 | + |
| 96 | +// Find index... |
| 97 | +var idx = sindexOf( 3, -6.0, x1, 2 ); |
| 98 | +// returns 2 |
| 99 | +``` |
| 100 | + |
| 101 | +#### sindexOf.ndarray( N, searchElement, x, strideX, offsetX ) |
| 102 | + |
| 103 | +Returns the first index of a specified search element in a single-precision floating-point strided array using alternative indexing semantics. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 107 | + |
| 108 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] ); |
| 109 | + |
| 110 | +var idx = sindexOf.ndarray( x.length, 3.0, x, 1, 0 ); |
| 111 | +// returns 2 |
| 112 | +``` |
| 113 | + |
| 114 | +The function has the following additional parameters: |
| 115 | + |
| 116 | +- **offsetX**: starting index. |
| 117 | + |
| 118 | +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 of the strided array |
| 119 | + |
| 120 | +```javascript |
| 121 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 122 | + |
| 123 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] ); |
| 124 | + |
| 125 | +var idx = sindexOf.ndarray( 3, 3.0, x, 1, x.length-3 ); |
| 126 | +// returns 2 |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.usage --> |
| 132 | + |
| 133 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 134 | + |
| 135 | +<section class="notes"> |
| 136 | + |
| 137 | +## Notes |
| 138 | + |
| 139 | +- When searching for a search element, the function checks for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct, and `-0` and `+0` are considered the same. |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.notes --> |
| 144 | + |
| 145 | +<!-- Package usage examples. --> |
| 146 | + |
| 147 | +<section class="examples"> |
| 148 | + |
| 149 | +## Examples |
| 150 | + |
| 151 | +<!-- eslint no-undef: "error" --> |
| 152 | + |
| 153 | +```javascript |
| 154 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 155 | +var sindexOf = require( '@stdlib/blas/ext/base/sindex-of' ); |
| 156 | + |
| 157 | +var x = discreteUniform( 10, -100, 100, { |
| 158 | + 'dtype': 'float32' |
| 159 | +}); |
| 160 | +console.log( x ); |
| 161 | + |
| 162 | +var idx = sindexOf( x.length, 80.0, x, 1 ); |
| 163 | +console.log( idx ); |
| 164 | +``` |
| 165 | + |
| 166 | +</section> |
| 167 | + |
| 168 | +<!-- /.examples --> |
| 169 | + |
| 170 | +<!-- C interface documentation. --> |
| 171 | + |
| 172 | +* * * |
| 173 | + |
| 174 | +<section class="c"> |
| 175 | + |
| 176 | +## C APIs |
| 177 | + |
| 178 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 179 | + |
| 180 | +<section class="intro"> |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.intro --> |
| 185 | + |
| 186 | +<!-- C usage documentation. --> |
| 187 | + |
| 188 | +<section class="usage"> |
| 189 | + |
| 190 | +### Usage |
| 191 | + |
| 192 | +```c |
| 193 | +#include "stdlib/blas/ext/base/sindex_of.h" |
| 194 | +``` |
| 195 | + |
| 196 | +#### stdlib_strided_sindex_of( N, searchElement, \*X, strideX ) |
| 197 | + |
| 198 | +Returns the first index of a specified search element in a single-precision floating-point strided array. |
| 199 | + |
| 200 | +```c |
| 201 | +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; |
| 202 | + |
| 203 | +int idx = stdlib_strided_sindex_of( 4, 3.0f, x, 1 ); |
| 204 | +// returns 2 |
| 205 | +``` |
| 206 | +
|
| 207 | +The function accepts the following arguments: |
| 208 | +
|
| 209 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 210 | +- **searchElement**: `[in] float` search element. |
| 211 | +- **X**: `[in] float*` input array. |
| 212 | +- **strideX**: `[in] CBLAS_INT` stride length. |
| 213 | +
|
| 214 | +```c |
| 215 | +CBLAS_INT N stdlib_strided_sindex_of( const CBLAS_INT N, const float searchElement, float *X, const CBLAS_INT strideX ); |
| 216 | +``` |
| 217 | + |
| 218 | +#### stdlib_strided_sindex_of_ndarray( N, searchElement, \*X, strideX, offsetX ) |
| 219 | + |
| 220 | +Returns the first index of a specified search element in a single-precision floating-point strided array using alternative indexing semantics. |
| 221 | + |
| 222 | +```c |
| 223 | +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; |
| 224 | + |
| 225 | +int idx = stdlib_strided_sindex_of( 4, 3.0f, x, 1, 0 ); |
| 226 | +// returns 2 |
| 227 | +``` |
| 228 | +
|
| 229 | +The function accepts the following arguments: |
| 230 | +
|
| 231 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 232 | +- **searchElement**: `[in] float` search element. |
| 233 | +- **X**: `[in] float*` input array. |
| 234 | +- **strideX**: `[in] CBLAS_INT` stride length. |
| 235 | +- **offsetX**: `[in] CBLAS_INT` starting index. |
| 236 | +
|
| 237 | +```c |
| 238 | +CBLAS_INT stdlib_strided_sindex_of_ndarray( const CBLAS_INT N, const float searchElement, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 239 | +``` |
| 240 | + |
| 241 | +</section> |
| 242 | + |
| 243 | +<!-- /.usage --> |
| 244 | + |
| 245 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 246 | + |
| 247 | +<section class="notes"> |
| 248 | + |
| 249 | +</section> |
| 250 | + |
| 251 | +<!-- /.notes --> |
| 252 | + |
| 253 | +<!-- C API usage examples. --> |
| 254 | + |
| 255 | +<section class="examples"> |
| 256 | + |
| 257 | +### Examples |
| 258 | + |
| 259 | +```c |
| 260 | +#include "stdlib/blas/ext/base/sindex_of.h" |
| 261 | +#include <stdio.h> |
| 262 | + |
| 263 | +int main( void ) { |
| 264 | + // Create a strided array: |
| 265 | + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; |
| 266 | + |
| 267 | + // Specify the number of indexed elements: |
| 268 | + const int N = 8; |
| 269 | + |
| 270 | + // Specify a stride: |
| 271 | + const int strideX = 1; |
| 272 | + |
| 273 | + // Perform a search: |
| 274 | + int idx = stdlib_strided_sindex_of( N, 5.0f, x, strideX ); |
| 275 | + |
| 276 | + // Print the result: |
| 277 | + printf( "index value: %d\n", idx ); |
| 278 | +} |
| 279 | +``` |
| 280 | +
|
| 281 | +</section> |
| 282 | +
|
| 283 | +<!-- /.examples --> |
| 284 | +
|
| 285 | +</section> |
| 286 | +
|
| 287 | +<!-- /.c --> |
| 288 | +
|
| 289 | +<!-- 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. --> |
| 290 | +
|
| 291 | +<section class="references"> |
| 292 | +
|
| 293 | +</section> |
| 294 | +
|
| 295 | +<!-- /.references --> |
| 296 | +
|
| 297 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 298 | +
|
| 299 | +<section class="related"> |
| 300 | +
|
| 301 | +</section> |
| 302 | +
|
| 303 | +<!-- /.related --> |
| 304 | +
|
| 305 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 306 | +
|
| 307 | +<section class="links"> |
| 308 | +
|
| 309 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 310 | +
|
| 311 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 312 | +
|
| 313 | +</section> |
| 314 | +
|
| 315 | +<!-- /.links --> |
0 commit comments