From 725104572e2b6d64dcfc145d4748124186427c7b Mon Sep 17 00:00:00 2001 From: Dax Date: Mon, 15 Sep 2025 03:28:08 -0400 Subject: feat: add desktop/web app package (#2606) Co-authored-by: adamdotdevin <2363879+adamdottv@users.noreply.github.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: GitHub Action --- packages/app/src/utils/binary.ts | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 packages/app/src/utils/binary.ts (limited to 'packages/app/src/utils/binary.ts') diff --git a/packages/app/src/utils/binary.ts b/packages/app/src/utils/binary.ts new file mode 100644 index 000000000..3d8f61851 --- /dev/null +++ b/packages/app/src/utils/binary.ts @@ -0,0 +1,41 @@ +export namespace Binary { + export function search(array: T[], id: string, compare: (item: T) => string): { found: boolean; index: number } { + let left = 0 + let right = array.length - 1 + + while (left <= right) { + const mid = Math.floor((left + right) / 2) + const midId = compare(array[mid]) + + if (midId === id) { + return { found: true, index: mid } + } else if (midId < id) { + left = mid + 1 + } else { + right = mid - 1 + } + } + + return { found: false, index: left } + } + + export function insert(array: T[], item: T, compare: (item: T) => string): T[] { + const id = compare(item) + let left = 0 + let right = array.length + + while (left < right) { + const mid = Math.floor((left + right) / 2) + const midId = compare(array[mid]) + + if (midId < id) { + left = mid + 1 + } else { + right = mid + } + } + + array.splice(left, 0, item) + return array + } +} -- cgit v1.2.3