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 new file mode 100644 index 000000000000..60639561f076 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/README.md @@ -0,0 +1,169 @@ + + +# gfindLastIndex + +> Return the index of the last element in a one-dimensional ndarray which passes a test implemented by a predicate function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); +``` + +#### gfindLastIndex( arrays, clbk\[, thisArg] ) + +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 ndarray = require( '@stdlib/ndarray/base/ctor' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var idx = gfindLastIndex( [ x ], isEven ); +// returns 3 +``` + +If the function is unable to find a search element, the function returns `-1`. + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var xbuf = [ 1.0, 3.0, 5.0, 7.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var idx = gfindLastIndex( [ x ], isEven ); +// returns -1 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. +- **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. + +To set the callback execution context, provide a `thisArg`. + +```javascript +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +function isEven( v ) { + this.count += 1; + return v % 2.0 === 0.0; +} + +var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +var ctx = { + 'count': 0 +}; + +var v = gfindLastIndex( [ x ], clbk, ctx ); +// returns 3 + +var count = ctx.count; +// returns 4 +``` + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `-1`. +- A provided callback function should return a boolean value. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var xbuf = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var idx = gfindLastIndex( [ x ], 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 new file mode 100644 index 000000000000..98a137745c17 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/benchmark/benchmark.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var gfindLastIndex = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - array element +* @returns {number} callback 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 xbuf; + var x; + + xbuf = uniform( len, 0.0, 100.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = gfindLastIndex( [ x ], 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( pkg+':len='+len, f ); + } +} + +main(); 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 new file mode 100644 index 000000000000..89d6f7378e95 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/repl.txt @@ -0,0 +1,46 @@ + +{{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. + + If provided an empty ndarray, the function returns `-1`. + + The callback function is provided three arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + The callback function should return a boolean value. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: integer + Index. + + Examples + -------- + > var xbuf = [ 1.0, -2.0, 2.0 ]; + > var dt = 'generic'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > function f ( v ) { return v % 2.0 === 0.0; }; + > {{alias}}( [ x ], 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 new file mode 100644 index 000000000000..bc6348fc9d26 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/index.d.ts @@ -0,0 +1,95 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 the result of callback function. +* +* @returns result +*/ +type Nullary = ( this: ThisArg ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @returns result +*/ +type Unary = ( this: ThisArg, value: T ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @returns result +*/ +type Binary = ( this: ThisArg, value: T, index: number ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns result +*/ +type Ternary = ( this: ThisArg, value: T, index: number, array: U ) => boolean; + +/** +* Returns the result of callback function. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns result +*/ +type Callback = 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. +* +* @param arrays - array-like object containing an input ndarray +* @param clbk - callback function +* @param thisArg - callback execution context +* @returns index +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* function clbk( v ) { +* return v % 2.0 === 0.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = gfindLastIndex( [ x ], clbk ); +* // returns 3 +*/ +declare function gfindLastIndex = typedndarray, ThisArg = unknown>( arrays: [ U ], clbk: Callback, thisArg?: ThisParameterType> ): number; + + +// EXPORTS // + +export = gfindLastIndex; 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 new file mode 100644 index 000000000000..2a2fb712c1ea --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/docs/types/test.ts @@ -0,0 +1,103 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 gfindLastIndex = 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' + }); + + gfindLastIndex( [ x ], clbk ); // $ExpectType number + gfindLastIndex( [ x ], clbk, {} ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + gfindLastIndex( '10', clbk ); // $ExpectError + gfindLastIndex( 10, clbk ); // $ExpectError + gfindLastIndex( true, clbk ); // $ExpectError + gfindLastIndex( false, clbk ); // $ExpectError + gfindLastIndex( null, clbk ); // $ExpectError + gfindLastIndex( undefined, clbk ); // $ExpectError + gfindLastIndex( [], clbk ); // $ExpectError + gfindLastIndex( {}, clbk ); // $ExpectError + gfindLastIndex( ( x: number ): number => x, clbk ); // $ExpectError + + gfindLastIndex( '10', clbk, {} ); // $ExpectError + gfindLastIndex( 10, clbk, {} ); // $ExpectError + gfindLastIndex( true, clbk, {} ); // $ExpectError + gfindLastIndex( false, clbk, {} ); // $ExpectError + gfindLastIndex( null, clbk, {} ); // $ExpectError + gfindLastIndex( undefined, clbk, {} ); // $ExpectError + gfindLastIndex( [], clbk, {} ); // $ExpectError + gfindLastIndex( {}, clbk, {} ); // $ExpectError + gfindLastIndex( ( 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' + }); + + 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 ], '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 +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + + gfindLastIndex(); // $ExpectError + gfindLastIndex( [ x ], 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 new file mode 100644 index 000000000000..ef322e2f5fe8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var gfindLastIndex = require( './../lib' ); + +function isEven( v ) { + return v % 2.0 === 0.0; +} + +var xbuf = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var idx = gfindLastIndex( [ x ], 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 new file mode 100644 index 000000000000..a585b1f98591 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +* +* @module @stdlib/blas/ext/base/ndarray/gfind-last-index +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var gfindLastIndex = require( '@stdlib/blas/ext/base/ndarray/gfind-last-index' ); +* +* function clbk( v ) { +* return v % 2.0 === 0.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = gfindLastIndex( [ x ], 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/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js new file mode 100644 index 000000000000..d5c7794d40f6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/lib/main.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +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. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {integer} index +* +* @example +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* function isEven( v ) { +* return v % 2.0 === 0.0; +* } +* +* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var v = gfindLastIndex( [ x ], clbk ); +* // returns 2 +*/ +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 + + /** + * 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, aidx, x ); + } +} + + +// EXPORTS // + +module.exports = gfindLastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/package.json new file mode 100644 index 000000000000..8629e8909e17 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/gfind-last-index", + "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.", + "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", + "search", + "get", + "ndarray" + ], + "__stdlib__": {} +} 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 new file mode 100644 index 000000000000..a72c2dc87c2b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfind-last-index/test/test.js @@ -0,0 +1,234 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var gfindLastIndex = 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 gfindLastIndex, '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', function test( t ) { + 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 ); + t.strictEqual( actual, 5, 'returns expected value' ); + + x = [ 4.0, 5.0 ]; + actual = gfindLastIndex( [ vector( x, 2, 1, 0 ) ], isEven ); + t.strictEqual( actual, 0, 'returns expected value' ); + + x = [ 1.0, 3.0, 5.0 ]; + actual = gfindLastIndex( [ vector( x, 3, 1, 0 ) ], 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 empty input ndarray', function test( t ) { + var actual; + var x; + + x = vector( [], 0, 1, 0 ); + + actual = gfindLastIndex( [ x ], isEven ); + t.strictEqual( actual, -1, '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 actual; + var x; + + x = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + -5.0, + -4.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + actual = gfindLastIndex( [ vector( x, 4, 2, 0 ) ], 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 actual; + var x; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -2.0, + -7.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + actual = gfindLastIndex( [ vector( x, 4, -2, 6 ) ], 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 actual; + var x; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + actual = gfindLastIndex( [ vector( x, 4, 2, 1 ) ], 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 expected; + var indices; + var values; + var arrays; + var actual; + var ctx; + var arr; + var x; + + x = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + -2.0, + -7.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + + arr = vector( x, 4, 2, 0 ); + actual = gfindLastIndex( [ arr ], isEven, ctx ); + + t.strictEqual( actual, 3, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + expected = [ + 1.0, + 3.0, + -7.0, + 4.0 + ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + 0, + 1, + 2, + 3 + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + arr, + 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; + } +});