Skip to content

Commit cec843e

Browse files
committed
feat: add findIndex method to array/fixed-endian-factory
1 parent 4eb9aea commit cec843e

File tree

5 files changed

+418
-0
lines changed

5 files changed

+418
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,62 @@ v = arr.at( -100 );
338338
// returns undefined
339339
```
340340

341+
<a name="method-find-index"></a>
342+
343+
#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] )
344+
345+
Returns the index of the first element in a typed array that satisfies the provided testing function.
346+
If no element satisfies function, return -1.
347+
348+
```javascript
349+
function greaterthantwo( v ) {
350+
return ( v > 2.0 );
351+
}
352+
353+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
354+
355+
var arr = new Float64ArrayFE( 'little-endian', 3 );
356+
357+
arr.set( 1.5, 0 );
358+
arr.set( 2.5, 1 );
359+
arr.set( 3.5, 2 );
360+
361+
var idx = arr.findIndex( greaterthantwo );
362+
// returns 1
363+
```
364+
365+
The invoked function is provided three arguments:
366+
367+
- **value**: current array element.
368+
- **index**: current array element index.
369+
- **arr**: the array on which this method was called.
370+
371+
To set the function execution context, provide a `thisArg`.
372+
373+
```javascript
374+
function fcn( v, i ) {
375+
this.count += 1;
376+
return (v === 2.0);
377+
}
378+
379+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
380+
381+
var arr = new Float64ArrayFE( 'little-endian', 3 );
382+
383+
var context = {
384+
'count': 0
385+
};
386+
387+
arr.set( 1.0, 0 );
388+
arr.set( 2.0, 1 );
389+
arr.set( 3.0, 2 );
390+
391+
arr.findIndex( fcn, context );
392+
393+
var count = context.count;
394+
// returns 2
395+
```
396+
341397
<a name="method-for-each"></a>
342398

343399
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':findIndex', function benchmark( b ) {
32+
var ctor;
33+
var arr;
34+
var idx;
35+
var i;
36+
37+
ctor = factory( 'float64' );
38+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
39+
idx = arr.findIndex( predicate );
40+
41+
function predicate( v ) {
42+
return ( v === 3.0 );
43+
}
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
idx = arr.findIndex( predicate );
48+
if ( typeof idx !== 'number' ) {
49+
b.fail( 'should return an integer' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isInteger( idx ) ) {
54+
b.fail( 'should return an integer' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 zeroTo = require( '@stdlib/array/zero-to' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveInteger} len - array length
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( len ) {
46+
var arr = new Float64ArrayFE( 'little-endian', zeroTo(len));
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var idx;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
idx=arr.findIndex( callback );
62+
if ( typeof idx !== 'number' ) {
63+
b.fail( 'should return an integer' );
64+
}
65+
}
66+
b.toc();
67+
if ( arr.length !== len ) {
68+
b.fail( 'should not change an array length' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
73+
function callback( value ) {
74+
if ( isnan( value ) ) {
75+
throw new Error( 'something went wrong' );
76+
}
77+
}
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':forEach:len='+len, f );
103+
}
104+
}
105+
106+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,39 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
538538
*/
539539
setReadOnly( TypedArray.prototype, 'BYTES_PER_ELEMENT', TypedArray.BYTES_PER_ELEMENT );
540540

541+
/**
542+
* Invokes a function once for each array element.
543+
*
544+
* @name findIndex
545+
* @memberof TypedArray.prototype
546+
* @type {Function}
547+
* @param {Function} callbackFn - function to invoke
548+
* @param {*} [thisArg] - function invocation context
549+
* @throws {TypeError} `this` must be a typed array instance
550+
* @throws {TypeError} first argument must be a function
551+
*/
552+
setReadOnly( TypedArray.prototype, 'findIndex', function findIndex( fcn, thisArg ) {
553+
var result;
554+
var buf;
555+
var i;
556+
557+
if ( !isTypedArray( this ) ) {
558+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
559+
}
560+
if ( !isFunction( fcn ) ) {
561+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
562+
}
563+
564+
buf = this._buffer;
565+
for (i = 0; i < this._length; i++) {
566+
result = fcn.call(thisArg, buf[GETTER](i * BYTES_PER_ELEMENT, this._isLE), i, this);
567+
if (result) {
568+
return i;
569+
}
570+
}
571+
return -1; // Not found
572+
});
573+
541574
/**
542575
* Invokes a function once for each array element.
543576
*

0 commit comments

Comments
 (0)