Skip to content

Commit ece4002

Browse files
authored
feat: add accessor arrays support and refactor blas/ext/base/gsortsh
PR-URL: #5122 Reviewed-by: Athan Reines <[email protected]>
1 parent ca52b7b commit ece4002

File tree

10 files changed

+1116
-151
lines changed

10 files changed

+1116
-151
lines changed

lib/node_modules/@stdlib/blas/ext/base/gsortsh/README.md

+17-34
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ limitations under the License.
3030
var gsortsh = require( '@stdlib/blas/ext/base/gsortsh' );
3131
```
3232

33-
#### gsortsh( N, order, x, stride )
33+
#### gsortsh( N, order, x, strideX )
3434

35-
Sorts a strided array `x` using Shellsort.
35+
Sorts a strided array using Shellsort.
3636

3737
```javascript
3838
var x = [ 1.0, -2.0, 3.0, -4.0 ];
@@ -46,41 +46,36 @@ The function has the following parameters:
4646
- **N**: number of indexed elements.
4747
- **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.
4848
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
49-
- **stride**: index increment.
49+
- **strideX**: stride length.
5050

51-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to sort every other element
51+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element:
5252

5353
```javascript
54-
var floor = require( '@stdlib/math/base/special/floor' );
55-
5654
var x = [ 1.0, -2.0, 3.0, -4.0 ];
57-
var N = floor( x.length / 2 );
5855

59-
gsortsh( N, -1.0, x, 2 );
56+
gsortsh( 2, -1.0, x, 2 );
6057
// x => [ 3.0, -2.0, 1.0, -4.0 ]
6158
```
6259

6360
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
6461

6562
```javascript
6663
var Float64Array = require( '@stdlib/array/float64' );
67-
var floor = require( '@stdlib/math/base/special/floor' );
6864

6965
// Initial array...
7066
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
7167

7268
// Create an offset view...
7369
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74-
var N = floor( x0.length/2 );
7570

7671
// Sort every other element...
77-
gsortsh( N, -1.0, x1, 2 );
72+
gsortsh( 2, -1.0, x1, 2 );
7873
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
7974
```
8075

81-
#### gsortsh.ndarray( N, order, x, stride, offset )
76+
#### gsortsh.ndarray( N, order, x, strideX, offsetX )
8277

83-
Sorts a strided array `x` using Shellsort and alternative indexing semantics.
78+
Sorts a strided array using Shellsort and alternative indexing semantics.
8479

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

9287
The function has the following additional parameters:
9388

94-
- **offset**: starting index.
89+
- **offsetX**: starting index.
9590

96-
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`
91+
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:
9792

9893
```javascript
9994
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
@@ -111,6 +106,7 @@ gsortsh.ndarray( 3, 1.0, x, 1, x.length-3 );
111106
## Notes
112107

113108
- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
109+
- 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])
114110
- 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`.
115111
- 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.
116112
- The algorithm has space complexity `O(1)` and worst case time complexity `O(N^(4/3))`.
@@ -130,27 +126,12 @@ gsortsh.ndarray( 3, 1.0, x, 1, x.length-3 );
130126
<!-- eslint no-undef: "error" -->
131127

132128
```javascript
133-
var round = require( '@stdlib/math/base/special/round' );
134-
var randu = require( '@stdlib/random/base/randu' );
135-
var Float64Array = require( '@stdlib/array/float64' );
129+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136130
var gsortsh = require( '@stdlib/blas/ext/base/gsortsh' );
137131

138-
var rand;
139-
var sign;
140-
var x;
141-
var i;
142-
143-
x = new Float64Array( 10 );
144-
for ( i = 0; i < x.length; i++ ) {
145-
rand = round( randu()*100.0 );
146-
sign = randu();
147-
if ( sign < 0.5 ) {
148-
sign = -1.0;
149-
} else {
150-
sign = 1.0;
151-
}
152-
x[ i ] = sign * rand;
153-
}
132+
var x = discreteUniform( 10, -100, 100, {
133+
'dtype': 'float64'
134+
});
154135
console.log( x );
155136

156137
gsortsh( x.length, -1.0, x, -1 );
@@ -199,6 +180,8 @@ console.log( x );
199180

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

183+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
184+
202185
[@shell:1959a]: https://doi.org/10.1145/368370.368387
203186

204187
[@sedgewick:1986a]: https://doi.org/10.1016/0196-6774(86)90001-5

lib/node_modules/@stdlib/blas/ext/base/gsortsh/docs/repl.txt

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
{{alias}}( N, order, x, stride )
2+
{{alias}}( N, order, x, strideX )
33
Sorts a strided array using Shellsort.
44

5-
The `N` and `stride` parameters determine which elements in `x` are accessed
6-
at runtime.
5+
The `N` and stride parameters determine which elements in the strided array
6+
are accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use typed
99
array views.
@@ -42,8 +42,8 @@
4242
x: Array<number>|TypedArray
4343
Input array.
4444

45-
stride: integer
46-
Index increment for `x`.
45+
strideX: integer
46+
Stride length.
4747

4848
Returns
4949
-------
@@ -57,27 +57,26 @@
5757
> {{alias}}( x.length, 1, x, 1 )
5858
[ -4.0, -2.0, 1.0, 3.0 ]
5959

60-
// Using `N` and `stride` parameters:
60+
// Using `N` and stride parameters:
6161
> x = [ 1.0, -2.0, 3.0, -4.0 ];
62-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
63-
> {{alias}}( N, -1, x, 2 )
62+
> {{alias}}( 2, -1, x, 2 )
6463
[ 3.0, -2.0, 1.0, -4.0 ]
6564

