-
Notifications
You must be signed in to change notification settings - Fork 19
refactor: Extract upper-bound binary search from findNearestLogEventByTimestamp into a function (resolves #343).
#347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
b41b648
427e61f
0618c19
7461712
a9b520c
cf142d7
ae6ae6f
7b42016
625ac0e
616a856
5c5d23b
682d6b2
3296637
953468e
bc9742f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| import {Nullable} from "../typings/common"; | ||
|
|
||
|
|
||
| /** | ||
| * Clamps a number to the given range. E.g. If the number is greater than the range's upper bound, | ||
| * the range's upper bound is returned. | ||
|
|
@@ -20,7 +23,49 @@ const clamp = (num: number, min: number, max: number) => Math.min(Math.max(num, | |
| const getChunkNum = | ||
| (itemNum: number, chunkSize: number) => Math.max(1, Math.ceil(itemNum / chunkSize)); | ||
|
|
||
| /** | ||
| * Finds the last element in a sorted collection that is less than or equal to `upperboundValue`. If | ||
| * all elements in the collection are greater than `upperboundValue`, returns the first index of the | ||
| * collection (i.e., `0`). | ||
| * | ||
| * @param get Function to access elements in the collection by index. | ||
| * @param lowIdx | ||
| * @param highIdx | ||
| * @param upperBoundValue | ||
Henry8192 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @return The index of the last element less than or equal to `upperboundValue`, `0` if all | ||
| * elements are greater, or `null` if the collection is empty or the indices are invalid. | ||
| */ | ||
| const upperBoundBinarySearch = <T>( | ||
| get: (index: number) => T, | ||
| lowIdx: number, | ||
| highIdx: number, | ||
| upperBoundValue: T, | ||
| ): Nullable<number> => { | ||
|
||
| if (highIdx < lowIdx || "undefined" === typeof (get(highIdx))) { | ||
Henry8192 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Henry8192 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return null; | ||
Henry8192 marked this conversation as resolved.
Show resolved
Hide resolved
Henry8192 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| while (lowIdx <= highIdx) { | ||
| const mid = Math.floor((lowIdx + highIdx) / 2); | ||
|
|
||
| // `mid` is guaranteed to be within bounds since `low <= high`. | ||
| if (get(mid) <= upperBoundValue) { | ||
| lowIdx = mid + 1; | ||
| } else { | ||
| highIdx = mid - 1; | ||
| } | ||
| } | ||
|
|
||
| // corner case: all values >= upperboundValue | ||
| if (0 > highIdx) { | ||
| return 0; | ||
| } | ||
|
|
||
| return highIdx; | ||
| }; | ||
Henry8192 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export { | ||
| clamp, | ||
| getChunkNum, | ||
| upperBoundBinarySearch, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.