diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/README.md new file mode 100644 index 000000000000..e7e2e3f7683d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/README.md @@ -0,0 +1,207 @@ + + +# gfindLastIndexBetween + +> Return the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range. + +
+ +
+ + + +
+ +## Usage + +```javascript +var gfindLastIndexBetween = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index-between' ); +``` + +#### gfindLastIndexBetween( arrays, clbk\[, thisArg] ) + +Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range. + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var vector = require( '@stdlib/ndarray/vector/ctor' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var x = vector( [ 2.0, 1.0, 3.0, 4.0, 5.0 ], 'generic' ); + +var fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' +}); + +var toIndex = scalar2ndarray( 1, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); +// returns 3 +``` + +The function has the following parameters: + +- **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. + - a zero-dimensional ndarray containing the index at which to stop searching (exclusive). + +- **clbk**: callback function. + +- **thisArg**: callback execution context (_optional_). + +The callback function is provided the following arguments: + +- **value**: current array element. +- **idx**: current array element index. +- **array**: the input ndarray. + +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 ) { + return v % 2.0 === 0.0; +} + +var x = vector( [ 2.0, 1.0, 3.0, 5.0, 7.0 ], 'generic' ); + +var fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' +}); + +var toIndex = scalar2ndarray( 1, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); +// returns -1 +``` + +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 ) { + this.count += 1; + return v % 2.0 === 0.0; +} + +var x = vector( [ 2.0, 1.0, 3.0, 4.0, 5.0 ], 'generic' ); +var ctx = { + 'count': 0 +}; + +var fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' +}); + +var toIndex = scalar2ndarray( 1, { + 'dtype': 'generic' +}); + +var v = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven, ctx ); +// returns 3 + +var count = ctx.count; +// returns 2 +``` + +
+ + + +
+ +## 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). +- If a specified ending search index is negative, the function resolves the ending search index by counting backward from the last element (where `-1` refers to the last element). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var gfindLastIndexBetween = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index-between' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var opts = { + 'dtype': 'generic' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var fromIndex = scalar2ndarray( 8, { + 'dtype': 'generic' +}); + +var toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); +console.log( idx ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/benchmark/benchmark.js new file mode 100644 index 000000000000..041e9d7b68e3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/benchmark/benchmark.js @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +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' ); +var pkg = require( './../package.json' ).name; +var gfindLastIndexBetween = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - array element +* @returns {boolean} result +*/ +function clbk( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var fromIndex; + var toIndex; + var x; + + x = uniform( [ len ], 0.0, 100.0, options ); + fromIndex = scalar2ndarray( len - 1, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/repl.txt new file mode 100644 index 000000000000..3198a8f949d8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/repl.txt @@ -0,0 +1,55 @@ + +{{alias}}( arrays, clbk[, thisArg] ) + Returns the index of the last element in a one-dimensional ndarray which + passes a test implemented by a predicate function within a specified range. + + 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 a specified ending search index is negative, the function resolves the + ending 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`. + + The callback function is provided three arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the index from which to begin + searching. + - a zero-dimensional ndarray containing the index at which to stop + searching (exclusive). + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: integer + Index. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ -2.0, 1.0, 3.0, 4.0 ], 'generic' ); + > var i = {{alias:@stdlib/ndarray/from-scalar}}( 3, { 'dtype': 'generic' } ); + > var j = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } ); + > function f ( v ) { return v % 2.0 === 0.0; }; + > {{alias}}( [ x, i, j ], f ) + 3 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/index.d.ts new file mode 100644 index 000000000000..88486ddd0960 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/index.d.ts @@ -0,0 +1,113 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an element passes a test +*/ +type Nullary = ( this: ThisArg ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element passes a test +*/ +type Unary = ( this: ThisArg, value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @returns boolean indicating whether an element passes a test +*/ +type Binary = ( this: ThisArg, value: T, index: number ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns boolean indicating whether an element passes a test +*/ +type Ternary = ( this: ThisArg, value: T, index: number, array: U ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns boolean indicating whether an element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the index from which to begin searching. +* - a zero-dimensional ndarray containing the index at which to stop searching (exclusive). +* +* - If no element passes a test implemented by a predicate function, the function returns `-1`. +* +* @param arrays - array-like object containing ndarrays +* @param clbk - predicate function +* @param thisArg - predicate function execution context +* @returns index +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* +* function clbk( v ) { +* return v % 2.0 === 0.0; +* } +* +* var x = vector( [ 2.0, 1.0, 3.0, 4.0, 5.0 ], 'generic' ); +* +* var fromIndex = scalar2ndarray( 4, { +* 'dtype': 'generic' +* }); +* +* var toIndex = scalar2ndarray( 1, { +* 'dtype': 'generic' +* }); +* +* var v = gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk ); +* // returns 3 +*/ +declare function gfindLastIndexBetween = typedndarray, ThisArg = unknown>( arrays: [ U, typedndarray, typedndarray ], clbk: Predicate, thisArg?: ThisParameterType> ): number; + + +// EXPORTS // + +export = gfindLastIndexBetween; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/test.ts new file mode 100644 index 000000000000..6d6b340005bf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/docs/types/test.ts @@ -0,0 +1,122 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import gfindLastIndexBetween = require( './index' ); + +/** +* Callback function. +* +* @param value - ndarray element +* @returns result +*/ +function clbk( value: any ): boolean { + return value % 2.0 === 0.0; +} + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const fromIndex = scalar2ndarray( 8, { + 'dtype': 'generic' + }); + const toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk ); // $ExpectType number + gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + gfindLastIndexBetween( '10', clbk ); // $ExpectError + gfindLastIndexBetween( 10, clbk ); // $ExpectError + gfindLastIndexBetween( true, clbk ); // $ExpectError + gfindLastIndexBetween( false, clbk ); // $ExpectError + gfindLastIndexBetween( null, clbk ); // $ExpectError + gfindLastIndexBetween( undefined, clbk ); // $ExpectError + gfindLastIndexBetween( [], clbk ); // $ExpectError + gfindLastIndexBetween( {}, clbk ); // $ExpectError + gfindLastIndexBetween( ( x: number ): number => x, clbk ); // $ExpectError + + gfindLastIndexBetween( '10', clbk, {} ); // $ExpectError + gfindLastIndexBetween( 10, clbk, {} ); // $ExpectError + gfindLastIndexBetween( true, clbk, {} ); // $ExpectError + gfindLastIndexBetween( false, clbk, {} ); // $ExpectError + gfindLastIndexBetween( null, clbk, {} ); // $ExpectError + gfindLastIndexBetween( undefined, clbk, {} ); // $ExpectError + gfindLastIndexBetween( [], clbk, {} ); // $ExpectError + gfindLastIndexBetween( {}, clbk, {} ); // $ExpectError + gfindLastIndexBetween( ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a callback function... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const fromIndex = scalar2ndarray( 8, { + 'dtype': 'generic' + }); + const toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + gfindLastIndexBetween( [ x, fromIndex, toIndex ], '10' ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], 10 ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], true ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], false ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], null ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], undefined ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], [] ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], {} ); // $ExpectError + + gfindLastIndexBetween( [ x, fromIndex, toIndex ], '10', {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], 10, {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], true, {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], false, {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], null, {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], undefined, {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], [], {} ); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const fromIndex = scalar2ndarray( 8, { + 'dtype': 'generic' + }); + const toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + gfindLastIndexBetween(); // $ExpectError + gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/examples/index.js new file mode 100644 index 000000000000..20af0ec4c71a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/examples/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var gfindLastIndexBetween = require( './../lib' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var opts = { + 'dtype': 'generic' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var fromIndex = scalar2ndarray( 8, { + 'dtype': 'generic' +}); + +var toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var idx = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); +console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/index.js new file mode 100644 index 000000000000..eaa0deab88a5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range. +* +* @module @stdlib/blas/ext/base/ndarray/gfind-last-index-between +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var gfindLastIndexBetween = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index-between' ); +* +* function clbk( v ) { +* return v % 2.0 === 0.0; +* } +* +* var x = vector( [ 2.0, 1.0, 3.0, 4.0, 5.0 ], 'generic' ); +* +* var fromIndex = scalar2ndarray( 4, { +* 'dtype': 'generic' +* }); +* +* var toIndex = scalar2ndarray( 1, { +* 'dtype': 'generic' +* }); +* +* var v = gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk ); +* // returns 3 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/main.js new file mode 100644 index 000000000000..666054670cc3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/lib/main.js @@ -0,0 +1,123 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +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' ); +var strided = require( '@stdlib/blas/ext/base/gfind-last-index' ).ndarray; + + +// MAIN // + +/** +* Returns the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the index from which to begin searching. +* - a zero-dimensional ndarray containing the index at which to stop searching (exclusive). +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {integer} index +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* +* function clbk( v ) { +* return v % 2.0 === 0.0; +* } +* +* var x = vector( [ 2.0, 1.0, 3.0, 4.0, 5.0 ], 'generic' ); +* +* var fromIndex = scalar2ndarray( 4, { +* 'dtype': 'generic' +* }); +* +* var toIndex = scalar2ndarray( 1, { +* 'dtype': 'generic' +* }); +* +* var v = gfindLastIndexBetween( [ x, fromIndex, toIndex ], clbk ); +* // returns 3 +*/ +function gfindLastIndexBetween( arrays, clbk, thisArg ) { + var fromIndex; + var toIndex; + var stride; + var offset; + var idx; + var N; + var n; + var x; + + x = arrays[ 0 ]; + fromIndex = ndarraylike2scalar( arrays[ 1 ] ); + toIndex = ndarraylike2scalar( arrays[ 2 ] ); + + N = numelDimension( x, 0 ); + fromIndex = clipUpperIndex( fromIndex, ( fromIndex < 0 ) ? N : N-1 ); + if ( fromIndex < 0 ) { + return -1; + } + toIndex = clipUpperIndex( toIndex, ( toIndex < 0 ) ? N : N-1 ); + + n = fromIndex - toIndex; + if ( n <= 0 ) { + return -1; + } + stride = getStride( x, 0 ); + offset = getOffset( x ) + ( stride*( toIndex+1 ) ); + + idx = strided( n, getData( x ), stride, offset, wrapper, null ); + if ( idx >= 0 ) { + idx += toIndex+1; + } + return idx; + + /** + * Invokes a provided callback. + * + * @private + * @param {*} value - current array element + * @param {NonNegativeInteger} aidx - current array element index + * @param {NonNegativeInteger} sidx - current strided array element index + * @param {Collection} arr - input array + * @returns {*} result + */ + function wrapper( value, aidx ) { + return clbk.call( thisArg, value, fromIndex - aidx, x ); + } +} + + +// EXPORTS // + +module.exports = gfindLastIndexBetween; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/package.json new file mode 100644 index 000000000000..3fe2ef4da2ec --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/gfind-last-index-between", + "version": "0.0.0", + "description": "Return the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function within a specified range.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "array", + "find", + "last", + "index", + "between", + "search", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/test/test.js new file mode 100644 index 000000000000..19891bb6c34f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index-between/test/test.js @@ -0,0 +1,537 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var gfindLastIndexBetween = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'generic', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gfindLastIndexBetween, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns the index of the last element which passes a test implemented by a predicate function within a specified range', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = [ 2.0, 1.0, 3.0, 4.0, 5.0 ]; + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ vector( x, 5, 1, 0 ), fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + x = [ 1.0, 4.0, 5.0, 6.0, 3.0 ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ vector( x, 5, 1, 0 ), fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + x = [ 2.0, 1.0, 3.0, 5.0, 4.0 ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ vector( x, 5, 1, 0 ), fromIndex, toIndex ], 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 toIndex; + var actual; + var x; + + x = vector( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ], 6, 1, 0 ); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + fromIndex = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 5, 'returns expected value' ); + + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + fromIndex = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], 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 (negative)', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ], 6, 1, 0 ); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + fromIndex = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( -2, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 2, 'returns expected value' ); + + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports a `toIndex` parameter (nonnegative)', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = vector( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ], 6, 1, 0 ); + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + toIndex = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + toIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + toIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports a `toIndex` parameter (negative)', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = vector( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ], 6, 1, 0 ); + fromIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + + toIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + toIndex = scalar2ndarray( -4, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + toIndex = scalar2ndarray( -6, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + toIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + x = vector( [ 0.0, 1.0, 1.0, 1.0, 1.0, 1.0 ], 6, 1, 0 ); + + toIndex = scalar2ndarray( -6, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + toIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function returns `-1` if provided an empty input ndarray', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = vector( [], 0, 1, 0 ); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function returns `-1` if provided an ending search index which is greater than or equal to the starting search index', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = vector( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ], 6, 1, 0 ); + + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +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 toIndex; + var actual; + var x; + + x = vector( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ], 6, 1, 0 ); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + fromIndex = scalar2ndarray( 6, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 4, 'returns expected value' ); + + fromIndex = scalar2ndarray( 7, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ x, fromIndex, toIndex ], 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 toIndex; + var actual; + var x; + + x = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + -4.0, + -5.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = gfindLastIndexBetween( [ vector( x, 4, 2, 0 ), fromIndex, toIndex ], isEven ); + + t.strictEqual( actual, 3, 'returns expected value' ); + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -2.0, + -7.0, // 1 + 4.0, + 3.0, // 0 + 2.0 + ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = gfindLastIndexBetween( [ vector( x, 4, -2, 6 ), fromIndex, toIndex ], isEven ); + + t.strictEqual( actual, 2, 'returns expected value' ); + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var fromIndex; + var toIndex; + var actual; + var x; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = gfindLastIndexBetween( [ vector( x, 4, 2, 1 ), fromIndex, toIndex ], isEven ); + t.strictEqual( actual, 3, 'returns expected value' ); + + t.end(); + + function isEven( v ) { + return v % 2.0 === 0.0; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var fromIndex; + var expected; + var toIndex; + var indices; + var values; + var arrays; + var actual; + var ctx; + var arr; + var x; + + x = [ + 1.0, // 0 + 2.0, + 4.0, // 1 + -2.0, + -7.0, // 2 + 3.0, + 3.0, // 3 + 2.0 + ]; + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + + arr = vector( x, 4, 2, 0 ); + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + toIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = gfindLastIndexBetween( [ arr, fromIndex, toIndex ], isEven, ctx ); + + t.strictEqual( actual, 1, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + expected = [ + 3.0, + -7.0, + 4.0 + ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + 3, + 2, + 1 + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + arr, + arr, + arr + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function isEven( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return v % 2.0 === 0.0; + } +});