From 00c43c08bbe1afa070a811fac165316b1f1ea87c Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 12 Jan 2026 23:54:25 +0500 Subject: [PATCH 1/5] feat: add blas/ext/base/gsort --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - 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 --- --- .../@stdlib/blas/ext/base/gsort/README.md | 171 +++ .../benchmark/benchmark.unsorted_random.js | 115 ++ .../benchmark.unsorted_random.ndarray.js | 115 ++ .../@stdlib/blas/ext/base/gsort/docs/repl.txt | 111 ++ .../blas/ext/base/gsort/docs/types/index.d.ts | 96 ++ .../blas/ext/base/gsort/docs/types/test.ts | 190 +++ .../blas/ext/base/gsort/examples/index.js | 30 + .../@stdlib/blas/ext/base/gsort/lib/index.js | 57 + .../@stdlib/blas/ext/base/gsort/lib/main.js | 51 + .../blas/ext/base/gsort/lib/ndarray.js | 51 + .../@stdlib/blas/ext/base/gsort/package.json | 67 + .../ext/base/gsort/test/fixtures/ascending.js | 75 ++ .../ext/base/gsort/test/fixtures/num2str.js | 45 + .../@stdlib/blas/ext/base/gsort/test/test.js | 38 + .../blas/ext/base/gsort/test/test.main.js | 995 +++++++++++++++ .../blas/ext/base/gsort/test/test.ndarray.js | 1092 +++++++++++++++++ 16 files changed, 3299 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/ascending.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/num2str.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md new file mode 100644 index 000000000000..0806fd970e9f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md @@ -0,0 +1,171 @@ + + +# gsort + +> Sort a strided array. + +
+ +## Usage + +```javascript +var gsort = require( '@stdlib/blas/ext/base/gsort' ); +``` + +#### gsort( N, order, x, strideX ) + +Sorts a strided array. + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsort( x.length, 1.0, x, 1 ); +// x => [ -4.0, -2.0, 1.0, 3.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **order**: sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length. + +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element: + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsort( 2, -1.0, x, 2 ); +// 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' ); + +// Initial array... +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +// Create an offset view... +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +// Sort every other element... +gsort( 2, -1.0, x1, 2 ); +// x0 => [ 1.0, 4.0, 3.0, 2.0 ] +``` + +#### gsort.ndarray( N, order, x, strideX, offsetX ) + +Sorts a strided array using alternative indexing semantics. + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0 ]; + +gsort.ndarray( x.length, 1.0, x, 1, 0 ); +// 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 +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +gsort.ndarray( 3, 1.0, x, 1, x.length-3 ); +// x => [ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged. +- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. +- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. +- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. +- 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 gsort = require( '@stdlib/blas/ext/base/gsort' ); + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +console.log( x ); + +gsort( x.length, -1.0, x, -1 ); +console.log( x ); +``` + +
+ + + +* * * + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.js new file mode 100644 index 000000000000..b0cd680e6657 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.js @@ -0,0 +1,115 @@ +/** +* @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/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsort = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* 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; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = uniform( len, -100.0, 100.0, { + 'dtype': 'generic' + }); + 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 = gsort( len, 1, xc[ i ], 1 ); + 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 // + +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,random:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js new file mode 100644 index 000000000000..011786fb1616 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gsort = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* 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; + + x = []; + for ( i = 0; i < iter; i++ ) { + tmp = uniform( len, -100.0, 100.0, { + 'dtype': 'generic' + }); + 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 = gsort( len, 1, xc[ i ], 1, 0 ); + 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 // + +function main() { + var opts; + var iter; + var len; + var min; + var max; + var f; + var i; + + iter = 1e6; + min = 1; // 10^min + max = 4; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( iter, len ); + opts = { + 'iterations': iter + }; + bench( format( '%s::unsorted,random:ndarray:len=%d', pkg, len ), opts, f ); + iter = floor( pow( iter, 3.0/4.0 ) ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt new file mode 100644 index 000000000000..fb15830fe9f3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt @@ -0,0 +1,111 @@ + +{{alias}}( N, order, x, strideX ) + Sorts a strided array. + + 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` or `order == 0`, the function returns `x` unchanged. + + The algorithm distinguishes between `-0` and `+0`. When sorted in increasing + order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is + sorted after `+0`. + + The algorithm sorts `NaN` values to the end. When sorted in increasing + order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` + values are sorted first. + + The algorithm has space complexity O(1) and time complexity O(N log2 N). + + The input strided array is sorted *in-place* (i.e., the input strided array + is *mutated*). + + Parameters + ---------- + N: integer + Number of indexed elements. + + order: number + Sort order. If `order < 0`, the function sorts `x` in decreasing order. + If `order > 0`, the function sorts `x` in increasing order. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + Returns + ------- + x: Array|TypedArray + Input array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}( x.length, 1, x, 1 ) + [ -4.0, -2.0, 1.0, 3.0 ] + + // Using `N` and stride parameters: + > x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}( 2, -1, x, 2 ) + [ 3.0, -2.0, 1.0, -4.0 ] + + // Using view offsets: + > 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, 1, x1, 2 ) + [ -4.0, 3.0, -2.0 ] + > x0 + [ 1.0, -4.0, 3.0, -2.0 ] + + +{{alias}}.ndarray( N, order, x, strideX, offsetX ) + Sorts a strided array 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. + + Parameters + ---------- + N: integer + Number of indexed elements. + + order: number + Sort order. If `order < 0`, the function sorts `x` in decreasing order. + If `order > 0`, the function sorts `x` in increasing order. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + offsetX: integer + Starting index. + + Returns + ------- + x: Array|TypedArray + Input array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}.ndarray( x.length, 1, x, 1, 0 ) + [ -4.0, -2.0, 1.0, 3.0 ] + + // Using an index offset: + > x = [ 1.0, -2.0, 3.0, -4.0 ]; + > {{alias}}.ndarray( 2, 1, x, 2, 1 ) + [ 1.0, -4.0, 3.0, -2.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/index.d.ts new file mode 100644 index 000000000000..aaddc82cadae --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/index.d.ts @@ -0,0 +1,96 @@ +/* +* @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 { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `gsort`. +*/ +interface Routine { + /** + * Sorts a strided array. + * + * @param N - number of indexed elements + * @param order - sort order + * @param x - input array + * @param strideX - stride length + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0 ]; + * + * gsort( x.length, 1, x, 1 ); + * // x => [ -4.0, -2.0, 1.0, 3.0 ] + */ + ( N: number, order: number, x: T, stride: number ): T; + + /** + * Sorts a strided array using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param order - sort order + * @param x - input array + * @param strideX - stride length + * @param offsetX - starting index + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0 ]; + * + * gsort.ndarray( x.length, 1, x, 1, 0 ); + * // x => [ -4.0, -2.0, 1.0, 3.0 ] + */ + ndarray( N: number, order: number, x: T, stride: number, offset: number ): T; +} + +/** +* Sorts a strided array. +* +* @param N - number of indexed elements +* @param order - sort order +* @param x - input array +* @param strideX - stride length +* @returns `x` +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort( x.length, 1, x, 1 ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort.ndarray( x.length, 1, x, 1, 0 ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +declare var gsort: Routine; + + +// EXPORTS // + +export = gsort; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/test.ts new file mode 100644 index 000000000000..0e27a3da9a70 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/types/test.ts @@ -0,0 +1,190 @@ +/* +* @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. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import gsort = require( './index' ); + + +// TESTS // + +// The function returns a numeric array... +{ + const x = new Float64Array( 10 ); + + gsort( x.length, 1, x, 1 ); // $ExpectType Float64Array + gsort( x.length, 1, new AccessorArray( x ), 1 ); // $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 ); + + gsort( '10', 1, x, 1 ); // $ExpectError + gsort( true, 1, x, 1 ); // $ExpectError + gsort( false, 1, x, 1 ); // $ExpectError + gsort( null, 1, x, 1 ); // $ExpectError + gsort( undefined, 1, x, 1 ); // $ExpectError + gsort( [], 1, x, 1 ); // $ExpectError + gsort( {}, 1, x, 1 ); // $ExpectError + gsort( ( x: number ): number => x, 1, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsort( x.length, '10', x, 1 ); // $ExpectError + gsort( x.length, true, x, 1 ); // $ExpectError + gsort( x.length, false, x, 1 ); // $ExpectError + gsort( x.length, null, x, 1 ); // $ExpectError + gsort( x.length, undefined, x, 1 ); // $ExpectError + gsort( x.length, [], x, 1 ); // $ExpectError + gsort( x.length, {}, x, 1 ); // $ExpectError + gsort( x.length, ( x: number ): number => x, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + gsort( x.length, 1, 10, 1 ); // $ExpectError + gsort( x.length, 1, '10', 1 ); // $ExpectError + gsort( x.length, 1, true, 1 ); // $ExpectError + gsort( x.length, 1, false, 1 ); // $ExpectError + gsort( x.length, 1, null, 1 ); // $ExpectError + gsort( x.length, 1, undefined, 1 ); // $ExpectError + gsort( x.length, 1, [ '1' ], 1 ); // $ExpectError + gsort( x.length, 1, {}, 1 ); // $ExpectError + gsort( x.length, 1, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsort( x.length, 1, x, '10' ); // $ExpectError + gsort( x.length, 1, x, true ); // $ExpectError + gsort( x.length, 1, x, false ); // $ExpectError + gsort( x.length, 1, x, null ); // $ExpectError + gsort( x.length, 1, x, undefined ); // $ExpectError + gsort( x.length, 1, x, [] ); // $ExpectError + gsort( x.length, 1, x, {} ); // $ExpectError + gsort( x.length, 1, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + gsort(); // $ExpectError + gsort( x.length ); // $ExpectError + gsort( x.length, 1 ); // $ExpectError + gsort( x.length, 1, x ); // $ExpectError + gsort( x.length, 1, x, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a numeric array... +{ + const x = new Float64Array( 10 ); + + gsort.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType Float64Array + gsort.ndarray( x.length, 1, new AccessorArray( x ), 1, 0 ); // $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 ); + + gsort.ndarray( '10', 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( true, 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( false, 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( null, 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( undefined, 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( [], 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( {}, 1, x, 1, 0 ); // $ExpectError + gsort.ndarray( ( x: number ): number => x, 1, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsort.ndarray( x.length, '10', x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, true, x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, false, x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, null, x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, undefined, x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, [], x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, {}, x, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, ( x: number ): number => x, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + gsort.ndarray( x.length, 1, 10, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, '10', 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, true, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, false, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, null, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, undefined, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, [ '1' ], 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, {}, 1, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, ( x: number ): number => x, 1, 0 ); // $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 ); + + gsort.ndarray( x.length, 1, x, '10', 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, true, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, false, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, null, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, undefined, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, [], 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, {}, 0 ); // $ExpectError + gsort.ndarray( x.length, 1, x, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + gsort.ndarray( x.length, 1, x, 1, '10' ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, true ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, false ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, null ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, undefined ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, [] ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, {} ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + gsort.ndarray(); // $ExpectError + gsort.ndarray( x.length ); // $ExpectError + gsort.ndarray( x.length, 1 ); // $ExpectError + gsort.ndarray( x.length, 1, x ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1 ); // $ExpectError + gsort.ndarray( x.length, 1, x, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/examples/index.js new file mode 100644 index 000000000000..a7a1158e2b66 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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 gsort = require( './../lib' ); + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +console.log( x ); + +gsort( x.length, -1.0, x, -1 ); +console.log( x ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/index.js new file mode 100644 index 000000000000..6b630e438733 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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. +* +* @module @stdlib/blas/ext/base/gsort +* +* @example +* var gsort = require( '@stdlib/blas/ext/base/gsort' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort( x.length, 1.0, x, 1 ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +* +* @example +* var gsort = require( '@stdlib/blas/ext/base/gsort' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort.ndarray( x.length, 1.0, x, 1, 0 ); +* // 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/gsort/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/main.js new file mode 100644 index 000000000000..ad215caa4c31 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/main.js @@ -0,0 +1,51 @@ +/** +* @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. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} order - sort order +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {NumericArray} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort( x.length, 1.0, x, 1 ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsort( N, order, x, strideX ) { + return ndarray( N, order, x, strideX, stride2offset( N, strideX ) ); +} + + +// EXPORTS // + +module.exports = gsort; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/ndarray.js new file mode 100644 index 000000000000..b8cee4bccbcb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/lib/ndarray.js @@ -0,0 +1,51 @@ +/** +* @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 gsorthp = require( '@stdlib/blas/ext/base/gsorthp' ).ndarray; + + +// MAIN // + +/** +* Sorts a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} order - sort order +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {NumericArray} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; +* +* gsort( x.length, 1.0, x, 1, 0 ); +* // x => [ -4.0, -2.0, 1.0, 3.0 ] +*/ +function gsort( N, order, x, strideX, offsetX ) { + return gsorthp( N, order, x, strideX, offsetX ); +} + + +// EXPORTS // + +module.exports = gsort; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/package.json b/lib/node_modules/@stdlib/blas/ext/base/gsort/package.json new file mode 100644 index 000000000000..22741e19b66b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/blas/ext/base/gsort", + "version": "0.0.0", + "description": "Sort a strided array.", + "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", + "strided", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/ascending.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/ascending.js new file mode 100644 index 000000000000..d67b3f475d99 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/ascending.js @@ -0,0 +1,75 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); + + +// MAIN // + +/** +* Determines sort order by comparing two values. +* +* ## Notes +* +* - Return values are as follows: +* +* - `-1`: sort `a` to a lower index than `b` (i.e., `a` comes first) +* - `1`: sort `b` to a lower index than `a` (i.e., `b` comes first) +* - `0`: leave the order of `a` and `b` unchanged with respect to one another +* +* @private +* @param {number} a - first value +* @param {number} b - second value +* @returns {integer} value indicating sort order +*/ +function ascending( a, b ) { + // Sort NaNs to the end... + if ( isnan( a ) ) { + return 1; + } + if ( isnan( b ) ) { + return -1; + } + // Sort negative 0s to the left... + if ( a === b && a === 0 ) { + if ( isNegativeZero( a ) ) { + return -1; + } + if ( isNegativeZero( b ) ) { + return 1; + } + return 0; + } + if ( a > b ) { + return 1; + } + if ( a < b ) { + return -1; + } + return 0; +} + + +// EXPORTS // + +module.exports = ascending; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/num2str.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/num2str.js new file mode 100644 index 000000000000..d4bce1cee666 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/fixtures/num2str.js @@ -0,0 +1,45 @@ +/** +* @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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); + + +// MAIN // + +/** +* Converts a number to a string. +* +* @private +* @param {number} value - input value +* @returns {string} string representation +*/ +function num2str( value ) { + if ( isNegativeZero( value ) ) { + return '-0'; + } + return value.toString(); +} + + +// EXPORTS // + +module.exports = num2str; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.js new file mode 100644 index 000000000000..7de9e25c2c1f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/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 gsort = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsort, '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 gsort.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.main.js new file mode 100644 index 000000000000..8d97b2aff080 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.main.js @@ -0,0 +1,995 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ascending = require( './fixtures/ascending.js' ); +var num2str = require( './fixtures/num2str.js' ); +var gsort = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsort, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( gsort.length, 4, 'has expected arity' ); + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order)', function test( t ) { + var expected; + var x; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, accessors)', function test( t ) { + var expected; + var x; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (increasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (increasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (decreasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (increasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (increasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (decreasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, special cases)', function test( t ) { + var expected; + var x; + var i; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, special cases, accessors)', function test( t ) { + var expected; + var x; + var i; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, special cases)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, special cases, accessors)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +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 = gsort( x.length, 1.0, x, 1 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +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 = gsort( x.length, 1.0, x, 1 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +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 ]; + + gsort( 0, 1.0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsort( -4, 1.0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if `order` equals `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + + gsort( x.length, 0.0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride (increasing order)', 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 + ]; + + gsort( 3, 1.0, x, 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (increasing order, 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 + ]; + + gsort( 3, 1.0, toAccessorArray( x ), 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 6.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 2 + ]; + + gsort( 3, -1.0, x, 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (decreasing order, accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 6.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 2 + ]; + + gsort( 3, -1.0, toAccessorArray( x ), 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (increasing order)', 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 + ]; + + gsort( 3, 1.0, x, -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (increasing order, 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 + ]; + + gsort( 3, 1.0, toAccessorArray( x ), -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -5.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 0 + ]; + + gsort( 3, -1.0, x, -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (decreasing order, accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -5.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 0 + ]; + + gsort( 3, -1.0, toAccessorArray( x ), -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets (increasing order)', 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 ); + + gsort( 3, 1.0, x1, 2 ); + t.deepEqual( x0, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets (decreasing order)', function test( t ) { + var expected; + var x0; + var x1; + + x0 = new Float64Array([ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]); + expected = new Float64Array([ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + gsort( 3, -1.0, x1, 2 ); + t.deepEqual( x0, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.ndarray.js new file mode 100644 index 000000000000..61bed16ed7dd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/test/test.ndarray.js @@ -0,0 +1,1092 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var randu = require( '@stdlib/random/base/randu' ); +var ascending = require( './fixtures/ascending.js' ); +var num2str = require( './fixtures/num2str.js' ); +var gsort = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gsort, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 5', function test( t ) { + t.strictEqual( gsort.length, 5, 'has expected arity' ); + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order)', function test( t ) { + var expected; + var x; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, accessors)', function test( t ) { + var expected; + var x; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + x.push( (randu()*20.0) - 10.0 ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (increasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (increasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (decreasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes NaNs (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = (randu()*20.0) - 10.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (increasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (increasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (decreasing order)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array which includes positive and negative zeros (decreasing order, accessors)', function test( t ) { + var expected; + var x; + var v; + var i; + var j; + + x = []; + for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.5 ) { + v = -0.0; + } else { + v = 0.0; + } + x.push( v ); + } + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, special cases)', function test( t ) { + var expected; + var x; + var i; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (increasing order, special cases, accessors)', function test( t ) { + var expected; + var x; + var i; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, 1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } else if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[i] )+'.' ); + } else if ( isPositiveZero( expected[ i ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[i]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ i ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[i]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, special cases)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, x, 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +tape( 'the function sorts a strided array (decreasing order, special cases, accessors)', function test( t ) { + var expected; + var x; + var i; + var j; + + x = [ NaN, 1.0, -1.0, 2.0, 2.0 ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + + x = [ NaN, 1.0, -1.0, 2.0, 2.0, NaN ]; + + // Note: we assume that the built-in sort returns a correctly sorted result + expected = x.slice().sort( ascending ); + + gsort( x.length, -1.0, toAccessorArray( x ), 1, 0 ); + for ( i = 0; i < expected.length; i++ ) { + j = expected.length - i - 1; + if ( isnan( expected[ j ] ) ) { + t.strictEqual( isnan( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } else if ( isNegativeZero( expected[ j ] ) ) { + t.strictEqual( isNegativeZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+num2str( expected[j] )+'.' ); + } else if ( isPositiveZero( expected[ j ] ) ) { + t.strictEqual( isPositiveZero( x[ i ] ), true, 'returns expected value. index: '+i+'. actual: '+num2str( x[i] )+'. expected: '+expected[j]+'.' ); + } else { + t.strictEqual( x[ i ], expected[ j ], 'returns expected value. index: '+i+' actual: '+x[i]+'. expected: '+expected[j]+'.' ); + } + } + t.end(); +}); + +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 = gsort( x.length, 1.0, x, 1, 0 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +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 = gsort( x.length, 1.0, x, 1, 0 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +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 ]; + + gsort( 0, 1.0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + gsort( -4, 1.0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if `order` equals `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + + gsort( x.length, 0.0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride (increasing order)', 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 + ]; + + gsort( 3, 1.0, x, 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (increasing order, 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 + ]; + + gsort( 3, 1.0, toAccessorArray( x ), 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 6.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 2 + ]; + + gsort( 3, -1.0, x, 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride (decreasing order, accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 6.0, // 0 + -3.0, + 2.0, // 1 + 7.0, + -5.0 // 2 + ]; + + gsort( 3, -1.0, toAccessorArray( x ), 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (increasing order)', 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 + ]; + + gsort( 3, 1.0, x, -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (increasing order, 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 + ]; + + gsort( 3, 1.0, toAccessorArray( x ), -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -5.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 0 + ]; + + gsort( 3, -1.0, x, -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride (decreasing order, accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -5.0, // 2 + -3.0, + 2.0, // 1 + 7.0, + 6.0 // 0 + ]; + + gsort( 3, -1.0, toAccessorArray( x ), -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying an offset (increasing order)', 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 + ]; + + gsort( 3, 1.0, x, 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying an offset (increasing order, 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 + ]; + + gsort( 3, 1.0, toAccessorArray( x ), 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying an offset (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]; + expected = [ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]; + + gsort( 3, -1.0, x, 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying an offset (decreasing order, accessors)', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -6.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 2 + ]; + expected = [ + 1.0, + -2.0, // 0 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 2 + ]; + + gsort( 3, -1.0, toAccessorArray( x ), 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns (increasing order)', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -4.0, // 2 + 3.0, + -2.0, // 1 + 5.0, + -6.0 // 0 + ]; + expected = [ + 1.0, + -2.0, // 2 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 0 + ]; + + gsort( 3, 1.0, x, -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns (decreasing order)', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + -2.0, // 2 + 3.0, + -4.0, // 1 + 5.0, + -6.0 // 0 + ]; + expected = [ + 1.0, + -6.0, // 2 + 3.0, + -4.0, // 1 + 5.0, + -2.0 // 0 + ]; + + gsort( 3, -1.0, x, -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); From ffb72674638d40486add927a5a8659d39b7dce6a Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 13 Jan 2026 00:04:22 +0500 Subject: [PATCH 2/5] fix: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../base/gsort/benchmark/benchmark.unsorted_random.ndarray.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js index 011786fb1616..dd2df8f7f178 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/benchmark/benchmark.unsorted_random.ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. From 2cd6934ffc7dae824046dfdf7d678356f1799831 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 13 Jan 2026 21:01:50 -0800 Subject: [PATCH 3/5] docs: remove note Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/base/gsort/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md index 0806fd970e9f..c76b27dd8f45 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md @@ -108,7 +108,6 @@ gsort.ndarray( 3, 1.0, x, 1, x.length-3 ); - If `N <= 0` or `order == 0.0`, both functions return `x` unchanged. - The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. - The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. -- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. - The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**). From fa8fb053a71af8be6be07f31f53c2ac7509c6160 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 13 Jan 2026 21:03:32 -0800 Subject: [PATCH 4/5] style: remove horizontal line Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/base/gsort/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md index c76b27dd8f45..6e00d86eed68 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/README.md @@ -137,8 +137,6 @@ console.log( x ); -* * * -
From 5396c2daa1dad50a2dbb1e5eaef2baf05b4ed7ff Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 13 Jan 2026 21:05:21 -0800 Subject: [PATCH 5/5] docs: remove note Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt index fb15830fe9f3..d23f4bec5295 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/gsort/docs/repl.txt @@ -18,8 +18,6 @@ order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. - The algorithm has space complexity O(1) and time complexity O(N log2 N). - The input strided array is sorted *in-place* (i.e., the input strided array is *mutated*).