Skip to content

Commit f468ab7

Browse files
Sachinn-64kgryte
andauthored
refactor: add support for descending order
PR-URL: #15681 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 5d3c88e commit f468ab7

8 files changed

Lines changed: 377 additions & 7 deletions

File tree

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ var x = [ 1.0, 2.0, 2.0, 3.0 ];
4949

5050
var idx = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
5151
// returns 3
52+
53+
x = [ 3.0, 2.0, 2.0, 1.0 ];
54+
55+
idx = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
56+
// returns 0
5257
```
5358

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

127132
## Notes
128133

129-
- The input strided array **must** be sorted in ascending order.
134+
- The input strided array must be sorted in either **strictly** ascending or descending order.
130135
- If `N <= 0`, both functions return `-1`.
131136
- 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.
132137
- 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]).

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/docs/repl.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
1111

12-
The input strided array must be sorted in ascending order.
12+
The input strided array must be sorted in either strictly ascending or
13+
descending order.
1314

1415
If unable to find an element which is greater than the search element, the
1516
function returns `-1`.
@@ -41,6 +42,9 @@
4142
> var x = [ 1.0, 2.0, 2.0, 3.0 ];
4243
> var idx = {{alias}}( x.length, 2.0, x, 1 )
4344
3
45+
> x = [ 3.0, 2.0, 2.0, 1.0 ];
46+
> idx = {{alias}}( x.length, 2.0, x, 1 )
47+
0
4448

4549
// Using `N` and stride parameters:
4650
> x = [ 1.0, 9.0, 2.0, 9.0, 3.0, 9.0 ];

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/docs/types/index.d.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ interface Routine {
3636
*
3737
* ## Notes
3838
*
39-
* - The input strided array must be sorted in ascending order.
4039
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
4140
*
4241
* @param N - number of indexed elements
@@ -58,7 +57,6 @@ interface Routine {
5857
*
5958
* ## Notes
6059
*
61-
* - The input strided array must be sorted in ascending order.
6260
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
6361
*
6462
* @param N - number of indexed elements
@@ -82,7 +80,6 @@ interface Routine {
8280
*
8381
* ## Notes
8482
*
85-
* - The input strided array must be sorted in ascending order.
8683
* - If the function is unable to find an element which is greater than a search element, the function returns `-1`.
8784
*
8885
* @param N - number of indexed elements

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/accessors.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,26 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
5353
var mid;
5454
var lo;
5555
var hi;
56+
var v1;
57+
var v2;
5658

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

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

65+
v1 = get( xbuf, offsetX );
66+
v2 = get( xbuf, offsetX + ( ( N - 1 ) * strideX ) );
67+
68+
// Descending order: elements which are greater than the search element form a prefix, so only the first indexed element needs to be checked...
69+
if ( v1 >= v2 ) {
70+
if ( v1 > searchElement ) {
71+
return 0;
72+
}
73+
return -1;
74+
}
75+
// Ascending order: perform a binary search for the first element which is greater than the search element...
6376
lo = 0;
6477
hi = N;
6578
while ( lo < hi ) {

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/main.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ var ndarray = require( './ndarray.js' );
3131
*
3232
* ## Notes
3333
*
34-
* - The input strided array must be sorted in ascending order.
3534
* - If unable to find an element which is greater than the search element, the function returns `-1`.
3635
*
3736
* @param {PositiveInteger} N - number of indexed elements

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/lib/ndarray.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ var accessors = require( './accessors.js' );
3232
*
3333
* ## Notes
3434
*
35-
* - The input strided array must be sorted in ascending order.
3635
* - If unable to find an element which is greater than the search element, the function returns `-1`.
3736
*
3837
* @param {PositiveInteger} N - number of indexed elements
@@ -52,6 +51,8 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
5251
var mid;
5352
var lo;
5453
var hi;
54+
var v1;
55+
var v2;
5556
var o;
5657

5758
if ( N <= 0 ) {
@@ -61,6 +62,17 @@ function gindexOfGreaterThanSorted( N, searchElement, x, strideX, offsetX ) {
6162
if ( o.accessorProtocol ) {
6263
return accessors( N, searchElement, o, strideX, offsetX );
6364
}
65+
v1 = x[ offsetX ];
66+
v2 = x[ offsetX + ( ( N - 1 ) * strideX ) ];
67+
68+
// Descending order: elements which are greater than the search element form a prefix, so only the first indexed element needs to be checked...
69+
if ( v1 >= v2 ) {
70+
if ( v1 > searchElement ) {
71+
return 0;
72+
}
73+
return -1;
74+
}
75+
// Ascending order: perform a binary search for the first element which is greater than the search element...
6476
lo = 0;
6577
hi = N;
6678
while ( lo < hi ) {

‎lib/node_modules/@stdlib/blas/ext/base/gindex-of-greater-than-sorted/test/test.main.js‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,161 @@ tape( 'the function supports view offsets', function test( t ) {
310310

311311
t.end();
312312
});
313+
314+
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 ) {
315+
var actual;
316+
var x;
317+
318+
x = [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ];
319+
320+
actual = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
321+
t.strictEqual( actual, 0, 'returns expected value' );
322+
323+
t.end();
324+
});
325+
326+
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 ) {
327+
var actual;
328+
var x;
329+
330+
x = toAccessorArray( [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ] );
331+
332+
actual = gindexOfGreaterThanSorted( x.length, 2.0, x, 1 );
333+
t.strictEqual( actual, 0, 'returns expected value' );
334+
335+
t.end();
336+
});
337+
338+
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 ) {
339+
var actual;
340+
var x;
341+
342+
x = [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ];
343+
344+
actual = gindexOfGreaterThanSorted( x.length, 2.5, x, 1 );
345+
t.strictEqual( actual, 0, 'returns expected value' );
346+
347+
t.end();
348+
});
349+
350+
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 ) {
351+
var actual;
352+
var x;
353+
354+
x = toAccessorArray( [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ] );
355+
356+
actual = gindexOfGreaterThanSorted( x.length, 2.5, x, 1 );
357+
t.strictEqual( actual, 0, 'returns expected value' );
358+
359+
t.end();
360+
});
361+
362+
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 ) {
363+
var actual;
364+
var x;
365+
366+
x = [ 3.0, 2.0, 1.0 ];
367+
368+
actual = gindexOfGreaterThanSorted( x.length, 3.0, x, 1 );
369+
t.strictEqual( actual, -1, 'returns expected value' );
370+
371+
t.end();
372+
});
373+
374+
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 ) {
375+
var actual;
376+
var x;
377+
378+
x = toAccessorArray( [ 3.0, 2.0, 1.0 ] );
379+
380+
actual = gindexOfGreaterThanSorted( x.length, 3.0, x, 1 );
381+
t.strictEqual( actual, -1, 'returns expected value' );
382+
383+
t.end();
384+
});
385+
386+
tape( 'the function supports an `x` stride (descending order)', function test( t ) {
387+
var actual;
388+
var x;
389+
390+
x = [
391+
3.0, // 0
392+
9.0,
393+
2.0, // 1
394+
9.0,
395+
1.0 // 2
396+
];
397+
398+
actual = gindexOfGreaterThanSorted( 3, 2.0, x, 2 );
399+
t.strictEqual( actual, 0, 'returns expected value' );
400+
401+
t.end();
402+
});
403+
404+
tape( 'the function supports an `x` stride (descending order) (accessors)', function test( t ) {
405+
var actual;
406+
var x;
407+
408+
x = [
409+
3.0, // 0
410+
9.0,
411+
2.0, // 1
412+
9.0,
413+
1.0 // 2
414+
];
415+
416+
actual = gindexOfGreaterThanSorted( 3, 2.0, toAccessorArray( x ), 2 );
417+
t.strictEqual( actual, 0, 'returns expected value' );
418+
419+
t.end();
420+
});
421+
422+
tape( 'the function supports a negative `x` stride (descending order)', function test( t ) {
423+
var actual;
424+
var x;
425+
426+
x = [
427+
1.0, // 2
428+
9.0,
429+
2.0, // 1
430+
9.0,
431+
3.0 // 0
432+
];
433+
434+
actual = gindexOfGreaterThanSorted( 3, 2.0, x, -2 );
435+
t.strictEqual( actual, 0, 'returns expected value' );
436+
437+
t.end();
438+
});
439+
440+
tape( 'the function supports a negative `x` stride (descending order) (accessors)', function test( t ) {
441+
var actual;
442+
var x;
443+
444+
x = [
445+
1.0, // 2
446+
9.0,
447+
2.0, // 1
448+
9.0,
449+
3.0 // 0
450+
];
451+
452+
actual = gindexOfGreaterThanSorted( 3, 2.0, toAccessorArray( x ), -2 );
453+
t.strictEqual( actual, 0, 'returns expected value' );
454+
455+
t.end();
456+
});
457+
458+
tape( 'the function supports view offsets (descending order)', function test( t ) {
459+
var actual;
460+
var x0;
461+
var x1;
462+
463+
x0 = new Float64Array( [ 9.0, 4.0, 3.0, 2.0, 1.0 ] );
464+
x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
465+
466+
actual = gindexOfGreaterThanSorted( 3, 2.0, x1, 1 );
467+
t.strictEqual( actual, 0, 'returns expected value' );
468+
469+
t.end();
470+
});

0 commit comments

Comments
 (0)