(
list: T[],
item: T,
sort: Comparer<T>,
)
| 96 | |
| 97 | /// Find the insertion position in a sorted array. |
| 98 | export function findInsertPos<T>( |
| 99 | list: T[], |
| 100 | item: T, |
| 101 | sort: Comparer<T>, |
| 102 | ): number { |
| 103 | let low = 0, |
| 104 | high = list.length; |
| 105 | |
| 106 | // fast path: insert at end |
| 107 | if (high === 0 || sort(list[high - 1], item) <= 0) { |
| 108 | return high; |
| 109 | } |
| 110 | |
| 111 | while (low < high) { |
| 112 | const middle = (low + high) >>> 1; |
| 113 | if (sort(item, list[middle]) >= 0) { |
| 114 | low = middle + 1; |
| 115 | } else { |
| 116 | high = middle; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return low; |
| 121 | } |
no test coverage detected
searching dependent graphs…