Skip to content

feat: add accessor arrays support and refactor blas/ext/base/gsort2sh #5113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 20 additions & 37 deletions lib/node_modules/@stdlib/blas/ext/base/gsort2sh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var gsort2sh = require( '@stdlib/blas/ext/base/gsort2sh' );

#### gsort2sh( N, order, x, strideX, y, strideY )

Simultaneously sorts two strided arrays based on the sort order of the first array `x` using Shellsort.
Simultaneously sorts two strided arrays based on the sort order of the first array using Shellsort.

```javascript
var x = [ 1.0, -2.0, 3.0, -4.0 ];
Expand All @@ -52,20 +52,17 @@ The function has the following parameters:
- **N**: number of indexed elements.
- **order**: sort order. If `order < 0.0`, the input strided array `x` is sorted in **decreasing** order. If `order > 0.0`, the input strided array `x` is sorted in **increasing** order. If `order == 0.0`, the input strided arrays are left unchanged.
- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **strideX**: `x` index increment.
- **strideX**: stride length for `x`.
- **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **strideY**: `y` index increment.
- **strideY**: stride length for `y`.

The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to sort every other element
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to sort every other element:

```javascript
var floor = require( '@stdlib/math/base/special/floor' );

var x = [ 1.0, -2.0, 3.0, -4.0 ];
var y = [ 0.0, 1.0, 2.0, 3.0 ];
var N = floor( x.length / 2 );

gsort2sh( N, -1.0, x, 2, y, 2 );
gsort2sh( 2, -1.0, x, 2, y, 2 );

console.log( x );
// => [ 3.0, -2.0, 1.0, -4.0 ]
Expand All @@ -78,7 +75,6 @@ Note that indexing is relative to the first index. To introduce an offset, use [

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

// Initial arrays...
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
Expand All @@ -87,10 +83,9 @@ var y0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
// Create offset views...
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var N = floor( x0.length/2 );

// Sort every other element...
gsort2sh( N, -1.0, x1, 2, y1, 2 );
gsort2sh( 2, -1.0, x1, 2, y1, 2 );

console.log( x0 );
// => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
Expand All @@ -101,7 +96,7 @@ console.log( y0 );

#### gsort2sh.ndarray( N, order, x, strideX, offsetX, y, strideY, offsetY )

Simultaneously sorts two strided arrays based on the sort order of the first array `x` using Shellsort and alternative indexing semantics.
Simultaneously sorts two strided arrays based on the sort order of the first array using Shellsort and alternative indexing semantics.

```javascript
var x = [ 1.0, -2.0, 3.0, -4.0 ];
Expand All @@ -118,10 +113,10 @@ console.log( y );

The function has the following additional parameters:

- **offsetX**: `x` starting index.
- **offsetY**: `y` starting index.
- **offsetX**: starting index for `x`.
- **offsetY**: starting index for `y`.

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 of `x`
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements:

```javascript
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
Expand All @@ -145,6 +140,7 @@ console.log( y );
## Notes

- If `N <= 0` or `order == 0.0`, both functions leave `x` and `y` unchanged.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor])
- The algorithm 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 worst case time complexity `O(N^(4/3))`.
Expand All @@ -164,30 +160,15 @@ console.log( y );
<!-- eslint no-undef: "error" -->

