Skip to content

Commit 6945eb9

Browse files
committed
feat: refactor and add protocol support to stats/base/max-by
--- 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: na - 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: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d9faaa4 commit 6945eb9

File tree

6 files changed

+161
-20
lines changed

6 files changed

+161
-20
lines changed

lib/node_modules/@stdlib/stats/base/max-by/benchmark/benchmark.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
2728
var pkg = require( './../package.json' ).name;
@@ -49,13 +50,7 @@ function accessor( value ) {
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
53+
var x = filledarrayBy( len, 'float64', discreteUniform( -50, 50 ) );
5954
return benchmark;
6055

6156
function benchmark( b ) {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
2728
var pkg = require( './../package.json' ).name;
@@ -49,13 +50,7 @@ function accessor( value ) {
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
53+
var x = filledarrayBy( len, 'float64', discreteUniform( -50, 50 ) );
5954
return benchmark;
6055

6156
function benchmark( b ) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Calculates the maximum value of a strided array via a callback function.
31+
*
32+
* @param {PositiveInteger} N - number of indexed elements
33+
* @param {Collection} x - input array/collection
34+
* @param {integer} stride - index increment
35+
* @param {Callback} clbk - callback
36+
* @param {*} [thisArg] - execution context
37+
* @returns {number} maximum value
38+
*
39+
* @example
40+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
41+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
42+
*
43+
* var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
44+
*
45+
* function accessor( v ) {
46+
* return v * 2.0;
47+
* }
48+
* var v = maxBy( x.length, toAccessorArray( x ), 1, accessor );
49+
*
50+
*/
51+
function maxBy( N, x, stride, clbk, thisArg ) {
52+
var itemx;
53+
var xbuf;
54+
var item;
55+
var get;
56+
var max;
57+
var ix;
58+
var v;
59+
var i;
60+
61+
// Cache reference to array data
62+
xbuf = x.data;
63+
64+
// Cache a reference to the element accessor:
65+
get = x.accessors[ 0 ];
66+
67+
if ( N <= 0 ) {
68+
return NaN;
69+
}
70+
71+
if ( N === 1 || stride === 0 ) {
72+
v = clbk.call( thisArg, x[ 0 ], 0, 0, x);
73+
if ( v === void 0 ) {
74+
return NaN;
75+
}
76+
return v;
77+
}
78+
if ( stride < 0 ) {
79+
ix = (1-N) * stride;
80+
} else {
81+
ix = 0;
82+
}
83+
for ( i = 0; i < N; i++) {
84+
item = get( xbuf, ix );
85+
max = clbk.call( thisArg, item, i, ix, x);
86+
if ( max !== void 0 ) {
87+
break;
88+
}
89+
ix += stride;
90+
}
91+
if ( i === N) {
92+
return NaN;
93+
}
94+
i += 1;
95+
for ( i; i < N; i++) {
96+
ix += stride;
97+
itemx = get( xbuf, ix );
98+
v = clbk.call( thisArg, itemx, i, ix, x );
99+
if ( v === void 0 ) {
100+
continue;
101+
}
102+
if ( isnan(v) ) {
103+
return v;
104+
}
105+
if ( v > max || ( v === max && isPositiveZero( v ))) {
106+
max = v;
107+
}
108+
}
109+
return max;
110+
}
111+
112+
113+
// EXPORTS //
114+
115+
module.exports = maxBy;

lib/node_modules/@stdlib/stats/base/max-by/lib/max_by.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2424
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
25+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
26+
var accessors = require( './accessors.js' );
2527

2628

2729
// MAIN //
@@ -49,12 +51,17 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
4951
function maxBy( N, x, stride, clbk, thisArg ) {
5052
var max;
5153
var ix;
54+
var o;
5255
var v;
5356
var i;
5457

5558
if ( N <= 0 ) {
5659
return NaN;
5760
}
61+
o = arraylike2object( x );
62+
if ( o.accessorProtocol ) {
63+
return accessors( N, o, stride, clbk, thisArg);
64+
}
5865
if ( N === 1 || stride === 0 ) {
5966
v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
6067
if ( v === void 0 ) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2424
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
25+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
26+
var accessors = require( './accessors.js' );
2527

2628

2729
// MAIN //
@@ -50,12 +52,17 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
5052
function maxBy( N, x, stride, offset, clbk, thisArg ) {
5153
var max;
5254
var ix;
55+
var o;
5356
var v;
5457
var i;
5558

5659
if ( N <= 0 ) {
5760
return NaN;
5861
}
62+
o = arraylike2object( x );
63+
if ( o.accessorProtocol ) {
64+
return accessors( N, o, stride, offset );
65+
}
5966
if ( N === 1 || stride === 0 ) {
6067
v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
6168
if ( v === void 0 ) {

lib/node_modules/@stdlib/stats/base/max-by/test/test.max_by.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var floor = require( '@stdlib/math/base/special/floor' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
2727
var Float64Array = require( '@stdlib/array/float64' );
28+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2829
var maxBy = require( './../lib/max_by.js' );
2930

3031

@@ -75,11 +76,11 @@ tape( 'the function calculates the maximum value of a strided array via a callba
7576
v = maxBy( x.length, x, 1, accessor );
7677
t.strictEqual( isnan( v ), true, 'returns expected value' );
7778

78-
x = new Array( 5 ); // sparse array
79+
x = [ 5 ]; // sparse array
7980
v = maxBy( x.length, x, 1, accessor );
8081
t.strictEqual( isnan( v ), true, 'returns expected value' );
8182

82-
x = new Array( 5 ); // sparse array
83+
x = [ 5 ]; // sparse array
8384
x[ 2 ] = 1.0;
8485
v = maxBy( x.length, x, 1, accessor );
8586
t.strictEqual( v, 2.0, 'returns expected value' );
@@ -111,7 +112,7 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first
111112
v = maxBy( 1, x, 1, accessor );
112113
t.strictEqual( v, 2.0, 'returns expected value' );
113114

114-
x = new Array( 1 ); // sparse array
115+
x = [ 1 ]; // sparse array
115116

116117
v = maxBy( 1, x, 1, accessor );
117118
t.strictEqual( isnan( v ), true, 'returns expected value' );
@@ -142,6 +143,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
142143
t.end();
143144
});
144145

146+
tape( 'the function calculates the maximum value of a accessor array via a callback function', function test( t ) {
147+
var x;
148+
var v;
149+
150+
x = [
151+
1.0, // 0
152+
2.0,
153+
2.0, // 1
154+
-7.0,
155+
-2.0, // 2
156+
3.0,
157+
4.0, // 3
158+
2.0
159+
];
160+
161+
v = maxBy( x.length, toAccessorArray( x ), 1, accessor );
162+
163+
t.strictEqual( v, 8.0, 'returns expected value' );
164+
t.end();
165+
});
166+
145167
tape( 'the function supports a negative `stride` parameter', function test( t ) {
146168
var N;
147169
var x;
@@ -174,7 +196,7 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
174196
v = maxBy( x.length, x, 0, accessor );
175197
t.strictEqual( v, 2.0, 'returns expected value' );
176198

177-
x = new Array( 1 ); // sparse array
199+
x = [ 1 ]; // sparse array
178200

179201
v = maxBy( 1, x, 0, accessor );
180202
t.strictEqual( isnan( v ), true, 'returns expected value' );

0 commit comments

Comments
 (0)