mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
rewrite binarySearch so it doesn't have to compare values of the same type.
This commit is contained in:
@@ -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])
|
||||||
})
|
})
|
||||||
|
|||||||
20
utils.ts
20
utils.ts
@@ -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]
|
||||||
|
|||||||
Reference in New Issue
Block a user