Skip to content

Commit bfaefbd

Browse files
committed
fix: implementation
1 parent 648f0e0 commit bfaefbd

File tree

5 files changed

+69
-92
lines changed

5 files changed

+69
-92
lines changed

lib/node_modules/@stdlib/stats/base/nanrange-by/lib/accessors.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2025 The Stdlib Authors
4+
* Copyright (c) 2020 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,27 +22,33 @@
2222

2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2424

25+
2526
// MAIN //
2627

2728
/**
28-
* Calculates the range of a strided array via a callback function, ignoring `NaN` values.
29+
* Computes the range of a strided array via a callback function, ignoring `NaN` values.
2930
*
3031
* @param {PositiveInteger} N - number of indexed elements
31-
* @param {Collection} x - input array/collection
32-
* @param {integer} strideX - index increment
32+
* @param {Object} x - input array object
33+
* @param {Collection} x.data - input array data
34+
* @param {Array<Function>} x.accessors - array element accessors
35+
* @param {integer} strideX - stride length
3336
* @param {NonNegativeInteger} offsetX - starting index
3437
* @param {Callback} clbk - callback
3538
* @param {*} [thisArg] - execution context
3639
* @returns {number} range
3740
*
3841
* @example
39-
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ];
42+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
43+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
44+
*
45+
* var x = toAccessorArray( [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ] );
4046
*
4147
* function accessor( v ) {
4248
* return v * 2.0;
4349
* }
4450
*
45-
* var v = nanrangeBy( x.length, x, 1, 0, accessor );
51+
* var v = nanrangeBy( x.length, arraylike2object( x ), 1, 0, accessor );
4652
* // returns 18.0
4753
*/
4854
function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) {
@@ -59,40 +65,38 @@ function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) {
5965

6066
// Cache a reference to the element accessor:
6167
get = x.accessors[0];
62-
if ( N <= 0 ) {
63-
return NaN;
64-
}
68+
6569
if ( N === 1 || strideX === 0 ) {
66-
v = clbk.call( thisArg, get( xbuf, 0 ), 0, 0, x );
67-
if ( v === void 0 || isnan( v ) ) {
68-
return NaN;
69-
}
70-
return 0.0;
70+
v = clbk.call( thisArg, get( xbuf, offsetX ), 0, offsetX, x );
71+
if ( v === void 0 || isnan( v ) ) {
72+
return NaN;
73+
}
74+
return 0.0;
7175
}
7276
ix = offsetX;
7377
for ( i = 0; i < N; i++ ) {
7478
min = clbk.call( thisArg, get( xbuf, ix ), i, ix, x );
75-
if ( min === min && min !== void 0 ) {
76-
break;
77-
}
78-
ix += strideX;
79+
if ( min === min && min !== void 0 ) {
80+
break;
81+
}
82+
ix += strideX;
7983
}
8084
if ( i === N ) {
81-
return NaN;
85+
return NaN;
8286
}
8387
max = min;
8488
i += 1;
8589
for ( i; i < N; i++ ) {
86-
ix += strideX;
87-
v = clbk.call( thisArg, get( xbuf, ix ), i, ix, x );
88-
if ( v === void 0 || isnan( v ) ) {
89-
continue;
90-
}
91-
if ( v < min ) {
92-
min = v;
93-
}else if ( v > max ) {
94-
max = v;
95-
}
90+
ix += strideX;
91+
v = clbk.call( thisArg, get( xbuf, ix ), i, ix, x );
92+
if ( v === void 0 || isnan( v ) ) {
93+
continue;
94+
}
95+
if ( v < min ) {
96+
min = v;
97+
} else if ( v > max ) {
98+
max = v;
99+
}
96100
}
97101
return max - min;
98102
}

lib/node_modules/@stdlib/stats/base/nanrange-by/lib/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Calculate the range of a strided array via a callback function and ignoring `NaN` values.
22+
* Compute the range of a strided array via a callback function and ignoring `NaN` values.
2323
*
2424
* @module @stdlib/stats/base/nanrange-by
2525
*
@@ -50,7 +50,14 @@
5050

5151
// MODULES //
5252

53+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
5354
var main = require( './main.js' );
55+
var ndarray = require( './ndarray.js' );
56+
57+
58+
// MAIN //
59+
60+
setReadOnly( main, 'ndarray', ndarray );
5461

5562

5663
// EXPORTS //

lib/node_modules/@stdlib/stats/base/nanrange-by/lib/main.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,35 @@
2020

2121
// MODULES //
2222

23-
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
24-
var nanrangeBy = require( './nanrange_by.js' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
2524
var ndarray = require( './ndarray.js' );
2625

2726

2827
// MAIN //
2928

30-
setReadOnly( nanrangeBy, 'ndarray', ndarray );
29+
/**
30+
* Computes the range of a strided array via a callback function, ignoring `NaN` values.
31+
*
32+
* @param {PositiveInteger} N - number of indexed elements
33+
* @param {Collection} x - input array
34+
* @param {integer} strideX - index increment
35+
* @param {Callback} clbk - callback
36+
* @param {*} [thisArg] - execution context
37+
* @returns {number} range
38+
*
39+
* @example
40+
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ];
41+
*
42+
* function accessor( v ) {
43+
* return v * 2.0;
44+
* }
45+
*
46+
* var v = nanrangeBy( x.length, x, 1, accessor );
47+
* // returns 18.0
48+
*/
49+
function nanrangeBy( N, x, strideX, clbk, thisArg ) {
50+
return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk, thisArg);
51+
}
3152

3253

3354
// EXPORTS //

lib/node_modules/@stdlib/stats/base/nanrange-by/lib/nanrange_by.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

lib/node_modules/@stdlib/stats/base/nanrange-by/lib/ndarray.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2025 The Stdlib Authors.
4+
* Copyright (c) 2020 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ var accessors = require( './accessors.js' );
2828
// MAIN //
2929

3030
/**
31-
* Calculates the range of a strided array via a callback function, ignoring `NaN` values.
31+
* Computes the range of a strided array via a callback function, ignoring `NaN` values.
3232
*
3333
* @param {PositiveInteger} N - number of indexed elements
3434
* @param {Collection} x - input array/collection
@@ -51,8 +51,8 @@ var accessors = require( './accessors.js' );
5151
function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) {
5252
var max;
5353
var min;
54-
var o;
5554
var ix;
55+
var o;
5656
var v;
5757
var i;
5858

@@ -64,7 +64,7 @@ function nanrangeBy( N, x, strideX, offsetX, clbk, thisArg ) {
6464
return accessors( N, o, strideX, offsetX, clbk, thisArg );
6565
}
6666
if ( N === 1 || strideX === 0 ) {
67-
v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
67+
v = clbk.call( thisArg, x[ offsetX ], 0, offsetX, x );
6868
if ( v === void 0 || isnan( v ) ) {
6969
return NaN;
7070
}

0 commit comments

Comments
 (0)