Skip to content

Commit 842c64e

Browse files
committed
fix: apply suggestions from code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d1d5493 commit 842c64e

8 files changed

Lines changed: 159 additions & 142 deletions

File tree

lib/node_modules/@stdlib/blas/ext/base/smskrev/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The function has the following parameters:
5050
- **N**: number of indexed elements.
5151
- **x**: input [`Float32Array`][@stdlib/array/float32].
5252
- **strideX**: stride length for `x`.
53-
- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered unmasked and will participate in the reversal. If a `mask` array element is non-zero, the corresponding element in `x` is considered masked and the element will not participate in the reversal.
53+
- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in the reversal. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from the reversal.
5454
- **strideMask**: stride length for `mask`.
5555

5656
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to reverse every other element:
@@ -127,7 +127,6 @@ smskrev.ndarray( 3, x, 1, x.length-3, mask, 1, mask.length-3 );
127127
## Notes
128128

129129
- If `N <= 0`, both functions return the strided array unchanged.
130-
- A swap is only performed when **both** elements in a pair are unmasked (i.e., their corresponding mask values are `0`). If either element is masked, neither element is modified.
131130
- Where possible, one should "reverse" a strided array by negating its stride, which is an `O(1)` operation, in contrast to performing an in-place reversal, which is `O(N)`. However, in certain circumstances, this is not tenable, particularly when interfacing with libraries which assume and/or expect a specific memory layout (e.g., strided array elements arranged in memory in ascending order). In general, when working with strided arrays, only perform an in-place reversal when strictly necessary.
132131

133132
</section>

lib/node_modules/@stdlib/blas/ext/base/smskrev/docs/repl.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
If `N <= 0`, the function returns `x` unchanged.
1313

14-
A mask value of `0` indicates that the corresponding element in `x`
15-
participates in the reversal, while a non-zero mask value indicates that the
16-
corresponding element should be skipped.
14+
If a `mask` array element is `0`, the corresponding element in `x` is
15+
considered valid and included in the reversal.
16+
17+
If a `mask` array element is `1`, the corresponding element in `x` is
18+
considered invalid/missing and excluded from the reversal.
1719

1820
Parameters
1921
----------

lib/node_modules/@stdlib/blas/ext/base/smskrev/lib/ndarray.js

Lines changed: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,10 @@
1818

1919
'use strict';
2020

21-
// MODULES //
22-
23-
var floor = require( '@stdlib/math/base/special/floor' );
24-
25-
26-
// VARIABLES //
27-
28-
var M = 3;
29-
30-
3121
// MAIN //
3222

