diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/README.md new file mode 100644 index 000000000000..caad8a6fbd0a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/README.md @@ -0,0 +1,159 @@ + + +# sindexOf + +> Return the first index of a search element in a one-dimensional single-precision floating-point ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' ); +``` + +#### sindexOf( arrays ) + +Returns the first index of a specified search element in a one-dimensional single-precision floating-point ndarray. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' +}); + +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOf( [ x, searchElement, fromIndex ] ); +// 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 search element. + - a zero-dimensional ndarray containing the index from which to begin searching. + +If the function is unable to find a search element, the function returns `-1`. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var searchElement = scalar2ndarray( 10.0, { + 'dtype': 'float32' +}); + +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var idx = sindexOf( [ x, searchElement, fromIndex ] ); +// returns -1 +``` + +
+ + + +
+ +## Notes + +- 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). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' ); + +var xbuf = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var searchElement = scalar2ndarray( 80.0, { + 'dtype': 'float32' +}); +console.log( 'Search Element:', ndarraylike2scalar( searchElement ) ); + +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); +console.log( 'From Index:', ndarraylike2scalar( fromIndex ) ); + +var idx = sindexOf( [ x, searchElement, fromIndex ] ); +console.log( idx ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/benchmark/benchmark.js new file mode 100644 index 000000000000..918b142482a8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/benchmark/benchmark.js @@ -0,0 +1,112 @@ +/** +* @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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pkg = require( './../package.json' ).name; +var sindexOf = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var searchElement; + var fromIndex; + var xbuf; + var x; + + xbuf = uniform( len, 0.0, 100.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + searchElement = scalar2ndarray( -10.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + return benchmark; + + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = sindexOf( [ x, searchElement, fromIndex ] ); + if ( out !== out ) { + b.fail( 'should return an integer' ); + } + } + b.toc(); + if ( !isInteger( out ) ) { + b.fail( 'should return an integer' ); + } + 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/sindex-of/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/repl.txt new file mode 100644 index 000000000000..07304b0db424 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( arrays ) + Returns the first index of a search element in a one-dimensional single- + precision floating-point ndarray. + + 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). + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the search element. + - a zero-dimensional ndarray containing the index from which to begin + searching. + + Returns + ------- + out: integer + Index. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] ); + > var dt = 'float32'; + > 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 ); + > var v = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, { 'dtype': dt } ); + > var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } ); + > {{alias}}( [ x, v, i ] ) + 1 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/index.d.ts new file mode 100644 index 000000000000..b17a1234ae11 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @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 { float32ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns the first index of a search element in a one-dimensional single-precision floating-point ndarray. +* +* @param arrays - array-like object containing a one-dimensional input ndarray, a zero-dimensional ndarray containing the search element, and a zero-dimensional ndarray containing the index from which to begin searching +* @returns index +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var searchElement = scalar2ndarray( 2.0, { +* 'dtype': 'float32' +* }); +* +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOf( [ x, searchElement, fromIndex ] ); +* // returns 3 +*/ +declare function sindexOf( arrays: [ float32ndarray, typedndarray, typedndarray ] ): number; + + +// EXPORTS // + +export = sindexOf; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/test.ts new file mode 100644 index 000000000000..605065cab5ae --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/docs/types/test.ts @@ -0,0 +1,70 @@ +/* +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import sindexOf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const searchElement = scalar2ndarray( 0.0, { + 'dtype': 'float32' + }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + sindexOf( [ x, searchElement, fromIndex ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + sindexOf( '10' ); // $ExpectError + sindexOf( 10 ); // $ExpectError + sindexOf( true ); // $ExpectError + sindexOf( false ); // $ExpectError + sindexOf( null ); // $ExpectError + sindexOf( undefined ); // $ExpectError + sindexOf( [] ); // $ExpectError + sindexOf( {} ); // $ExpectError + sindexOf( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const searchElement = scalar2ndarray( 0.0, { + 'dtype': 'float32' + }); + const fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + sindexOf(); // $ExpectError + sindexOf( [ x, searchElement, fromIndex ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/examples/index.js new file mode 100644 index 000000000000..42496dd072ab --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/examples/index.js @@ -0,0 +1,45 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var sindexOf = require( './../lib' ); + +var xbuf = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var searchElement = scalar2ndarray( 80.0, { + 'dtype': 'float32' +}); +console.log( 'Search Element:', ndarraylike2scalar( searchElement ) ); + +var fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' +}); +console.log( 'From Index:', ndarraylike2scalar( fromIndex ) ); + +var idx = sindexOf( [ x, searchElement, fromIndex ] ); +console.log( idx ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/index.js new file mode 100644 index 000000000000..5ed07125048d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/index.js @@ -0,0 +1,54 @@ +/** +* @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 first index of a search element in a one-dimensional single-precision floating-point ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/sindex-of +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var searchElement = scalar2ndarray( 2.0, { +* 'dtype': 'float32' +* }); +* +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOf( [ x, searchElement, fromIndex ] ); +* // returns 3 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/main.js new file mode 100644 index 000000000000..b3417b8cb951 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/lib/main.js @@ -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. +*/ + +'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/sindex-of' ).ndarray; +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); + + +// MAIN // + +/** +* Returns the first index of a search element in a one-dimensional single-precision floating-point ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing a one-dimensional input ndarray, a zero-dimensional ndarray containing the search element, and a zero-dimensional ndarray containing the index from which to begin searching +* @returns {integer} index +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var sindexOf = require( '@stdlib/blas/ext/base/ndarray/sindex-of' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var searchElement = scalar2ndarray( 2.0, { +* 'dtype': 'float32' +* }); +* +* var fromIndex = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var v = sindexOf( [ x, searchElement, fromIndex ] ); +* // returns 3 +*/ +function sindexOf( arrays ) { + var searchElement; + var fromIndex; + var stride; + var offset; + var idx; + var N; + var x; + + x = arrays[ 0 ]; + N = numelDimension( x, 0 ); + searchElement = ndarraylike2scalar( arrays[ 1 ] ); + fromIndex = ndarraylike2scalar( arrays[ 2 ] ); + + if ( fromIndex < 0 ) { + fromIndex += N; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } else if ( fromIndex >= N ) { + return -1; + } + N -= fromIndex; + stride = getStride( x, 0 ); + offset = getOffset( x ) + ( stride*fromIndex ); + + idx = strided( N, searchElement, getData( x ), stride, offset ); + if ( idx >= 0 ) { + idx += fromIndex; + } + return idx; +} + + +// EXPORTS // + +module.exports = sindexOf; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/package.json new file mode 100644 index 000000000000..21d017200ce3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/sindex-of", + "version": "0.0.0", + "description": "Return the first index of a specified search element in a one-dimensional single-precision floating-point ndarray.", + "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", + "index", + "search", + "get", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/test/test.js new file mode 100644 index 000000000000..5d3bb296fabd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of/test/test.js @@ -0,0 +1,158 @@ +/** +* @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 Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var sindexOf = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} 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( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sindexOf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns the first index of an element which equals a provided search element', function test( t ) { + var searchElement; + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ), 6, 1, 0 ); + + // Nonnegative fromIndex... + searchElement = scalar2ndarray( 1.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 0, 'returns expected value' ); + + searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 2, 'returns expected value' ); + + searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + searchElement = scalar2ndarray( 4.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + // Negative fromIndex... + searchElement = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 5, 'returns expected value' ); + + searchElement = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( -2, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 4, 'returns expected value' ); + + searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 3, 'returns expected value' ); + + searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( -7, { + 'dtype': 'generic' + }); + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if provided a starting search index which is greater than or equal to number of elements in the input ndarray', function test( t ) { + var searchElement; + var fromIndex; + var actual; + var x; + + x = vector( new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ), 6, 1, 0 ); + searchElement = scalar2ndarray( 2.0, { + 'dtype': 'float32' + }); + fromIndex = scalar2ndarray( 7, { + 'dtype': 'generic' + }); + + actual = sindexOf( [ x, searchElement, fromIndex ] ); + t.strictEqual( actual, -1, 'returns expected value' ); + + t.end(); +});