-
Notifications
You must be signed in to change notification settings - Fork 16
new-log-viewer: Add bounds checking and search methods to support log filtering around a specific log event. #81
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e07805b
search pr
davemarco 8cbe208
Apply suggestions from code review
davemarco 0e8b80d
junhao review
davemarco da3f4ad
fix lint
davemarco 0cc5234
reorder functions
davemarco 55ce5bd
add extra line
davemarco 71a5626
Apply suggestions from code review
davemarco b3f890f
junhao review
davemarco a18d4c0
shorten doc
davemarco 5997721
change x to target
davemarco f9eb7f8
fix typo
davemarco 9507607
fix lint
davemarco 6f33822
Apply suggestions from code review
davemarco 6794146
kirk changes
davemarco f95d90d
attempt 2
davemarco 7ba6706
attempt 2
davemarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,100 @@ | ||||||||||||||||||
import {Nullable} from "../typings/common"; | ||||||||||||||||||
import {clamp} from "./math"; | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Checks if `target` is bounded by the first and last value in a sorted array of numbers. | ||||||||||||||||||
* | ||||||||||||||||||
* @param data An array sorted in ascending order. | ||||||||||||||||||
* @param target | ||||||||||||||||||
* @return Whether `target` is within the bounds of the array's values. | ||||||||||||||||||
*/ | ||||||||||||||||||
const isWithinBounds = (data: number[], target: number): boolean => { | ||||||||||||||||||
const {length} = data; | ||||||||||||||||||
if (0 === length) { | ||||||||||||||||||
return false; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return (target >= (data[0] as number)) && (target <= (data[length - 1] as number)); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Clamps a number using the first and last value in a sorted array of numbers. | ||||||||||||||||||
* | ||||||||||||||||||
* @param data An array sorted in ascending order. | ||||||||||||||||||
* @param num The number to be clamped. | ||||||||||||||||||
* @return The clamped number. | ||||||||||||||||||
*/ | ||||||||||||||||||
const clampWithinBounds = (data: number[], num: number): number => { | ||||||||||||||||||
const {length} = data; | ||||||||||||||||||
if (0 === length) { | ||||||||||||||||||
return num; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return clamp(num, data[0] as number, data[length - 1] as number); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Performs binary search to find the smallest index `i` in the range `[0, length)` where | ||||||||||||||||||
* `predicate` is true. `predicate` should be false for some prefix of the input range and true | ||||||||||||||||||
* for the remainder. | ||||||||||||||||||
* | ||||||||||||||||||
* @param length The length of the range to search. | ||||||||||||||||||
* @param predicate A function that takes an index and returns `true` or `false`. | ||||||||||||||||||
* @return The smallest index where `predicate(i)` is true, or `length` if no such index exists. | ||||||||||||||||||
* @example | ||||||||||||||||||
* const arr = [1, 3, 5, 7, 10, 15, 20]; | ||||||||||||||||||
* const result = binarySearch(arr.length, (i) => arr[i] >= 10); | ||||||||||||||||||
* console.log(result); // Output: 4 (since arr[4] is 10). | ||||||||||||||||||
*/ | ||||||||||||||||||
const binarySearch = (length: number, predicate: (index: number) => boolean): number => { | ||||||||||||||||||
// Generic implementation based on Go standard library implementation. | ||||||||||||||||||
// Reference: https://pkg.go.dev/sort#Search | ||||||||||||||||||
let i = 0; | ||||||||||||||||||
let j = length; | ||||||||||||||||||
while (i < j) { | ||||||||||||||||||
const mid = Math.floor((i + j) / 2); | ||||||||||||||||||
if (false === predicate(mid)) { | ||||||||||||||||||
i = mid + 1; | ||||||||||||||||||
} else { | ||||||||||||||||||
j = mid; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return i; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Finds the largest index `i` in a sorted array `data` such that `data[i] <= target`. Uses binary | ||||||||||||||||||
* search for efficiency. | ||||||||||||||||||
Comment on lines
+68
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see review comment |
||||||||||||||||||
* | ||||||||||||||||||
* @param data An array sorted in ascending order. | ||||||||||||||||||
* @param target | ||||||||||||||||||
* @return The largest index where `data[i] <= target` or: | ||||||||||||||||||
* - `length` if no such index exists. | ||||||||||||||||||
* - `null` if array is empty. | ||||||||||||||||||
* @example | ||||||||||||||||||
* const arr = [2, 3, 5, 7, 10, 15, 20]; | ||||||||||||||||||
* const result = findNearestLessThanOrEqualElement(arr, 8); | ||||||||||||||||||
* console.log(result); // Output: 3 (since arr[3] is 7). | ||||||||||||||||||
*/ | ||||||||||||||||||
const findNearestLessThanOrEqualElement = (data: number[], target: number): Nullable<number> => { | ||||||||||||||||||
const {length} = data; | ||||||||||||||||||
|
||||||||||||||||||
if (0 === length) { | ||||||||||||||||||
return null; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// Binary search to find the first index where data[i] > target. | ||||||||||||||||||
const firstGreaterIdx: number = binarySearch(length, (i) => data[i] as number > target); | ||||||||||||||||||
|
||||||||||||||||||
if (0 === firstGreaterIdx) { | ||||||||||||||||||
return length; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return firstGreaterIdx - 1; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Finds the key in a map based on the provided value. | ||||||||||||||||||
* | ||||||||||||||||||
|
@@ -38,7 +132,11 @@ const getMapValueWithNearestLessThanOrEqualKey = <T>( | |||||||||||||||||
map.get(lowerBoundKey) as T; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
export { | ||||||||||||||||||
clampWithinBounds, | ||||||||||||||||||
findNearestLessThanOrEqualElement, | ||||||||||||||||||
getMapKeyByValue, | ||||||||||||||||||
getMapValueWithNearestLessThanOrEqualKey, | ||||||||||||||||||
isWithinBounds, | ||||||||||||||||||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.