3323
/**
34-
* Reverses a single-precision floating-point strided array in-place according to a mask.
24+
* Reverses a single-precision floating-point strided array in-place according to a mask and using alternative indexing semantics.
3525
*
3626
* @param {PositiveInteger} N - number of indexed elements
3727
* @param {Float32Array} x - input array
@@ -53,80 +43,45 @@ var M = 3;
5343
* // x => <Float32Array>[ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ]
5444
*/
5545
function smskrev( N, x, strideX, offsetX, mask, strideMask, offsetMask ) {
46+
var right;
47+
var left;
5648
var tmp;
5749
var ix;
5850
var iy;
5951
var im;
6052
var jm;
61-
var m;
62-
var n;
63-
var i;
6453

6554
if ( N <= 0 ) {
6655
return x;
6756
}
68-
n = floor( N/2 );
57+
left = 0;
58+
right = N - 1;
6959
ix = offsetX;
60+
iy = offsetX + ( (N-1) * strideX );
7061
im = offsetMask;
71-
72-
// Use loop unrolling if the strides are equal to `1`...
73-
if ( strideX === 1 && strideMask === 1 ) {
74-
m = n % M;
75-
iy = ix + N - 1;
76-
jm = im + N - 1;
77-
78-
// If we have a remainder, run a clean-up loop...
79-
if ( m > 0 ) {
80-
for ( i = 0; i < m; i++ ) {
81-
if ( mask[ im ] === 0 && mask[ jm ] === 0 ) {
82-
tmp = x[ ix ];
83-
x[ ix ] = x[ iy ];
84-
x[ iy ] = tmp;
85-
}
86-
ix += 1;
87-
im += 1;
88-
iy -= 1;
89-
jm -= 1;
90-
}
91-
}
92-
if ( n < M ) {
93-
return x;
62+
jm = offsetMask + ( (N-1) * strideMask );
63+
while ( left < right ) {
64+
while ( left < right && mask[ im ] !== 0 ) {
65+
left += 1;
66+
ix += strideX;
67+
im += strideMask;
9468
}
95-
for ( i = m; i < n; i += M ) {
96-
if ( mask[ im ] === 0 && mask[ jm ] === 0 ) {
97-
tmp = x[ ix ];
98-
x[ ix ] = x[ iy ];
99-
x[ iy ] = tmp;
100-
}
101-
if ( mask[ im+1 ] === 0 && mask[ jm-1 ] === 0 ) {
102-
tmp = x[ ix+1 ];
103-
x[ ix+1 ] = x[ iy-1 ];
104-
x[ iy-1 ] = tmp;
105-
}
106-
if ( mask[ im+2 ] === 0 && mask[ jm-2 ] === 0 ) {
107-
tmp = x[ ix+2 ];
108-
x[ ix+2 ] = x[ iy-2 ];
109-
x[ iy-2 ] = tmp;
110-
}
111-
ix += M;
112-
im += M;
113-
iy -= M;
114-
jm -= M;
69+
while ( right > left && mask[ jm ] !== 0 ) {
70+
right -= 1;
71+
iy -= strideX;
72+
jm -= strideMask;
11573
}
116-
return x;
117-
}
118-
iy = ix + ( (N-1) * strideX );
119-
jm = im + ( (N-1) * strideMask );
120-
for ( i = 0; i < n; i++ ) {
121-
if ( mask[ im ] === 0 && mask[ jm ] === 0 ) {
74+
if ( left < right ) {
12275
tmp = x[ ix ];
12376
x[ ix ] = x[ iy ];
12477
x[ iy ] = tmp;
78+
left += 1;
79+
ix += strideX;
80+
im += strideMask;
81+
right -= 1;
82+
iy -= strideX;
83+
jm -= strideMask;
12584
}
126-
ix += strideX;
127-
im += strideMask;
128-
iy -= strideX;
129-
jm -= strideMask;
13085
}
13186
return x;
13287
}

lib/node_modules/@stdlib/blas/ext/base/smskrev/src/main.c

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,80 +48,45 @@ void API_SUFFIX(stdlib_strided_smskrev)( const CBLAS_INT N, float *X, const CBLA
4848
* @param offsetMask starting index for Mask
4949
*/
5050
void API_SUFFIX(stdlib_strided_smskrev_ndarray)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask ) {
51+
CBLAS_INT right;
52+
CBLAS_INT left;
5153
CBLAS_INT ix;
5254
CBLAS_INT iy;
5355
CBLAS_INT im;
5456
CBLAS_INT jm;
55-
CBLAS_INT m;
56-
CBLAS_INT n;
57-
CBLAS_INT i;
5857
float tmp;
5958

6059
if ( N <= 0 ) {
6160
return;
6261
}
63-
n = N / 2;
62+
left = 0;
63+
right = N - 1;
6464
ix = offsetX;
65+
iy = offsetX + ( (N-1) * strideX );
6566
im = offsetMask;
66-
67-
// Use loop unrolling if the strides are equal to `1`...
68-
if ( strideX == 1 && strideMask == 1 ) {
69-
m = n % 3;
70-
iy = ix + N - 1;
71-
jm = im + N - 1;
72-
73-
// If we have a remainder, run a clean-up loop...
74-
if ( m > 0 ) {
75-
for ( i = 0; i < m; i++ ) {
76-
if ( Mask[ im ] == 0 && Mask[ jm ] == 0 ) {
77-
tmp = X[ ix ];
78-
X[ ix ] = X[ iy ];
79-
X[ iy ] = tmp;
80-
}
81-
ix += 1;
82-
im += 1;
83-
iy -= 1;
84-
jm -= 1;
85-
}
86-
}
87-
if ( n < 3 ) {
88-
return;
67+
jm = offsetMask + ( (N-1) * strideMask );
68+
while ( left < right ) {
69+
while ( left < right && Mask[ im ] != 0 ) {
70+
left += 1;
71+
ix += strideX;
72+
im += strideMask;
8973
}
90-
for ( i = m; i < n; i += 3 ) {
91-
if ( Mask[ im ] == 0 && Mask[ jm ] == 0 ) {
92-
tmp = X[ ix ];
93-
X[ ix ] = X[ iy ];
94-
X[ iy ] = tmp;
95-
}
96-
if ( Mask[ im+1 ] == 0 && Mask[ jm-1 ] == 0 ) {
97-
tmp = X[ ix+1 ];
98-
X[ ix+1 ] = X[ iy-1 ];
99-
X[ iy-1 ] = tmp;
100-
}
101-
if ( Mask[ im+2 ] == 0 && Mask[ jm-2 ] == 0 ) {
102-
tmp = X[ ix+2 ];
103-
X[ ix+2 ] = X[ iy-2 ];
104-
X[ iy-2 ] = tmp;
105-
}
106-
ix += 3;
107-
im += 3;
108-
iy -= 3;
109-
jm -= 3;
74+
while ( right > left && Mask[ jm ] != 0 ) {
75+
right -= 1;
76+
iy -= strideX;
77+
jm -= strideMask;
11078
}
111-
return;
112-
}
113-
iy = ix + ( (N-1) * strideX );
114-
jm = im + ( (N-1) * strideMask );
115-
for ( i = 0; i < n; i++ ) {
116-
if ( Mask[ im ] == 0 && Mask[ jm ] == 0 ) {
79+
if ( left < right ) {
11780
tmp = X[ ix ];
11881
X[ ix ] = X[ iy ];
11982
X[ iy ] = tmp;
83+
left += 1;
84+
ix += strideX;
85+
im += strideMask;
86+
right -= 1;
87+
iy -= strideX;
88+
jm -= strideMask;
12089
}
121-
ix += strideX;
122-
im += strideMask;
123-
iy -= strideX;
124-
jm -= strideMask;
12590
}
12691
return;
12792
}

lib/node_modules/@stdlib/blas/ext/base/smskrev/test/test.ndarray.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,23 @@ tape( 'the function supports masking', function test( t ) {
137137
t.end();
138138
});
139139

140-
tape( 'the function skips pairs where either element is masked', function test( t ) {
140+
tape( 'the function excludes masked elements from the reversal', function test( t ) {
141141
var expected;
142142
var mask;
143143
var x;
144144

145+
// Mask the first element:
145146
x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
146-
mask = new Uint8Array( [ 1, 0, 0, 1 ] );
147-
expected = new Float32Array( [ 1.0, 3.0, 2.0, 4.0 ] );
147+
mask = new Uint8Array( [ 1, 0, 0, 0 ] );
148+
expected = new Float32Array( [ 1.0, 4.0, 3.0, 2.0 ] );
149+
150+
smskrev( x.length, x, 1, 0, mask, 1, 0 );
151+
t.deepEqual( x, expected, 'returns expected value' );
152+
153+
// Mask the last element:
154+
x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
155+
mask = new Uint8Array( [ 0, 0, 0, 1 ] );
156+
expected = new Float32Array( [ 3.0, 2.0, 1.0, 4.0 ] );
148157

149158
smskrev( x.length, x, 1, 0, mask, 1, 0 );
150159
t.deepEqual( x, expected, 'returns expected value' );
@@ -264,6 +273,21 @@ tape( 'the function supports specifying a negative stride', function test( t ) {
264273
t.end();
265274
});
266275

276+
tape( 'the function supports specifying a mask stride', function test( t ) {
277+
var expected;
278+
var mask;
279+
var x;
280+
281+
x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
282+
mask = new Uint8Array( [ 0, 0, 0, 0, 1 ] );
283+
expected = new Float32Array( [ 2.0, 1.0, 3.0 ] );
284+
285+
smskrev( x.length, x, 1, 0, mask, 2, 0 );
286+
t.deepEqual( x, expected, 'returns expected value' );
287+
288+
t.end();
289+
});
290+
267291
tape( 'the function supports specifying an offset', function test( t ) {
268292
var expected;
269293
var mask;

lib/node_modules/@stdlib/blas/ext/base/smskrev/test/test.ndarray.native.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,23 @@ tape( 'the function supports masking', opts, function test( t ) {
146146
t.end();
147147
});
148148

149-
tape( 'the function skips pairs where either element is masked', opts, function test( t ) {
149+
tape( 'the function excludes masked elements from the reversal', opts, function test( t ) {
150150
var expected;
151151
var mask;
152152
var x;
153153

154+
// Mask the first element:
154155
x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
155-
mask = new Uint8Array( [ 1, 0, 0, 1 ] );
156-
expected = new Float32Array( [ 1.0, 3.0, 2.0, 4.0 ] );
156+
mask = new Uint8Array( [ 1, 0, 0, 0 ] );
157+
expected = new Float32Array( [ 1.0, 4.0, 3.0, 2.0 ] );
158+
159+
smskrev( x.length, x, 1, 0, mask, 1, 0 );
160+
t.deepEqual( x, expected, 'returns expected value' );
161+
162+
// Mask the last element:
163+
x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
164+
mask = new Uint8Array( [ 0, 0, 0, 1 ] );
165+
expected = new Float32Array( [ 3.0, 2.0, 1.0, 4.0 ] );
157166

158167
smskrev( x.length, x, 1, 0, mask, 1, 0 );
159168
t.deepEqual( x, expected, 'returns expected value' );
@@ -273,6 +282,21 @@ tape( 'the function supports specifying a negative stride', opts, function test(
273282
t.end();
274283
});
275284

285+
tape( 'the function supports specifying a mask stride', opts, function test( t ) {
286+
var expected;
287+
var mask;
288+
var x;
289+
290+
x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
291+
mask = new Uint8Array( [ 0, 0, 0, 0, 1 ] );
292+
expected = new Float32Array( [ 2.0, 1.0, 3.0 ] );
293+
294+
smskrev( x.length, x, 1, 0, mask, 2, 0 );
295+
t.deepEqual( x, expected, 'returns expected value' );
296+
297+
t.end();
298+
});
299+
276300
tape( 'the function supports specifying an offset', opts, function test( t ) {
277301
var expected;
278302
var mask;

0 commit comments

Comments
 (0)