-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArray.ts
More file actions
45 lines (42 loc) · 1.33 KB
/
Copy pathSortedArray.ts
File metadata and controls
45 lines (42 loc) · 1.33 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
/**
* SortedArray class for FlashData - Realtime Database Library
* https://github.com/phamngocduy98/node_flashdata_library
*/
export class SortedArray<D> extends Array<D> {
constructor() {
super();
}
insert(position: number, item: D) {
// shift-right 1 step from position
for (let j = this.length - 1; j >= position; j--) {
this[j + 1] = this[j];
}
this[position] = item;
}
findInsertIndex(item: D, range: [number, number]): number | null {
const [start, end] = range;
const center = start + Math.floor((end - start) / 2);
if (end < start) {
return start;
}
if (this[center] === item) {
return null;
} else if (this[center] < item) {
return this.findInsertIndex(item, [center + 1, end]);
} else {
return this.findInsertIndex(item, [start, center - 1]);
}
}
// TODO: use binary search for better performance
push(...items: D[]): number {
if (this.length === 0) {
super.push(items[0]);
items.splice(0, 1);
}
for (let item of items) {
const index = this.findInsertIndex(item, [0, this.length - 1]);
if (index != null) this.insert(index, item);
}
return this.length;
}
}