Skip to content

Commit

Permalink
💥 BREAKING CHANGE: Rename issorted -> isSorted and return boolean.
Browse files Browse the repository at this point in the history
Fixes #67. Use `firstInversion` to get old behaviour.
  • Loading branch information
make-github-pseudonymous-again committed Sep 20, 2020
1 parent 80f257e commit 036aa5a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 30 deletions.
23 changes: 23 additions & 0 deletions src/utils/firstInversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/**
* Returns k <= right such that [left,k[ is sorted. If k < right, then
* compare( array[k-1] , array[k] ) > 0.
*/

export function firstInversion ( compare , array , left , right ) {

if ( left >= right ) return right ;

while ( ++left < right ) {

if ( compare( array[left-1] , array[left] ) > 0 ) {

break ;

}

}

return left ;

}
3 changes: 2 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './issorted' ;
export * from './firstInversion' ;
export * from './isSorted' ;
export * from './whole' ;
8 changes: 8 additions & 0 deletions src/utils/isSorted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {firstInversion} from './firstInversion';

/**
* Returns whether range [left,right[ of array is sorted.
*/
export function isSorted ( compare , array , left , right ) {
return firstInversion(compare, array, left, right) === right;
}
23 changes: 0 additions & 23 deletions src/utils/issorted.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/src/issorted.js → test/src/firstInversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import test from 'ava' ;

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

import { issorted } from '../../src' ;
import { firstInversion } from '../../src' ;

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

const n = array.length ;

t.is( issorted( increasing , array , left , right ) , k1 ) ;
t.is( issorted( decreasing , array , left , right ) , k2 ) ;
t.is( k1, firstInversion( increasing , array , left , right ) ) ;
t.is( k2, firstInversion( decreasing , array , left , right ) ) ;

t.is( array.length , n ) ;
t.is( n, array.length ) ;

}

Expand Down
26 changes: 26 additions & 0 deletions test/src/isSorted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava' ;

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

import { isSorted } from '../../src' ;

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

const n = array.length ;

t.is( k1, isSorted( increasing , array , left , right ) ) ;
t.is( k2, isSorted( decreasing , array , left , right ) ) ;

t.is( n, array.length ) ;

}

macro.title = ( _ , ...args ) => args.join(' , ') ;

test( macro , [ ] , 0 , 0 , true , true ) ;
test( macro , [ 0 , 1 , 2 ] , 1 , 1 , true , true ) ;
test( macro , [ 1 , 1 , 1 ] , 0 , 3 , true , true ) ;
test( macro , [ 1 , 2 , 3 ] , 0 , 3 , true , false ) ;
test( macro , [ 1 , 2 , 4 , 3 ] , 0 , 4 , false , false ) ;
test( macro , [ 1 , 0 , 1 , 1 , 2 , 3 , 1 , 0 , 1 ] , 3 , 6 , true , false ) ;
test( macro , [ 1 , 0 , 1 , 1 , 2 , 3 , 1 , 0 , 1 ] , 0 , 9 , false , false ) ;
4 changes: 2 additions & 2 deletions test/src/whole.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function check ( sortname, arraysort, ctor, n, comparename, compare ) {
shuffle( a, 0, n );
arraysort( compare, a );

t.is( sort.issorted( compare , a , 0 , n ) , n , "check sorted" );
t.is( a.length, n, "check length a" );
t.true( sort.isSorted( compare , a , 0 , n ) , "check sorted" );
t.is( n, a.length, "check length a" );

} );
}
Expand Down

0 comments on commit 036aa5a

Please sign in to comment.