Skip to content

Commit f00edc4

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add blas/ext/base/glast-index-of
PR-URL: #7598 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 0322739 commit f00edc4

File tree

14 files changed

+1295
-0
lines changed

14 files changed

+1295
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
# glastIndexOf
22+
23+
> Return the last index of a specified search element in a 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 glastIndexOf = require( '@stdlib/blas/ext/base/glast-index-of' );
41+
```
42+
43+
#### glastIndexOf( N, searchElement, x, strideX )
44+
45+
Returns the last index of a specified search element in a strided array.
46+
47+
```javascript
48+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, 3.0, -1.0 ];
49+
50+
var idx = glastIndexOf( x.length, 3.0, x, 1 );
51+
// returns 6
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **N**: number of indexed elements.
57+
- **searchElement**: search element.
58+
- **x**: input array.
59+
- **strideX**: stride length.
60+
61+
If the function is unable to find a search element, the function returns `-1`.
62+
63+
```javascript
64+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ];
65+
66+
var idx = glastIndexOf( x.length, 8.0, x, 1 );
67+
// returns -1
68+
```
69+
70+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to search every other element:
71+
72+
```javascript
73+
var x = [ -2.0, -1.0, 3.0, -5.0, 3.0, 4.0, -1.0, 0.0 ];
74+
75+
var idx = glastIndexOf( 4, 3.0, x, 2 );
76+
// returns 2
77+
```
78+
79+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
80+
81+
```javascript
82+
var Float64Array = require( '@stdlib/array/float64' );
83+
84+
// Initial array:
85+
var x0 = new Float64Array( [ -2.0, 3.0, -6.0, -4.0, 5.0, 3.0 ] );
86+
87+
// Create an offset view:
88+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
// Find index:
91+
var idx = glastIndexOf( 3, 3.0, x1, 2 );
92+
// returns 2
93+
```
94+
95+
#### glastIndexOf.ndarray( N, searchElement, x, strideX, offsetX )
96+
97+
Returns the last index of a specified search element in a strided array using alternative indexing semantics.
98+
99+
```javascript
100+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 3.0, -1.0, 0.0 ];
101+
102+
var idx = glastIndexOf.ndarray( x.length, 3.0, x, 1, 0 );
103+
// returns 5
104+
```
105+
106+
The function has the following additional parameters:
107+
108+
- **offsetX**: starting index.
109+
110+
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
111+
112+
```javascript
113+
var x = [ -2.0, 1.0, 0.0, -5.0, 4.0, 3.0, 3.0, -1.0 ];
114+
115+
var idx = glastIndexOf.ndarray( 3, 3.0, x, 1, x.length-3 );
116+
// returns 1
117+
```
118+
119+
</section>
120+
121+
<!-- /.usage -->
122+
123+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="notes">
126+
127+
## Notes
128+
129+
- 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.
130+
- 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]).
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<!-- Package usage examples. -->
137+
138+
<section class="examples">
139+
140+
## Examples
141+
142+
<!-- eslint no-undef: "error" -->
143+
144+
```javascript
145+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
146+
var glastIndexOf = require( '@stdlib/blas/ext/base/glast-index-of' );
147+
148+
var x = discreteUniform( 10, -100, 100, {
149+
'dtype': 'generic'
150+
});
151+
console.log( x );
152+
153+
var idx = glastIndexOf.ndarray( x.length, 80.0, x, 1, 0 );
154+
console.log( idx );
155+
```
156+
157+
</section>
158+
159+
<!-- /.examples -->
160+
161+
<!-- 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. -->
162+
163+
<section class="references">
164+
165+
</section>
166+
167+
<!-- /.references -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
182+
183+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
184+
185+
</section>
186+
187+
<!-- /.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 glastIndexOf = 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, 'generic' );
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 = glastIndexOf( x.length, len+1, x, 1 );
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+':len='+len, f );
93+
}
94+
}
95+
96+
main();
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 glastIndexOf = require( './../lib/ndarray.js' );
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, 'generic' );
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 = glastIndexOf( x.length, len+1, 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+':ndarray:len='+len, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)