Skip to content

Commit

Permalink
refactor: update blas/ext/base/ssorthp to follow current project co…
Browse files Browse the repository at this point in the history
…nventions

PR-URL: #1770
Closes: #1537 
Ref: #1152

---------

Co-authored-by: Praveen Kumar <[email protected]>
Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]> 
Reviewed-by: Pranav Goswami <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
3 people committed Apr 27, 2024
1 parent ededcf6 commit 726cca7
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 254 deletions.
44 changes: 13 additions & 31 deletions lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var ssorthp = require( '@stdlib/blas/ext/base/ssorthp' );

#### ssorthp( N, order, x, stride )

Sorts a single-precision floating-point strided array `x` using heapsort.
Sorts a single-precision floating-point strided array using heapsort.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -50,40 +50,36 @@ The function has the following parameters:
- **x**: input [`Float32Array`][@stdlib/array/float32].
- **stride**: index increment.

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 arrays are accessed at runtime. For example, to sort every other element

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

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

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

// Initial array...
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );

// Create an offset view...
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var N = floor( x0.length/2 );
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 );

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

#### ssorthp.ndarray( N, order, x, stride, offset )

Sorts a single-precision floating-point strided array `x` using heapsort and alternative indexing semantics.
Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -98,7 +94,7 @@ The function has the following additional parameters:

- **offset**: 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 of the strided array

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -117,7 +113,7 @@ ssorthp.ndarray( 3, 1.0, x, 1, x.length-3 );

## Notes

- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
- If `N <= 0` or `order == 0.0`, both functions return the strided array 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)`.
Expand All @@ -135,27 +131,13 @@ ssorthp.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 Float32Array = require( '@stdlib/array/float32' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var ssorthp = require( '@stdlib/blas/ext/base/ssorthp' );

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

x = new Float32Array( 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 rand = discreteUniform( -100, 100 );
var x = filledarrayBy( 10, 'float32', rand );

console.log( x );

ssorthp( x.length, -1.0, x, -1 );
Expand Down
38 changes: 20 additions & 18 deletions lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
{{alias}}( N, order, x, stride )
Sorts a single-precision floating-point strided array using heapsort.

The `N` and `stride` parameters determine which elements in `x` are accessed
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.
If `N <= 0` or `order == 0`, the function returns the strided
array 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
Expand All @@ -33,19 +35,20 @@
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.
Sort order. If `order < 0`, the function sorts the strided array in
decreasing order. If `order > 0`, the function sorts the strided array
in increasing order.

x: Float32Array
Input array.

stride: integer
Index increment for `x`.
Index increment for the strided array.

Returns
-------
x: Float32Array
Input array `x`.
Input array the strided array.

Examples
--------
Expand All @@ -54,21 +57,20 @@
> {{alias}}( x.length, 1, x, 1 )
<Float32Array>[ -4.0, -2.0, 1.0, 3.0 ]

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = new {{alias:@stdlib/array/float32}}( [ 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 )
<Float32Array>[ 3.0, -2.0, 1.0, -4.0 ]

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, -4.0 ] );
> var x1 = new {{alias:@stdlib/array/float32}}( 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 )
<Float32Array>[ -4.0, 3.0, -2.0 ]
> x0
<Float32Array>[ 1.0, -4.0, 3.0, -2.0 ]


{{alias}}.ndarray( N, order, x, stride, offset )
Sorts a single-precision floating-point strided array using heapsort and
alternative indexing semantics.
Expand All @@ -83,22 +85,23 @@
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.
Sort order. If `order < 0`, the function sorts the strided array in
decreasing order. If `order > 0`, the function sorts the strided array
in increasing order.

x: Float32Array
Input array.

stride: integer
Index increment for `x`.
Index increment for the strided array.

offset: integer
Starting index of `x`.
Starting index of the strided array.

Returns
-------
x: Float32Array
Input array `x`.
Output array.

Examples
--------
Expand All @@ -109,8 +112,7 @@

// Using an index offset:
> x = new {{alias:@stdlib/array/float32}}( [ 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 )
<Float32Array>[ 1.0, -4.0, 3.0, -2.0 ]

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface Routine {
* @param order - sort order
* @param x - input array
* @param stride - stride length
* @returns `x`
* @returns input array
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -49,7 +49,7 @@ interface Routine {
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @returns `x`
* @returns input array
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -69,7 +69,7 @@ interface Routine {
* @param order - sort order
* @param x - input array
* @param stride - stride length
* @returns `x`
* @returns input array
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
Expand Down
26 changes: 4 additions & 22 deletions lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,13 @@

'use strict';

var round = require( '@stdlib/math/base/special/round' );
var randu = require( '@stdlib/random/base/randu' );
var Float32Array = require( '@stdlib/array/float32' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var ssorthp = require( './../lib' );

var rand;
var sign;
var x;
var i;
var rand = discreteUniform( -100, 100 );

x = new Float32Array( 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 = filledarrayBy( 10, 'float32', rand );
console.log( x );

ssorthp( x.length, -1.0, x, -1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Source files:
'src_files': [
'<(src_dir)/addon.cpp',
'<(src_dir)/addon.c',
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).src; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// MODULES //

var Float32Array = require( '@stdlib/array/float32' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var addon = require( './ssorthp.native.js' );


Expand Down Expand Up @@ -50,7 +50,7 @@ function ssorthp( N, order, x, stride, offset ) {
stride *= -1;
offset -= (N-1) * stride;
}
view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
view = offsetView( x, offset );
addon( N, order, view, stride );
return x;
}
Expand Down
87 changes: 46 additions & 41 deletions lib/node_modules/@stdlib/blas/ext/base/ssorthp/manifest.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
{
"options": {},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"src": [
"./src/ssorthp.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-positive-zerof"
]
}
]
"options": {},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"src": [
"./src/ssorthp.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-positive-zerof",
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/napi/argv-float",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-strided-float32array"
]
}
]
}
Loading

1 comment on commit 726cca7

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/ssorthp $\color{green}550/550$
$\color{green}+100.00\%$
$\color{green}63/63$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}550/550$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.