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 320429f commit 6249bfd
Showing 1 changed file with 14 additions and 51 deletions.
65 changes: 14 additions & 51 deletions lib/node_modules/@stdlib/strided/base/ternary/lib/main.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( './ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -47,60 +53,17 @@
* // => <Float64Array>[ 3.0, 6.0, 9.0, 12.0, 15.0 ]
*/
function ternary( arrays, shape, strides, fcn ) {
var sx;
var sy;
var sz;
var sw;
var ix;
var iy;
var iz;
var iw;
var x;
var y;
var z;
var w;
var offsets;
var N;
var i;

N = shape[ 0 ];
if ( N <= 0 ) {
return;
}
sx = strides[ 0 ];
sy = strides[ 1 ];
sz = strides[ 2 ];
sw = strides[ 3 ];
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;
}
if ( sw < 0 ) {
iw = (1-N) * sw;
} else {
iw = 0;
}
x = arrays[ 0 ];
y = arrays[ 1 ];
z = arrays[ 2 ];
w = arrays[ 3 ];
for ( i = 0; i < N; i++ ) {
w[ iw ] = fcn( x[ ix ], y[ iy ], z[ iz ] );
ix += sx;
iy += sy;
iz += sz;
iw += sw;
}
offsets = [
stride2offset( N, strides[ 0 ] ),
stride2offset( N, strides[ 1 ] ),
stride2offset( N, strides[ 2 ] ),
stride2offset( N, strides[ 3 ] )
];
return ndarray( arrays, shape, strides, offsets, fcn );
}


Expand Down

0 comments on commit 6249bfd

Please sign in to comment.