rewrite binarySearch so it doesn't have to compare values of the same type.

This commit is contained in:
fiatjaf
2023-12-17 18:12:56 -03:00
parent 1ebe098805
commit 2a7fd83be8
2 changed files with 15 additions and 15 deletions

View File

@@ -257,9 +257,9 @@ describe('enqueue a message into MessageQueue', () => {
}) })
test('binary search', () => { test('binary search', () => {
expect(binarySearch(['a', 'b', 'd', 'e'], 'e', (a, b) => (a < b ? -1 : a === b ? 0 : 1))).toEqual([3, true]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('e' < b ? -1 : 'e' === b ? 0 : 1))).toEqual([3, true])
expect(binarySearch(['a', 'b', 'd', 'e'], 'x', (a, b) => (a < b ? -1 : a === b ? 0 : 1))).toEqual([4, false]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('x' < b ? -1 : 'x' === b ? 0 : 1))).toEqual([4, false])
expect(binarySearch(['a', 'b', 'd', 'e'], 'c', (a, b) => (a < b ? -1 : a === b ? 0 : 1))).toEqual([2, false]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('c' < b ? -1 : 'c' === b ? 0 : 1))).toEqual([2, false])
expect(binarySearch(['a', 'b', 'd', 'e'], 'a', (a, b) => (a < b ? -1 : a === b ? 0 : 1))).toEqual([0, true]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('a' < b ? -1 : 'a' === b ? 0 : 1))).toEqual([0, true])
expect(binarySearch(['a', 'b', 'd', 'e'], '[', (a, b) => (a < b ? -1 : a === b ? 0 : 1))).toEqual([0, false]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
}) })

View File

@@ -14,10 +14,10 @@ export function normalizeURL(url: string): string {
} }
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event) { export function insertEventIntoDescendingList(sortedArray: Event[], event: Event) {
const [idx, found] = binarySearch(sortedArray, event, (a, b) => { const [idx, found] = binarySearch(sortedArray, b => {
if (a.id === b.id) return 0 if (event.id === b.id) return 0
if (a.created_at === b.created_at) return -1 if (event.created_at === b.created_at) return -1
return b.created_at - a.created_at return b.created_at - event.created_at
}) })
if (!found) { if (!found) {
sortedArray.splice(idx, 0, event) sortedArray.splice(idx, 0, event)
@@ -26,10 +26,10 @@ export function insertEventIntoDescendingList(sortedArray: Event[], event: Event
} }
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event) { export function insertEventIntoAscendingList(sortedArray: Event[], event: Event) {
const [idx, found] = binarySearch(sortedArray, event, (a, b) => { const [idx, found] = binarySearch(sortedArray, b => {
if (a.id === b.id) return 0 if (event.id === b.id) return 0
if (a.created_at === b.created_at) return -1 if (event.created_at === b.created_at) return -1
return a.created_at - b.created_at return event.created_at - b.created_at
}) })
if (!found) { if (!found) {
sortedArray.splice(idx, 0, event) sortedArray.splice(idx, 0, event)
@@ -37,13 +37,13 @@ export function insertEventIntoAscendingList(sortedArray: Event[], event: Event)
return sortedArray return sortedArray
} }
export function binarySearch<T>(arr: T[], val: T, compare: (a: T, b: T) => number): [number, boolean] { export function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, boolean] {
let start = 0 let start = 0
let end = arr.length - 1 let end = arr.length - 1
while (start <= end) { while (start <= end) {
const mid = Math.floor((start + end) / 2) const mid = Math.floor((start + end) / 2)
const cmp = compare(val, arr[mid]) const cmp = compare(arr[mid])
if (cmp === 0) { if (cmp === 0) {
return [mid, true] return [mid, true]