Skip to content

Commit 66e919e

Browse files
committed
refactor: apply suggestions from code review
1 parent a9d0847 commit 66e919e

11 files changed

Lines changed: 166 additions & 69 deletions

File tree

lib/node_modules/@stdlib/blas/ext/base/gwhere/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var out = gwhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1 );
135135

136136
- If `N <= 0`, both functions return an empty array.
137137
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
138-
- Both functions return a new generic `Array`. To assign results to a specific output array, use [`@stdlib/array/base/where`][@stdlib/array/base/where].
138+
- Depending on the environment, the typed versions ([`dwhere`][@stdlib/blas/ext/base/dwhere], [`swhere`][@stdlib/blas/ext/base/swhere], etc.) are likely to be significantly more performant.
139139

140140
</section>
141141

@@ -155,14 +155,14 @@ var gwhere = require( '@stdlib/blas/ext/base/gwhere' );
155155
var condition = bernoulli( 20, 0.5, {
156156
'dtype': 'generic'
157157
});
158+
console.log( condition );
158159
var x = discreteUniform( 20, 0, 100, {
159160
'dtype': 'generic'
160161
});
162+
console.log( x );
161163
var y = discreteUniform( 20, -100, 0, {
162164
'dtype': 'generic'
163165
});
164-
console.log( condition );
165-
console.log( x );
166166
console.log( y );
167167

168168
var out = gwhere( condition.length, condition, 1, x, 1, y, 1 );
@@ -191,7 +191,9 @@ console.log( out );
191191

192192
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
193193

194-
[@stdlib/array/base/where]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/where
194+
[@stdlib/blas/ext/base/dwhere]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dwhere
195+
196+
[@stdlib/blas/ext/base/swhere]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/swhere
195197

196198
</section>
197199

