-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterpolationSearch.ts
More file actions
52 lines (46 loc) · 1.61 KB
/
InterpolationSearch.ts
File metadata and controls
52 lines (46 loc) · 1.61 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
import { ToDoTask } from "./types";
export const interpolationSearch = (
arr: ToDoTask[],
searchDate: Date
): ToDoTask[] => {
if (searchDate) {
let left = 0;
let right = arr.length - 1;
searchDate.setHours(23);
// Continue searching while the left index is less than or equal to the right index
while (left <= right) {
const interpolationIndex = Math.floor(
left +
((right - left) /
(arr[right].dueDate.getTime() - arr[left].dueDate.getTime())) *
(searchDate.getTime() - arr[left].dueDate.getTime())
);
// Check if the interpolation index is out of bounds
if (interpolationIndex < 0 || interpolationIndex >= arr.length) {
return [];
}
// Compare the element at the interpolation index with the search value
const currentItem = arr[interpolationIndex];
const itemDate = new Date(currentItem.dueDate);
if (
itemDate.getMonth() === searchDate.getMonth() &&
itemDate.getDate() === searchDate.getDate()
) {
// Use Array.prototype.filter to filter elements that match the search date
return arr.filter((item) => {
const itemDate = new Date(item.dueDate);
return (
itemDate.getMonth() === searchDate.getMonth() &&
itemDate.getDate() === searchDate.getDate()
);
});
} else if (itemDate.getTime() < searchDate.getTime()) {
left = interpolationIndex + 1;
} else {
right = interpolationIndex - 1;
}
}
}
// If the search value is not found, return an empty array
return [];
};