Skip to content

Commit 22dbb6f

Browse files
committed
refactor: cleanup & refactor implementation
--- 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: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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 e4c2567 commit 22dbb6f

File tree

4 files changed

+103
-203
lines changed

4 files changed

+103
-203
lines changed

lib/node_modules/@stdlib/blas/ext/index-of/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ The function has the following parameters:
5353

5454
- **x**: input [ndarray][@stdlib/ndarray/ctor].
5555
- **searchElement**: element in an input [ndarray][@stdlib/ndarray/ctor] for which to find an index. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] same as the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], the search element [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
56-
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a numeric or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`.
56+
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an `integer` or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`.
5757
- **options**: function options (_optional_).
5858

5959
The function accepts the following options:
6060

61-
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a "integer_index" [data type][@stdlib/ndarray/dtypes].
61+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be an "integer_index_and_generic" [data type][@stdlib/ndarray/dtypes].
6262
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
6363
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
6464

@@ -181,8 +181,8 @@ The method has the following parameters:
181181

182182
- **x**: input [ndarray][@stdlib/ndarray/ctor].
183183
- **searchElement**: element in an input [ndarray][@stdlib/ndarray/ctor] for which to find an index. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] same as the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided a scalar value, the value is cast to the data type of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, the search element [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] initial value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
184-
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a numeric or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`.
185-
- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or generic [data type][@stdlib/ndarray/dtypes].
184+
- **fromIndex**: index from which to begin searching (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having an `integer` or `generic` [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] containing the index from which to begin searching must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the the index from which to begin searching is `0`.
185+
- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have an `integer` or `generic` [data type][@stdlib/ndarray/dtypes].
186186
- **options**: function options (_optional_).
187187

188188
The method accepts the following options:
@@ -221,7 +221,7 @@ var xbuf = discreteUniform( 10, 0, 20, {
221221
});
222222

223223
// Wrap in an ndarray:
224-
var x = new ndarray( 'float64', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
224+
var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' );
225225
console.log( ndarray2array( x ) );
226226

227227
// Find index:

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

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

2323
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
24-
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
24+
var objectAssign = require( '@stdlib/object/assign' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2526
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2627
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
2728
var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
@@ -47,16 +48,17 @@ var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' );
4748
* @name assign
4849
* @type {Function}
4950
* @param {ndarrayLike} x - input ndarray
50-
* @param {(ndarrayLike|number)} searchElement - search element
51-
* @param {(ndarrayLike|number)} [fromIndex] - index from which to begin searching
51+
* @param {(ndarrayLike|*)} searchElement - search element
52+
* @param {(ndarrayLike|integer)} [fromIndex] - index from which to begin searching
5253
* @param {ndarrayLike} out - output ndarray
5354
* @param {Options} [options] - function options
5455
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation
5556
* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions
5657
* @param {string} [options.dtype] - output ndarray data type
58+
* @throws {TypeError} function must be provided at least two arguments
5759
* @throws {TypeError} first argument must be an ndarray-like object
58-
* @throws {TypeError} search element argument must be either an ndarray-like object or a numeric value
59-
* @throws {TypeError} from index argument must be either an ndarray-like object or a numeric value
60+
* @throws {TypeError} third argument must be either an ndarray-like object or an integer
61+
* @throws {TypeError} fourth argument must be an ndarray-like object
6062
* @throws {TypeError} options argument must be an object
6163
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
6264
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
@@ -66,7 +68,6 @@ var DEFAULT_DTYPE = defaults.get( 'dtypes.integer_index' );
6668
* @example
6769
* var Float64Array = require( '@stdlib/array/float64' );
6870
* var zeros = require( '@stdlib/ndarray/zeros' );
69-
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
7071
* var ndarray = require( '@stdlib/ndarray/ctor' );
7172
*
7273
* // Create data buffers:
@@ -107,118 +108,62 @@ function assign( x, searchElement, fromIndex, out, options ) {
107108
var sh;
108109

109110
nargs = arguments.length;
111+
if ( nargs < 3 ) {
112+
throw new TypeError( format( 'invalid argument. The function must be provided at least two arguments. Value: `%s`.', nargs ) );
113+
}
114+
115+
if ( !isndarrayLike( x ) ) {
116+
throw new TypeError( format( 'invalid argument. The first argument must be an ndarray-like object. Value: `%s`.', x ) );
117+
}
110118

111119
// Resolve input ndarray meta data:
112120
dt = getDType( x );
113121
ord = getOrder( x );
114122

123+
// Initialize options object:
124+
opts = {};
125+
126+
// Case: assign( x, searchElement, out )
115127
if ( nargs === 3 ) {
116-
// Case: assign( x, searchElement, out )
117128
out = fromIndex;
118-
119-
if ( isndarrayLike( out ) ) {
120-
// Case: indexOf( x, searchElement_ndarray, out )
121-
if ( isndarrayLike( searchElement ) ) {
122-
return base( x, searchElement, broadcastScalar( 0, DEFAULT_DTYPE, [], ord ), out ); // eslint-disable-line max-len
123-
}
124-
// Case: indexOf( x, searchElement_scalar, out )
125-
if ( isNumber( searchElement ) ) {
126-
return base( x, broadcastScalar( searchElement, dt, [], ord ), broadcastScalar( 0, DEFAULT_DTYPE, [], ord ), out ); // eslint-disable-line max-len
127-
}
128-
// Check for other scalars than `Number` i.e. bool
129-
} else {
130-
throw new TypeError( format( 'invalid argument. Third argument must be an ndarray. Value: `%s`.', out ) );
131-
}
129+
fromIndex = 0;
132130
}
133-
// Case: assign( x, searchElement, fromIndex, out )
134-
else if ( nargs < 5 ) {
135-
if ( isndarrayLike( out ) ) {
136-
// Case: indexOf( x, searchElement_ndarray, fromIndex_ndarray, out )
137-
if ( isndarrayLike( searchElement ) & isndarrayLike( fromIndex ) ) {
138-
return base( x, searchElement, fromIndex, out );
139-
}
140-
// Case: indexOf( x, searchElement_scalar, fromIndex_scalar out )
141-
if ( isNumber( searchElement ) && isNumber( fromIndex ) ) {
142-
return base( x, broadcastScalar( searchElement, dt, [], ord ), broadcastScalar( fromIndex, DEFAULT_DTYPE, [], ord ), out ); // eslint-disable-line max-len
143-
}
144-
// Case: indexOf( x, searchElement_ndarray, fromIndex_scalar, out )
145-
if ( isndarrayLike( searchElement ) & isNumber( fromIndex ) ) {
146-
return base( x, searchElement, broadcastScalar( fromIndex, DEFAULT_DTYPE, [], ord ), out ); // eslint-disable-line max-len
147-
}
148-
// Case: indexOf( x, searchElement_scalar, fromIndex_ndarray out )
149-
if ( isNumber( searchElement ) && isndarrayLike( fromIndex ) ) {
150-
return base( x, broadcastScalar( searchElement, dt, [], ord ), fromIndex, out ); // eslint-disable-line max-len
151-
}
152-
// Check for other scalars than `Number` i.e. bool
153-
154-
// Case: assign( x, searchElement, out, options )
155-
out = fromIndex;
156-
opts = out;
157-
158-
// Case: indexOf( x, searchElement_ndarray, out, options )
159-
if ( isndarrayLike( searchElement ) ) {
160-
// When not provided `dims`, the operation is performed across all dimensions and `v` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `v` to match the shape of the non-core dimensions...
161-
if ( hasOwnProp( opts, 'dims' ) ) {
162-
searchElement = maybeBroadcastArray( searchElement, nonCoreShape( getShape( x ), opts.dims ) ); // eslint-disable-line max-len
163-
fromIndex = broadcastScalar( 0, DEFAULT_DTYPE, nonCoreShape( getShape( x ), opts.dims ), ord ); // eslint-disable-line max-len
164-
}
165-
}
166-
// Case: indexOf( x, searchElement_scalar, opts )
167-
else if ( isNumber( searchElement ) ) {
168-
if ( hasOwnProp( opts, 'dims' ) ) {
169-
sh = nonCoreShape( getShape( x ), opts.dims );
170-
} else {
171-
sh = [];
172-
}
173-
searchElement = broadcastScalar( searchElement, dt, sh, getOrder( x ) ); // eslint-disable-line max-len
174-
fromIndex = broadcastScalar( 0, DEFAULT_DTYPE, sh, ord );
175-
} else {
176-
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or a numeric scalar value. Value: `%s`.', searchElement ) );
177-
}
178-
return base( x, searchElement, fromIndex, out, opts );
179-
}
131+
// Case: assign( x, searchElement, out, options )
132+
else if (
133+
nargs === 4 &&
134+
isndarrayLike( fromIndex ) &&
135+
!isndarrayLike( out )
136+
) {
137+
opts = objectAssign( opts, out );
138+
out = fromIndex;
139+
fromIndex = 0;
140+
} else if ( nargs === 5 ) {
141+
opts = objectAssign( opts, options );
180142
}
181143

182-
opts = options;
183-
184-
// Case: assign( x, searchElement_ndarray, fromIndex_ndarray, out, options )
185-
if ( isndarrayLike( searchElement ) && isndarrayLike( fromIndex ) ) {
186-
// When not provided `dims`, the operation is performed across all dimensions and `v` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `v` to match the shape of the non-core dimensions...
187-
if ( hasOwnProp( opts, 'dims' ) ) {
188-
searchElement = maybeBroadcastArray( searchElement, nonCoreShape( getShape( x ), opts.dims ) ); // eslint-disable-line max-len
189-
fromIndex = maybeBroadcastArray( fromIndex, nonCoreShape( getShape( x ), opts.dims ) ); // eslint-disable-line max-len
190-
}
191-
}
192-
// Case: assign( x, searchElement_scalar, fromIndex_scalar, out, options )
193-
else if ( isNumber( searchElement ) && isNumber( fromIndex ) ) {
194-
if ( hasOwnProp( opts, 'dims' ) ) {
195-
sh = nonCoreShape( getShape( x ), opts.dims );
196-
} else {
197-
sh = [];
198-
}
199-
searchElement = broadcastScalar( searchElement, dt, sh, getOrder( x ) );
200-
fromIndex = broadcastScalar( fromIndex, dt, sh, getOrder( x ) );
201-
}
202-
// Case: assign( x, searchElement_ndarray, fromIndex_scalar, out, options )
203-
else if ( isndarrayLike( searchElement ) && isNumber( fromIndex ) ) {
204-
// When not provided `dims`, the operation is performed across all dimensions and `v` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `v` to match the shape of the non-core dimensions...
205-
if ( hasOwnProp( opts, 'dims' ) ) {
206-
searchElement = maybeBroadcastArray( searchElement, nonCoreShape( getShape( x ), opts.dims ) ); // eslint-disable-line max-len
207-
fromIndex = broadcastScalar( fromIndex, dt, nonCoreShape( getShape( x ), opts.dims ), ord ); // eslint-disable-line max-len
208-
}
144+
// Resolve shape for broadcasting
145+
if ( hasOwnProp( opts, 'dims' ) ) {
146+
sh = nonCoreShape( getShape( x ), opts.dims );
147+
} else {
148+
sh = [];
209149
}
210-
// Case: assign( x, searchElement_scalar, fromIndex_ndarray, out, options )
211-
else if ( isNumber( searchElement ) && isndarrayLike( fromIndex ) ) {
212-
if ( hasOwnProp( opts, 'dims' ) ) {
213-
sh = nonCoreShape( getShape( x ), opts.dims );
214-
} else {
215-
sh = [];
216-
}
150+
151+
// Normalize search element:
152+
if ( isndarrayLike( searchElement ) ) {
153+
searchElement = maybeBroadcastArray( searchElement, sh );
154+
} else {
217155
searchElement = broadcastScalar( searchElement, dt, sh, ord );
218-
fromIndex = maybeBroadcastArray( fromIndex, dt, sh, ord );
156+
}
157+
158+
// Normalize from index:
159+
if ( isndarrayLike( fromIndex ) ) {
160+
fromIndex = maybeBroadcastArray( fromIndex, sh );
161+
} else if ( isInteger( fromIndex ) ) {
162+
fromIndex = broadcastScalar( fromIndex, DEFAULT_DTYPE, sh, ord );
219163
} else {
220-
throw new TypeError( format( 'invalid argument. Second and third arguments must be either an ndarray or a scalar value. Value: `%s`.', searchElement, fromIndex ) );
164+
throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray or an integer. Value: `%s`.', fromIndex ) );
221165
}
166+
222167
return base( x, searchElement, fromIndex, out, opts );
223168
}
224169

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* @example
2727
* var Float64Array = require( '@stdlib/array/float64' );
2828
* var ndarray = require( '@stdlib/ndarray/ctor' );
29-
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
3029
* var indexOf = require( '@stdlib/blas/ext/index-of' );
3130
*
3231
* // Create a data buffer:
@@ -48,8 +47,8 @@
4847
* var out = indexOf( x, 4.0 );
4948
* // returns <ndarray>
5049
*
51-
* var idx = out.data;
52-
* // returns [ 3 ]
50+
* var idx = out.get();
51+
* // returns 3
5352
*/
5453

5554
// MODULES //

0 commit comments

Comments
 (0)