From 938f5da97ec20fe63d263160a6166fd5b679280f Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 18 Aug 2026 14:28:58 +0500 Subject: [PATCH] feat: add blas/ext/base/gsortins-by --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../blas/ext/base/gsortins-by/README.md | 250 +++++++++ .../benchmark.mostly_sorted_few_uniques.js | 151 ++++++ ...hmark.mostly_sorted_few_uniques.ndarray.js | 151 ++++++ .../benchmark.mostly_sorted_random.js | 142 +++++ .../benchmark.mostly_sorted_random.ndarray.js | 142 +++++ ...benchmark.rev_mostly_sorted_few_uniques.js | 151 ++++++ ...k.rev_mostly_sorted_few_uniques.ndarray.js | 151 ++++++ .../benchmark.rev_mostly_sorted_random.js | 142 +++++ ...chmark.rev_mostly_sorted_random.ndarray.js | 142 +++++ .../benchmark.rev_sorted_few_uniques.js | 152 ++++++ ...enchmark.rev_sorted_few_uniques.ndarray.js | 152 ++++++ .../benchmark/benchmark.rev_sorted_random.js | 142 +++++ .../benchmark.rev_sorted_random.ndarray.js | 142 +++++ .../benchmark/benchmark.sorted_few_uniques.js | 147 ++++++ .../benchmark.sorted_few_uniques.ndarray.js | 147 ++++++ .../benchmark/benchmark.sorted_random.js | 136 +++++ .../benchmark.sorted_random.ndarray.js | 136 +++++ .../benchmark.unsorted_few_uniques.js | 145 ++++++ .../benchmark.unsorted_few_uniques.ndarray.js | 145 ++++++ .../benchmark/benchmark.unsorted_random.js | 142 +++++ .../benchmark.unsorted_random.ndarray.js | 142 +++++ .../blas/ext/base/gsortins-by/docs/repl.txt | 170 ++++++ .../base/gsortins-by/docs/types/index.d.ts | 183 +++++++ .../ext/base/gsortins-by/docs/types/test.ts | 297 +++++++++++ .../ext/base/gsortins-by/examples/index.js | 40 ++ .../ext/base/gsortins-by/lib/accessors.js | 122 +++++ .../blas/ext/base/gsortins-by/lib/index.js | 77 +++ .../blas/ext/base/gsortins-by/lib/main.js | 62 +++ .../blas/ext/base/gsortins-by/lib/ndarray.js | 123 +++++ .../blas/ext/base/gsortins-by/package.json | 70 +++ .../blas/ext/base/gsortins-by/test/test.js | 38 ++ .../ext/base/gsortins-by/test/test.main.js | 417 +++++++++++++++ .../ext/base/gsortins-by/test/test.ndarray.js | 493 ++++++++++++++++++ 33 files changed, 5242 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_few_uniques.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_few_uniques.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/accessors.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/README.md b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/README.md new file mode 100644 index 000000000000..90eb678f58ca --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/README.md @@ -0,0 +1,250 @@ + + +# gsortinsBy + +> Sort a strided array using insertion sort according to a provided callback function. + +
+ +## Usage + +```javascript +var gsortinsBy = require( '@stdlib/blas/ext/base/gsortins-by' ); +``` + +#### gsortinsBy( N, x, strideX, clbk\[, thisArg] ) + +Sorts a strided array using insertion sort 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 ]; + +gsortinsBy( 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 ]; + +gsortinsBy( 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 ]; + +gsortinsBy( 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 + +gsortinsBy( 2, x1, 2, clbk ); +// x0 => [ 1.0, -4.0, 3.0, -2.0 ] +``` + +#### gsortinsBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) + +Sorts a strided array using insertion sort 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 ]; + +gsortinsBy.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 ]; + +gsortinsBy.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^2)`. +- The algorithm is efficient for **small** strided arrays (typically `N <= 20`) and is particularly efficient for sorting strided arrays which are already substantially sorted. +- The algorithm is **stable**, meaning that the algorithm does **not** 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 gsortinsBy = require( '@stdlib/blas/ext/base/gsortins-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 ); + +gsortinsBy( x.length, x, 1, clbk ); +console.log( x ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.js new file mode 100644 index 000000000000..9f464f8d8342 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.js @@ -0,0 +1,151 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..03d5167bd659 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_few_uniques.ndarray.js @@ -0,0 +1,151 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.js new file mode 100644 index 000000000000..5845aea3a5e6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.ndarray.js new file mode 100644 index 000000000000..80ce2b98ef86 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.mostly_sorted_random.ndarray.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js new file mode 100644 index 000000000000..1eb77bdd507e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.js @@ -0,0 +1,151 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..b0cb4d239003 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_few_uniques.ndarray.js @@ -0,0 +1,151 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.js new file mode 100644 index 000000000000..f5692009aa75 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js new file mode 100644 index 000000000000..b46cd714fa7a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_mostly_sorted_random.ndarray.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.js new file mode 100644 index 000000000000..6acea9c7fdd7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.js @@ -0,0 +1,152 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..075516589502 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_few_uniques.ndarray.js @@ -0,0 +1,152 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.js new file mode 100644 index 000000000000..adb116878666 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.ndarray.js new file mode 100644 index 000000000000..6adc5e1c7856 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.rev_sorted_random.ndarray.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_few_uniques.js new file mode 100644 index 000000000000..738e7e81ec36 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 = gsortinsBy( 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/gsortins-by/benchmark/benchmark.sorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..20965d418d69 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 = gsortinsBy( 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/gsortins-by/benchmark/benchmark.sorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_random.js new file mode 100644 index 000000000000..a4ef94a31139 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 = gsortinsBy( 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/gsortins-by/benchmark/benchmark.sorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.sorted_random.ndarray.js new file mode 100644 index 000000000000..7a0c75ef7b17 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 = gsortinsBy( 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/gsortins-by/benchmark/benchmark.unsorted_few_uniques.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.js new file mode 100644 index 000000000000..031ca3e921fe --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.js @@ -0,0 +1,145 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js new file mode 100644 index 000000000000..ec1cda599592 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_few_uniques.ndarray.js @@ -0,0 +1,145 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.js new file mode 100644 index 000000000000..f1d993f9e1e0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.ndarray.js new file mode 100644 index 000000000000..9f4b7fc7fcc7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/benchmark/benchmark.unsorted_random.ndarray.js @@ -0,0 +1,142 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsortinsBy = 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 = gsortinsBy( 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; + + // NOTE: parameters tuned according to worst case time complexity `O(n^2)` + 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( sqrt( iter ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/repl.txt new file mode 100644 index 000000000000..173561e19289 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/repl.txt @@ -0,0 +1,170 @@ + +{{alias}}( N, x, strideX, clbk[, thisArg] ) + Sorts a strided array using insertion sort 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^2). + + The algorithm is efficient for *small* strided arrays (typically N <= 20) + and is particularly efficient for sorting strided arrays which are already + substantially sorted. + + The algorithm is *stable*, meaning that the algorithm does *not* 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 insertion sort 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/gsortins-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/types/index.d.ts new file mode 100644 index 000000000000..55150998009f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 `gsortinsBy`. +*/ +interface Routine { + /** + * Sorts a strided array using insertion sort 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; + * } + * + * gsortinsBy( 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 insertion sort 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; + * } + * + * gsortinsBy.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 insertion sort 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; +* } +* +* gsortinsBy( 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; +* } +* +* gsortinsBy.ndarray( x.length, x, 1, 0, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +declare var gsortinsBy: Routine; + + +// EXPORTS // + +export = gsortinsBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/docs/types/test.ts new file mode 100644 index 000000000000..bf5b1e6346f6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 ); + + gsortinsBy( x.length, x, 1, clbk ); // $ExpectType Float64Array + gsortinsBy( x.length, new AccessorArray( x ), 1, clbk ); // $ExpectType AccessorArray + + gsortinsBy( x.length, x, 1, clbk, {} ); // $ExpectType Float64Array + gsortinsBy( 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 ); + + gsortinsBy( '10', x, 1, clbk ); // $ExpectError + gsortinsBy( true, x, 1, clbk ); // $ExpectError + gsortinsBy( false, x, 1, clbk ); // $ExpectError + gsortinsBy( null, x, 1, clbk ); // $ExpectError + gsortinsBy( undefined, x, 1, clbk ); // $ExpectError + gsortinsBy( [], x, 1, clbk ); // $ExpectError + gsortinsBy( {}, x, 1, clbk ); // $ExpectError + gsortinsBy( ( x: number ): number => x, x, 1, clbk ); // $ExpectError + + gsortinsBy( '10', x, 1, clbk, {} ); // $ExpectError + gsortinsBy( true, x, 1, clbk, {} ); // $ExpectError + gsortinsBy( false, x, 1, clbk, {} ); // $ExpectError + gsortinsBy( null, x, 1, clbk, {} ); // $ExpectError + gsortinsBy( undefined, x, 1, clbk, {} ); // $ExpectError + gsortinsBy( [], x, 1, clbk, {} ); // $ExpectError + gsortinsBy( {}, x, 1, clbk, {} ); // $ExpectError + gsortinsBy( ( 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... +{ + gsortinsBy( 10, 10, 1, clbk ); // $ExpectError + gsortinsBy( 10, '10', 1, clbk ); // $ExpectError + gsortinsBy( 10, true, 1, clbk ); // $ExpectError + gsortinsBy( 10, false, 1, clbk ); // $ExpectError + gsortinsBy( 10, null, 1, clbk ); // $ExpectError + gsortinsBy( 10, undefined, 1, clbk ); // $ExpectError + gsortinsBy( 10, [ '1' ], 1, clbk ); // $ExpectError + gsortinsBy( 10, {}, 1, clbk ); // $ExpectError + gsortinsBy( 10, ( x: number ): number => x, 1, clbk ); // $ExpectError + + gsortinsBy( 10, 10, 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, '10', 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, true, 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, false, 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, null, 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, undefined, 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, [ '1' ], 1, clbk, {} ); // $ExpectError + gsortinsBy( 10, {}, 1, clbk, {} ); // $ExpectError + gsortinsBy( 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 ); + + gsortinsBy( x.length, x, '10', clbk ); // $ExpectError + gsortinsBy( x.length, x, true, clbk ); // $ExpectError + gsortinsBy( x.length, x, false, clbk ); // $ExpectError + gsortinsBy( x.length, x, null, clbk ); // $ExpectError + gsortinsBy( x.length, x, undefined, clbk ); // $ExpectError + gsortinsBy( x.length, x, [], clbk ); // $ExpectError + gsortinsBy( x.length, x, {}, clbk ); // $ExpectError + gsortinsBy( x.length, x, ( x: number ): number => x, clbk ); // $ExpectError + + gsortinsBy( x.length, x, '10', clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, true, clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, false, clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, null, clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, undefined, clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, [], clbk, {} ); // $ExpectError + gsortinsBy( x.length, x, {}, clbk, {} ); // $ExpectError + gsortinsBy( 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 ); + + gsortinsBy( x.length, x, 1, 10 ); // $ExpectError + gsortinsBy( x.length, x, 1, '10' ); // $ExpectError + gsortinsBy( x.length, x, 1, true ); // $ExpectError + gsortinsBy( x.length, x, 1, false ); // $ExpectError + gsortinsBy( x.length, x, 1, null ); // $ExpectError + gsortinsBy( x.length, x, 1, undefined ); // $ExpectError + gsortinsBy( x.length, x, 1, [] ); // $ExpectError + gsortinsBy( x.length, x, 1, {} ); // $ExpectError + + gsortinsBy( x.length, x, 1, 10, {} ); // $ExpectError + gsortinsBy( x.length, x, 1, '10', {} ); // $ExpectError + gsortinsBy( x.length, x, 1, true, {} ); // $ExpectError + gsortinsBy( x.length, x, 1, false, {} ); // $ExpectError + gsortinsBy( x.length, x, 1, null, {} ); // $ExpectError + gsortinsBy( x.length, x, 1, undefined, {} ); // $ExpectError + gsortinsBy( x.length, x, 1, [], {} ); // $ExpectError + gsortinsBy( 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 ); + + gsortinsBy(); // $ExpectError + gsortinsBy( x.length ); // $ExpectError + gsortinsBy( x.length, x ); // $ExpectError + gsortinsBy( x.length, x, 1 ); // $ExpectError + gsortinsBy( x.length, x, 1, clbk, {}, {} ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const x = new Float64Array( 10 ); + + gsortinsBy.ndarray( x.length, x, 1, 0, clbk ); // $ExpectType Float64Array + gsortinsBy.ndarray( x.length, new AccessorArray( x ), 1, 0, clbk ); // $ExpectType AccessorArray + + gsortinsBy.ndarray( x.length, x, 1, 0, clbk, {} ); // $ExpectType Float64Array + gsortinsBy.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 ); + + gsortinsBy.ndarray( '10', x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( true, x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( false, x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( null, x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( undefined, x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( [], x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( {}, x, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( ( x: number ): number => x, x, 1, 0, clbk ); // $ExpectError + + gsortinsBy.ndarray( '10', x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( true, x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( false, x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( null, x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( undefined, x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( [], x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( {}, x, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.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... +{ + gsortinsBy.ndarray( 10, 10, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, '10', 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, true, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, false, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, null, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, undefined, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, [ '1' ], 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, {}, 1, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( 10, ( x: number ): number => x, 1, 0, clbk ); // $ExpectError + + gsortinsBy.ndarray( 10, 10, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, '10', 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, true, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, false, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, null, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, undefined, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, [ '1' ], 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( 10, {}, 1, 0, clbk, {} ); // $ExpectError + gsortinsBy.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 ); + + gsortinsBy.ndarray( x.length, x, '10', 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, true, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, false, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, null, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, undefined, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, [], 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, {}, 0, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, ( x: number ): number => x, 0, clbk ); // $ExpectError + + gsortinsBy.ndarray( x.length, x, '10', 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, true, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, false, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, null, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, undefined, 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, [], 0, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, {}, 0, clbk, {} ); // $ExpectError + gsortinsBy.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 ); + + gsortinsBy.ndarray( x.length, x, 1, '10', clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, true, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, false, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, null, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, undefined, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, [], clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, {}, clbk ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, ( x: number ): number => x, clbk ); // $ExpectError + + gsortinsBy.ndarray( x.length, x, 1, '10', clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, true, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, false, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, null, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, undefined, clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, [], clbk, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, {}, clbk, {} ); // $ExpectError + gsortinsBy.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 ); + + gsortinsBy.ndarray( x.length, x, 1, 0, 10 ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, '10' ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, true ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, false ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, null ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, undefined ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, [] ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, {} ); // $ExpectError + + gsortinsBy.ndarray( x.length, x, 1, 0, 10, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, '10', {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, true, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, false, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, null, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, undefined, {} ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, [], {} ); // $ExpectError + gsortinsBy.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 ); + + gsortinsBy.ndarray(); // $ExpectError + gsortinsBy.ndarray( x.length ); // $ExpectError + gsortinsBy.ndarray( x.length, x ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1 ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0 ); // $ExpectError + gsortinsBy.ndarray( x.length, x, 1, 0, clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/examples/index.js new file mode 100644 index 000000000000..842215349b1b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = 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 ); + +gsortinsBy( x.length, x, 1, clbk ); +console.log( x ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/accessors.js new file mode 100644 index 000000000000..7654750f2708 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/accessors.js @@ -0,0 +1,122 @@ +/** +* @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'; + +// MAIN // + +/** +* Sorts a strided array using insertion sort according to a provided callback function. +* +* @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; +* } +* +* gsortinsBy( x.length, arraylike2object( toAccessorArray( x ) ), 1, 0, clbk ); +* +* console.log( x ); +* // => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortinsBy( N, x, strideX, offsetX, clbk, thisArg ) { + var xbuf; + var xget; + var xset; + var ix; + var jx; + var fx; + var v; + var u; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + xset = x.accessors[ 1 ]; + + fx = offsetX; // first index + ix = fx + strideX; + + if ( strideX < 0 ) { + // Traverse the strided array from right-to-left... + for ( i = 1; i < N; i++ ) { + v = xget( xbuf, ix ); + jx = ix - strideX; + + // Shift all larger values to the right of the current element to the left... + while ( jx <= fx ) { + u = xget( xbuf, jx ); + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + xset( xbuf, jx+strideX, u ); + jx -= strideX; + } + xset( xbuf, jx+strideX, v ); + ix += strideX; + } + return x; + } + // Traverse the strided array from left-to-right... + for ( i = 1; i < N; i++ ) { + v = xget( xbuf, ix ); + jx = ix - strideX; + + // Shift all larger values to the left of the current element to the right... + while ( jx >= fx ) { + u = xget( xbuf, jx ); + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + xset( xbuf, jx+strideX, u ); + jx -= strideX; + } + xset( xbuf, jx+strideX, v ); + ix += strideX; + } + return x; +} + + +// EXPORTS // + +module.exports = gsortinsBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/index.js new file mode 100644 index 000000000000..725493150ef9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 insertion sort according to a provided callback function. +* +* @module @stdlib/blas/ext/base/gsortins-by +* +* @example +* var gsortinsBy = require( '@stdlib/blas/ext/base/gsortins-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; +* } +* +* gsortinsBy( x.length, x, 1, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +* +* @example +* var gsortinsBy = require( '@stdlib/blas/ext/base/gsortins-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; +* } +* +* gsortinsBy.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/gsortins-by/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/main.js new file mode 100644 index 000000000000..4c09c94ba04a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/main.js @@ -0,0 +1,62 @@ +/** +* @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 insertion sort according to a provided callback function. +* +* @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; +* } +* +* gsortinsBy( x.length, x, 1, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortinsBy( N, x, strideX, clbk ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ), clbk, arguments[ 4 ] ); +} + + +// EXPORTS // + +module.exports = gsortinsBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/ndarray.js new file mode 100644 index 000000000000..ac30a98668e6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/lib/ndarray.js @@ -0,0 +1,123 @@ +/** +* @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 accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Sorts a strided array using insertion sort according to a provided callback function and using alternative indexing semantics. +* +* @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; +* } +* +* gsortinsBy( x.length, x, 1, 0, clbk ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsortinsBy( N, x, strideX, offsetX, clbk ) { + var thisArg; + var ix; + var jx; + var fx; + var ox; + var v; + var u; + var i; + + 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; + } + fx = offsetX; // first index + ix = fx + strideX; + + if ( strideX < 0 ) { + // Traverse the strided array from right-to-left... + for ( i = 1; i < N; i++ ) { + v = x[ ix ]; + jx = ix - strideX; + + // Shift all larger values to the right of the current element to the left... + while ( jx <= fx ) { + u = x[ jx ]; + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + x[ jx+strideX ] = u; + jx -= strideX; + } + x[ jx+strideX ] = v; + ix += strideX; + } + return x; + } + // Traverse the strided array from left-to-right... + for ( i = 1; i < N; i++ ) { + v = x[ ix ]; + jx = ix - strideX; + + // Shift all larger values to the left of the current element to the right... + while ( jx >= fx ) { + u = x[ jx ]; + if ( clbk.call( thisArg, u, v, x ) <= 0 ) { + break; + } + x[ jx+strideX ] = u; + jx -= strideX; + } + x[ jx+strideX ] = v; + ix += strideX; + } + return x; +} + + +// EXPORTS // + +module.exports = gsortinsBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/package.json b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/package.json new file mode 100644 index 000000000000..5a8b01466ada --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/blas/ext/base/gsortins-by", + "version": "0.0.0", + "description": "Sort a strided array using insertion sort 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", + "callback", + "comparator", + "strided", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.js new file mode 100644 index 000000000000..338fa8abcd12 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortinsBy, '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 gsortinsBy.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.main.js new file mode 100644 index 000000000000..9c7a300ccbd9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortinsBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( gsortinsBy.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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 = gsortinsBy( 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 = gsortinsBy( 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 ]; + + gsortinsBy( 0, x, 1, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsortinsBy( -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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 ); + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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/gsortins-by/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsortins-by/test/test.ndarray.js new file mode 100644 index 000000000000..475dcda06c18 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsortins-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 gsortinsBy = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsortinsBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 5', function test( t ) { + t.strictEqual( gsortinsBy.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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 = gsortinsBy( 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 = gsortinsBy( 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 ]; + + gsortinsBy( 0, x, 1, 0, clbk ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsortinsBy( -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 + ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 + ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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 ]; + + gsortinsBy( 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; + } +});