( sortedArray: Array<[T, any]>, value: T, compareFn: (a: T, b: T) => number, )
| 35 | * @returns The index where the value should be inserted to maintain order |
| 36 | */ |
| 37 | export function findInsertPosition<T>( |
| 38 | sortedArray: Array<[T, any]>, |
| 39 | value: T, |
| 40 | compareFn: (a: T, b: T) => number, |
| 41 | ): number { |
| 42 | let left = 0 |
| 43 | let right = sortedArray.length |
| 44 | |
| 45 | while (left < right) { |
| 46 | const mid = Math.floor((left + right) / 2) |
| 47 | const comparison = compareFn(sortedArray[mid]![0], value) |
| 48 | |
| 49 | if (comparison < 0) { |
| 50 | left = mid + 1 |
| 51 | } else { |
| 52 | right = mid |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return left |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Deletes a value from a sorted array while maintaining sort order |
nothing calls this directly
no outgoing calls
no test coverage detected