From 726cca7d78896f87428818a08ac788e5f3ed5be0 Mon Sep 17 00:00:00 2001 From: naveen Date: Sun, 28 Apr 2024 03:57:16 +0530 Subject: [PATCH] refactor: update `blas/ext/base/ssorthp` to follow current project conventions PR-URL: #1770 Closes: #1537 Ref: #1152 --------- Co-authored-by: Praveen Kumar Co-authored-by: Philipp Burckhardt Reviewed-by: Philipp Burckhardt Reviewed-by: Pranav Goswami Reviewed-by: Athan Reines --- .../@stdlib/blas/ext/base/ssorthp/README.md | 44 ++---- .../blas/ext/base/ssorthp/docs/repl.txt | 38 +++--- .../ext/base/ssorthp/docs/types/index.d.ts | 6 +- .../blas/ext/base/ssorthp/examples/index.js | 26 +--- .../blas/ext/base/ssorthp/include.gypi | 2 +- .../ext/base/ssorthp/lib/ndarray.native.js | 4 +- .../blas/ext/base/ssorthp/manifest.json | 87 ++++++------ .../@stdlib/blas/ext/base/ssorthp/src/addon.c | 46 +++++++ .../blas/ext/base/ssorthp/src/addon.cpp | 128 ------------------ .../ext/base/ssorthp/test/test.ndarray.js | 4 +- .../base/ssorthp/test/test.ndarray.native.js | 4 +- .../ext/base/ssorthp/test/test.ssorthp.js | 4 +- .../base/ssorthp/test/test.ssorthp.native.js | 4 +- 13 files changed, 143 insertions(+), 254 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.c delete mode 100644 lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.cpp diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md index f4219834f2d..5f0b462a8db 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md @@ -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' ); @@ -50,16 +50,14 @@ 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 => [ 3.0, -2.0, 1.0, -4.0 ] ``` @@ -67,23 +65,21 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```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 => [ 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' ); @@ -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' ); @@ -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)`. @@ -135,27 +131,13 @@ ssorthp.ndarray( 3, 1.0, x, 1, x.length-3 ); ```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 ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/repl.txt index a3ba2063048..39173edee4f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/repl.txt @@ -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 @@ -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 -------- @@ -54,21 +57,20 @@ > {{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 = 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 ) [ 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 ) [ -4.0, 3.0, -2.0 ] > x0 [ 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. @@ -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 -------- @@ -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 ) [ 1.0, -4.0, 3.0, -2.0 ] See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/types/index.d.ts index 10cf2f149cc..b18977d731c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/docs/types/index.d.ts @@ -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' ); @@ -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' ); @@ -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' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/index.js index 1a9f0911521..d900b4e600c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/index.js @@ -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 ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include.gypi b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include.gypi index 868c5c12e85..26476a8c265 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include.gypi +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' + + +/** +* Receives JavaScript callback invocation data. +* +* @private +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_FLOAT( env, order, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, stride, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, stride, argv, 2 ); + c_ssorthp( N, order, X , stride ); + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.cpp b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.cpp deleted file mode 100644 index 759de023808..00000000000 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/** -* @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. -*/ - -#include "stdlib/blas/ext/base/ssorthp.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_blas_ext_base_ssorthp { - - /** - * Sorts a single-precision floating-point strided array using heapsort. - * - * ## Notes - * - * - When called from JavaScript, the function expects four arguments: - * - * - `N`: number of indexed elements - * - `order`: sort order - * - `X`: input array - * - `strideX`: `X` stride length - */ - napi_value node_ssorthp( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 4; - napi_value argv[ 4 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 4 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 4 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - napi_valuetype vtype1; - status = napi_typeof( env, argv[ 1 ], &vtype1 ); - assert( status == napi_ok ); - if ( vtype1 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." ); - return nullptr; - } - - bool res2; - status = napi_is_typedarray( env, argv[ 2 ], &res2 ); - assert( status == napi_ok ); - if ( res2 == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." ); - return nullptr; - } - - napi_valuetype vtype3; - status = napi_typeof( env, argv[ 3 ], &vtype3 ); - assert( status == napi_ok ); - if ( vtype3 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - double order; - status = napi_get_value_double( env, argv[ 1 ], &order ); - assert( status == napi_ok ); - - int64_t strideX; - status = napi_get_value_int64( env, argv[ 3 ], &strideX ); - assert( status == napi_ok ); - - napi_typedarray_type vtype2; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 2 ], &vtype2, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype2 != napi_float32_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." ); - return nullptr; - } - if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Third argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - c_ssorthp( N, (float)order, (float *)X, strideX ); - - return nullptr; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_ssorthp, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_blas_ext_base_ssorthp diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.js index 45ed3607527..d55efd5b1d1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.js @@ -392,7 +392,7 @@ tape( 'the function returns a reference to the input array', function test( t ) t.end(); }); -tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', function test( t ) { var expected; var x; @@ -408,7 +408,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); -tape( 'if `order` equals `0`, the function returns `x` unchanged', function test( t ) { +tape( 'if `order` equals `0`, the function returns the output array unchanged', function test( t ) { var expected; var x; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.native.js index 47252f151e4..b4477fea73c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ndarray.native.js @@ -401,7 +401,7 @@ tape( 'the function returns a reference to the input array', opts, function test t.end(); }); -tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', opts, function test( t ) { +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', opts, function test( t ) { var expected; var x; @@ -417,7 +417,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); -tape( 'if `order` equals `0`, the function returns `x` unchanged', opts, function test( t ) { +tape( 'if `order` equals `0`, the function returns the output array unchanged', opts, function test( t ) { var expected; var x; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.js index 6fd73da9b93..d8f9d0db3f6 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.js @@ -392,7 +392,7 @@ tape( 'the function returns a reference to the input array', function test( t ) t.end(); }); -tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', function test( t ) { var expected; var x; @@ -408,7 +408,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); -tape( 'if `order` equals `0`, the function returns `x` unchanged', function test( t ) { +tape( 'if `order` equals `0`, the function returns the output array unchanged', function test( t ) { var expected; var x; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.native.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.native.js index 8a7bc377cc8..f276d5748f8 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/test/test.ssorthp.native.js @@ -401,7 +401,7 @@ tape( 'the function returns a reference to the input array', opts, function test t.end(); }); -tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', opts, function test( t ) { +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', opts, function test( t ) { var expected; var x; @@ -417,7 +417,7 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu t.end(); }); -tape( 'if `order` equals `0`, the function returns `x` unchanged', opts, function test( t ) { +tape( 'if `order` equals `0`, the function returns the output array unchanged', opts, function test( t ) { var expected; var x;