adds frontend history
This commit is contained in:
@@ -2,3 +2,40 @@ export interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export class PointLine {
|
||||
data: Point[];
|
||||
|
||||
constructor() {
|
||||
this.data = [];
|
||||
}
|
||||
|
||||
find_index(x: number) {
|
||||
if (this.data.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// left should be too early to insert
|
||||
// right should be too late to insert
|
||||
let left = 0;
|
||||
let size = this.data.length;
|
||||
|
||||
while (size > 1) {
|
||||
const half = Math.floor(size / 2);
|
||||
const mid = left + half;
|
||||
|
||||
const is_less = this.data[mid].x < x;
|
||||
if (is_less) {
|
||||
left = mid;
|
||||
}
|
||||
size -= half;
|
||||
}
|
||||
|
||||
return left + (this.data[left].x < x ? 1 : 0);
|
||||
}
|
||||
|
||||
insert(point: Point) {
|
||||
const index = this.find_index(point.x);
|
||||
this.data.splice(index, 0, point);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user