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 gindexOfGreaterThanEqual = require( '@stdlib/blas/ext/base/gindex-of-greater-than-equal' ).ndarray;


// MAIN //
Expand All @@ -47,14 +49,16 @@
* var idx = gindexOfGreaterThanEqualSorted( x.length, 2.0, arraylike2object( toAccessorArray( x ) ), 1, 0 );
* // returns 1
*/
function gindexOfGreaterThanEqualSorted( N, searchElement, x, strideX, offsetX ) {

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Identifier name 'gindexOfGreaterThanEqualSorted' is too long (> 25)

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 82. Maximum allowed is 80
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 gindexOfGreaterThanEqual( N, searchElement, xbuf, strideX, offsetX );

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
}
// Descending order: elements which are greater than or equal to the search element form a prefix, so only the first indexed element needs to be checked...
if ( v1 >= v2 ) {
if ( searchElement <= v1 ) {
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 over the remaining candidate elements...
if ( isnan( v ) ) {
idx = gindexOfGreaterThanEqual( 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-equal-sorted/lib/accessors.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 109. 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 gindexOfGreaterThanEqual = require( '@stdlib/blas/ext/base/gindex-of-greater-than-equal' ).ndarray;
var accessors = require( './accessors.js' );


Expand All @@ -47,13 +49,15 @@
* var idx = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1, 0 );
* // returns 1
*/
function gindexOfGreaterThanEqualSorted( N, searchElement, x, strideX, offsetX ) {

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Identifier name 'gindexOfGreaterThanEqualSorted' is too long (> 25)

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 82. Maximum allowed is 80
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 gindexOfGreaterThanEqual( N, searchElement, x, strideX, offsetX );

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 81. Maximum allowed is 80
}
// Descending order: elements which are greater than or equal to the search element form a prefix, so only the first indexed element needs to be checked...
if ( v1 >= v2 ) {
if ( searchElement <= v1 ) {
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 over the remaining candidate elements...
if ( isnan( v ) ) {
idx = gindexOfGreaterThanEqual( 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-equal-sorted/lib/ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 106. 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 @@ -23,7 +23,7 @@
var tape = require( 'tape' );
var Float64Array = require( '@stdlib/array/float64' );
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
var gindexOfGreaterThanEqualSorted = require( './../lib' );

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Identifier name 'gindexOfGreaterThanEqualSorted' is too long (> 25)


// TESTS //
Expand Down Expand Up @@ -156,7 +156,7 @@
tape( 'the function returns `-1` if comparisons involve `NaN` values (accessors)', function test( t ) {
var actual;

actual = gindexOfGreaterThanEqualSorted( 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-equal-sorted/test/test.main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

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

actual = gindexOfGreaterThanEqualSorted( 2, 0.0, toAccessorArray( [ NaN, NaN ] ), 1 );
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 = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1 );
t.strictEqual( actual, 2, 'returns expected value' );

x = [ NaN, 1.0, 2.0, 3.0 ];

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1 );
t.strictEqual( actual, 2, 'returns expected value' );

x = [ 1.0, 2.0, 3.0, NaN ];

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1 );
t.strictEqual( actual, 1, 'returns expected value' );

x = [ 3.0, NaN, 2.0, 1.0 ];

actual = gindexOfGreaterThanEqualSorted( x.length, 3.0, 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 = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1 );
t.strictEqual( actual, 2, 'returns expected value' );

x = toAccessorArray( [ NaN, 1.0, 2.0, 3.0 ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1 );
t.strictEqual( actual, 2, 'returns expected value' );

x = toAccessorArray( [ 1.0, 2.0, 3.0, NaN ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1 );
t.strictEqual( actual, 1, 'returns expected value' );

x = toAccessorArray( [ 3.0, NaN, 2.0, 1.0 ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1, 0 );
t.strictEqual( actual, 2, 'returns expected value' );

x = [ NaN, 1.0, 2.0, 3.0 ];

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1, 0 );
t.strictEqual( actual, 2, 'returns expected value' );

x = [ 1.0, 2.0, 3.0, NaN ];

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1, 0 );
t.strictEqual( actual, 1, 'returns expected value' );

x = [ 3.0, NaN, 2.0, 1.0 ];

actual = gindexOfGreaterThanEqualSorted( x.length, 3.0, 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 = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1, 0 );
t.strictEqual( actual, 2, 'returns expected value' );

x = toAccessorArray( [ NaN, 1.0, 2.0, 3.0 ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1, 0 );
t.strictEqual( actual, 2, 'returns expected value' );

x = toAccessorArray( [ 1.0, 2.0, 3.0, NaN ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 2.0, x, 1, 0 );
t.strictEqual( actual, 1, 'returns expected value' );

x = toAccessorArray( [ 3.0, NaN, 2.0, 1.0 ] );

actual = gindexOfGreaterThanEqualSorted( x.length, 3.0, x, 1, 0 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});
Loading