Skip to content

Commit 3735e43

Browse files
committed
refactor: apply the requested changes
1 parent 1205cdc commit 3735e43

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/where/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# Where
2222

23-
> Applies a condition to elements in two input ndarrays and assigns results to elements in an output ndarray.
23+
> Apply a condition to elements in two input ndarrays and assign results to elements in an output ndarray.
2424
2525
<section class="intro">
2626

@@ -160,6 +160,8 @@ var condition = {
160160
'offset': 0,
161161
'order': 'row-major'
162162
};
163+
console.log( ndarray2array( condition.data, condition.shape, condition.strides, condition.offset, condition.order ) ); // eslint-disable-line max-len
164+
163165
var x = {
164166
'dtype': 'generic',
165167
'data': filledarrayBy( N, 'generic', discreteUniform( 0, 100 ) ),
@@ -168,6 +170,8 @@ var x = {
168170
'offset': 0,
169171
'order': 'row-major'
170172
};
173+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
174+
171175
var y = {
172176
'dtype': 'generic',
173177
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 0 ) ),
@@ -176,6 +180,8 @@ var y = {
176180
'offset': 0,
177181
'order': 'row-major'
178182
};
183+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
184+
179185
var out = {
180186
'dtype': 'generic',
181187
'data': filledarray( 0, N, 'generic' ),
@@ -186,11 +192,8 @@ var out = {
186192
};
187193

188194
where( [ condition, x, y, out ] );
189-
190-
console.log( ndarray2array( condition.data, condition.shape, condition.strides, condition.offset, condition.order ) ); // eslint-disable-line max-len
191-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
192-
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
193195
console.log( ndarray2array( out.data, out.shape, out.strides, out.offset, out.order ) ); // eslint-disable-line max-len
196+
194197
```
195198

196199
</section>

lib/node_modules/@stdlib/ndarray/base/where/examples/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var condition = {
3636
'offset': 0,
3737
'order': 'row-major'
3838
};
39+
console.log( ndarray2array( condition.data, condition.shape, condition.strides, condition.offset, condition.order ) ); // eslint-disable-line max-len
40+
3941
var x = {
4042
'dtype': 'generic',
4143
'data': filledarrayBy( N, 'generic', discreteUniform( 0, 100 ) ),
@@ -44,6 +46,8 @@ var x = {
4446
'offset': 0,
4547
'order': 'row-major'
4648
};
49+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
50+
4751
var y = {
4852
'dtype': 'generic',
4953
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 0 ) ),
@@ -52,6 +56,8 @@ var y = {
5256
'offset': 0,
5357
'order': 'row-major'
5458
};
59+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
60+
5561
var out = {
5662
'dtype': 'generic',
5763
'data': filledarray( 0, N, 'generic' ),
@@ -62,7 +68,4 @@ var out = {
6268
};
6369

6470
where( [ condition, x, y, out ] );
65-
console.log( ndarray2array( condition.data, condition.shape, condition.strides, condition.offset, condition.order ) ); // eslint-disable-line max-len
66-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
67-
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
6871
console.log( ndarray2array( out.data, out.shape, out.strides, out.offset, out.order ) ); // eslint-disable-line max-len

lib/node_modules/@stdlib/ndarray/base/where/lib/main.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var isComplexArray = require( '@stdlib/array/base/assert/is-complex-typed-array'
2727
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
2828
var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' );
2929
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
30+
var anyIsEntryIn = require( '@stdlib/array/base/any-is-entry-in' );
3031
var castReturn = require( '@stdlib/complex/base/cast-return' );
3132
var complexCtors = require( '@stdlib/complex/ctors' );
3233
var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' );
@@ -128,7 +129,7 @@ var BLOCKED_ACCESSOR_WHERE = [
128129
blockedaccessorwhere9d,
129130
blockedaccessorwhere10d // 8
130131
];
131-
var MAX_DIMS = where.length - 1;
132+
var MAX_DIMS = WHERE.length - 1;
132133

133134
// TODO: consider adding a package utility for mapping a complex dtype to its complementary real-valued counterpart
134135
var COMPLEX_TO_REAL = { // WARNING: this table needs to be manually updated if we add support for additional complex number dtypes
@@ -140,6 +141,19 @@ var COMPLEX_TO_REAL = { // WARNING: this table needs to be manually updated if w
140141

141142
// FUNCTIONS //
142143

144+
/**
145+
* Returns a boolean indicating if at least one ndarray data buffer implements the accessor protocol.
146+
*
147+
* @private
148+
* @param {ndarrayLike} x - first ndarray
149+
* @param {ndarrayLike} y - second ndarray
150+
* @param {ndarrayLike} z - third ndarray
151+
* @returns {boolean} boolean indicating whether an ndarray data buffer implements the accessor protocol
152+
*/
153+
function hasAccessors( x, y, z ) {
154+
return anyIsEntryIn( [ x, y, z ], 'accessorProtocol', true );
155+
}
156+
143157
/**
144158
* Converts a boolean ndarray to an 8-bit unsigned integer ndarray.
145159
*
@@ -206,7 +220,7 @@ function complex2real( x ) {
206220
* - **offset**: index offset.
207221
* - **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
208222
*
209-
* @param {ArrayLikeObject<Object>} arrays - array-like object containing once condition array, two input arrays, and one output array
223+
* @param {ArrayLikeObject<Object>} arrays - array-like object containing one condition array, two input arrays, and one output array
210224
* @throws {Error} arrays must have the same number of dimensions
211225
* @throws {Error} arrays must have the same shape
212226
* @returns {void}
@@ -315,8 +329,8 @@ function where( arrays ) {
315329
o = ndarray2object( arrays[ 3 ] );
316330

317331
// Always reinterpret condition array to uint8
318-
if ( isBooleanArray(c.data) ) {
319-
c = boolean2uint8(c);
332+
if ( isBooleanArray( c.data ) ) {
333+
c = boolean2uint8( c );
320334
}
321335

322336
// Check for known array types which can be reinterpreted for better iteration performance...
@@ -349,7 +363,7 @@ function where( arrays ) {
349363
}
350364
// Determine whether we can avoid iteration altogether...
351365
if ( ndims === 0 ) {
352-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
366+
if ( hasAccessors( x, y, o ) ) {
353367
return ACCESSOR_WHERE[ ndims ]( c, x, y, o );
354368
}
355369
return WHERE[ ndims ]( c, x, y, o );
@@ -376,7 +390,7 @@ function where( arrays ) {
376390
}
377391
// Determine whether the ndarrays are one-dimensional and thus readily translate to one-dimensional strided arrays...
378392
if ( ndims === 1 ) {
379-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
393+
if ( hasAccessors( x, y, o ) ) {
380394
return ACCESSOR_WHERE[ ndims ]( c, x, y, o );
381395
}
382396
return WHERE[ ndims ]( c, x, y, o );
@@ -402,7 +416,7 @@ function where( arrays ) {
402416
x.strides = [ sx[i] ];
403417
y.strides = [ sy[i] ];
404418
o.strides = [ so[i] ];
405-
if ( x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
419+
if ( hasAccessors( x, y, o ) ) {
406420
return ACCESSOR_WHERE[ 1 ]( c, x, y, o );
407421
}
408422
return WHERE[ 1 ]( c, x, y, o );
@@ -456,7 +470,7 @@ function where( arrays ) {
456470
x.offset = ox;
457471
y.offset = oy;
458472
o.offset = oo;
459-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
473+
if ( hasAccessors( x, y, o ) ) {
460474
return ACCESSOR_WHERE[ 1 ]( c, x, y, o );
461475
}
462476
return WHERE[ 1 ]( c, x, y, o );
@@ -466,7 +480,7 @@ function where( arrays ) {
466480
// Determine whether we can use simple nested loops...
467481
if ( ndims <= MAX_DIMS ) {
468482
// So long as iteration for each respective array always moves in the same direction (i.e., no mixed sign strides), we can leverage cache-optimal (i.e., normal) nested loops without resorting to blocked iteration...
469-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
483+
if ( hasAccessors( x, y, o ) ) {
470484
return ACCESSOR_WHERE[ ndims ]( c, x, y, o, ord === 1 );
471485
}
472486
return WHERE[ ndims ]( c, x, y, o, ord === 1 );
@@ -477,13 +491,13 @@ function where( arrays ) {
477491

478492
// Determine whether we can perform blocked iteration...
479493
if ( ndims <= MAX_DIMS ) {
480-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
494+
if ( hasAccessors( x, y, o ) ) {
481495
return BLOCKED_ACCESSOR_WHERE[ ndims-2 ]( c, x, y, o );
482496
}
483497
return BLOCKED_WHERE[ ndims-2 ]( c, x, y, o );
484498
}
485499
// Fall-through to linear view iteration without regard for how data is stored in memory (i.e., take the slow path)...
486-
if ( c.accessorProtocol || x.accessorProtocol || y.accessorProtocol || o.accessorProtocol ) {
500+
if ( hasAccessors( x, y, o ) ) {
487501
return accessorwherend( c, x, y, o );
488502
}
489503
wherend( c, x, y, o );

0 commit comments

Comments
 (0)