mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Add missing file
This commit is contained in:
parent
a3f477f57a
commit
c0d5d12ec0
1 changed files with 32 additions and 0 deletions
32
packages/api/src/utils/interpolationSearch.ts
Normal file
32
packages/api/src/utils/interpolationSearch.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* Finds the index of the element which value is the closest to the target
|
||||
* @param arr - An array of numbers to search from
|
||||
* @param target - The target number to find
|
||||
* @returns The index of the closest to the target value array element
|
||||
*/
|
||||
|
||||
export function interpolationSearch(arr: number[], target: number): number {
|
||||
let left = 0
|
||||
let right = arr.length - 1
|
||||
while (left < right) {
|
||||
const rangeDelta = arr[right] - arr[left]
|
||||
const indexDelta = right - left
|
||||
const valueDelta = target - arr[left]
|
||||
if (valueDelta < 0) {
|
||||
throw new Error('Unable to find text node')
|
||||
}
|
||||
if (!rangeDelta) {
|
||||
return left
|
||||
}
|
||||
const middleIndex =
|
||||
left + Math.floor((valueDelta * indexDelta) / rangeDelta)
|
||||
if (target < arr[middleIndex]) {
|
||||
right = middleIndex
|
||||
} else if (target >= arr[middleIndex + 1]) {
|
||||
left = middleIndex + 1
|
||||
} else {
|
||||
return middleIndex
|
||||
}
|
||||
}
|
||||
throw new Error('Unable to find text node')
|
||||
}
|
||||
Loading…
Reference in a new issue