diff --git a/src/utils/firstInversion.js b/src/utils/firstInversion.js new file mode 100644 index 0000000..2b68a12 --- /dev/null +++ b/src/utils/firstInversion.js @@ -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 ; + +} diff --git a/src/utils/index.js b/src/utils/index.js index 5265a41..3cf77a8 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,2 +1,3 @@ -export * from './issorted' ; +export * from './firstInversion' ; +export * from './isSorted' ; export * from './whole' ; diff --git a/src/utils/isSorted.js b/src/utils/isSorted.js new file mode 100644 index 0000000..fc817a9 --- /dev/null +++ b/src/utils/isSorted.js @@ -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; +} diff --git a/src/utils/issorted.js b/src/utils/issorted.js deleted file mode 100644 index 3b15780..0000000 --- a/src/utils/issorted.js +++ /dev/null @@ -1,23 +0,0 @@ - -/** - * Checks whether range [left,right[ of array is sorted. Returns k <= right - * such that [left,k[ is sorted. - */ - -export function issorted ( compare , array , left , right ) { - - if ( left >= right ) return right ; - - while ( ++left < right ) { - - if ( compare( array[left-1] , array[left] ) > 0 ) { - - break ; - - } - - } - - return left ; - -} diff --git a/test/src/issorted.js b/test/src/firstInversion.js similarity index 74% rename from test/src/issorted.js rename to test/src/firstInversion.js index 01f592f..145aae5 100644 --- a/test/src/issorted.js +++ b/test/src/firstInversion.js @@ -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 ) ; } diff --git a/test/src/isSorted.js b/test/src/isSorted.js new file mode 100644 index 0000000..1706559 --- /dev/null +++ b/test/src/isSorted.js @@ -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 ) ; diff --git a/test/src/whole.js b/test/src/whole.js index fda6953..dac17ca 100644 --- a/test/src/whole.js +++ b/test/src/whole.js @@ -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" ); } ); }