mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-04 07:44:32 +00:00
utils: merging two (reverse) sorted lists of events.
This commit is contained in:
62
utils.ts
62
utils.ts
@@ -1,4 +1,4 @@
|
||||
import type { Event } from './core.ts'
|
||||
import type { NostrEvent } from './core.ts'
|
||||
|
||||
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
||||
export const utf8Encoder: TextEncoder = new TextEncoder()
|
||||
@@ -22,7 +22,7 @@ export function normalizeURL(url: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] {
|
||||
export function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
|
||||
const [idx, found] = binarySearch(sortedArray, b => {
|
||||
if (event.id === b.id) return 0
|
||||
if (event.created_at === b.created_at) return -1
|
||||
@@ -34,7 +34,7 @@ export function insertEventIntoDescendingList(sortedArray: Event[], event: Event
|
||||
return sortedArray
|
||||
}
|
||||
|
||||
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event): Event[] {
|
||||
export function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
|
||||
const [idx, found] = binarySearch(sortedArray, b => {
|
||||
if (event.id === b.id) return 0
|
||||
if (event.created_at === b.created_at) return -1
|
||||
@@ -68,6 +68,62 @@ export function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, b
|
||||
return [start, false]
|
||||
}
|
||||
|
||||
export function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {
|
||||
const result: NostrEvent[] = new Array(list1.length + list2.length)
|
||||
result.length = 0
|
||||
let i1 = 0
|
||||
let i2 = 0
|
||||
let sameTimestampIds: string[] = []
|
||||
|
||||
while (i1 < list1.length && i2 < list2.length) {
|
||||
let next: NostrEvent
|
||||
if (list1[i1]?.created_at > list2[i2]?.created_at) {
|
||||
next = list1[i1]
|
||||
i1++
|
||||
} else {
|
||||
next = list2[i2]
|
||||
i2++
|
||||
}
|
||||
|
||||
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||
if (sameTimestampIds.includes(next.id)) continue
|
||||
} else {
|
||||
sameTimestampIds.length = 0
|
||||
}
|
||||
|
||||
result.push(next)
|
||||
sameTimestampIds.push(next.id)
|
||||
}
|
||||
|
||||
while (i1 < list1.length) {
|
||||
const next = list1[i1]
|
||||
i1++
|
||||
|
||||
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||
if (sameTimestampIds.includes(next.id)) continue
|
||||
} else {
|
||||
sameTimestampIds.length = 0
|
||||
}
|
||||
result.push(next)
|
||||
sameTimestampIds.push(next.id)
|
||||
}
|
||||
|
||||
while (i2 < list2.length) {
|
||||
const next = list2[i2]
|
||||
i2++
|
||||
|
||||
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||
if (sameTimestampIds.includes(next.id)) continue
|
||||
} else {
|
||||
sameTimestampIds.length = 0
|
||||
}
|
||||
result.push(next)
|
||||
sameTimestampIds.push(next.id)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export class QueueNode<V> {
|
||||
public value: V
|
||||
public next: QueueNode<V> | null = null
|
||||
|
||||
Reference in New Issue
Block a user