Skip to content

Commit 5f6e350

Browse files
committed
fix: refactor implementation to only support a single reduction dimension
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 0c7ccf4 commit 5f6e350

File tree

5 files changed

+97
-70
lines changed

5 files changed

+97
-70
lines changed

lib/node_modules/@stdlib/blas/ext/index-of/examples/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ var xbuf = discreteUniform( 10, 0, 20, {
3232
var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' );
3333
console.log( ndarray2array( x ) );
3434

35-
// Find index:
35+
// Perform operation:
3636
var idx = indexOf( x, 10.0, {
37-
'dims': [ 0 ]
37+
'dim': 0
3838
});
3939

4040
// Print the results:

lib/node_modules/@stdlib/blas/ext/index-of/lib/base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var table = {
5555
// MAIN //
5656

5757
/**
58-
* Returns the first index of a specified search element along one or more ndarray dimensions.
58+
* Returns the first index of a specified search element along an ndarray dimension.
5959
*
6060
* @private
6161
* @name indexOf
@@ -84,10 +84,10 @@ var table = {
8484
* var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] );
8585
*
8686
* // Define the shape of the input array:
87-
* var sh = [ 3, 1, 2 ];
87+
* var sh = [ 6 ];
8888
*
8989
* // Define the array strides:
90-
* var sx = [ 2, 2, 1 ];
90+
* var sx = [ 1 ];
9191
*
9292
* // Define the index offset:
9393
* var ox = 0;
@@ -105,7 +105,7 @@ var table = {
105105
* 'dtype': 'int32'
106106
* })
107107
*
108-
* // Find index:
108+
* // Perform operation:
109109
* var out = indexOf( x, searchElement, fromIndex );
110110
* // returns <ndarray>
111111
*

lib/node_modules/@stdlib/blas/ext/index-of/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Return the first index of a specified search element along one or more ndarray dimensions.
22+
* Return the first index of a specified search element along an ndarray dimension.
2323
*
2424
* @module @stdlib/blas/ext/index-of
2525
*
@@ -43,7 +43,7 @@
4343
* // Create an input ndarray:
4444
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
4545
*
46-
* // Find index:
46+
* // Perform operation:
4747
* var out = indexOf( x, 4.0 );
4848
* // returns <ndarray>
4949
*

lib/node_modules/@stdlib/blas/ext/index-of/lib/main.js

Lines changed: 87 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
24-
var objectAssign = require( '@stdlib/object/assign' );
25-
var isObject = require( '@stdlib/assert/is-object' );
24+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2625
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2726
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2827
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
@@ -44,118 +43,151 @@ var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' );
4443
// MAIN //
4544

4645
/**
47-
* Returns the first index of a specified search element along one or more ndarray dimensions.
46+
* Returns the first index of a specified search element along an ndarray dimension.
4847
*
4948
* @name indexOf
5049
* @type {Function}
5150
* @param {ndarrayLike} x - input ndarray
5251
* @param {(ndarrayLike|*)} searchElement - search element
5352
* @param {(ndarrayLike|integer)} [fromIndex] - index from which to begin searching
5453
* @param {Options} [options] - function options
55-
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation
54+
* @param {IntegerArray} [options.dim=-1] - dimension over which to perform operation
5655
* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions
5756
* @param {string} [options.dtype] - output ndarray data type
5857
* @throws {TypeError} first argument must be an ndarray-like object
5958
* @throws {TypeError} the function must be provided at least two arguments
6059
* @throws {TypeError} third argument must be either an ndarray-like object or an integer
6160
* @throws {TypeError} options argument must be an object
62-
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
63-
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
61+
* @throws {RangeError} dimension index must not exceed input ndarray bounds
6462
* @throws {Error} must provide valid options
6563
* @returns {ndarray} output ndarray
6664
*
6765
* @example
6866
* var Float64Array = require( '@stdlib/array/float64' );
67+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
6968
* var ndarray = require( '@stdlib/ndarray/ctor' );
7069
*
7170
* // Create a data buffer:
7271
* var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] );
7372
*
7473
* // Define the shape of the input array:
75-
* var sh = [ 3, 1, 2 ];
74+
* var sh = [ 2, 3 ];
7675
*
7776
* // Define the array strides:
78-
* var sx = [ 2, 2, 1 ];
77+
* var sx = [ 3, 1 ];
7978
*
8079
* // Define the index offset:
8180
* var ox = 0;
8281
*
8382
* // Create an input ndarray:
8483
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
8584
*
86-
* // Find index:
87-
* var out = indexOf( x, 4.0 );
85+
* // Perform operation:
86+
* var out = indexOf( x, -5.0 );
8887
* // returns <ndarray>
8988
*
90-
* var idx = out.get();
91-
* // returns 3
89+
* var arr = ndarray2array( out );
90+
* // returns [ -1, 1 ]
9291
*/
93-
function indexOf( x, searchElement, fromIndex, options ) {
92+
function indexOf( x, searchElement, fromIndex ) {
93+
var hasOptions;
94+
var options;
9495
var nargs;
9596
var opts;
97+
var fidx;
98+
var iflg;
9699
var ord;
97100
var dt;
98101
var sh;
99-
100-
if ( !isndarrayLike( x ) ) {
101-
throw new TypeError( format( 'invalid argument. The first argument must be an ndarray-like object. Value: `%s`.', x ) );
102-
}
102+
var v;
103103

104104
nargs = arguments.length;
105-
if ( nargs < 2 ) {
106-
throw new TypeError( format( 'invalid argument. The function must be provided at least two arguments. Value: `%s`.', nargs ) );
107-
}
108105

106+
if ( !isndarrayLike( x ) ) {
107+
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
108+
}
109109
// Resolve input ndarray meta data:
110110
dt = getDType( x );
111111
ord = getOrder( x );
112112

113-
// Initialize options object:
114-
opts = {};
115-
116-
// Case: indexOf( x, searchElement )
117-
if ( nargs === 2 ) {
118-
fromIndex = 0;
113+
if ( nargs < 2 ) {
114+
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or a scalar value. Value: `%s`.', searchElement ) );
119115
}
120-
// Case: indexOf( x, searchElement, options )
121-
else if (
122-
nargs === 3 &&
123-
!isndarrayLike( fromIndex ) &&
124-
isObject( fromIndex )
125-
) {
126-
opts = objectAssign( opts, fromIndex );
127-
fromIndex = 0;
116+
// Initialize an options object:
117+
opts = {
118+
'dims': [ -1 ], // default behavior is to perform a reduction over the last dimension
119+
'keepdims': false
120+
};
121+
122+
// Initialize the `fromIndex` to the first element along a dimension:
123+
fidx = 0;
124+
125+
// Initialize a flag indicating whether the `fromIndex` argument is a scalar:
126+
iflg = true;
127+
128+
// Initialize a flag indicating whether an `options` argument was provided:
129+
hasOptions = false;
130+
131+
// Case: indexOf( x, search_element, ??? )
132+
if ( nargs === 3 ) {
133+
// Case: indexOf( x, search_element, from_index_scalar )
134+
if ( isInteger( fromIndex ) ) {
135+
fidx = fromIndex;
136+
}
137+
// Case: indexOf( x, search_element, from_index_ndarray )
138+
else if ( isndarrayLike( fromIndex ) ) {
139+
fidx = fromIndex;
140+
iflg = false;
141+
}
142+
// Case: indexOf( x, search_element, options )
143+
else {
144+
options = fromIndex;
145+
hasOptions = true;
146+
}
128147
}
129-
// Case: indexOf( x, searchElement, fromIndex, options )
130-
else if ( nargs === 4 ) {
131-
if ( !isObject( options ) ) {
132-
throw new TypeError( format( 'invalid argument. The fourth argument must be an object. Value: `%s`.', options ) );
148+
// Case: indexOf( x, search_element, from_index, options )
149+
else if ( nargs > 3 ) {
150+
// Case: indexOf( x, search_element, from_index_scalar, options )
151+
if ( isInteger( fromIndex ) ) {
152+
fidx = fromIndex;
153+
}
154+
// Case: indexOf( x, search_element, from_index_ndarray, options )
155+
else if ( isndarrayLike( fromIndex ) ) {
156+
fidx = fromIndex;
133157
}
134-
opts = objectAssign( opts, options );
158+
// Case: indexOf( x, search_element, ???, options )
159+
else {
160+
throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) );
161+
}
162+
options = arguments[ 3 ];
163+
hasOptions = true;
135164
}
136-
137-
// Resolve shape for broadcasting
138-
if ( hasOwnProp( opts, 'dims' ) ) {
139-
sh = nonCoreShape( getShape( x ), opts.dims );
140-
} else {
141-
sh = [];
165+
if ( hasOptions && !isPlainObject( options ) ) {
166+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
167+
}
168+
// Resolve provided options...
169+
if ( hasOwnProp( options, 'dim' ) ) {
170+
opts.dims[ 0 ] = options.dim;
171+
}
172+
if ( hasOwnProp( options, 'keepdims' ) ) {
173+
opts.keepdims = options.keepdims;
142174
}
175+
// Resolve the list of non-reduced dimensions:
176+
sh = nonCoreShape( getShape( x ), opts.dims );
143177

178+
// Broadcast the search element to match the shape of the non-reduced dimensions...
144179
if ( isndarrayLike( searchElement ) ) {
145-
searchElement = maybeBroadcastArray( searchElement, sh );
180+
v = maybeBroadcastArray( searchElement, sh );
146181
} else {
147-
searchElement = broadcastScalar( searchElement, dt, sh, ord );
182+
v = broadcastScalar( searchElement, dt, sh, ord );
148183
}
149-
150-
if ( isndarrayLike( fromIndex ) ) {
151-
fromIndex = maybeBroadcastArray( fromIndex, sh );
152-
} else if ( isInteger( fromIndex ) ) {
153-
fromIndex = broadcastScalar( fromIndex, DEFAULT_DTYPE, sh, ord );
184+
// Broadcast the `fromIndex` to match the shape of the non-reduced dimensions...
185+
if ( iflg ) {
186+
fidx = broadcastScalar( fidx, DEFAULT_DTYPE, sh, ord );
154187
} else {
155-
throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) );
188+
fidx = maybeBroadcastArray( fidx, sh );
156189
}
157-
158-
return base( x, searchElement, fromIndex, opts );
190+
return base( x, v, fidx, opts );
159191
}
160192

161193

lib/node_modules/@stdlib/blas/ext/index-of/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/blas/ext/index-of",
33
"version": "0.0.0",
4-
"description": "Return the first index of a specified search element along one or more ndarray dimensions.",
4+
"description": "Return the first index of a specified search element along an ndarray dimension.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",
@@ -56,12 +56,7 @@
5656
"search",
5757
"element",
5858
"array",
59-
"ndarray",
60-
"strided",
61-
"double",
62-
"float",
63-
"float64",
64-
"float64array"
59+
"ndarray"
6560
],
6661
"__stdlib__": {}
6762
}

0 commit comments

Comments
 (0)