lib/node_modules/@stdlib/blas/ext/base/gwhere/benchmark/benchmark.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ function createBenchmark( len ) {
6464

6565
b.tic();
6666
for ( i = 0; i < b.iterations; i++ ) {
67-
condition[ i%len ] = ( condition[ i%len ] + 1 ) % 2;
6867
out = gwhere( len, condition, 1, x, 1, y, 1 );
6968
if ( !isArray( out ) ) {
7069
b.fail( 'should return an array' );

lib/node_modules/@stdlib/blas/ext/base/gwhere/benchmark/benchmark.ndarray.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ function createBenchmark( len ) {
6464

6565
b.tic();
6666
for ( i = 0; i < b.iterations; i++ ) {
67-
condition[ i%len ] = ( condition[ i%len ] + 1 ) % 2;
6867
out = gwhere( len, condition, 1, 0, x, 1, 0, y, 1, 0 );
6968
if ( !isArray( out ) ) {
7069
b.fail( 'should return an array' );

lib/node_modules/@stdlib/blas/ext/base/gwhere/docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@
8282
Condition array.
8383

8484
sc: integer
85-
Stride length for `condition`.
85+
Stride length for `c`.
8686

8787
oc: integer
88-
Starting index for `condition`.
88+
Starting index for `c`.
8989

9090
x: Array|TypedArray
9191
First input array.

lib/node_modules/@stdlib/blas/ext/base/gwhere/docs/types/index.d.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,34 @@
2222

2323
import { Collection, AccessorArrayLike } from '@stdlib/types/array';
2424

25-
/**
26-
* Input array.
27-
*/
28-
type InputArray = Collection | AccessorArrayLike<any>;
29-
30-
/**
31-
* Output array.
32-
*/
33-
type OutputArray = Collection | AccessorArrayLike<any>;
34-
3525
/**
3626
* Interface describing `gwhere`.
3727
*/
3828
interface Routine {
29+
/**
30+
* Takes elements from one of two strided arrays depending on a condition.
31+
*
32+
* @param N - number of indexed elements
33+
* @param condition - condition array
34+
* @param strideC - stride length for `condition`
35+
* @param x - first input array
36+
* @param strideX - stride length for `x`
37+
* @param y - second input array
38+
* @param strideY - stride length for `y`
39+
* @returns output array
40+
*
41+
* @example
42+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
43+
*
44+
* var condition = [ 1, 0, 1 ];
45+
* var x = [ 1.0, 2.0, 3.0 ];
46+
* var y = [ 4.0, 5.0, 6.0 ];
47+
*
48+
* var out = gwhere( 3, toAccessorArray( condition ), 1, toAccessorArray( x ), 1, toAccessorArray( y ), 1 );
49+
* // returns <AccessorArray>
50+
*/
51+
<T = unknown, U = unknown, V = unknown>( N: number, condition: AccessorArrayLike<T>, strideC: number, x: AccessorArrayLike<U>, strideX: number, y: AccessorArrayLike<V>, strideY: number ): AccessorArrayLike<U | V>;
52+
3953
/**
4054
* Takes elements from one of two strided arrays depending on a condition.
4155
*
@@ -56,7 +70,34 @@ interface Routine {
5670
* var out = gwhere( 3, condition, 1, x, 1, y, 1 );
5771
* // returns [ 1.0, 5.0, 3.0 ]
5872
*/
59-
( N: number, condition: InputArray, strideC: number, x: InputArray, strideX: number, y: InputArray, strideY: number ): OutputArray;
73+
<T = unknown, U = unknown, V = unknown>( N: number, condition: Collection<T>, strideC: number, x: Collection<U>, strideX: number, y: Collection<V>, strideY: number ): Collection<U | V>;
74+
75+
/**
76+
* Takes elements from one of two strided arrays depending on a condition using alternative indexing semantics.
77+
*
78+
* @param N - number of indexed elements
79+
* @param condition - condition array
80+
* @param strideC - stride length for `condition`
81+
* @param offsetC - starting index for `condition`
82+
* @param x - first input array
83+
* @param strideX - stride length for `x`
84+
* @param offsetX - starting index for `x`
85+
* @param y - second input array
86+
* @param strideY - stride length for `y`
87+
* @param offsetY - starting index for `y`
88+
* @returns output array
89+
*
90+
* @example
91+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
92+
*
93+
* var condition = [ 1, 0, 1 ];
94+
* var x = [ 1.0, 2.0, 3.0 ];
95+
* var y = [ 4.0, 5.0, 6.0 ];
96+
*
97+
* var out = gwhere.ndarray( 3, toAccessorArray( condition ), 1, 0, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 );
98+
* // returns <AccessorArray>
99+
*/
100+
ndarray<T = unknown, U = unknown, V = unknown>( N: number, condition: AccessorArrayLike<T>, strideC: number, offsetC: number, x: AccessorArrayLike<U>, strideX: number, offsetX: number, y: AccessorArrayLike<V>, strideY: number, offsetY: number ): AccessorArrayLike<U | V>;
60101

61102
/**
62103
* Takes elements from one of two strided arrays depending on a condition using alternative indexing semantics.
@@ -81,7 +122,7 @@ interface Routine {
81122
* var out = gwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0 );
82123
* // returns [ 1.0, 5.0, 3.0 ]
83124
*/
84-
ndarray( N: number, condition: InputArray, strideC: number, offsetC: number, x: InputArray, strideX: number, offsetX: number, y: InputArray, strideY: number, offsetY: number ): OutputArray;
125+
ndarray<T = unknown, U = unknown, V = unknown>( N: number, condition: Collection<T>, strideC: number, offsetC: number, x: Collection<U>, strideX: number, offsetX: number, y: Collection<V>, strideY: number, offsetY: number ): Collection<U | V>;
85126
}
86127

87128
/**

lib/node_modules/@stdlib/blas/ext/base/gwhere/docs/types/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import gwhere = require( './index' );
2828
const x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
2929
const y = new Float64Array( [ 4.0, 5.0, 6.0 ] );
3030

31-
gwhere( 3, condition, 1, x, 1, y, 1 ); // $ExpectType float64Array
32-
gwhere( 3, condition, 1, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArray<number>
31+
gwhere( 3, condition, 1, x, 1, y, 1 ); // $ExpectType Collection<number>
32+
gwhere( 3, new AccessorArray( condition ), 1, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArrayLike<number>
3333
}
3434

3535
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -163,8 +163,8 @@ import gwhere = require( './index' );
163163
const x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
164164
const y = new Float64Array( [ 4.0, 5.0, 6.0 ] );
165165

166-
gwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0 ); // $ExpectType float64Array
167-
gwhere.ndarray( 3, condition, 1, 0, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray<number>
166+
gwhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0 ); // $ExpectType Collection<number>
167+
gwhere.ndarray( 3, new AccessorArray( condition ), 1, 0, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArrayLike<number>
168168
}
169169

170170
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

lib/node_modules/@stdlib/blas/ext/base/gwhere/lib/accessors.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var AccessorArray = require( '@stdlib/array/base/accessor' );
24+
var zeros = require( '@stdlib/array/base/zeros' );
25+
26+
2127
// MAIN //
2228

2329
/**
2430
* Takes elements from one of two strided arrays depending on a condition.
2531
*
2632
* @private
27-
* @param {PositiveInteger} N - number of indexed elements
33+
* @param {NonNegativeInteger} N - number of indexed elements
2834
* @param {Object} condition - condition array object
2935
* @param {Collection} condition.data - condition array data
3036
* @param {Array<Function>} condition.accessors - array element accessors
@@ -40,33 +46,43 @@
4046
* @param {Array<Function>} y.accessors - array element accessors
4147
* @param {integer} strideY - stride length for `y`
4248
* @param {NonNegativeInteger} offsetY - starting index for `y`
43-
* @param {Array} out - output array
44-
* @returns {Array} output array
49+
* @returns {AccessorArray} output array
4550
*
4651
* @example
4752
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
4853
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
4954
*
50-
* var condition = [ 1, 0, 1 ];
51-
* var x = [ 1.0, 2.0, 3.0 ];
52-
* var y = [ 4.0, 5.0, 6.0 ];
53-
* var out = [ 0.0, 0.0, 0.0 ];
55+
* var condition = arraylike2object( toAccessorArray( [ 1, 0, 1 ] ) );
56+
* var x = arraylike2object( toAccessorArray( [ 1.0, 2.0, 3.0 ] ) );
57+
* var y = arraylike2object( toAccessorArray( [ 4.0, 5.0, 6.0 ] ) );
58+
*
59+
* var out = gwhere( 3, condition, 1, 0, x, 1, 0, y, 1, 0 );
60+
* // returns <AccessorArray>
5461
*
55-
* gwhere( 3, arraylike2object( toAccessorArray( condition ) ), 1, 0, arraylike2object( toAccessorArray( x ) ), 1, 0, arraylike2object( toAccessorArray( y ) ), 1, 0, out );
56-
* // out => [ 1.0, 5.0, 3.0 ]
62+
* var v = out.get( 0 );
63+
* // returns 1.0
64+
*
65+
* v = out.get( 1 );
66+
* // returns 5.0
67+
*
68+
* v = out.get( 2 );
69+
* // returns 3.0
5770
*/
58-
function gwhere( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY, out ) { // eslint-disable-line max-len, max-params
71+
function gwhere( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY ) { // eslint-disable-line max-len
5972
var cbuf;
6073
var xbuf;
6174
var ybuf;
6275
var cget;
6376
var xget;
6477
var yget;
78+
var out;
6579
var ic;
6680
var ix;
6781
var iy;
6882
var i;
6983

84+
out = new AccessorArray( zeros( N ) );
85+
7086
// Cache references to array data:
7187
cbuf = condition.data;
7288
xbuf = x.data;
@@ -82,9 +98,9 @@ function gwhere( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY
8298
iy = offsetY;
8399
for ( i = 0; i < N; i++ ) {
84100
if ( cget( cbuf, ic ) ) {
85-
out[ i ] = xget( xbuf, ix );
101+
out.set( xget( xbuf, ix ), i );
86102
} else {
87-
out[ i ] = yget( ybuf, iy );
103+
out.set( yget( ybuf, iy ), i );
88104
}
89105
ic += strideC;
90106
ix += strideX;

lib/node_modules/@stdlib/blas/ext/base/gwhere/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var ndarray = require( './ndarray.js' );
2929
/**
3030
* Takes elements from one of two strided arrays depending on a condition.
3131
*
32-
* @param {PositiveInteger} N - number of indexed elements
32+
* @param {NonNegativeInteger} N - number of indexed elements
3333
* @param {Collection} condition - condition array
3434
* @param {integer} strideC - stride length for `condition`
3535
* @param {Collection} x - first input array

lib/node_modules/@stdlib/blas/ext/base/gwhere/lib/ndarray.js

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

2323
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var zeros = require( '@stdlib/array/base/zeros' );
2425
var accessors = require( './accessors.js' );
2526

2627

@@ -29,7 +30,7 @@ var accessors = require( './accessors.js' );
2930
/**
3031
* Takes elements from one of two strided arrays depending on a condition.
3132
*
32-
* @param {PositiveInteger} N - number of indexed elements
33+
* @param {NonNegativeInteger} N - number of indexed elements
3334
* @param {Collection} condition - condition array
3435
* @param {integer} strideC - stride length for `condition`
3536
* @param {NonNegativeInteger} offsetC - starting index for `condition`
@@ -59,24 +60,24 @@ function gwhere( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY
5960
var iy;
6061
var i;
6162

62-
out = [];
6363
if ( N <= 0 ) {
64-
return out;
64+
return zeros( 0 );
6565
}
6666
oc = arraylike2object( condition );
6767
ox = arraylike2object( x );
6868
oy = arraylike2object( y );
6969
if ( oc.accessorProtocol || ox.accessorProtocol || oy.accessorProtocol ) {
70-
return accessors( N, oc, strideC, offsetC, ox, strideX, offsetX, oy, strideY, offsetY, out ); // eslint-disable-line max-len
70+
return accessors( N, oc, strideC, offsetC, ox, strideX, offsetX, oy, strideY, offsetY ); // eslint-disable-line max-len
7171
}
72+
out = zeros( N );
7273
ic = offsetC;
7374
ix = offsetX;
7475
iy = offsetY;
7576
for ( i = 0; i < N; i++ ) {
7677
if ( condition[ ic ] ) {
77-
out.push( x[ ix ] );
78+
out[ i ] = x[ ix ];
7879
} else {
79-
out.push( y[ iy ] );
80+
out[ i ] = y[ iy ];
8081
}
8182
ic += strideC;
8283
ix += strideX;

0 commit comments

Comments
 (0)