summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src/utils/binary.ts
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-10-03 09:04:28 -0500
committerAdam <[email protected]>2025-10-03 09:04:28 -0500
commit3fa280d21878ae391674a21758199df3d2d8c3b5 (patch)
treef70c6ecafffeecc8e7a59dc9acef66c59a9ea54a /packages/desktop/src/utils/binary.ts
parent1d58b5548287a3e32ffce3abdcf0f2db08fdb155 (diff)
downloadopencode-3fa280d21878ae391674a21758199df3d2d8c3b5.tar.gz
opencode-3fa280d21878ae391674a21758199df3d2d8c3b5.zip
chore: app -> desktop
Diffstat (limited to 'packages/desktop/src/utils/binary.ts')
-rw-r--r--packages/desktop/src/utils/binary.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/desktop/src/utils/binary.ts b/packages/desktop/src/utils/binary.ts
new file mode 100644
index 000000000..3d8f61851
--- /dev/null
+++ b/packages/desktop/src/utils/binary.ts
@@ -0,0 +1,41 @@
+export namespace Binary {
+ export function search<T>(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<T>(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
+ }
+}