Skip to content

feat: add accessor arrays support and refactor blas/ext/base/gsortsh #5122

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 9, 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
51 changes: 17 additions & 34 deletions lib/node_modules/@stdlib/blas/ext/base/gsortsh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ limitations under the License.
var gsortsh = require( '@stdlib/blas/ext/base/gsortsh' );
```

#### gsortsh( N, order, x, stride )
#### gsortsh( N, order, x, strideX )

Sorts a strided array `x` using Shellsort.
Sorts a strided array using Shellsort.

```javascript
var x = [ 1.0, -2.0, 3.0, -4.0 ];
Expand All @@ -46,41 +46,36 @@ 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].
- **stride**: index increment.
- **strideX**: stride length.

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to sort every other element
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 floor = require( '@stdlib/math/base/special/floor' );

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

gsortsh( N, -1.0, x, 2 );
gsortsh( 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' );
var floor = require( '@stdlib/math/base/special/floor' );

// 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
var N = floor( x0.length/2 );

// Sort every other element...
gsortsh( N, -1.0, x1, 2 );
gsortsh( 2, -1.0, x1, 2 );
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
```

#### gsortsh.ndarray( N, order, x, stride, offset )
#### gsortsh.ndarray( N, order, x, strideX, offsetX )

Sorts a strided array `x` using Shellsort and alternative indexing semantics.
Sorts a strided array using Shellsort and alternative indexing semantics.

```javascript
var x = [ 1.0, -2.0, 3.0, -4.0 ];
Expand All @@ -91,9 +86,9 @@ gsortsh.ndarray( x.length, 1.0, x, 1, 0 );

The function has the following additional parameters:

- **offset**: starting index.
- **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 of `x`
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 ];
Expand All @@ -111,6 +106,7 @@ gsortsh.ndarray( 3, 1.0, x, 1, x.length-3 );
## Notes

- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor])
- The algorithm 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 @@ -130,27 +126,12 @@ gsortsh.ndarray( 3, 1.0, x, 1, x.length-3 );
<!-- 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 gsortsh = require( '@stdlib/blas/ext/base/gsortsh' );

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

x = new Float64Array( 10 );
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;
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );

gsortsh( x.length, -1.0, x, -1 );
Expand Down Expand Up @@ -199,6 +180,8 @@ console.log( x );

[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
36 changes: 17 additions & 19 deletions lib/node_modules/@stdlib/blas/ext/base/gsortsh/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

{{alias}}( N, order, x, stride )
{{alias}}( N, order, x, strideX )
Sorts a strided array using Shellsort.

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
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.
Expand Down Expand Up @@ -42,8 +42,8 @@
x: Array<number>|TypedArray
Input array.

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

Returns
-------
Expand All @@ -57,27 +57,26 @@
> {{alias}}( x.length, 1, x, 1 )
[ -4.0, -2.0, 1.0, 3.0 ]

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = [ 1.0, -2.0, 3.0, -4.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}( N, -1, x, 2 )
> {{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 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> {{alias}}( N, 1, x1, 2 )
> {{alias}}( 2, 1, x1, 2 )
<Float64Array>[ -4.0, 3.0, -2.0 ]
> x0
<Float64Array>[ 1.0, -4.0, 3.0, -2.0 ]

{{alias}}.ndarray( N, order, x, stride, offset )

{{alias}}.ndarray( N, order, x, strideX, offsetX )
Sorts a strided 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 parameter supports indexing semantics based on a starting
index.

Parameters
----------
Expand All @@ -91,11 +90,11 @@
x: Array<number>|TypedArray
Input array.

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

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

Returns
-------
Expand All @@ -111,8 +110,7 @@

// Using an index offset:
> x = [ 1.0, -2.0, 3.0, -4.0 ];
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, 1, x, 2, 1 )
> {{alias}}.ndarray( 2, 1, x, 2, 1 )
[ 1.0, -4.0, 3.0, -2.0 ]

See Also
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 `gsortsh`.
Expand All @@ -32,7 +37,7 @@ interface Routine {
* @param N - number of indexed elements
* @param order - sort order
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns `x`
*
* @example
Expand All @@ -41,16 +46,16 @@ interface Routine {
* gsortsh( x.length, 1, x, 1 );
* // x => [ -4.0, -2.0, 1.0, 3.0 ]
*/
( N: number, order: number, x: NumericArray, stride: number ): NumericArray;
<T extends InputArray>( N: number, order: number, x: T, strideX: number ): T;

/**
* Sorts a strided array using Shellsort and alternative indexing semantics.
*
* @param N - number of indexed elements
* @param order - sort order
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns `x`
*
* @example
Expand All @@ -59,7 +64,7 @@ interface Routine {
* gsortsh.ndarray( x.length, 1, x, 1, 0 );
* // x => [ -4.0, -2.0, 1.0, 3.0 ]
*/
ndarray( N: number, order: number, x: NumericArray, stride: number, offset: number ): NumericArray;
ndarray<T extends InputArray>( N: number, order: number, x: T, strideX: number, offsetX: number ): T;
}

/**
Expand All @@ -68,7 +73,7 @@ interface Routine {
* @param N - number of indexed elements
* @param order - sort order
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @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 gsortsh = require( './index' );


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

gsortsh( x.length, 1, x, 1 ); // $ExpectType NumericArray
gsortsh( x.length, 1, x, 1 ); // $ExpectType Float64Array
gsortsh( x.length, 1, new AccessorArray( x ), 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 @@ -100,7 +102,8 @@ import gsortsh = require( './index' );
{
const x = new Float64Array( 10 );

gsortsh.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType NumericArray
gsortsh.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType Float64Array
gsortsh.ndarray( x.length, 1, new AccessorArray( x ), 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
27 changes: 4 additions & 23 deletions lib/node_modules/@stdlib/blas/ext/base/gsortsh/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@

'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 gsortsh = require( './../lib' );

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

x = new Float64Array( 10 );
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;
}
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );

gsortsh( x.length, -1.0, x, -1 );
Expand Down
Loading