diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/README.md b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/README.md new file mode 100644 index 000000000000..844b1efe8f92 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/README.md @@ -0,0 +1,270 @@ + + +# gsortshBy + +> Sort a strided array using Shellsort according to a provided callback function. + +
+ +## Usage + +```javascript +var gsortshBy = require( '@stdlib/blas/ext/base/gsortsh-by' ); +``` + +#### gsortshBy( N, x, strideX, clbk\[, thisArg] ) + +Sorts a strided array using Shellsort according to a provided callback function. + +```javascript +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsortshBy( x.length, x, 1, clbk ); +// x => [ -4.0, -2.0, 1.0, 3.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length. +- **clbk**: callback function. The function should compare two values `a` and `b` and return a negative value if `a` should come before `b`, a positive value if `a` should come after `b`, and zero if `a` and `b` are equivalent. +- **thisArg**: callback execution context (_optional_). + +To set the callback execution context, provide a `thisArg`. + +```javascript +function clbk( a, b ) { + this.count += 1; + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; +} + +var context = { + 'count': 0 +}; + +var x = [ 10.0, -1.0, 3.0, 50.0 ]; + +gsortshBy( x.length, x, 1, clbk, context ); +// x => [ 50.0, 10.0, 3.0, -1.0 ] + +var cnt = context.count; +// returns 6 +``` + +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element: + +```javascript +function clbk( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; +} + +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsortshBy( 2, x, 2, clbk ); +// x => [ 3.0, -2.0, 1.0, -4.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +gsortshBy( 2, x1, 2, clbk ); +// x0 => [ 1.0, -4.0, 3.0, -2.0 ] +``` + +#### gsortshBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) + +Sorts a strided array using Shellsort according to a provided callback function and using alternative indexing semantics. + +```javascript +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsortshBy.ndarray( x.length, x, 1, 0, clbk ); +// x => [ -4.0, -2.0, 1.0, 3.0 ] +``` + +The function has the following additional parameters: + +- **offsetX**: starting index. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements: + +```javascript +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +gsortshBy.ndarray( 3, x, 1, x.length-3, clbk ); +// x => [ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions return `x` unchanged. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). +- The algorithm has space complexity `O(1)` and worst case time complexity `O(N^(4/3))`. +- The algorithm is efficient for **shorter** strided arrays (typically `N <= 50`). +- The algorithm is **unstable**, meaning that the algorithm may change the order of strided array elements which are equal or equivalent. +- The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var gsortshBy = require( '@stdlib/blas/ext/base/gsortsh-by' ); + +function clbk( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; +} + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float64' +}); +console.log( x ); + +gsortshBy( x.length, x, 1, clbk ); +console.log( x ); +``` + +
+ + + +* * * + +
+ +## References + +- Shell, Donald L. 1959. "A High-Speed Sorting Procedure." _Communications of the ACM_ 2 (7). Association for Computing Machinery: 30–32. doi:[10.1145/368370.368387][@shell:1959a]. +- Sedgewick, Robert. 1986. "A new upper bound for Shellsort." _Journal of Algorithms_ 7 (2): 159–73. doi:[10.1016/0196-6774(86)90001-5][@sedgewick:1986a]. +- Ciura, Marcin. 2001. "Best Increments for the Average Case of Shellsort." In _Fundamentals of Computation Theory_, 106–17. Springer Berlin Heidelberg. doi:[10.1007/3-540-44669-9_12][@ciura:2001a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.js new file mode 100644 index 000000000000..bfb0817a506d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.js @@ -0,0 +1,149 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var sa; + var sb; + var a; + var b; + var x; + var i; + var j; + + a = 1.0; + b = 10.0; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + sa = (b-a) * (j/len); + sb = sa / 2.0; + tmp.push( floor( uniform( a+sa, b+sb ) ) ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::mostly_sorted,few_uniques:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..0653af0c20d5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js @@ -0,0 +1,149 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var sa; + var sb; + var a; + var b; + var x; + var i; + var j; + + a = 1.0; + b = 10.0; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + sa = (b-a) * (j/len); + sb = sa / 2.0; + tmp.push( floor( uniform( a+sa, b+sb ) ) ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::mostly_sorted,few_uniques:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.js new file mode 100644 index 000000000000..e5cf5f252944 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randu() * j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::mostly_sorted,random:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.ndarray.js new file mode 100644 index 000000000000..e6a39d9abb53 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.mostly_sorted_random.ndarray.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randu() * j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::mostly_sorted,random:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js new file mode 100644 index 000000000000..ca0ddd1cf9e6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js @@ -0,0 +1,149 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var sa; + var sb; + var a; + var b; + var x; + var i; + var j; + + a = -10.0; + b = -1.0; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + sb = (b-a) * (j/len); + sa = sb / 2.0; + tmp.push( floor( uniform( a-sa, b-sb ) ) ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_mostly_sorted,few_uniques:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..e81fcd9831d0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js @@ -0,0 +1,149 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var sa; + var sb; + var a; + var b; + var x; + var i; + var j; + + a = -10.0; + b = -1.0; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + sb = (b-a) * (j/len); + sa = sb / 2.0; + tmp.push( floor( uniform( a-sa, b-sb ) ) ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_mostly_sorted,few_uniques:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.js new file mode 100644 index 000000000000..34f01cfaffeb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( -1.0 * randu() * j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_mostly_sorted,random:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js new file mode 100644 index 000000000000..4beb16996fc3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( -1.0 * randu() * j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_mostly_sorted,random:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.js new file mode 100644 index 000000000000..69ba136fc706 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.js @@ -0,0 +1,150 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var M; + var x; + var v; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + M = floor( len*0.333 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + v = randi(); + for ( j = 0; j < len; j++ ) { + if ( i % M === 0 ) { + v -= randi(); + } + tmp.push( v ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_sorted,few_uniques:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..90abf79b81b4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js @@ -0,0 +1,150 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var M; + var x; + var v; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + M = floor( len*0.333 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + v = randi(); + for ( j = 0; j < len; j++ ) { + if ( i % M === 0 ) { + v -= randi(); + } + tmp.push( v ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_sorted,few_uniques:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.js new file mode 100644 index 000000000000..38727cc3f249 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( iter - j - randu() ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_sorted,random:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.ndarray.js new file mode 100644 index 000000000000..ab76b875a344 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.rev_sorted_random.ndarray.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( iter - j - randu() ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::reverse_sorted,random:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.js new file mode 100644 index 000000000000..7a08b8a26825 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.js @@ -0,0 +1,147 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var M; + var x; + var v; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + M = floor( len*0.333 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + v = randi(); + for ( j = 0; j < len; j++ ) { + if ( j % M === 0 ) { + v += randi(); + } + tmp.push( v ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + opts = { + 'iterations': 1e7 / len + }; + f = createBenchmark( opts.iterations, len ); + bench( format( '%s::sorted,few_uniques:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..ac6bfc25a160 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_few_uniques.ndarray.js @@ -0,0 +1,147 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var M; + var x; + var v; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + M = floor( len*0.333 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + v = randi(); + for ( j = 0; j < len; j++ ) { + if ( j % M === 0 ) { + v += randi(); + } + tmp.push( v ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + opts = { + 'iterations': 1e7 / len + }; + f = createBenchmark( opts.iterations, len ); + bench( format( '%s::sorted,few_uniques:ndarray:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.js new file mode 100644 index 000000000000..bc134ee05786 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.js @@ -0,0 +1,136 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randu() + j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + opts = { + 'iterations': 1e7 / len + }; + f = createBenchmark( opts.iterations, len ); + bench( format( '%s::sorted,random:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.ndarray.js new file mode 100644 index 000000000000..9f0871ea6ea0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.sorted_random.ndarray.js @@ -0,0 +1,136 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randu() + j ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 5; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + opts = { + 'iterations': 1e7 / len + }; + f = createBenchmark( opts.iterations, len ); + bench( format( '%s::sorted,random:ndarray:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.js new file mode 100644 index 000000000000..c2993bd19b5b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var x; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randi() ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,few_uniques:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..0257829b483a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var randi; + var tmp; + var x; + var i; + var j; + + randi = discreteUniform( 1, 10 ); + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( randi() ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,few_uniques:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.js new file mode 100644 index 000000000000..8b55a79e05f5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( (randu()*20.0) - 10.0 ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,random:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.ndarray.js new file mode 100644 index 000000000000..3ed5214276e0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/benchmark/benchmark.unsorted_random.ndarray.js @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortshBy = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {number} comparison result +*/ +function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} iter - number of iterations +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( iter, len ) { + var tmp; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = []; + for ( j = 0; j < len; j++ ) { + tmp.push( (randu()*20.0) - 10.0 ); + } + x.push( tmp ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var xc; + var y; + var i; + + xc = x.slice(); + for ( i = 0; i < iter; i++ ) { + xc[ i ] = x[ i ].slice(); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gsortshBy( len, xc[ i ], 1, 0, clbk ); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,random:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/repl.txt new file mode 100644 index 000000000000..bfc2c1e4d55b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/repl.txt @@ -0,0 +1,167 @@ + +{{alias}}( N, x, strideX, clbk[, thisArg] ) + Sorts a strided array using Shellsort according to a provided callback + function. + + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `N <= 0`, the function returns `x` unchanged. + + The callback function should return a negative value if `a` should come + before `b`, a positive value if `a` should come after `b`, and zero if `a` + and `b` are equivalent. + + The algorithm has space complexity O(1) and worst case time complexity + O(N^(4/3)). + + The algorithm is efficient for *shorter* strided arrays (typically N <= 50). + + The algorithm is *unstable*, meaning that the algorithm may change the order + of strided array elements which are equal or equivalent. + + The input strided array is sorted *in-place* (i.e., the input strided array + is *mutated*). + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: ArrayLikeObject + Input array. + + strideX: integer + Stride length. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + x: ArrayLikeObject + Input array. + + Examples + -------- + // Standard Usage: + > function clbk( a, b ) { + ... if ( a < b ) { + ... return -1; + ... } + ... if ( a > b ) { + ... return 1; + ... } + ... return 0; + ... }; + > var x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}( x.length, x, 1, clbk ) + [ -4.0, -2.0, 1.0, 3.0 ] + + // Using `N` and stride parameters: + > function clbk( a, b ) { + ... if ( a > b ) { + ... return -1; + ... } + ... if ( a < b ) { + ... return 1; + ... } + ... return 0; + ... }; + > x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}( 2, x, 2, clbk ) + [ 3.0, -2.0, 1.0, -4.0 ] + + // Using view offsets: + > function clbk( a, b ) { + ... if ( a < b ) { + ... return -1; + ... } + ... if ( a > b ) { + ... return 1; + ... } + ... return 0; + ... }; + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > {{alias}}( 2, x1, 2, clbk ) + [ -4.0, 3.0, -2.0 ] + > x0 + [ 1.0, -4.0, 3.0, -2.0 ] + + +{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] ) + Sorts a strided array using Shellsort according to a provided callback + function and using alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameter supports indexing semantics based on a starting + index. + + The callback function should return a negative value if `a` should come + before `b`, a positive value if `a` should come after `b`, and zero if `a` + and `b` are equivalent. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: ArrayLikeObject + Input array. + + strideX: integer + Stride length. + + offsetX: integer + Starting index. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + x: ArrayLikeObject + Input array. + + Examples + -------- + // Standard Usage: + > function clbk( a, b ) { + ... if ( a < b ) { + ... return -1; + ... } + ... if ( a > b ) { + ... return 1; + ... } + ... return 0; + ... }; + > var x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}.ndarray( x.length, x, 1, 0, clbk ) + [ -4.0, -2.0, 1.0, 3.0 ] + + // Using an index offset: + > function clbk( a, b ) { + ... if ( a < b ) { + ... return -1; + ... } + ... if ( a > b ) { + ... return 1; + ... } + ... return 0; + ... }; + > x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}.ndarray( 2, x, 2, 1, clbk ) + [ 1.0, -4.0, 3.0, -2.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/index.d.ts new file mode 100644 index 000000000000..643f3474b19b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/index.d.ts @@ -0,0 +1,183 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = Collection | AccessorArrayLike; + +/** +* Comparator function. +* +* @returns result +*/ +type Unary = ( this: ThisArg ) => number; + +/** +* Comparator function. +* +* @param a - first value +* @returns result +*/ +type Binary = ( this: ThisArg, a: T ) => number; + +/** +* Comparator function. +* +* @param a - first value +* @param b - second value +* @returns result +*/ +type Ternary = ( this: ThisArg, a: T, b: T ) => number; + +/** +* Comparator function. +* +* @param a - first value +* @param b - second value +* @param array - input array +* @returns result +*/ +type Quaternary = ( this: ThisArg, a: T, b: T, array: U ) => number; + +/** +* Comparator function. +* +* @param a - first value +* @param b - second value +* @param array - input array +* @returns result +*/ +type Callback = Unary | Binary | Ternary | Quaternary; + +/** +* Interface describing `gsortshBy`. +*/ +interface Routine { + /** + * Sorts a strided array using Shellsort according to a provided callback function. + * + * @param N - number of indexed elements + * @param x - input array + * @param strideX - stride length + * @param clbk - callback function + * @param thisArg - execution context + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0 ]; + * + * function clbk( a, b ) { + * if ( a < b ) { + * return -1; + * } + * if ( a > b ) { + * return 1; + * } + * return 0; + * } + * + * gsortshBy( x.length, x, 1, clbk ); + * // x => [ -4.0, -2.0, 1.0, 3.0 ] + */ + = InputArray, ThisArg = unknown>( N: number, x: U, strideX: number, clbk: Callback, thisArg?: ThisParameterType> ): U; + + /** + * Sorts a strided array using Shellsort according to a provided callback function and using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param x - input array + * @param strideX - stride length + * @param offsetX - starting index + * @param clbk - callback function + * @param thisArg - execution context + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0 ]; + * + * function clbk( a, b ) { + * if ( a < b ) { + * return -1; + * } + * if ( a > b ) { + * return 1; + * } + * return 0; + * } + * + * gsortshBy.ndarray( x.length, x, 1, 0, clbk ); + * // x => [ -4.0, -2.0, 1.0, 3.0 ] + */ + ndarray = InputArray, ThisArg = unknown>( N: number, x: U, strideX: number, offsetX: number, clbk: Callback, thisArg?: ThisParameterType> ): U; +} + +/** +* Sorts a strided array using Shellsort according to a provided callback function. +* +* @param N - number of indexed elements +* @param x - input array +* @param strideX - stride length +* @param clbk - callback function +* @param thisArg - execution context +* @returns `x` +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy( x.length, x, 1, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy.ndarray( x.length, x, 1, 0, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +declare var gsortshBy: Routine; + + +// EXPORTS // + +export = gsortshBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/test.ts new file mode 100644 index 000000000000..fdc8c18c37a6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/docs/types/test.ts @@ -0,0 +1,297 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import gsortshBy = require( './index' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @param a - first value +* @param b - second value +* @returns comparison result +*/ +function clbk( a: number, b: number ): number { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; +} + + +// TESTS // + +// The function returns a collection... +{ + const x = new Float64Array( 10 ); + + gsortshBy( x.length, x, 1, clbk ); // $ExpectType Float64Array + gsortshBy( x.length, new AccessorArray( x ), 1, clbk ); // $ExpectType AccessorArray + + gsortshBy( x.length, x, 1, clbk, {} ); // $ExpectType Float64Array + gsortshBy( x.length, new AccessorArray( x ), 1, clbk, {} ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsortshBy( '10', x, 1, clbk ); // $ExpectError + gsortshBy( true, x, 1, clbk ); // $ExpectError + gsortshBy( false, x, 1, clbk ); // $ExpectError + gsortshBy( null, x, 1, clbk ); // $ExpectError + gsortshBy( undefined, x, 1, clbk ); // $ExpectError + gsortshBy( [], x, 1, clbk ); // $ExpectError + gsortshBy( {}, x, 1, clbk ); // $ExpectError + gsortshBy( ( x: number ): number => x, x, 1, clbk ); // $ExpectError + + gsortshBy( '10', x, 1, clbk, {} ); // $ExpectError + gsortshBy( true, x, 1, clbk, {} ); // $ExpectError + gsortshBy( false, x, 1, clbk, {} ); // $ExpectError + gsortshBy( null, x, 1, clbk, {} ); // $ExpectError + gsortshBy( undefined, x, 1, clbk, {} ); // $ExpectError + gsortshBy( [], x, 1, clbk, {} ); // $ExpectError + gsortshBy( {}, x, 1, clbk, {} ); // $ExpectError + gsortshBy( ( x: number ): number => x, x, 1, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a collection... +{ + gsortshBy( 10, 10, 1, clbk ); // $ExpectError + gsortshBy( 10, '10', 1, clbk ); // $ExpectError + gsortshBy( 10, true, 1, clbk ); // $ExpectError + gsortshBy( 10, false, 1, clbk ); // $ExpectError + gsortshBy( 10, null, 1, clbk ); // $ExpectError + gsortshBy( 10, undefined, 1, clbk ); // $ExpectError + gsortshBy( 10, [ '1' ], 1, clbk ); // $ExpectError + gsortshBy( 10, {}, 1, clbk ); // $ExpectError + gsortshBy( 10, ( x: number ): number => x, 1, clbk ); // $ExpectError + + gsortshBy( 10, 10, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, '10', 1, clbk, {} ); // $ExpectError + gsortshBy( 10, true, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, false, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, null, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, undefined, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, [ '1' ], 1, clbk, {} ); // $ExpectError + gsortshBy( 10, {}, 1, clbk, {} ); // $ExpectError + gsortshBy( 10, ( x: number ): number => x, 1, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsortshBy( x.length, x, '10', clbk ); // $ExpectError + gsortshBy( x.length, x, true, clbk ); // $ExpectError + gsortshBy( x.length, x, false, clbk ); // $ExpectError + gsortshBy( x.length, x, null, clbk ); // $ExpectError + gsortshBy( x.length, x, undefined, clbk ); // $ExpectError + gsortshBy( x.length, x, [], clbk ); // $ExpectError + gsortshBy( x.length, x, {}, clbk ); // $ExpectError + gsortshBy( x.length, x, ( x: number ): number => x, clbk ); // $ExpectError + + gsortshBy( x.length, x, '10', clbk, {} ); // $ExpectError + gsortshBy( x.length, x, true, clbk, {} ); // $ExpectError + gsortshBy( x.length, x, false, clbk, {} ); // $ExpectError + gsortshBy( x.length, x, null, clbk, {} ); // $ExpectError + gsortshBy( x.length, x, undefined, clbk, {} ); // $ExpectError + gsortshBy( x.length, x, [], clbk, {} ); // $ExpectError + gsortshBy( x.length, x, {}, clbk, {} ); // $ExpectError + gsortshBy( x.length, x, ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a function... +{ + const x = new Float64Array( 10 ); + + gsortshBy( x.length, x, 1, 10 ); // $ExpectError + gsortshBy( x.length, x, 1, '10' ); // $ExpectError + gsortshBy( x.length, x, 1, true ); // $ExpectError + gsortshBy( x.length, x, 1, false ); // $ExpectError + gsortshBy( x.length, x, 1, null ); // $ExpectError + gsortshBy( x.length, x, 1, undefined ); // $ExpectError + gsortshBy( x.length, x, 1, [] ); // $ExpectError + gsortshBy( x.length, x, 1, {} ); // $ExpectError + + gsortshBy( x.length, x, 1, 10, {} ); // $ExpectError + gsortshBy( x.length, x, 1, '10', {} ); // $ExpectError + gsortshBy( x.length, x, 1, true, {} ); // $ExpectError + gsortshBy( x.length, x, 1, false, {} ); // $ExpectError + gsortshBy( x.length, x, 1, null, {} ); // $ExpectError + gsortshBy( x.length, x, 1, undefined, {} ); // $ExpectError + gsortshBy( x.length, x, 1, [], {} ); // $ExpectError + gsortshBy( x.length, x, 1, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + gsortshBy(); // $ExpectError + gsortshBy( x.length ); // $ExpectError + gsortshBy( x.length, x ); // $ExpectError + gsortshBy( x.length, x, 1 ); // $ExpectError + gsortshBy( x.length, x, 1, clbk, {}, {} ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray( x.length, x, 1, 0, clbk ); // $ExpectType Float64Array + gsortshBy.ndarray( x.length, new AccessorArray( x ), 1, 0, clbk ); // $ExpectType AccessorArray + + gsortshBy.ndarray( x.length, x, 1, 0, clbk, {} ); // $ExpectType Float64Array + gsortshBy.ndarray( x.length, new AccessorArray( x ), 1, 0, clbk, {} ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray( '10', x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( true, x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( false, x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( null, x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( undefined, x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( [], x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( {}, x, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( ( x: number ): number => x, x, 1, 0, clbk ); // $ExpectError + + gsortshBy.ndarray( '10', x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( true, x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( false, x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( null, x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( undefined, x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( [], x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( {}, x, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( ( x: number ): number => x, x, 1, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a collection... +{ + gsortshBy.ndarray( 10, 10, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, '10', 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, true, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, false, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, null, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, undefined, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, [ '1' ], 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, {}, 1, 0, clbk ); // $ExpectError + gsortshBy.ndarray( 10, ( x: number ): number => x, 1, 0, clbk ); // $ExpectError + + gsortshBy.ndarray( 10, 10, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, '10', 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, true, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, false, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, null, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, undefined, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, [ '1' ], 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, {}, 1, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( 10, ( x: number ): number => x, 1, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray( x.length, x, '10', 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, true, 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, false, 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, null, 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, undefined, 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, [], 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, {}, 0, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, ( x: number ): number => x, 0, clbk ); // $ExpectError + + gsortshBy.ndarray( x.length, x, '10', 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, true, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, false, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, null, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, undefined, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, [], 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, {}, 0, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, ( x: number ): number => x, 0, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray( x.length, x, 1, '10', clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, true, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, false, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, null, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, undefined, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, [], clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, {}, clbk ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, ( x: number ): number => x, clbk ); // $ExpectError + + gsortshBy.ndarray( x.length, x, 1, '10', clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, true, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, false, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, null, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, undefined, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, [], clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, {}, clbk, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a function... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray( x.length, x, 1, 0, 10 ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, '10' ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, true ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, false ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, null ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, undefined ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, [] ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, {} ); // $ExpectError + + gsortshBy.ndarray( x.length, x, 1, 0, 10, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, '10', {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, true, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, false, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, null, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, undefined, {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, [], {} ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, {}, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + gsortshBy.ndarray(); // $ExpectError + gsortshBy.ndarray( x.length ); // $ExpectError + gsortshBy.ndarray( x.length, x ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1 ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0 ); // $ExpectError + gsortshBy.ndarray( x.length, x, 1, 0, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/examples/index.js new file mode 100644 index 000000000000..da65f3316061 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/examples/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var gsortshBy = require( './../lib' ); + +function clbk( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; +} + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float64' +}); +console.log( x ); + +gsortshBy( x.length, x, 1, clbk ); +console.log( x ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/accessors.js new file mode 100644 index 000000000000..4de2d3013927 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/accessors.js @@ -0,0 +1,117 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var GAPS = require( './gaps.json' ); + + +// VARIABLES // + +var NGAPS = GAPS.length; + + +// MAIN // + +/** +* Sorts a strided array using Shellsort according to a provided callback function. +* +* ## Notes +* +* - This implementation uses the gap sequence proposed by Ciura (2001). +* +* ## References +* +* - Shell, Donald L. 1959. "A High-Speed Sorting Procedure." _Communications of the ACM_ 2 (7). Association for Computing Machinery: 30–32. doi:[10.1145/368370.368387](https://doi.org/10.1145/368370.368387). +* - Ciura, Marcin. 2001. "Best Increments for the Average Case of Shellsort." In _Fundamentals of Computation Theory_, 106–17. Springer Berlin Heidelberg. doi:[10.1007/3-540-44669-9\_12](https://doi.org/10.1007/3-540-44669-9_12). +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @param {Callback} clbk - callback function +* @param {*} [thisArg] - execution context +* @returns {Object} `x` +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy( x.length, arraylike2object( toAccessorArray( x ) ), 1, 0, clbk ); +* +* console.log( x ); +* // => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortshBy( N, x, strideX, offsetX, clbk, thisArg ) { + var xbuf; + var xget; + var xset; + var gap; + var v; + var u; + var i; + var j; + var k; + + // Cache reference to array data: + xbuf = x.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + xset = x.accessors[ 1 ]; + + for ( i = 0; i < NGAPS; i++ ) { + gap = GAPS[ i ]; + for ( j = gap; j < N; j++ ) { + v = xget( xbuf, offsetX+(j*strideX) ); + + // Perform insertion sort on the "gapped" subarray... + for ( k = j; k >= gap; k -= gap ) { + u = xget( xbuf, offsetX+((k-gap)*strideX) ); + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + xset( xbuf, offsetX+(k*strideX), u ); + } + xset( xbuf, offsetX+(k*strideX), v ); + } + } + return x; +} + + +// EXPORTS // + +module.exports = gsortshBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/gaps.json b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/gaps.json new file mode 100644 index 000000000000..1c087b701864 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/gaps.json @@ -0,0 +1 @@ +[701,301,132,57,23,10,4,1] diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/index.js new file mode 100644 index 000000000000..ea790c6e34d5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/index.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Sort a strided array using Shellsort according to a provided callback function. +* +* @module @stdlib/blas/ext/base/gsortsh-by +* +* @example +* var gsortshBy = require( '@stdlib/blas/ext/base/gsortsh-by' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy( x.length, x, 1, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +* +* @example +* var gsortshBy = require( '@stdlib/blas/ext/base/gsortsh-by' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy.ndarray( x.length, x, 1, 0, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/main.js new file mode 100644 index 000000000000..fc7bc7c3e1f4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/main.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Sorts a strided array using Shellsort according to a provided callback function. +* +* ## Notes +* +* - This implementation uses the gap sequence proposed by Ciura (2001). +* +* ## References +* +* - Shell, Donald L. 1959. "A High-Speed Sorting Procedure." _Communications of the ACM_ 2 (7). Association for Computing Machinery: 30–32. doi:[10.1145/368370.368387](https://doi.org/10.1145/368370.368387). +* - Ciura, Marcin. 2001. "Best Increments for the Average Case of Shellsort." In _Fundamentals of Computation Theory_, 106–17. Springer Berlin Heidelberg. doi:[10.1007/3-540-44669-9\_12](https://doi.org/10.1007/3-540-44669-9_12). +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Collection} x - input array +* @param {integer} strideX - stride length +* @param {Callback} clbk - callback function +* @param {*} [thisArg] - execution context +* @returns {Collection} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy( x.length, x, 1, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortshBy( N, x, strideX, clbk ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk, arguments[ 4 ] ); +} + + +// EXPORTS // + +module.exports = gsortshBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/ndarray.js new file mode 100644 index 000000000000..b0cd2df47a7f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/lib/ndarray.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var GAPS = require( './gaps.json' ); +var accessors = require( './accessors.js' ); + + +// VARIABLES // + +var NGAPS = GAPS.length; + + +// MAIN // + +/** +* Sorts a strided array using Shellsort according to a provided callback function and using alternative indexing semantics. +* +* ## Notes +* +* - This implementation uses the gap sequence proposed by Ciura (2001). +* +* ## References +* +* - Shell, Donald L. 1959. "A High-Speed Sorting Procedure." _Communications of the ACM_ 2 (7). Association for Computing Machinery: 30–32. doi:[10.1145/368370.368387](https://doi.org/10.1145/368370.368387). +* - Ciura, Marcin. 2001. "Best Increments for the Average Case of Shellsort." In _Fundamentals of Computation Theory_, 106–17. Springer Berlin Heidelberg. doi:[10.1007/3-540-44669-9\_12](https://doi.org/10.1007/3-540-44669-9_12). +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Collection} x - input array +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @param {Callback} clbk - callback function +* @param {*} [thisArg] - execution context +* @returns {Collection} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* function clbk( a, b ) { +* if ( a < b ) { +* return -1; +* } +* if ( a > b ) { +* return 1; +* } +* return 0; +* } +* +* gsortshBy( x.length, x, 1, 0, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortshBy( N, x, strideX, offsetX, clbk ) { + var thisArg; + var gap; + var ox; + var v; + var u; + var i; + var j; + var k; + + if ( arguments.length > 5 ) { + thisArg = arguments[ 5 ]; + } + if ( N <= 0 ) { + return x; + } + ox = arraylike2object( x ); + if ( ox.accessorProtocol ) { + accessors( N, ox, strideX, offsetX, clbk, thisArg ); + return x; + } + for ( i = 0; i < NGAPS; i++ ) { + gap = GAPS[ i ]; + for ( j = gap; j < N; j++ ) { + v = x[ offsetX+(j*strideX) ]; + + // Perform insertion sort on the "gapped" subarray... + for ( k = j; k >= gap; k -= gap ) { + u = x[ offsetX+((k-gap)*strideX) ]; + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + x[ offsetX+(k*strideX) ] = u; + } + x[ offsetX+(k*strideX) ] = v; + } + } + return x; +} + + +// EXPORTS // + +module.exports = gsortshBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/package.json b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/package.json new file mode 100644 index 000000000000..1e29eb8af6cb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/blas/ext/base/gsortsh-by", + "version": "0.0.0", + "description": "Sort a strided array using Shellsort according to a provided callback function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "sort", + "order", + "arrange", + "permute", + "insertion", + "shell", + "shellsort", + "callback", + "comparator", + "strided", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.js new file mode 100644 index 000000000000..640f316968d6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var gsortshBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortshBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof gsortshBy.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.main.js new file mode 100644 index 000000000000..72a0f2439363 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.main.js @@ -0,0 +1,417 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var gsortshBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortshBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( gsortshBy.length, 4, 'has expected arity' ); + t.end(); +}); + +tape( 'the function sorts a strided array', function test( t ) { + var expected; + var x; + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ 50.0, 10.0, 10.0, 8.0, 3.0, -1.0, -4.0 ]; + + gsortshBy( 7, x, 1, clbk1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ -4.0, -1.0, 3.0, 8.0, 10.0, 10.0, 50.0 ]; + + gsortshBy( 7, x, 1, clbk2 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk1( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } + + function clbk2( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function sorts a strided array (accessors)', function test( t ) { + var expected; + var x; + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ 50.0, 10.0, 10.0, 8.0, 3.0, -1.0, -4.0 ]; + + gsortshBy( 7, toAccessorArray( x ), 1, clbk1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ -4.0, -1.0, 3.0, 8.0, 10.0, 10.0, 50.0 ]; + + gsortshBy( 7, toAccessorArray( x ), 1, clbk2 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk1( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } + + function clbk2( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function returns a reference to the input array', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = gsortshBy( x.length, x, 1, clbk ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function returns a reference to the input array (accessors)', function test( t ) { + var out; + var x; + + x = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + out = gsortshBy( x.length, x, 1, clbk ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0 ]; + expected = [ 3.0, -4.0, 1.0 ]; + + gsortshBy( 0, x, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsortshBy( -4, x, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + -5.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 2 + ]; + + gsortshBy( 3, x, 2, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + -5.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 2 + ]; + + gsortshBy( 3, toAccessorArray( x ), 2, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 6.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 0 + ]; + + gsortshBy( 3, x, -2, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a negative stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 6.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 0 + ]; + + gsortshBy( 3, toAccessorArray( x ), -2, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports view offsets', function test( t ) { + var expected; + var x0; + var x1; + + x0 = new Float64Array([ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]); + expected = new Float64Array([ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + gsortshBy( 3, x1, 2, clbk ); + t.deepEqual( x0, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var x; + + ctx = { + 'count': 0 + }; + x = [ 10.0, -1.0, 3.0, 50.0 ]; + expected = [ 50.0, 10.0, 3.0, -1.0 ]; + + gsortshBy( 4, x, 1, clbk, ctx ); + t.deepEqual( x, expected, 'returns expected value' ); + t.strictEqual( ctx.count > 0, true, 'context was used' ); + t.end(); + + function clbk( a, b ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var expected; + var ctx; + var x; + + ctx = { + 'count': 0 + }; + x = [ 10.0, -1.0, 3.0, 50.0 ]; + expected = [ 50.0, 10.0, 3.0, -1.0 ]; + + gsortshBy( 4, toAccessorArray( x ), 1, clbk, ctx ); + t.deepEqual( x, expected, 'returns expected value' ); + t.strictEqual( ctx.count > 0, true, 'context was used' ); + t.end(); + + function clbk( a, b ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.ndarray.js new file mode 100644 index 000000000000..76be797acdaa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortsh-by/test/test.ndarray.js @@ -0,0 +1,493 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gsortshBy = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortshBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 5', function test( t ) { + t.strictEqual( gsortshBy.length, 5, 'has expected arity' ); + t.end(); +}); + +tape( 'the function sorts a strided array', function test( t ) { + var expected; + var x; + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ 50.0, 10.0, 10.0, 8.0, 3.0, -1.0, -4.0 ]; + + gsortshBy( 7, x, 1, 0, clbk1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ -4.0, -1.0, 3.0, 8.0, 10.0, 10.0, 50.0 ]; + + gsortshBy( 7, x, 1, 0, clbk2 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk1( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } + + function clbk2( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function sorts a strided array (accessors)', function test( t ) { + var expected; + var x; + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ 50.0, 10.0, 10.0, 8.0, 3.0, -1.0, -4.0 ]; + + gsortshBy( 7, toAccessorArray( x ), 1, 0, clbk1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 10.0, -1.0, 3.0, 50.0, 10.0, -4.0, 8.0 ]; + expected = [ -4.0, -1.0, 3.0, 8.0, 10.0, 10.0, 50.0 ]; + + gsortshBy( 7, toAccessorArray( x ), 1, 0, clbk2 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk1( a, b ) { + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } + + function clbk2( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function returns a reference to the input array', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = gsortshBy( x.length, x, 1, 0, clbk ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function returns a reference to the input array (accessors)', function test( t ) { + var out; + var x; + + x = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + out = gsortshBy( x.length, x, 1, 0, clbk ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0 ]; + expected = [ 3.0, -4.0, 1.0 ]; + + gsortshBy( 0, x, 1, 0, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsortshBy( -4, x, 1, 0, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying an offset', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]; + expected = [ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]; + + gsortshBy( 3, x, 2, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying an offset', function test( t ) { + var expected; + var x; + + x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; + expected = [ 1.0, -4.0, -2.0, 3.0, 5.0 ]; + + gsortshBy( 4, x, 1, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying an offset (accessors)', function test( t ) { + var expected; + var x; + + x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; + expected = [ 1.0, -4.0, -2.0, 3.0, 5.0 ]; + + gsortshBy( 4, toAccessorArray( x ), 1, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying an offset (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]; + expected = [ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]; + + gsortshBy( 3, toAccessorArray( x ), 2, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + -5.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 2 + ]; + + gsortshBy( 3, x, 2, 0, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + -5.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 2 + ]; + + gsortshBy( 3, toAccessorArray( x ), 2, 0, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 6.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 0 + ]; + + gsortshBy( 3, x, -2, 4, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports specifying a negative stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 6.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 0 + ]; + + gsortshBy( 3, toAccessorArray( x ), -2, 4, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); + + function clbk( a, b ) { + if ( a < b ) { + return -1; + } + if ( a > b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var x; + + ctx = { + 'count': 0 + }; + x = [ 10.0, -1.0, 3.0, 50.0 ]; + expected = [ 50.0, 10.0, 3.0, -1.0 ]; + + gsortshBy( 4, x, 1, 0, clbk, ctx ); + t.deepEqual( x, expected, 'returns expected value' ); + t.strictEqual( ctx.count > 0, true, 'context was used' ); + t.end(); + + function clbk( a, b ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } +}); + +tape( 'the function supports providing a callback execution context (accessors)', function test( t ) { + var expected; + var ctx; + var x; + + ctx = { + 'count': 0 + }; + x = [ 10.0, -1.0, 3.0, 50.0 ]; + expected = [ 50.0, 10.0, 3.0, -1.0 ]; + + gsortshBy( 4, toAccessorArray( x ), 1, 0, clbk, ctx ); + t.deepEqual( x, expected, 'returns expected value' ); + t.strictEqual( ctx.count > 0, true, 'context was used' ); + t.end(); + + function clbk( a, b ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( a > b ) { + return -1; + } + if ( a < b ) { + return 1; + } + return 0; + } +});