forked from y-scope/yscope-log-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.ts
More file actions
70 lines (61 loc) · 1.84 KB
/
math.ts
File metadata and controls
70 lines (61 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.
*
* @param num The number to be clamped.
* @param min The lower boundary of the output range.
* @param max The upper boundary of the output range.
* @return The clamped number.
*/
const clamp = (num: number, min: number, max: number) => Math.min(Math.max(num, min), max);
/**
* Gets the chunk number that contains an item (assuming that item is in a collection and that
* collection is divided into chunks of a given size).
*
* @param itemNum
* @param chunkSize
* @return The chunk number.
*/
const getChunkNum =
(itemNum: number, chunkSize: number) => Math.max(1, Math.ceil(itemNum / chunkSize));
/**
* Finds the index of 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`, return
* the first index of the collection (i.e., `0`).
*
* @param get
* @param lowIdx
* @param highIdx
* @param upperboundValue
* @return
*/
const upperBoundBinarySearch = <T>(
get: (index: number) => T,
lowIdx: number,
highIdx: number,
upperboundValue: T,
): Nullable<number> => {
if (highIdx < lowIdx || "undefined" === typeof (get(highIdx))) {
return null;
}
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;
};
export {
clamp,
getChunkNum,
upperBoundBinarySearch,
};