Skip to content

Commit c8143c4

Browse files
feat: add lastIndexOf method to array/fixed-endian-factory
PR-URL: #3281 Closes: #3149 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 4231541 commit c8143c4

File tree

5 files changed

+442
-0
lines changed

5 files changed

+442
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,38 @@ var idx = arr.indexOf( 5.0 );
515515
// returns -1
516516
```
517517

518+
<a name="method-last-index-of"></a>
519+
520+
#### TypedArrayFE.prototype.lastIndexOf( searchElement\[, fromIndex] )
521+
522+
Returns the last index at which a given element can be found in the array.
523+
524+
```javascript
525+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
526+
527+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
528+
529+
var idx = arr.lastIndexOf( 2.0 );
530+
// returns 4
531+
532+
idx = arr.lastIndexOf( 2.0, 3 );
533+
// returns 1
534+
535+
idx = arr.lastIndexOf( 2.0, -2 );
536+
// returns 1
537+
```
538+
539+
If `searchElement` is not present in the array, the method returns `-1`.
540+
541+
```javascript
542+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
543+
544+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
545+
546+
var idx = arr.lastIndexOf( 5.0 );
547+
// returns -1
548+
```
549+
518550
<a name="method-map"></a>
519551

520552
#### TypedArray.prototype.map( callbackFn\[, thisArg] )
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 factory = require( './../lib' );
26+
var pkg = require( './../package.json' ).name;
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':lastIndexOf', function benchmark( b ) {
37+
var arr;
38+
var idx;
39+
var i;
40+
41+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
idx = arr.lastIndexOf( 5.0, arr.length - 1 );
46+
if ( typeof idx !== 'number' ) {
47+
b.fail( 'should return an integer' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isInteger( idx ) ) {
52+
b.fail( 'should return an integer' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var factory = require( './../lib' );
28+
var pkg = require( './../package.json' ).name;
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 v;
58+
var i;
59+
60+
v = len - 1;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
idx = arr.lastIndexOf( v );
65+
if ( typeof idx !== 'number' ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isInteger( idx ) ) {
71+
b.fail( 'should return an integer' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':lastIndexOf:len='+len, f );
100+
}
101+
}
102+
103+
main();

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,51 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
676676
return -1;
677677
});
678678

679+
/**
680+
* Returns the index of the last occurrence of a given element.
681+
*
682+
* @private
683+
* @name lastIndexOf
684+
* @memberof TypedArray.prototype
685+
* @type {Function}
686+
* @param {*} searchElement - element to search for
687+
* @param {integer} [fromIndex=this._length-1] - starting index (inclusive)
688+
* @throws {TypeError} `this` must be a typed array instance
689+
* @throws {TypeError} second argument must be an integer
690+
* @returns {integer} index or -1
691+
*/
692+
setReadOnly( TypedArray.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) {
693+
var buf;
694+
var i;
695+
696+
if ( !isTypedArray( this ) ) {
697+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[ 0 ] ], CTOR_NAME ) );
698+
}
699+
if ( arguments.length > 1 ) {
700+
if ( !isInteger( fromIndex ) ) {
701+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
702+
}
703+
if ( fromIndex < 0 ) {
704+
fromIndex += this._length;
705+
}
706+
if ( fromIndex < 0 ) {
707+
return -1;
708+
}
709+
if ( fromIndex >= this._length ) {
710+
fromIndex = this._length - 1;
711+
}
712+
} else {
713+
fromIndex = this._length - 1;
714+
}
715+
buf = this._buffer;
716+
for ( i = fromIndex; i >= 0; i-- ) {
717+
if ( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) === searchElement ) {
718+
return i;
719+
}
720+
}
721+
return -1;
722+
});
723+
679724
/**
680725
* Number of array elements.
681726
*

0 commit comments

Comments
 (0)