```javascript
var round = require( '@stdlib/math/base/special/round' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gsort2sh = require( '@stdlib/blas/ext/base/gsort2sh' );

var rand;
var sign;
var x;
var y;
var i;

x = new Float64Array( 10 );
y = new Float64Array( 10 ); // index array
for ( i = 0; i < x.length; i++ ) {
rand = round( randu()*100.0 );
sign = randu();
if ( sign < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
x[ i ] = sign * rand;
y[ i ] = i;
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
var y = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );
console.log( y );

Expand Down Expand Up @@ -238,6 +219,8 @@ console.log( y );

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

[@shell:1959a]: https://doi.org/10.1145/368370.368387

[@sedgewick:1986a]: https://doi.org/10.1016/0196-6774(86)90001-5
Expand Down
32 changes: 15 additions & 17 deletions lib/node_modules/@stdlib/blas/ext/base/gsort2sh/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Simultaneously sorts two strided arrays based on the sort order of the first
array using Shellsort.

The `N` and `stride` parameters determine which elements in `x` and `y` are
accessed at runtime.
The `N` and stride parameters determine which elements in the strided arrays
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use typed
array views.
Expand Down Expand Up @@ -44,13 +44,13 @@
First input array.

strideX: integer
Index increment for `x`.
Stride length for `x`.

y: Array<number>|TypedArray
Second input array.

strideY: integer
Index increment for `y`.
Stride length for `y`.

Returns
-------
Expand All @@ -67,11 +67,10 @@
> y
[ 3.0, 1.0, 0.0, 2.0 ]

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = [ 1.0, -2.0, 3.0, -4.0 ];
> y = [ 0.0, 1.0, 2.0, 3.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}( N, -1, x, 2, y, 2 )
> {{alias}}( 2, -1, x, 2, y, 2 )
[ 3.0, -2.0, 1.0, -4.0 ]
> y
[ 2.0, 1.0, 0.0, 3.0 ]
Expand All @@ -81,21 +80,21 @@
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> var y0 = new {{alias:@stdlib/array/float64}}( [ 0.0, 1.0, 2.0, 3.0 ] );
> var y1 = new {{alias:@stdlib/array/float64}}( y0.buffer, y0.BYTES_PER_ELEMENT*1 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> {{alias}}( N, 1, x1, 2, y1, 2 )
> {{alias}}( 2, 1, x1, 2, y1, 2 )
<Float64Array>[ -4.0, 3.0, -2.0 ]
> x0
<Float64Array>[ 1.0, -4.0, 3.0, -2.0 ]
> y0
<Float64Array>[ 0.0, 3.0, 2.0, 1.0 ]


{{alias}}.ndarray( N, order, x, strideX, offsetX, y, strideY, offsetY )
Simultaneously sorts two strided arrays based on the sort order of the first
array using Shellsort and 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.
buffer, the offset parameters support indexing semantics based on starting
indices.

Parameters
----------
Expand All @@ -110,19 +109,19 @@
First input array.

strideX: integer
Index increment for `x`.
Stride length for `x`.

offsetX: integer
Starting index of `x`.
Starting index for `x`.

y: Array<number>|TypedArray
Second input array.

strideY: integer
Index increment for `y`.
Stride length for `y`.

offsetY: integer
Starting index of `y`.
Starting index for `y`.

Returns
-------
Expand All @@ -142,8 +141,7 @@
// Using an index offset:
> x = [ 1.0, -2.0, 3.0, -4.0 ];
> y = [ 0.0, 1.0, 2.0, 3.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 1, x, 2, 1, y, 2, 1 )
> {{alias}}.ndarray( 2, 1, x, 2, 1, y, 2, 1 )
[ 1.0, -4.0, 3.0, -2.0 ]
> y
[ 0.0, 3.0, 2.0, 1.0 ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

/// <reference types="@stdlib/types"/>

import { NumericArray } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Input array.
*/
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Interface describing `gsort2sh`.
Expand All @@ -32,9 +37,9 @@ interface Routine {
* @param N - number of indexed elements
* @param order - sort order
* @param x - first input array
* @param strideX - `x` stride length
* @param strideX - stride length for `x`
* @param y - second input array
* @param strideY - `y` stride length
* @param strideY - stride length for `y`
* @returns `x`
*
* @example
Expand All @@ -49,19 +54,19 @@ interface Routine {
* console.log( y );
* // => [ 3.0, 1.0, 0.0, 2.0 ]
*/
( N: number, order: number, x: NumericArray, strideX: number, y: NumericArray, strideY: number ): NumericArray;
<T extends InputArray>( N: number, order: number, x: InputArray, strideX: number, y: T, strideY: number ): T;

/**
* Simultaneously sorts two strided arrays based on the sort order of the first array using Shellsort and alternative indexing semantics.
*
* @param N - number of indexed elements
* @param order - sort order
* @param x - first input array
* @param strideX - `x` stride length
* @param offsetX - `x` starting index
* @param strideX - stride length for `x`
* @param offsetX - starting index for `x`
* @param y - second input array
* @param strideY - `y` stride length
* @param offsetY - `y` starting index
* @param strideY - stride length for `y`
* @param offsetY - starting index for `y`
* @returns `x`
*
* @example
Expand All @@ -76,7 +81,7 @@ interface Routine {
* console.log( y );
* // => [ 3.0, 1.0, 0.0, 2.0 ]
*/
ndarray( N: number, order: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray;
ndarray<T extends InputArray>( N: number, order: number, x: InputArray, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ): T;
}

/**
Expand All @@ -85,9 +90,9 @@ interface Routine {
* @param N - number of indexed elements
* @param order - sort order
* @param x - first input array
* @param strideX - `x` stride length
* @param strideX - stride length for `x`
* @param y - second input array
* @param strideY - `y` stride length
* @param strideY - stride length for `y`
* @returns `x`
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import AccessorArray = require( '@stdlib/array/base/accessor' );
import gsort2sh = require( './index' );


Expand All @@ -26,7 +27,8 @@ import gsort2sh = require( './index' );
const x = new Float64Array( 10 );
const y = new Float64Array( 10 );

gsort2sh( x.length, 1, x, 1, y, 1 ); // $ExpectType NumericArray
gsort2sh( x.length, 1, x, 1, y, 1 ); // $ExpectType Float64Array
gsort2sh( x.length, 1, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArray<number>
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down Expand Up @@ -139,7 +141,8 @@ import gsort2sh = require( './index' );
const x = new Float64Array( 10 );
const y = new Float64Array( 10 );

gsort2sh.ndarray( x.length, 1, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray
gsort2sh.ndarray( x.length, 1, x, 1, 0, y, 1, 0 ); // $ExpectType Float64Array
gsort2sh.ndarray( x.length, 1, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray<number>
}

// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Expand Down
33 changes: 7 additions & 26 deletions lib/node_modules/@stdlib/blas/ext/base/gsort2sh/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,15 @@

'use strict';

var round = require( '@stdlib/math/base/special/round' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gsort2sh = require( './../lib' );

var rand;
var sign;
var x;
var y;
var i;

x = new Float64Array( 10 );
y = new Float64Array( 10 ); // index array
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.2 ) {
x[ i ] = NaN;
} else {
rand = round( randu()*100.0 );
sign = randu();
if ( sign < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
x[ i ] = sign * rand;
}
y[ i ] = i;
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
var y = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );
console.log( y );

Expand Down
Loading