(array: T[], id: string, compare: (item: T) => string)
| 1 | export namespace Binary { |
| 2 | export function search<T>(array: T[], id: string, compare: (item: T) => string): { found: boolean; index: number } { |
| 3 | let left = 0 |
| 4 | let right = array.length - 1 |
| 5 | |
| 6 | while (left <= right) { |
| 7 | const mid = Math.floor((left + right) / 2) |
| 8 | const midId = compare(array[mid]) |
| 9 | |
| 10 | if (midId === id) { |
| 11 | return { found: true, index: mid } |
| 12 | } else if (midId < id) { |
| 13 | left = mid + 1 |
| 14 | } else { |
| 15 | right = mid - 1 |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | return { found: false, index: left } |
| 20 | } |
| 21 | |
| 22 | export function insert<T>(array: T[], item: T, compare: (item: T) => string): T[] { |
| 23 | const id = compare(item) |
nothing calls this directly
no outgoing calls
no test coverage detected