-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 BREAKING CHANGE: Rename issorted -> isSorted and return boolean.
Fixes #67. Use `firstInversion` to get old behaviour.
- Loading branch information
1 parent
80f257e
commit 036aa5a
Showing
7 changed files
with
65 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters