Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -50,11 +52,13 @@
function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
var xbuf;
var get;
var idx;
var mid;
var lo;
var hi;
var v1;
var v2;
var v;

// Cache reference to array data:
xbuf = x.data;
Expand All @@ -65,6 +69,10 @@
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 ) {
Expand All @@ -77,7 +85,14 @@
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 ) ) {
idx = gindexOfGreaterThan( N-lo, searchElement, xbuf, strideX, offsetX + ( lo * strideX ) );

Check warning on line 92 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 104. Maximum allowed is 80
return ( idx < 0 ) ? idx : idx + lo;
}
if ( searchElement < v ) {
hi = mid;
} else {
lo = mid + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );


Expand All @@ -48,12 +50,14 @@
* // returns 3
*/
function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
var idx;
var mid;
var lo;
var hi;
var v1;
var v2;
var o;
var v;

if ( N <= 0 ) {
return -1;
Expand All @@ -65,6 +69,10 @@
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 ) {
Expand All @@ -77,7 +85,14 @@
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 ) ) {
idx = gindexOfGreaterThan( N-lo, searchElement, x, strideX, offsetX + ( lo * strideX ) );

Check warning on line 92 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 101. Maximum allowed is 80
return ( idx < 0 ) ? idx : idx + lo;
}
if ( searchElement < v ) {
hi = mid;
} else {
lo = mid + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@
tape( 'the function returns `-1` if comparisons involve `NaN` values (accessors)', function test( t ) {
var actual;

actual = gindexOfGreaterThanSorted( 2, NaN, toAccessorArray( [ 0.0, 1.0 ] ), 1 );

Check warning on line 159 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 85. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

actual = gindexOfGreaterThanSorted( 2, 0.0, toAccessorArray( [ NaN, NaN ] ), 1 );

Check warning on line 162 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 85. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

t.end();
Expand All @@ -180,10 +180,10 @@
tape( 'the function treats `-0` and `+0` as equal (accessors)', function test( t ) {
var actual;

actual = gindexOfGreaterThanSorted( 1, -0.0, toAccessorArray( [ 0.0 ] ), 1 );

Check warning on line 183 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 81. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

actual = gindexOfGreaterThanSorted( 1, 0.0, toAccessorArray( [ -0.0 ] ), 1 );

Check warning on line 186 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 81. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

t.end();
Expand Down Expand Up @@ -468,3 +468,57 @@

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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@
tape( 'the function returns `-1` if comparisons involve `NaN` values (accessors)', function test( t ) {
var actual;

actual = gindexOfGreaterThanSorted( 2, NaN, toAccessorArray( [ 0.0, 1.0 ] ), 1, 0 );

Check warning on line 158 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 88. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

actual = gindexOfGreaterThanSorted( 2, 0.0, toAccessorArray( [ NaN, NaN ] ), 1, 0 );

Check warning on line 161 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 88. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

t.end();
Expand All @@ -179,10 +179,10 @@
tape( 'the function treats `-0` and `+0` as equal (accessors)', function test( t ) {
var actual;

actual = gindexOfGreaterThanSorted( 1, -0.0, toAccessorArray( [ 0.0 ] ), 1, 0 );

Check warning on line 182 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

actual = gindexOfGreaterThanSorted( 1, 0.0, toAccessorArray( [ -0.0 ] ), 1, 0 );

Check warning on line 185 in lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
t.strictEqual( actual, -1, 'returns expected value' );

t.end();
Expand Down Expand Up @@ -517,3 +517,57 @@

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();
});
Loading