Skip to content

Commit 5a74fcb

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/dlast-index-of-truthy
PR-URL: #13001 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#857
1 parent 6a301dd commit 5a74fcb

33 files changed

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

0 commit comments

Comments
 (0)