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 @@ -49,6 +49,11 @@ var x = [ 1.0, 2.0, 2.0, 3.0 ];

var idx = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
// returns 3

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

idx = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
// returns 0
```

The function has the following parameters:
Expand Down Expand Up @@ -126,7 +131,7 @@ var idx = gindexOfGreaterThanSorted.ndarray( 3, 1.0, x, 1, x.length-3 );

## Notes

- The input strided array **must** be sorted in ascending order.
- The input strided array must be sorted in either **strictly** ascending or descending order.
- If `N <= 0`, both functions return `-1`.
- When comparing elements, both functions use the less-than operator `<`. As a consequence, comparisons involving `NaN` always evaluate to `false`, and `-0` and `+0` are considered the same.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
Indexing is relative to the first index. To introduce an offset, use a typed
array view.

The input strided array must be sorted in ascending order.
The input strided array must be sorted in either strictly ascending or
descending order.

If unable to find an element which is greater than the search element, the
function returns `-1`.
Expand Down Expand Up @@ -41,6 +42,9 @@
> var x = [ 1.0, 2.0, 2.0, 3.0 ];
> var idx = {{alias}}( x.length, 2.0, x, 1 )
3
> x = [ 3.0, 2.0, 2.0, 1.0 ];
> idx = {{alias}}( x.length, 2.0, x, 1 )
0

// Using `N` and stride parameters:
> x = [ 1.0, 9.0, 2.0, 9.0, 3.0, 9.0 ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ interface Routine {
*
* ## Notes
*
* - The input strided array must be sorted in ascending order.
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
*
* @param N - number of indexed elements
Expand All @@ -58,7 +57,6 @@ interface Routine {
*
* ## Notes
*
* - The input strided array must be sorted in ascending order.
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
*
* @param N - number of indexed elements
Expand All @@ -82,7 +80,6 @@ interface Routine {
*
* ## Notes
*
* - The input strided array must be sorted in ascending order.
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
*
* @param N - number of indexed elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,26 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
var mid;
var lo;
var hi;
var v1;
var v2;

// Cache reference to array data:
xbuf = x.data;

// Cache a reference to the element accessors:
get = x.accessors[ 0 ];

v1 = get( xbuf, offsetX );
v2 = get( xbuf, offsetX + ( ( N - 1 ) * strideX ) );

// 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 ) {
return 0;
}
return -1;
}
// Ascending order: perform a binary search for the first element which is greater than the search element...
lo = 0;
hi = N;
while ( lo < hi ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var ndarray = require( './ndarray.js' );
*
* ## Notes
*
* - The input strided array must be sorted in ascending order.
* - If unable to find an element which is greater than the search element, the function returns `-1`.
*
* @param {PositiveInteger} N - number of indexed elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var accessors = require( './accessors.js' );
*
* ## Notes
*
* - The input strided array must be sorted in ascending order.
* - If unable to find an element which is greater than the search element, the function returns `-1`.
*
* @param {PositiveInteger} N - number of indexed elements
Expand All @@ -52,6 +51,8 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
var mid;
var lo;
var hi;
var v1;
var v2;
var o;

if ( N <= 0 ) {
Expand All @@ -61,6 +62,17 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
if ( o.accessorProtocol ) {
return accessors( N, searchElement, o, strideX, offsetX );
}
v1 = x[ offsetX ];
v2 = x[ offsetX + ( ( N - 1 ) * strideX ) ];

// 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 ) {
return 0;
}
return -1;
}
// Ascending order: perform a binary search for the first element which is greater than the search element...
lo = 0;
hi = N;
while ( lo < hi ) {
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 @@ -310,3 +310,161 @@

t.end();
});

tape( 'the function returns the index of the first element in a sorted strided array which is greater than a provided search element (descending order)', function test( t ) {
var actual;
var x;

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

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

t.end();
});

tape( 'the function returns the index of the first element in a sorted strided array which is greater than a provided search element (descending order) (accessors)', function test( t ) {
var actual;
var x;

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

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

t.end();
});

tape( 'the function returns the index of the first element in a sorted strided array which is greater than a provided search element when the search element is not present in the strided array (descending order)', function test( t ) {
var actual;
var x;

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

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

t.end();
});

tape( 'the function returns the index of the first element in a sorted strided array which is greater than a provided search element when the search element is not present in the strided array (descending order) (accessors)', function test( t ) {
var actual;
var x;

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

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

t.end();
});

tape( 'the function returns `-1` if every indexed element in the array is less than or equal to the search element (descending order)', function test( t ) {
var actual;
var x;

x = [ 3.0, 2.0, 1.0 ];

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

t.end();
});

tape( 'the function returns `-1` if every indexed element in the array is less than or equal to the search element (descending order) (accessors)', function test( t ) {
var actual;
var x;

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

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

t.end();
});

tape( 'the function supports an `x` stride (descending order)', function test( t ) {
var actual;
var x;

x = [
3.0, // 0
9.0,
2.0, // 1
9.0,
1.0 // 2
];

actual = gindexOfGreaterThanSorted( 3, 2.0, x, 2 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});

tape( 'the function supports an `x` stride (descending order) (accessors)', function test( t ) {
var actual;
var x;

x = [
3.0, // 0
9.0,
2.0, // 1
9.0,
1.0 // 2
];

actual = gindexOfGreaterThanSorted( 3, 2.0, toAccessorArray( x ), 2 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});

tape( 'the function supports a negative `x` stride (descending order)', function test( t ) {
var actual;
var x;

x = [
1.0, // 2
9.0,
2.0, // 1
9.0,
3.0 // 0
];

actual = gindexOfGreaterThanSorted( 3, 2.0, x, -2 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});

tape( 'the function supports a negative `x` stride (descending order) (accessors)', function test( t ) {
var actual;
var x;

x = [
1.0, // 2
9.0,
2.0, // 1
9.0,
3.0 // 0
];

actual = gindexOfGreaterThanSorted( 3, 2.0, toAccessorArray( x ), -2 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});

tape( 'the function supports view offsets (descending order)', function test( t ) {
var actual;
var x0;
var x1;

x0 = new Float64Array( [ 9.0, 4.0, 3.0, 2.0, 1.0 ] );
x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 );

actual = gindexOfGreaterThanSorted( 3, 2.0, x1, 1 );
t.strictEqual( actual, 0, 'returns expected value' );

t.end();
});
Loading
Loading