From f5720916f1c5fe2c08bca1f98072d053aad24dc8 Mon Sep 17 00:00:00 2001 From: Sachinn-64 Date: Fri, 2 Oct 2026 20:20:51 +0530 Subject: [PATCH 1/2] refactor: add NaN handling support --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../lib/accessors.js | 15 +++++- .../lib/ndarray.js | 15 +++++- .../test/test.main.js | 54 +++++++++++++++++++ .../test/test.ndarray.js | 54 +++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js index 2189c70e9c8a..21c37f41f987 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js @@ -20,7 +20,9 @@ // MODULES // +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var floor = require( '@stdlib/math/base/special/floor' ); +var gindexOfGreaterThan = require( '@stdlib/blas/ext/base/gindex-of-greater-than' ).ndarray; // MAIN // @@ -55,6 +57,7 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { var hi; var v1; var v2; + var v; // Cache reference to array data: xbuf = x.data; @@ -65,6 +68,10 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { v1 = get( xbuf, offsetX ); v2 = get( xbuf, offsetX + ( ( N - 1 ) * strideX ) ); + // If either the first or last indexed element is NaN, we cannot determine the sort order, so fall back to a linear search... + if ( isnan( v1 ) || isnan( v2 ) ) { + return gindexOfGreaterThan( N, searchElement, xbuf, strideX, offsetX ); + } // Descending order: elements which are greater than the search element form a prefix, so only the first indexed element needs to be checked... if ( v1 >= v2 ) { if ( v1 > searchElement ) { @@ -77,7 +84,13 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { hi = N; while ( lo < hi ) { mid = lo + floor( ( hi - lo ) / 2 ); - if ( searchElement < get( xbuf, offsetX + ( mid * strideX ) ) ) { + v = get( xbuf, offsetX + ( mid * strideX ) ); + + // If we encounter a NaN, fall back to a linear search... + if ( isnan( v ) ) { + return gindexOfGreaterThan( N, searchElement, xbuf, strideX, offsetX ); + } + if ( searchElement < v ) { hi = mid; } else { lo = mid + 1; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js index 38f777a247bd..97ce3f546ae0 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js @@ -20,8 +20,10 @@ // MODULES // +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var floor = require( '@stdlib/math/base/special/floor' ); var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var gindexOfGreaterThan = require( '@stdlib/blas/ext/base/gindex-of-greater-than' ).ndarray; var accessors = require( './accessors.js' ); @@ -54,6 +56,7 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { var v1; var v2; var o; + var v; if ( N <= 0 ) { return -1; @@ -65,6 +68,10 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { v1 = x[ offsetX ]; v2 = x[ offsetX + ( ( N - 1 ) * strideX ) ]; + // If either the first or last indexed element is NaN, we cannot determine the sort order, so fall back to a linear search... + if ( isnan( v1 ) || isnan( v2 ) ) { + return gindexOfGreaterThan( N, searchElement, x, strideX, offsetX ); + } // Descending order: elements which are greater than the search element form a prefix, so only the first indexed element needs to be checked... if ( v1 >= v2 ) { if ( v1 > searchElement ) { @@ -77,7 +84,13 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { hi = N; while ( lo < hi ) { mid = lo + floor( ( hi - lo ) / 2 ); - if ( searchElement < x[ offsetX + ( mid * strideX ) ] ) { + v = x[ offsetX + ( mid * strideX ) ]; + + // If we encounter a NaN, fall back to a linear search... + if ( isnan( v ) ) { + return gindexOfGreaterThan( N, searchElement, x, strideX, offsetX ); + } + if ( searchElement < v ) { hi = mid; } else { lo = mid + 1; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js index 95a9c0f21bf7..af0b769fe461 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js @@ -468,3 +468,57 @@ tape( 'the function supports view offsets (descending order)', function test( t t.end(); }); + +tape( 'the function supports input arrays containing `NaN` values', function test( t ) { + var actual; + var x; + + x = [ 1.0, NaN, 3.0, 4.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = [ NaN, 1.0, 2.0, 3.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = [ 1.0, 2.0, 3.0, NaN ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 1, 'returns expected value' ); + + x = [ 3.0, NaN, 2.0, 1.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input arrays containing `NaN` values (accessors)', function test( t ) { + var actual; + var x; + + x = toAccessorArray( [ 1.0, NaN, 3.0, 4.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 1.0, 2.0, 3.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = toAccessorArray( [ 1.0, 2.0, 3.0, NaN ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 1, 'returns expected value' ); + + x = toAccessorArray( [ 3.0, NaN, 2.0, 1.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1 ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js index 217b405fb9a0..f8f3208ef5c3 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js @@ -517,3 +517,57 @@ tape( 'the function supports an `x` offset (descending order) (accessors)', func t.end(); }); + +tape( 'the function supports input arrays containing `NaN` values', function test( t ) { + var actual; + var x; + + x = [ 1.0, NaN, 3.0, 4.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = [ NaN, 1.0, 2.0, 3.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = [ 1.0, 2.0, 3.0, NaN ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 1, 'returns expected value' ); + + x = [ 3.0, NaN, 2.0, 1.0 ]; + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input arrays containing `NaN` values (accessors)', function test( t ) { + var actual; + var x; + + x = toAccessorArray( [ 1.0, NaN, 3.0, 4.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 1.0, 2.0, 3.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 2, 'returns expected value' ); + + x = toAccessorArray( [ 1.0, 2.0, 3.0, NaN ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 1, 'returns expected value' ); + + x = toAccessorArray( [ 3.0, NaN, 2.0, 1.0 ] ); + + actual = gindexOfGreaterThanSorted( x.length, 1.5, x, 1, 0 ); + t.strictEqual( actual, 0, 'returns expected value' ); + + t.end(); +}); From 9987e7cfab870fb74f542de1f94187053af1d228 Mon Sep 17 00:00:00 2001 From: Sachinn-64 Date: Sat, 3 Oct 2026 01:57:19 +0530 Subject: [PATCH 2/2] refactor: update implementation --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../ext/base/gindex-of-greater-than-sorted/lib/accessors.js | 4 +++- .../ext/base/gindex-of-greater-than-sorted/lib/ndarray.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js index 21c37f41f987..c3eeb919f2ca 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js @@ -52,6 +52,7 @@ var gindexOfGreaterThan = require( '@stdlib/blas/ext/base/gindex-of-greater-than function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { var xbuf; var get; + var idx; var mid; var lo; var hi; @@ -88,7 +89,8 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { // If we encounter a NaN, fall back to a linear search... if ( isnan( v ) ) { - return gindexOfGreaterThan( N, searchElement, xbuf, strideX, offsetX ); + idx = gindexOfGreaterThan( N-lo, searchElement, xbuf, strideX, offsetX + ( lo * strideX ) ); + return ( idx < 0 ) ? idx : idx + lo; } if ( searchElement < v ) { hi = mid; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js index 97ce3f546ae0..75952d4b5791 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js @@ -50,6 +50,7 @@ var accessors = require( './accessors.js' ); * // returns 3 */ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { + var idx; var mid; var lo; var hi; @@ -88,7 +89,8 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) { // If we encounter a NaN, fall back to a linear search... if ( isnan( v ) ) { - return gindexOfGreaterThan( N, searchElement, x, strideX, offsetX ); + idx = gindexOfGreaterThan( N-lo, searchElement, x, strideX, offsetX + ( lo * strideX ) ); + return ( idx < 0 ) ? idx : idx + lo; } if ( searchElement < v ) { hi = mid;