From 875d83eea653d640bbe5fb0367f8f36585d61e68 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 21 Aug 2026 18:13:18 +0500 Subject: [PATCH 1/6] feat: add fromIndex support to blas/ext/base/ndarray/gfind-last-index --- .../base/ndarray/gfind-last-index/README.md | 36 +++- .../gfind-last-index/benchmark/benchmark.js | 11 +- .../ndarray/gfind-last-index/docs/repl.txt | 13 +- .../gfind-last-index/docs/types/index.d.ts | 10 +- .../gfind-last-index/docs/types/test.ts | 48 +++-- .../gfind-last-index/examples/index.js | 7 +- .../ndarray/gfind-last-index/lib/index.js | 7 +- .../base/ndarray/gfind-last-index/lib/main.js | 27 ++- .../ndarray/gfind-last-index/test/test.js | 178 ++++++++++++++++-- 9 files changed, 290 insertions(+), 47 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/README.md index cc9e83da408d..7da35c028a41 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/README.md @@ -41,6 +41,7 @@ var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ) Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function. ```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var vector = require( '@stdlib/ndarray/vector/ctor' ); function isEven( v ) { @@ -49,13 +50,18 @@ function isEven( v ) { var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' ); -var idx = gfindLastIndex( [ x ], isEven ); +var fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndex( [ x, fromIndex ], isEven ); // returns 3 ``` If no element passes a test implemented by a predicate function, the function returns `-1`. ```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var vector = require( '@stdlib/ndarray/vector/ctor' ); function isEven( v ) { @@ -64,14 +70,23 @@ function isEven( v ) { var x = vector( [ 1.0, 3.0, 5.0, 7.0 ], 'generic' ); -var idx = gfindLastIndex( [ x ], isEven ); +var fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndex( [ x, fromIndex ], isEven ); // returns -1 ``` The function has the following parameters: -- **arrays**: array-like object containing a one-dimensional input ndarray. +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the index from which to begin searching. + - **clbk**: callback function. + - **thisArg**: callback execution context (_optional_). The callback function is provided the following arguments: @@ -83,6 +98,7 @@ The callback function is provided the following arguments: To set the callback execution context, provide a `thisArg`. ```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var vector = require( '@stdlib/ndarray/vector/ctor' ); function isEven( v ) { @@ -95,7 +111,11 @@ var ctx = { 'count': 0 }; -var v = gfindLastIndex( [ x ], isEven, ctx ); +var fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var v = gfindLastIndex( [ x, fromIndex ], isEven, ctx ); // returns 3 var count = ctx.count; @@ -111,6 +131,7 @@ var count = ctx.count; ## Notes - If provided an empty one-dimensional ndarray, the function returns `-1`. +- If a specified starting search index is negative, the function resolves the starting search index by counting backward from the last element (where `-1` refers to the last element). @@ -124,6 +145,7 @@ var count = ctx.count; ```javascript var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); @@ -138,7 +160,11 @@ var opts = { var x = discreteUniform( [ 10 ], -100, 100, opts ); console.log( ndarray2array( x ) ); -var idx = gfindLastIndex( [ x ], isEven ); +var fromIndex = scalar2ndarray( 9, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndex( [ x, fromIndex ], isEven ); console.log( idx ); ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/benchmark/benchmark.js index c4f53079a3d8..bf9d63a078c1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var uniform = require( '@stdlib/random/uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var format = require( '@stdlib/string/format' ); @@ -57,7 +58,13 @@ function clbk( v ) { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = uniform( [ len ], 0.0, 100.0, options ); + var fromIndex; + var x; + + x = uniform( [ len ], 0.0, 100.0, options ); + fromIndex = scalar2ndarray( len - 1, { + 'dtype': 'generic' + }); return benchmark; /** @@ -72,7 +79,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = gfindLastIndex( [ x ], clbk ); + v = gfindLastIndex( [ x, fromIndex ], clbk ); if ( isnan( v ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/repl.txt index 16e78d37065f..90a6fe2267d9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/repl.txt @@ -3,6 +3,10 @@ Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function. + If a specified starting search index is negative, the function resolves the + starting search index by counting backward from the last element (where `-1` + refers to the last element). + If provided an empty ndarray or no element passes a test implemented by a predicate function, the function returns `-1`. @@ -15,7 +19,11 @@ Parameters ---------- arrays: ArrayLikeObject - Array-like object containing a one-dimensional input ndarray. + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the index from which to begin + searching. clbk: Function Callback function. @@ -31,8 +39,9 @@ Examples -------- > var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 1.0, -2.0, 2.0 ], 'generic' ); + > var i = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } ); > function f ( v ) { return v % 2.0 === 0.0; }; - > {{alias}}( [ x ], f ) + > {{alias}}( [ x, i ], f ) 2 See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/index.d.ts index 14f478b465fc..110903465b62 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/index.d.ts @@ -74,6 +74,7 @@ type Predicate = Nullary | Unary | Binary = Nullary | Unary | Binary = Nullary | Unary | Binary = typedndarray, ThisArg = unknown>( arrays: [ U ], clbk: Predicate, thisArg?: ThisParameterType> ): number; +declare function gfindLastIndex = typedndarray, ThisArg = unknown>( arrays: [ U, typedndarray ], clbk: Predicate, thisArg?: ThisParameterType> ): number; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/test.ts index 2a2fb712c1ea..efa35528a1bd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/test.ts @@ -19,6 +19,7 @@ /* eslint-disable space-in-parens */ import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); import gfindLastIndex = require( './index' ); /** @@ -39,9 +40,12 @@ function clbk( value: any ): boolean { const x = zeros( [ 10 ], { 'dtype': 'generic' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - gfindLastIndex( [ x ], clbk ); // $ExpectType number - gfindLastIndex( [ x ], clbk, {} ); // $ExpectType number + gfindLastIndex( [ x, fromIndex ], clbk ); // $ExpectType number + gfindLastIndex( [ x, fromIndex ], clbk, {} ); // $ExpectType number } // The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... @@ -72,24 +76,27 @@ function clbk( value: any ): boolean { const x = zeros( [ 10 ], { 'dtype': 'generic' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - gfindLastIndex( [ x ], '10' ); // $ExpectError - gfindLastIndex( [ x ], 10 ); // $ExpectError - gfindLastIndex( [ x ], true ); // $ExpectError - gfindLastIndex( [ x ], false ); // $ExpectError - gfindLastIndex( [ x ], null ); // $ExpectError - gfindLastIndex( [ x ], undefined ); // $ExpectError - gfindLastIndex( [ x ], [] ); // $ExpectError - gfindLastIndex( [ x ], {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], '10' ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], 10 ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], true ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], false ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], null ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], undefined ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], [] ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], {} ); // $ExpectError - gfindLastIndex( [ x ], '10', {} ); // $ExpectError - gfindLastIndex( [ x ], 10, {} ); // $ExpectError - gfindLastIndex( [ x ], true, {} ); // $ExpectError - gfindLastIndex( [ x ], false, {} ); // $ExpectError - gfindLastIndex( [ x ], null, {} ); // $ExpectError - gfindLastIndex( [ x ], undefined, {} ); // $ExpectError - gfindLastIndex( [ x ], [], {} ); // $ExpectError - gfindLastIndex( [ x ], {}, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], '10', {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], 10, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], true, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], false, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], null, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], undefined, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], [], {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], {}, {} ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -97,7 +104,10 @@ function clbk( value: any ): boolean { const x = zeros( [ 10 ], { 'dtype': 'generic' }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); gfindLastIndex(); // $ExpectError - gfindLastIndex( [ x ], clbk, {}, {} ); // $ExpectError + gfindLastIndex( [ x, fromIndex ], clbk, {}, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/examples/index.js index 0e8f69cf35f5..6f06385b6457 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/examples/index.js @@ -19,6 +19,7 @@ 'use strict'; var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var gfindLastIndex = require( './../lib' ); @@ -33,5 +34,9 @@ var opts = { var x = discreteUniform( [ 10 ], -100, 100, opts ); console.log( ndarray2array( x ) ); -var idx = gfindLastIndex( [ x ], isEven ); +var fromIndex = scalar2ndarray( 9, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndex( [ x, fromIndex ], isEven ); console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/index.js index 7baa54d88ccf..aaccec378425 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/index.js @@ -24,6 +24,7 @@ * @module @stdlib/blas/ext/base/ndarray/gfind-last-index * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var vector = require( '@stdlib/ndarray/vector/ctor' ); * var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); * @@ -33,7 +34,11 @@ * * var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' ); * -* var v = gfindLastIndex( [ x ], clbk ); +* var fromIndex = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var v = gfindLastIndex( [ x, fromIndex ], clbk ); * // returns 3 */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js index e50329db048a..f05c20ddc5e5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js @@ -20,7 +20,9 @@ // MODULES // +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); @@ -37,6 +39,7 @@ var strided = require( '@stdlib/blas/ext/base/gfind-last-index' ).ndarray; * - The function expects the following ndarrays: * * - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the index from which to begin searching. * * @param {ArrayLikeObject} arrays - array-like object containing ndarrays * @param {Function} clbk - callback function @@ -44,6 +47,7 @@ var strided = require( '@stdlib/blas/ext/base/gfind-last-index' ).ndarray; * @returns {integer} index * * @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var vector = require( '@stdlib/ndarray/vector/ctor' ); * * function isEven( v ) { @@ -52,12 +56,27 @@ var strided = require( '@stdlib/blas/ext/base/gfind-last-index' ).ndarray; * * var x = vector( [ 1.0, 3.0, 4.0, 2.0 ], 'generic' ); * -* var v = gfindLastIndex( [ x ], isEven ); +* var fromIndex = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var v = gfindLastIndex( [ x, fromIndex ], isEven ); * // returns 3 */ function gfindLastIndex( arrays, clbk, thisArg ) { - var x = arrays[ 0 ]; - return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), wrapper, null ); // eslint-disable-line max-len + var fromIndex; + var N; + var x; + + x = arrays[ 0 ]; + fromIndex = ndarraylike2scalar( arrays[ 1 ] ); + + N = numelDimension( x, 0 ); + fromIndex = clipIndex( fromIndex, N ); + if ( fromIndex >= N ) { + fromIndex = N - 1; + } + return strided( fromIndex+1, getData( x ), getStride( x, 0 ), getOffset( x ), wrapper, null ); // eslint-disable-line max-len /** * Invokes a provided callback. @@ -70,7 +89,7 @@ function gfindLastIndex( arrays, clbk, thisArg ) { * @returns {*} result */ function wrapper( value, aidx ) { - return clbk.call( thisArg, value, aidx, x ); + return clbk.call( thisArg, value, fromIndex - aidx, x ); } } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index 15178ac3e6e7..cb5d8b933476 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -21,6 +21,7 @@ // MODULES // var tape = require( 'tape' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var gfindLastIndex = require( './../lib' ); @@ -51,21 +52,124 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function returns the index of the last element which passes a test implemented by a predicate function', function test( t ) { + var fromIndex; var actual; var x; x = [ 1.0, 3.0, -2.0, 5.0, 0.0, 4.0 ]; - actual = gfindLastIndex( [ vector( x, 6, 1, 0 ) ], isEven ); + fromIndex = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + + actual = gfindLastIndex( [ vector( x, 6, 1, 0 ), fromIndex ], isEven ); t.strictEqual( actual, 5, 'returns expected value' ); x = [ 4.0, 5.0 ]; - actual = gfindLastIndex( [ vector( x, 2, 1, 0 ) ], isEven ); + fromIndex = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + + actual = gfindLastIndex( [ vector( x, 2, 1, 0 ), fromIndex ], isEven ); t.strictEqual( actual, 0, 'returns expected value' ); x = [ 1.0, 3.0, 5.0 ]; - actual = gfindLastIndex( [ vector( x, 3, 1, 0 ) ], isEven ); + fromIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + actual = gfindLastIndex( [ vector( x, 3, 1, 0 ), fromIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports a `fromIndex` parameter (nonnegative)', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 2, 'returns expected value' ); + + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports a `fromIndex` parameter (negative)', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( -2, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); t.strictEqual( actual, -1, 'returns expected value' ); + x = vector( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( -6, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 0, 'returns expected value' ); + + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 0, 'returns expected value' ); + t.end(); function isEven( v ) { @@ -74,12 +178,16 @@ tape( 'the function returns the index of the last element which passes a test im }); tape( 'the function returns `-1` if provided an empty input ndarray', function test( t ) { + var fromIndex; var actual; var x; x = vector( [], 0, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); - actual = gfindLastIndex( [ x ], isEven ); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); t.strictEqual( actual, -1, 'returns expected value' ); t.end(); @@ -89,7 +197,34 @@ tape( 'the function returns `-1` if provided an empty input ndarray', function t } }); +tape( 'the function clamps a starting search index which is greater than or equal to number of elements in the input ndarray', function test( t ) { + var fromIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( 6, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( 7, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var fromIndex; var actual; var x; @@ -103,8 +238,11 @@ tape( 'the function supports one-dimensional ndarrays having non-unit strides', 4.0, // 3 2.0 ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); - actual = gfindLastIndex( [ vector( x, 4, 2, 0 ) ], isEven ); + actual = gfindLastIndex( [ vector( x, 4, 2, 0 ), fromIndex ], isEven ); t.strictEqual( actual, 3, 'returns expected value' ); t.end(); @@ -115,6 +253,7 @@ tape( 'the function supports one-dimensional ndarrays having non-unit strides', }); tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var fromIndex; var actual; var x; @@ -128,8 +267,11 @@ tape( 'the function supports one-dimensional ndarrays having negative strides', 4.0, // 0 2.0 ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); - actual = gfindLastIndex( [ vector( x, 4, -2, 6 ) ], isEven ); + actual = gfindLastIndex( [ vector( x, 4, -2, 6 ), fromIndex ], isEven ); t.strictEqual( actual, 2, 'returns expected value' ); t.end(); @@ -140,6 +282,7 @@ tape( 'the function supports one-dimensional ndarrays having negative strides', }); tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var fromIndex; var actual; var x; @@ -153,8 +296,11 @@ tape( 'the function supports one-dimensional ndarrays having non-zero offsets', 3.0, 4.0 // 3 ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); - actual = gfindLastIndex( [ vector( x, 4, 2, 1 ) ], isEven ); + actual = gfindLastIndex( [ vector( x, 4, 2, 1 ), fromIndex ], isEven ); t.strictEqual( actual, 3, 'returns expected value' ); t.end(); @@ -165,6 +311,7 @@ tape( 'the function supports one-dimensional ndarrays having non-zero offsets', }); tape( 'the function supports providing an execution context', function test( t ) { + var fromIndex; var expected; var indices; var values; @@ -193,22 +340,31 @@ tape( 'the function supports providing an execution context', function test( t ) arrays = []; arr = vector( x, 4, 2, 0 ); - actual = gfindLastIndex( [ arr ], isEven, ctx ); + fromIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ arr, fromIndex ], isEven, ctx ); - t.strictEqual( actual, 3, 'returns expected value' ); - t.strictEqual( ctx.count, 1, 'returns expected value' ); + t.strictEqual( actual, -1, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); expected = [ - 4.0 + -7.0, + 3.0, + 1.0 ]; t.deepEqual( values, expected, 'returns expected value' ); expected = [ + 2, + 1, 0 ]; t.deepEqual( indices, expected, 'returns expected value' ); expected = [ + arr, + arr, arr ]; t.deepEqual( arrays, expected, 'returns expected value' ); From c92f39f8370d20c13b6c1cd1ba55923ee3eb9959 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 23 Aug 2026 17:00:46 +0500 Subject: [PATCH 2/6] refactor: apply suggestions from code review --- .../blas/ext/base/ndarray/gfind-last-index/lib/main.js | 8 ++++---- .../blas/ext/base/ndarray/gfind-last-index/test/test.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js index f05c20ddc5e5..73d10e528707 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js @@ -22,7 +22,7 @@ var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); -var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); +var clipUpperIndex = require( '@stdlib/ndarray/base/clip-upper-index' ); var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); @@ -72,9 +72,9 @@ function gfindLastIndex( arrays, clbk, thisArg ) { fromIndex = ndarraylike2scalar( arrays[ 1 ] ); N = numelDimension( x, 0 ); - fromIndex = clipIndex( fromIndex, N ); - if ( fromIndex >= N ) { - fromIndex = N - 1; + fromIndex = clipUpperIndex( fromIndex, N-1 ); + if ( fromIndex < 0 ) { + return -1; } return strided( fromIndex+1, getData( x ), getStride( x, 0 ), getOffset( x ), wrapper, null ); // eslint-disable-line max-len diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index cb5d8b933476..aa7ef4518e48 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -136,7 +136,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 5, 'returns expected value' ); + t.strictEqual( actual, 3, 'returns expected value' ); fromIndex = scalar2ndarray( -2, { 'dtype': 'generic' @@ -148,7 +148,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 3, 'returns expected value' ); + t.strictEqual( actual, 1, 'returns expected value' ); fromIndex = scalar2ndarray( -7, { 'dtype': 'generic' @@ -162,13 +162,13 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 0, 'returns expected value' ); + t.strictEqual( actual, -1, 'returns expected value' ); fromIndex = scalar2ndarray( -7, { 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 0, 'returns expected value' ); + t.strictEqual( actual, -1, 'returns expected value' ); t.end(); From 01b98d6bfb72d898e65e4dabf1bf17b3b1bac3b3 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 25 Aug 2026 18:35:47 -0700 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../ext/base/ndarray/gfind-last-index/lib/main.js | 2 +- .../ext/base/ndarray/gfind-last-index/test/test.js | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js index 73d10e528707..faa0fd71cd70 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js @@ -72,7 +72,7 @@ function gfindLastIndex( arrays, clbk, thisArg ) { fromIndex = ndarraylike2scalar( arrays[ 1 ] ); N = numelDimension( x, 0 ); - fromIndex = clipUpperIndex( fromIndex, N-1 ); + fromIndex = clipUpperIndex( fromIndex, ( fromIndex < 0 ) ? N : N-1 ); if ( fromIndex < 0 ) { return -1; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index aa7ef4518e48..ef150df295ed 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -136,7 +136,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 3, 'returns expected value' ); + t.strictEqual( actual, 5, 'returns expected value' ); fromIndex = scalar2ndarray( -2, { 'dtype': 'generic' @@ -162,13 +162,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, -1, 'returns expected value' ); - - fromIndex = scalar2ndarray( -7, { - 'dtype': 'generic' - }); - actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, -1, 'returns expected value' ); + t.strictEqual( actual, 0, 'returns expected value' ); t.end(); @@ -197,7 +191,7 @@ tape( 'the function returns `-1` if provided an empty input ndarray', function t } }); -tape( 'the function clamps a starting search index which is greater than or equal to number of elements in the input ndarray', function test( t ) { +tape( 'the function clamps a starting search index which is greater than or equal to the number of elements in the input ndarray', function test( t ) { var fromIndex; var actual; var x; From ca3a2327f64d6e95b194233d02d2b09cc8ae2c1f Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 25 Aug 2026 18:39:36 -0700 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../blas/ext/base/ndarray/gfind-last-index/test/test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index ef150df295ed..899539f4d33c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -148,6 +148,12 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( -4, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); t.strictEqual( actual, 1, 'returns expected value' ); fromIndex = scalar2ndarray( -7, { From c6217de424b2046a4091874985ce9fd1dfe22624 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 25 Aug 2026 18:41:58 -0700 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../blas/ext/base/ndarray/gfind-last-index/test/test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index 899539f4d33c..31ce01409386 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -156,6 +156,12 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( actual = gfindLastIndex( [ x, fromIndex ], isEven ); t.strictEqual( actual, 1, 'returns expected value' ); + fromIndex = scalar2ndarray( -5, { + 'dtype': 'generic' + }); + actual = gfindLastIndex( [ x, fromIndex ], isEven ); + t.strictEqual( actual, 1, 'returns expected value' ); + fromIndex = scalar2ndarray( -7, { 'dtype': 'generic' }); @@ -168,7 +174,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, 0, 'returns expected value' ); + t.strictEqual( actual, -1, 'returns expected value' ); t.end(); From 88b0f8f750433b6a9b0098fc397ddf6286047120 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 25 Aug 2026 18:42:35 -0700 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js index 31ce01409386..1b47e038f299 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -174,7 +174,7 @@ tape( 'the function supports a `fromIndex` parameter (negative)', function test( 'dtype': 'generic' }); actual = gfindLastIndex( [ x, fromIndex ], isEven ); - t.strictEqual( actual, -1, 'returns expected value' ); + t.strictEqual( actual, 0, 'returns expected value' ); t.end();