MCPcopy Create free account
hub / github.com/arctic-cli/interface / search

Function search

packages/util/src/binary.ts:2–20  ·  view source on GitHub ↗
(array: T[], id: string, compare: (item: T) => string)

Source from the content-addressed store, hash-verified

1export 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)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected