Skip to content

Commit 07f85a1

Browse files
committed
refactor: reduce code duplication
1 parent 3656b32 commit 07f85a1

File tree

3 files changed

+13
-33
lines changed

3 files changed

+13
-33
lines changed

lib/node_modules/@stdlib/strided/base/smap/lib/main.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -47,28 +53,7 @@
4753
* // => <Float32Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ]
4854
*/
4955
function smap( N, x, strideX, y, strideY, fcn ) {
50-
var ix;
51-
var iy;
52-
var i;
53-
if ( N <= 0 ) {
54-
return y;
55-
}
56-
if ( strideX < 0 ) {
57-
ix = (1-N) * strideX;
58-
} else {
59-
ix = 0;
60-
}
61-
if ( strideY < 0 ) {
62-
iy = (1-N) * strideY;
63-
} else {
64-
iy = 0;
65-
}
66-
for ( i = 0; i < N; i++ ) {
67-
y[ iy ] = fcn( x[ ix ] );
68-
ix += strideX;
69-
iy += strideY;
70-
}
71-
return y;
56+
return ndarray( N, x, strideX, stride2offset( N, strideX ), y, strideY, stride2offset( N, strideY ), fcn ); // eslint-disable-line max-len
7257
}
7358

7459

lib/node_modules/@stdlib/strided/base/smap/manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
],
3333
"libraries": [],
3434
"libpath": [],
35-
"dependencies": []
35+
"dependencies": [
36+
"@stdlib/strided/base/stride2offset"
37+
]
3638
}
3739
]
3840
}

lib/node_modules/@stdlib/strided/base/smap/src/main.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "stdlib/strided/base/smap.h"
20+
#include "stdlib/strided/base/stride2offset.h"
2021
#include <stdint.h>
2122

2223
/**
@@ -51,16 +52,8 @@ void stdlib_strided_smap( const int64_t N, const float *X, const int64_t strideX
5152
if ( N <= 0 ) {
5253
return;
5354
}
54-
if ( strideX < 0 ) {
55-
ix = (1-N) * strideX;
56-
} else {
57-
ix = 0;
58-
}
59-
if ( strideY < 0 ) {
60-
iy = (1-N) * strideY;
61-
} else {
62-
iy = 0;
63-
}
55+
ix = stdlib_strided_stride2offset( N, strideX );
56+
iy = stdlib_strided_stride2offset( N, strideY );
6457
for ( i = 0; i < N; i++ ) {
6558
Y[ iy ] = fcn( X[ ix ] );
6659
ix += strideX;

0 commit comments

Comments
 (0)