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 5f0b462a8db9..0215da88d967 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/README.md @@ -30,7 +30,7 @@ limitations under the License. var ssorthp = require( '@stdlib/blas/ext/base/ssorthp' ); ``` -#### ssorthp( N, order, x, stride ) +#### ssorthp( N, order, x, strideX ) Sorts a single-precision floating-point strided array using heapsort. @@ -48,9 +48,9 @@ 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 [`Float32Array`][@stdlib/array/float32]. -- **stride**: index increment. +- **strideX**: stride length. -The `N` and stride parameters determine which elements in the strided arrays 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' ); @@ -77,7 +77,7 @@ ssorthp( 2, -1.0, x1, 2 ); // x0 => [ 1.0, 4.0, 3.0, 2.0 ] ``` -#### ssorthp.ndarray( N, order, x, stride, offset ) +#### ssorthp.ndarray( N, order, x, strideX, offsetX ) Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics. @@ -92,9 +92,9 @@ ssorthp.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 the strided array +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 Float32Array = require( '@stdlib/array/float32' ); @@ -131,13 +131,12 @@ ssorthp.ndarray( 3, 1.0, x, 1, x.length-3 ); ```javascript -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var ssorthp = require( '@stdlib/blas/ext/base/ssorthp' ); -var rand = discreteUniform( -100, 100 ); -var x = filledarrayBy( 10, 'float32', rand ); - +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); ssorthp( x.length, -1.0, x, -1 ); @@ -167,6 +166,125 @@ console.log( x ); * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/ssorthp.h" +``` + +#### stdlib_strided_ssorthp( N, order, \*X, strideX ) + +Sorts a single-precision floating-point strided array using heapsort. + +```c +float x[] = { 1.0f, -2.0f, 3.0f, -4.0f }; + +stdlib_strided_ssorthp( 2, -1.0f, x, 1 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **order**: `[in] float` 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**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. + +```c +stdlib_strided_ssorthp( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX ); +``` + + + +#### stdlib_strided_ssorthp_ndarray( N, order, \*X, strideX, offsetX ) + + + +Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics. + +```c +float x[] = { 1.0f, -2.0f, 3.0f, -4.0f }; + +stdlib_strided_ssorthp_ndarray( 4, 1.0f, x, 1, 0 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **order**: `[in] float` 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**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. +- **offsetX**: `[in] CBLAS_INT` starting index. + +```c +stdlib_strided_ssorthp_ndarray( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/ssorthp.h" +#include + +int main( void ) { + // Create a strided array: + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + + // Specify the number of elements: + int N = 8; + + // Specify a stride: + int strideX = 1; + + // Sort the array: + stdlib_strided_ssorthp( N, 1.0f, x, strideX ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "x[ %i ] = %f\n", i, x[ i ] ); + } +} +``` + +
+ + + +
+ + + ## See Also - [`@stdlib/blas/ext/base/dsorthp`][@stdlib/blas/ext/base/dsorthp]: sort a double-precision floating-point strided array using heapsort. diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/Makefile b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/benchmark.unsorted_random.length.c b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/benchmark.unsorted_random.length.c new file mode 100644 index 000000000000..c462644dd2e9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/benchmark/c/benchmark.unsorted_random.length.c @@ -0,0 +1,191 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 + +#define NAME "ssorthp" +#define ITERATIONS 10000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param iterations number of iterations +* @param elapsed elapsed time in seconds +*/ +static void print_results( int iterations, double elapsed ) { + double rate = (double)iterations / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", iterations ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark1( int iterations, int len ) { + double elapsed; + float x[ len ]; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float()*20.0f ) - 10.0f; + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + stdlib_strided_ssorthp( len, 1.0f, x, 1 ); + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int len ) { + double elapsed; + float x[ len ]; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float()*20.0f ) - 10.0f; + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + stdlib_strided_ssorthp_ndarray( len, 1.0f, x, 1, 0 ); + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int iter; + int len; + int i; + int j; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + count = 0; + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:unsorted,random:len=%d\n", NAME, len ); + elapsed = benchmark1( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:ndarray:unsorted,random:len=%d\n", NAME, len ); + elapsed = benchmark2( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + print_summary( count, count ); +} 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 39173edee4ff..046d928707e4 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 @@ -1,10 +1,9 @@ -{{alias}}( N, order, x, stride ) +{{alias}}( N, order, x, strideX ) Sorts a single-precision floating-point strided array using heapsort. - The `N` and stride parameters determine which elements in the strided - array 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. @@ -42,8 +41,8 @@ x: Float32Array Input array. - stride: integer - Index increment for the strided array. + strideX: integer + Stride length. Returns ------- @@ -71,13 +70,13 @@ [ 1.0, -4.0, 3.0, -2.0 ] -{{alias}}.ndarray( N, order, x, stride, offset ) +{{alias}}.ndarray( N, order, x, strideX, offsetX ) Sorts a single-precision floating-point strided array using heapsort 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 ---------- @@ -92,11 +91,11 @@ x: Float32Array Input array. - stride: integer - Index increment for the strided array. + strideX: integer + Stride length. - offset: integer - Starting index of the strided array. + offsetX: integer + Starting index. Returns ------- 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 b18977d731c8..70fa28b34a58 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 @@ -28,7 +28,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 input array * * @example @@ -39,7 +39,7 @@ interface Routine { * ssorthp( x.length, 1, x, 1 ); * // x => [ -4.0, -2.0, 1.0, 3.0 ] */ - ( N: number, order: number, x: Float32Array, stride: number ): Float32Array; + ( N: number, order: number, x: Float32Array, strideX: number ): Float32Array; /** * Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics. @@ -47,8 +47,8 @@ interface Routine { * @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 offsetXX - starting index * @returns input array * * @example @@ -59,7 +59,7 @@ interface Routine { * ssorthp.ndarray( x.length, 1, x, 1, 0 ); * // x => [ -4.0, -2.0, 1.0, 3.0 ] */ - ndarray( N: number, order: number, x: Float32Array, stride: number, offset: number ): Float32Array; + ndarray( N: number, order: number, x: Float32Array, strideX: number, offsetX: number ): Float32Array; } /** @@ -68,7 +68,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 input array * * @example diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/c/example.c index 110f89c05813..1a702e821bf5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/examples/c/example.c @@ -21,7 +21,7 @@ int main( void ) { // Create a strided array: - float x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; // Specify the number of elements: int N = 8; @@ -30,7 +30,7 @@ int main( void ) { int strideX = 1; // Sort the array: - c_ssorthp( N, 1.0f, x, strideX ); + stdlib_strided_ssorthp( N, 1.0f, x, strideX ); // Print the result: for ( int i = 0; i < 8; i++ ) { 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 d900b4e600ce..5f27e668138e 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,13 +18,12 @@ 'use strict'; -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var ssorthp = require( './../lib' ); -var rand = discreteUniform( -100, 100 ); - -var x = filledarrayBy( 10, 'float32', rand ); +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); ssorthp( x.length, -1.0, x, -1 ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include/stdlib/blas/ext/base/ssorthp.h b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include/stdlib/blas/ext/base/ssorthp.h index 03550b39a759..cb2e69081e73 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include/stdlib/blas/ext/base/ssorthp.h +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/include/stdlib/blas/ext/base/ssorthp.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_SSORTHP_H #define STDLIB_BLAS_EXT_BASE_SSORTHP_H -#include +#include "stdlib/blas/base/shared.h" /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. @@ -31,7 +31,12 @@ extern "C" { /** * Sorts a single-precision floating-point strided array using heapsort. */ -void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stride ); +void API_SUFFIX(stdlib_strided_ssorthp)( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX ); + +/** +* Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics. +*/ +void API_SUFFIX(stdlib_strided_ssorthp_ndarray)( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.js index 6bb464f6bf9f..6aee88a259fa 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.js @@ -42,8 +42,8 @@ var floor = require( '@stdlib/math/base/special/floor' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} order - sort order * @param {Float32Array} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Float32Array} input array * * @example @@ -54,7 +54,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); * ssorthp( x.length, 1.0, x, 1, 0 ); * // x => [ -4.0, -2.0, 1.0, 3.0 ] */ -function ssorthp( N, order, x, stride, offset ) { +function ssorthp( N, order, x, strideX, offsetX ) { var parent; var child; var v1; @@ -70,8 +70,8 @@ function ssorthp( N, order, x, stride, offset ) { } // For a positive stride, sorting in decreasing order is equivalent to providing a negative stride and sorting in increasing order, and, for a negative stride, sorting in decreasing order is equivalent to providing a positive stride and sorting in increasing order... if ( order < 0.0 ) { - stride *= -1; - offset -= (N-1) * stride; + strideX *= -1; + offsetX -= (N-1) * strideX; } // Set the initial heap size: n = N; @@ -84,7 +84,7 @@ function ssorthp( N, order, x, stride, offset ) { if ( parent > 0 ) { // We need to build the heap... parent -= 1; - t = x[ offset+(parent*stride) ]; + t = x[ offsetX+(parent*strideX) ]; } else { // Reduce the heap size: n -= 1; @@ -94,11 +94,11 @@ function ssorthp( N, order, x, stride, offset ) { return x; } // Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position: - i = offset + (n*stride); + i = offsetX + (n*strideX); t = x[ i ]; // Move the heap root to its sorted position: - x[ i ] = x[ offset ]; + x[ i ] = x[ offsetX ]; } // We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property... @@ -112,8 +112,8 @@ function ssorthp( N, order, x, stride, offset ) { // Find the largest child... k = child + 1; if ( k < n ) { - v1 = x[ offset+(k*stride) ]; - v2 = x[ offset+(child*stride) ]; + v1 = x[ offsetX+(k*strideX) ]; + v2 = x[ offsetX+(child*strideX) ]; // Check if a "right" child exists and is "bigger"... if ( v1 > v2 || isnanf( v1 ) || (v1 === v2 && isPositiveZerof( v1 ) ) ) { // eslint-disable-line max-len @@ -121,10 +121,10 @@ function ssorthp( N, order, x, stride, offset ) { } } // Check if the largest child is bigger than `t`... - v1 = x[ offset+(child*stride) ]; + v1 = x[ offsetX+(child*strideX) ]; if ( v1 > t || isnanf( v1 ) || ( v1 === t && isPositiveZerof( v1 ) ) ) { // eslint-disable-line max-len // Insert the larger child value: - x[ offset+(j*stride) ] = v1; + x[ offsetX+(j*strideX) ] = v1; // Update `j` to point to the child index: j = child; @@ -137,7 +137,7 @@ function ssorthp( N, order, x, stride, offset ) { } } // Insert `t` into the heap: - x[ offset+(j*stride) ] = t; + x[ offsetX+(j*strideX) ] = t; } } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.native.js index 21a73e687cfa..c7e19218d27b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var offsetView = require( '@stdlib/strided/base/offset-view' ); -var addon = require( './ssorthp.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -32,8 +31,8 @@ var addon = require( './ssorthp.native.js' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} order - sort order * @param {Float32Array} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Float32Array} input array * * @example @@ -43,15 +42,8 @@ var addon = require( './ssorthp.native.js' ); * * ssorthp( x.length, 1.0, x, 1, 0 ); */ -function ssorthp( N, order, x, stride, offset ) { - var view; - if ( stride < 0 ) { - order *= -1.0; - stride *= -1; - offset -= (N-1) * stride; - } - view = offsetView( x, offset ); - addon( N, order, view, stride ); +function ssorthp( N, order, x, strideX, offsetX ) { + addon.ndarray( N, order, x, strideX, offsetX ); return x; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.js index 1ec750b5dec1..4aae02791973 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.js @@ -20,9 +20,8 @@ // MODULES // -var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' ); -var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); -var floor = require( '@stdlib/math/base/special/floor' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -42,7 +41,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} order - sort order * @param {Float32Array} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {Float32Array} input array * * @example @@ -53,96 +52,8 @@ var floor = require( '@stdlib/math/base/special/floor' ); * ssorthp( x.length, 1.0, x, 1 ); * // x => [ -4.0, -2.0, 1.0, 3.0 ] */ -function ssorthp( N, order, x, stride ) { - var offset; - var parent; - var child; - var v1; - var v2; - var n; - var t; - var i; - var j; - var k; - - if ( N <= 0 || order === 0.0 ) { - return x; - } - // For a positive stride, sorting in decreasing order is equivalent to providing a negative stride and sorting in increasing order, and, for a negative stride, sorting in decreasing order is equivalent to providing a positive stride and sorting in increasing order... - if ( order < 0.0 ) { - stride *= -1; - } - if ( stride < 0 ) { - offset = (1-N) * stride; - } else { - offset = 0; - } - // Set the initial heap size: - n = N; - - // Specify an initial "parent" index for building the heap: - parent = floor( N / 2 ); - - // Continue looping until the array is sorted... - while ( true ) { - if ( parent > 0 ) { - // We need to build the heap... - parent -= 1; - t = x[ offset+(parent*stride) ]; - } else { - // Reduce the heap size: - n -= 1; - - // Check if the heap is empty, and, if so, we are finished sorting... - if ( n === 0 ) { - return x; - } - // Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position: - i = offset + (n*stride); - t = x[ i ]; - - // Move the heap root to its sorted position: - x[ i ] = x[ offset ]; - } - // We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property... - - // Start at the parent index: - j = parent; - - // Get the "left" child index: - child = (j*2) + 1; - - while ( child < n ) { - // Find the largest child... - k = child + 1; - if ( k < n ) { - v1 = x[ offset+(k*stride) ]; - v2 = x[ offset+(child*stride) ]; - - // Check if a "right" child exists and is "bigger"... - if ( v1 > v2 || isnanf( v1 ) || (v1 === v2 && isPositiveZerof( v1 ) ) ) { // eslint-disable-line max-len - child += 1; - } - } - // Check if the largest child is bigger than `t`... - v1 = x[ offset+(child*stride) ]; - if ( v1 > t || isnanf( v1 ) || ( v1 === t && isPositiveZerof( v1 ) ) ) { // eslint-disable-line max-len - // Insert the larger child value: - x[ offset+(j*stride) ] = v1; - - // Update `j` to point to the child index: - j = child; - - // Get the "left" child index and repeat... - child = (j*2) + 1; - } else { - // We've found `t`'s place in the heap... - break; - } - } - // Insert `t` into the heap: - x[ offset+(j*stride) ] = t; - } +function ssorthp( N, order, x, strideX ) { + return ndarray( N, order, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.native.js b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.native.js index e8e603fa785a..8319808d9079 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/lib/ssorthp.native.js @@ -31,7 +31,7 @@ var addon = require( './../src/addon.node' ); * @param {PositiveInteger} N - number of indexed elements * @param {number} order - sort order * @param {Float32Array} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {Float32Array} input array * * @example @@ -42,8 +42,8 @@ var addon = require( './../src/addon.node' ); * ssorthp( x.length, 1.0, x, 1 ); * // x => [ -4.0, -2.0, 1.0, 3.0 ] */ -function ssorthp( N, order, x, stride ) { - addon( N, order, x, stride ); +function ssorthp( N, order, x, strideX ) { + addon( N, order, x, strideX ); return x; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/manifest.json index 7452140b7fe3..4ca8ce4ed4f5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/manifest.json @@ -1,5 +1,7 @@ { - "options": {}, + "options": { + "task": "build" + }, "fields": [ { "field": "src", @@ -24,24 +26,59 @@ ], "confs": [ { + "task": "build", "src": [ - "./src/ssorthp.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "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" + "@stdlib/napi/argv-float", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-positive-zerof", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-positive-zerof", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/assert/is-positive-zerof", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" ] } ] diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.c index 6c4ff23f525c..b0854575e9a4 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/addon.c @@ -24,7 +24,6 @@ #include "stdlib/napi/argv_strided_float32array.h" #include - /** * Receives JavaScript callback invocation data. * @@ -36,10 +35,28 @@ 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 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ); + API_SUFFIX(stdlib_strided_ssorthp)( N, order, X, strideX ); + return NULL; +} + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 5 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_FLOAT( env, order, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ); + API_SUFFIX(stdlib_strided_ssorthp_ndarray)( N, order, X, strideX, offsetX ); return NULL; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/ssorthp.c b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/main.c similarity index 62% rename from lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/ssorthp.c rename to lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/main.c index f1fd6121ed73..bf250d77f936 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/ssorthp.c +++ b/lib/node_modules/@stdlib/blas/ext/base/ssorthp/src/main.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* Copyright (c) 2025 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. @@ -19,8 +19,8 @@ #include "stdlib/blas/ext/base/ssorthp.h" #include "stdlib/math/base/assert/is_positive_zerof.h" #include "stdlib/math/base/assert/is_nanf.h" -#include -#include +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" /** * Sorts a single-precision floating-point strided array using heapsort. @@ -34,20 +34,43 @@ * - Williams, John William Joseph. 1964. "Algorithm 232: Heapsort." _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347–49. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284). * - Floyd, Robert W. 1964. "Algorithm 245: Treesort." _Communications of the ACM_ 7 (12). New York, NY, USA: Association for Computing Machinery: 701. doi:[10.1145/355588.365103](https://doi.org/10.1145/355588.365103). * -* @param N number of indexed elements -* @param order sort order -* @param X input array -* @param stride index increment +* @param N number of indexed elements +* @param order sort order +* @param X input array +* @param strideX stride length */ -void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stride ) { - int64_t offset; - int64_t parent; - int64_t child; - int64_t sx; - int64_t n; - int64_t i; - int64_t j; - int64_t k; +void API_SUFFIX(stdlib_strided_ssorthp)( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_ssorthp_ndarray)( N, order, X, strideX, ox ); +} + +/** +* Sorts a single-precision floating-point strided array using heapsort and alternative indexing semantics. +* +* ## Notes +* +* - This implementation uses an in-place algorithm derived from the work of Floyd (1964). +* +* ## References +* +* - Williams, John William Joseph. 1964. "Algorithm 232: Heapsort." _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347–49. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284). +* - Floyd, Robert W. 1964. "Algorithm 245: Treesort." _Communications of the ACM_ 7 (12). New York, NY, USA: Association for Computing Machinery: 701. doi:[10.1145/355588.365103](https://doi.org/10.1145/355588.365103). +* +* @param N number of indexed elements +* @param order sort order +* @param X input array +* @param strideX stride length +* @param offsetX starting index +*/ +void API_SUFFIX(stdlib_strided_ssorthp_ndarray)( const CBLAS_INT N, const float order, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT parent; + CBLAS_INT child; + CBLAS_INT sx; + CBLAS_INT ox; + CBLAS_INT n; + CBLAS_INT i; + CBLAS_INT j; + CBLAS_INT k; float v1; float v2; float t; @@ -57,27 +80,24 @@ void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stri } // For a positive stride, sorting in decreasing order is equivalent to providing a negative stride and sorting in increasing order, and, for a negative stride, sorting in decreasing order is equivalent to providing a positive stride and sorting in increasing order... if ( order < 0.0f ) { - sx = -stride; - } else { - sx = stride; - } - if ( sx < 0 ) { - offset = (1-N) * sx; + sx = -strideX; + ox = offsetX - ( (N-1) * sx ); } else { - offset = 0; + sx = strideX; + ox = offsetX; } // Set the initial heap size: n = N; // Specify an initial "parent" index for building the heap: - parent = floorf( N / 2 ); + parent = N / 2; // Continue looping until the array is sorted... while ( true ) { if ( parent > 0 ) { // We need to build the heap... parent -= 1; - t = X[ offset+(parent*sx) ]; + t = X[ ox+(parent*sx) ]; } else { // Reduce the heap size: n -= 1; @@ -87,11 +107,11 @@ void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stri return; } // Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position: - i = offset + (n*sx); + i = ox + (n*sx); t = X[ i ]; // Move the heap root to its sorted position: - X[ i ] = X[ offset ]; + X[ i ] = X[ ox ]; } // We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property... @@ -105,8 +125,8 @@ void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stri // Find the largest child... k = child + 1; if ( k < n ) { - v1 = X[ offset+(k*sx) ]; - v2 = X[ offset+(child*sx) ]; + v1 = X[ ox+(k*sx) ]; + v2 = X[ ox+(child*sx) ]; // Check if a "right" child exists and is "bigger"... if ( v1 > v2 || stdlib_base_is_nanf( v1 ) || (v1 == v2 && stdlib_base_is_positive_zerof( v1 ) ) ) { @@ -114,10 +134,10 @@ void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stri } } // Check if the largest child is bigger than `t`... - v1 = X[ offset+(child*sx) ]; + v1 = X[ ox+(child*sx) ]; if ( v1 > t || stdlib_base_is_nanf( v1 ) || ( v1 == t && stdlib_base_is_positive_zerof( v1 ) ) ) { // Insert the larger child value: - X[ offset+(j*sx) ] = v1; + X[ ox+(j*sx) ] = v1; // Update `j` to point to the child index: j = child; @@ -130,6 +150,6 @@ void c_ssorthp( const int64_t N, const float order, float *X, const int64_t stri } } // Insert `t` into the heap: - X[ offset+(j*sx) ] = t; + X[ ox+(j*sx) ] = t; } }