Skip to content

Commit

Permalink
refactor: reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jul 6, 2024
1 parent 741144c commit 0d4ef60
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 88 deletions.
59 changes: 13 additions & 46 deletions lib/node_modules/@stdlib/strided/base/binary/lib/accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './accessors.ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -56,55 +62,16 @@
* // => <Float64Array>[ 4.0, 8.0, 12.0, 16.0, 20.0 ]
*/
function binary( arrays, shape, strides, accessors, fcn ) {
var xget;
var yget;
var zset;
var sx;
var sy;
var sz;
var ix;
var iy;
var iz;
var x;
var y;
var z;
var offsets;
var N;
var i;

N = shape[ 0 ];
if ( N <= 0 ) {
return;
}
sx = strides[ 0 ];
sy = strides[ 1 ];
sz = strides[ 2 ];
if ( sx < 0 ) {
ix = (1-N) * sx;
} else {
ix = 0;
}
if ( sy < 0 ) {
iy = (1-N) * sy;
} else {
iy = 0;
}
if ( sz < 0 ) {
iz = (1-N) * sz;
} else {
iz = 0;
}
x = arrays[ 0 ];
y = arrays[ 1 ];
z = arrays[ 2 ];
xget = accessors[ 0 ];
yget = accessors[ 1 ];
zset = accessors[ 2 ];
for ( i = 0; i < N; i++ ) {
zset( z, iz, fcn( xget( x, ix ), yget( y, iy ) ) );
ix += sx;
iy += sy;
iz += sz;
}
offsets = [
stride2offset( N, strides[ 0 ] ),
stride2offset( N, strides[ 1 ] ),
stride2offset( N, strides[ 2 ] )
];
return ndarray( arrays, shape, strides, offsets, accessors, fcn );
}


Expand Down
53 changes: 13 additions & 40 deletions lib/node_modules/@stdlib/strided/base/binary/lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './binary.ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -47,49 +53,16 @@
* // => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0 ]
*/
function binary( arrays, shape, strides, fcn ) {
var sx;
var sy;
var sz;
var ix;
var iy;
var iz;
var x;
var y;
var z;
var offsets;
var N;
var i;

N = shape[ 0 ];
if ( N <= 0 ) {
return;
}
sx = strides[ 0 ];
sy = strides[ 1 ];
sz = strides[ 2 ];
if ( sx < 0 ) {
ix = (1-N) * sx;
} else {
ix = 0;
}
if ( sy < 0 ) {
iy = (1-N) * sy;
} else {
iy = 0;
}
if ( sz < 0 ) {
iz = (1-N) * sz;
} else {
iz = 0;
}
x = arrays[ 0 ];
y = arrays[ 1 ];
z = arrays[ 2 ];
for ( i = 0; i < N; i++ ) {
z[ iz ] = fcn( x[ ix ], y[ iy ] );
ix += sx;
iy += sy;
iz += sz;
}
offsets = [
stride2offset( N, strides[ 0 ] ),
stride2offset( N, strides[ 1 ] ),
stride2offset( N, strides[ 2 ] )
];
return ndarray( arrays, shape, strides, offsets, fcn );
}


Expand Down
2 changes: 0 additions & 2 deletions lib/node_modules/@stdlib/strided/base/binary/scripts/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,9 @@ function signatures( dtypes ) {

// Resolve the promoted type for the current dtype pair:
t3 = promotionRules( t1, t2 );

if ( t3 === -1 ) {
continue;
}

// Resolve single-letter dtype abbreviations:
ch2 = dtypeChar( t2 );
ch3 = dtypeChar( t3 );
Expand Down

1 comment on commit 0d4ef60

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
strided/base/binary $\color{green}604/604$
$\color{green}+100.00\%$
$\color{green}38/38$
$\color{green}+100.00\%$
$\color{green}6/6$
$\color{green}+100.00\%$
$\color{green}604/604$
$\color{green}+100.00\%$
strided/base/map-by $\color{green}551/551$
$\color{green}+100.00\%$
$\color{green}36/36$
$\color{green}+100.00\%$
$\color{green}6/6$
$\color{green}+100.00\%$
$\color{green}551/551$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.