Skip to content

Commit 864303f

Browse files
committed
feat: add index-of
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 38c6617 commit 864303f

File tree

10 files changed

+838
-0
lines changed

10 files changed

+838
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 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\[, offsetX ] )
44+
45+
Returns the 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**: element to search the index of.
60+
- **x**: input [`Float32Array`][@stdlib/array/float32].
61+
- **strideX**: index increment.
62+
- **offsetX**: starting index (_optional_).
63+
64+
To begin searching from specific index, provide an `offset`.
65+
66+
```javascript
67+
var Float32Array = require( '@stdlib/array/float32' );
68+
69+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );
70+
71+
var idx = sindexof( x.length, 3.0, x, 1, 5 );
72+
// returns 7
73+
```
74+
75+
If both `offset` and `stride` are less than zero, the search begins from the last array element and proceeds towards the first element.
76+
77+
```javascript
78+
var Float32Array = require( '@stdlib/array/float32' );
79+
80+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );
81+
82+
var idx = sindexof( x.length, 3.0, x, -1, -2 );
83+
// returns 2
84+
```
85+
86+
If the function is unable to find an element which equals a provided search element, the function returns `-1`.
87+
88+
```javascript
89+
var Float32Array = require( '@stdlib/array/float32' );
90+
91+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );
92+
93+
var idx = sindexof( x.length, 8.0, x, 1 );
94+
// returns -1
95+
```
96+
97+
</section>
98+
99+
<!-- /.usage -->
100+
101+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- 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.
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<!-- Package usage examples. -->
114+
115+
<section class="examples">
116+
117+
## Examples
118+
119+
<!-- eslint no-undef: "error" -->
120+
121+
```javascript
122+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
123+
var sindexof = require( '@stdlib/blas/ext/base/sindex-of' );
124+
125+
var x = discreteUniform( 10, -100, 100, {
126+
'dtype': 'float32'
127+
});
128+
129+
var idx = sindexof( x.length, 2.0, x, 1 );
130+
console.log( idx );
131+
132+
idx = sindexof( x.length, 12.0, x, 2, 5 );
133+
console.log( idx );
134+
135+
idx = sindexof( x.length, -57.0, x, 1, -2 );
136+
console.log( idx );
137+
138+
idx = sindexof( x.length, 92.0, x, -1, -2 );
139+
console.log( idx );
140+
```
141+
142+
</section>
143+
144+
<!-- /.examples -->
145+
146+
<!-- 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. -->
147+
148+
<section class="references">
149+
150+
</section>
151+
152+
<!-- /.references -->
153+
154+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
155+
156+
<section class="related">
157+
158+
</section>
159+
160+
<!-- /.related -->
161+
162+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
163+
164+
<section class="links">
165+
166+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
167+
168+
</section>
169+
170+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var oneTo = require( '@stdlib/array/one-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var sindexof = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = oneTo( len, 'float32' );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = sindexof( x.length, 2.0, x, 1, 0 );
57+
if ( out !== out ) {
58+
b.fail( 'should return an integer' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isInteger( out ) ) {
63+
b.fail( 'should return an integer' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=float32,len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( N, searchElement, x, strideX[, offsetX ] )
3+
Returns the index of a specified search element in a single-precision
4+
floating-point strided array.
5+
6+
If unable to find an element which equals a provided search element, the
7+
function returns -1.
8+
9+
Parameters
10+
----------
11+
N: integer
12+
Number of indexed elements.
13+
14+
searchElement: number
15+
Search element.
16+
17+
x: Float32Array
18+
Input array.
19+
20+
strideX: integer
21+
Index increment.
22+
23+
offsetX: integer (optional)
24+
Starting index (inclusive). If less than zero, the starting index is
25+
resolved relative to the last array element, with the last array element
26+
corresponding to `offsetX = -1`.
27+
28+
Returns
29+
-------
30+
out: integer
31+
Index or -1.
32+
33+
Examples
34+
--------
35+
> var x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ] );
36+
> var out = {{alias}}( x.length, 1.0, x, 1 )
37+
1
38+
39+
See Also
40+
--------
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the index of a specified search element in a single-precision floating-point strided array.
23+
*
24+
* ## Notes
25+
*
26+
* - If unable to find an element which equals a provided search element, the function returns `-1`.
27+
* - If `offsetX` is less than zero, the starting index is resolved relative to the last array element, with the last array element corresponding to `offsetX = -1`.
28+
*
29+
* @param N - number of indexed elements
30+
* @param searchElement - search element
31+
* @param x - input array
32+
* @param strideX - stride length
33+
* @param offsetX - starting index
34+
* @returns index
35+
*
36+
* @example
37+
* var Float32Array = require( '@stdlib/array/float32' );
38+
* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
39+
*
40+
* var idx = sindexof( x.length, 2.0, x, 1 );
41+
* // returns 1
42+
*/
43+
declare function sindexof( N: number, searchElement: number, x: Float32Array, strideX: number, offsetX?: number ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = sindexof;

0 commit comments

Comments
 (0)