6665
// Using view offsets:
6766
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0 ] );
6867
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
69-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
70-
> {{alias}}( N, 1, x1, 2 )
68+
> {{alias}}( 2, 1, x1, 2 )
7169
<Float64Array>[ -4.0, 3.0, -2.0 ]
7270
> x0
7371
<Float64Array>[ 1.0, -4.0, 3.0, -2.0 ]
7472

75-
{{alias}}.ndarray( N, order, x, stride, offset )
73+
74+
{{alias}}.ndarray( N, order, x, strideX, offsetX )
7675
Sorts a strided array using Shellsort and alternative indexing semantics.
7776

7877
While typed array views mandate a view offset based on the underlying
79-
buffer, the `offset` parameter supports indexing semantics based on a
80-
starting index.
78+
buffer, the offset parameter supports indexing semantics based on a starting
79+
index.
8180

8281
Parameters
8382
----------
@@ -91,11 +90,11 @@
9190
x: Array<number>|TypedArray
9291
Input array.
9392

94-
stride: integer
95-
Index increment for `x`.
93+
strideX: integer
94+
Stride length.
9695

97-
offset: integer
98-
Starting index of `x`.
96+
offsetX: integer
97+
Starting index.
9998

10099
Returns
101100
-------
@@ -111,8 +110,7 @@
111110

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

118116
See Also

lib/node_modules/@stdlib/blas/ext/base/gsortsh/docs/types/index.d.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

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

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2429

2530
/**
2631
* Interface describing `gsortsh`.
@@ -32,7 +37,7 @@ interface Routine {
3237
* @param N - number of indexed elements
3338
* @param order - sort order
3439
* @param x - input array
35-
* @param stride - stride length
40+
* @param strideX - stride length
3641
* @returns `x`
3742
*
3843
* @example
@@ -41,16 +46,16 @@ interface Routine {
4146
* gsortsh( x.length, 1, x, 1 );
4247
* // x => [ -4.0, -2.0, 1.0, 3.0 ]
4348
*/
44-
( N: number, order: number, x: NumericArray, stride: number ): NumericArray;
49+
<T extends InputArray>( N: number, order: number, x: T, strideX: number ): T;
4550

4651
/**
4752
* Sorts a strided array using Shellsort and alternative indexing semantics.
4853
*
4954
* @param N - number of indexed elements
5055
* @param order - sort order
5156
* @param x - input array
52-
* @param stride - stride length
53-
* @param offset - starting index
57+
* @param strideX - stride length
58+
* @param offsetX - starting index
5459
* @returns `x`
5560
*
5661
* @example
@@ -59,7 +64,7 @@ interface Routine {
5964
* gsortsh.ndarray( x.length, 1, x, 1, 0 );
6065
* // x => [ -4.0, -2.0, 1.0, 3.0 ]
6166
*/
62-
ndarray( N: number, order: number, x: NumericArray, stride: number, offset: number ): NumericArray;
67+
ndarray<T extends InputArray>( N: number, order: number, x: T, strideX: number, offsetX: number ): T;
6368
}
6469

6570
/**
@@ -68,7 +73,7 @@ interface Routine {
6873
* @param N - number of indexed elements
6974
* @param order - sort order
7075
* @param x - input array
71-
* @param stride - stride length
76+
* @param strideX - stride length
7277
* @returns `x`
7378
*
7479
* @example

lib/node_modules/@stdlib/blas/ext/base/gsortsh/docs/types/test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import gsortsh = require( './index' );
2021

2122

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

28-
gsortsh( x.length, 1, x, 1 ); // $ExpectType NumericArray
29+
gsortsh( x.length, 1, x, 1 ); // $ExpectType Float64Array
30+
gsortsh( x.length, 1, new AccessorArray( x ), 1 ); // $ExpectType AccessorArray<number>
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -100,7 +102,8 @@ import gsortsh = require( './index' );
100102
{
101103
const x = new Float64Array( 10 );
102104

103-
gsortsh.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType NumericArray
105+
gsortsh.ndarray( x.length, 1, x, 1, 0 ); // $ExpectType Float64Array
106+
gsortsh.ndarray( x.length, 1, new AccessorArray( x ), 1, 0 ); // $ExpectType AccessorArray<number>
104107
}
105108

106109
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

lib/node_modules/@stdlib/blas/ext/base/gsortsh/examples/index.js

+4-23
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,12 @@
1818

1919
'use strict';
2020

21-
var round = require( '@stdlib/math/base/special/round' );
22-
var randu = require( '@stdlib/random/base/randu' );
23-
var Float64Array = require( '@stdlib/array/float64' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2422
var gsortsh = require( './../lib' );
2523

26-
var rand;
27-
var sign;
28-
var x;
29-
var i;
30-
31-
x = new Float64Array( 10 );
32-
for ( i = 0; i < x.length; i++ ) {
33-
if ( randu() < 0.2 ) {
34-
x[ i ] = NaN;
35-
} else {
36-
rand = round( randu()*100.0 );
37-
sign = randu();
38-
if ( sign < 0.5 ) {
39-
sign = -1.0;
40-
} else {
41-
sign = 1.0;
42-
}
43-
x[ i ] = sign * rand;
44-
}
45-
}
24+
var x = discreteUniform( 10, -100, 100, {
25+
'dtype': 'float64'
26+
});
4627
console.log( x );
4728

4829
gsortsh( x.length, -1.0, x, -1 );

0 commit comments

Comments
 (0)