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 6249bfd commit aeb7b51
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 62 deletions.
45 changes: 12 additions & 33 deletions lib/node_modules/@stdlib/strided/base/unary/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 @@ -58,42 +64,15 @@
* // => <Float64Array>[ 20.0, 40.0, 60.0, 80.0, 100.0 ]
*/
function unary( arrays, shape, strides, accessors, fcn ) {
var xget;
var yset;
var sx;
var sy;
var ix;
var iy;
var x;
var y;
var offsets;
var N;
var i;

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


Expand Down
41 changes: 12 additions & 29 deletions lib/node_modules/@stdlib/strided/base/unary/lib/unary.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( './unary.ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -49,38 +55,15 @@
* // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ]
*/
function unary( arrays, shape, strides, fcn ) {
var sx;
var sy;
var ix;
var iy;
var x;
var y;
var offsets;
var N;
var i;

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


Expand Down

0 comments on commit aeb7b51

Please sign in to comment.