Skip to content

Commit 036aa5a

Browse files
committedSep 20, 2020
💥 BREAKING CHANGE: Rename issorted -> isSorted and return boolean.
Fixes #67. Use `firstInversion` to get old behaviour.
1 parent 80f257e commit 036aa5a

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed
 

‎src/utils/firstInversion.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/**
3+
* Returns k <= right such that [left,k[ is sorted. If k < right, then
4+
* compare( array[k-1] , array[k] ) > 0.
5+
*/
6+
7+
export function firstInversion ( compare , array , left , right ) {
8+
9+
if ( left >= right ) return right ;
10+
11+
while ( ++left < right ) {
12+
13+
if ( compare( array[left-1] , array[left] ) > 0 ) {
14+
15+
break ;
16+
17+
}
18+
19+
}
20+
21+
return left ;
22+
23+
}

‎src/utils/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './issorted' ;
1+
export * from './firstInversion' ;
2+
export * from './isSorted' ;
23
export * from './whole' ;

‎src/utils/isSorted.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {firstInversion} from './firstInversion';
2+
3+
/**
4+
* Returns whether range [left,right[ of array is sorted.
5+
*/
6+
export function isSorted ( compare , array , left , right ) {
7+
return firstInversion(compare, array, left, right) === right;
8+
}

‎src/utils/issorted.js

-23
This file was deleted.

‎test/src/issorted.js renamed to ‎test/src/firstInversion.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import test from 'ava' ;
22

33
import { increasing , decreasing } from "@aureooms/js-compare" ;
44

5-
import { issorted } from '../../src' ;
5+
import { firstInversion } from '../../src' ;
66

77
function macro ( t , array , left , right , k1 , k2 ) {
88

99
const n = array.length ;
1010

11-
t.is( issorted( increasing , array , left , right ) , k1 ) ;
12-
t.is( issorted( decreasing , array , left , right ) , k2 ) ;
11+
t.is( k1, firstInversion( increasing , array , left , right ) ) ;
12+
t.is( k2, firstInversion( decreasing , array , left , right ) ) ;
1313

14-
t.is( array.length , n ) ;
14+
t.is( n, array.length ) ;
1515

1616
}
1717

‎test/src/isSorted.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava' ;
2+
3+
import { increasing , decreasing } from "@aureooms/js-compare" ;
4+
5+
import { isSorted } from '../../src' ;
6+
7+
function macro ( t , array , left , right , k1 , k2 ) {
8+
9+
const n = array.length ;
10+
11+
t.is( k1, isSorted( increasing , array , left , right ) ) ;
12+
t.is( k2, isSorted( decreasing , array , left , right ) ) ;
13+
14+
t.is( n, array.length ) ;
15+
16+
}
17+
18+
macro.title = ( _ , ...args ) => args.join(' , ') ;
19+
20+
test( macro , [ ] , 0 , 0 , true , true ) ;
21+
test( macro , [ 0 , 1 , 2 ] , 1 , 1 , true , true ) ;
22+
test( macro , [ 1 , 1 , 1 ] , 0 , 3 , true , true ) ;
23+
test( macro , [ 1 , 2 , 3 ] , 0 , 3 , true , false ) ;
24+
test( macro , [ 1 , 2 , 4 , 3 ] , 0 , 4 , false , false ) ;
25+
test( macro , [ 1 , 0 , 1 , 1 , 2 , 3 , 1 , 0 , 1 ] , 3 , 6 , true , false ) ;
26+
test( macro , [ 1 , 0 , 1 , 1 , 2 , 3 , 1 , 0 , 1 ] , 0 , 9 , false , false ) ;

‎test/src/whole.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function check ( sortname, arraysort, ctor, n, comparename, compare ) {
2525
shuffle( a, 0, n );
2626
arraysort( compare, a );
2727

28-
t.is( sort.issorted( compare , a , 0 , n ) , n , "check sorted" );
29-
t.is( a.length, n, "check length a" );
28+
t.true( sort.isSorted( compare , a , 0 , n ) , "check sorted" );
29+
t.is( n, a.length, "check length a" );
3030

3131
} );
3232
}

0 commit comments

Comments
 (0)
Please sign in to comment.