Add a new utility class
This commit is contained in:
parent
046fb7bc0c
commit
38aecdd021
1 changed files with 65 additions and 0 deletions
65
utils/javascript/SortedList.mjs
Normal file
65
utils/javascript/SortedList.mjs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
export class SortedList {
|
||||
|
||||
#data = [];
|
||||
|
||||
add(value) {
|
||||
|
||||
// Simple cases that we can short-circuit on
|
||||
if (this.#data.length === 0 || this.#data.at(-1) < value) {
|
||||
this.#data.push(value);
|
||||
return;
|
||||
};
|
||||
if (this.#data.at(0) > value) {
|
||||
this.#data.unshift(value);
|
||||
return
|
||||
};
|
||||
|
||||
let locationToAdd;
|
||||
let lStart = 0;
|
||||
let lEnd = this.#data.length;
|
||||
do {
|
||||
let midpoint = Math.floor((lStart + lEnd) / 2);
|
||||
|
||||
if (lStart > lEnd) {
|
||||
// console.log(`breaking for ${value} at lStart=${lStart}, lEnd=${lEnd}`)
|
||||
locationToAdd = lStart;
|
||||
break
|
||||
};
|
||||
|
||||
let valAtMidpoint =this.#data.at(midpoint);
|
||||
if (valAtMidpoint < value) {
|
||||
// console.log(`${valAtMidpoint} < ${value}, using second half of list`)
|
||||
lStart = midpoint + 1;
|
||||
}
|
||||
else if (valAtMidpoint > value) {
|
||||
// console.log(`${valAtMidpoint} > ${value}, using first half of list`)
|
||||
lEnd = midpoint - 1;
|
||||
}
|
||||
else {
|
||||
locationToAdd = midpoint;
|
||||
}
|
||||
} while (locationToAdd === undefined);
|
||||
|
||||
// console.log(`locationToAdd (${value}) = ${locationToAdd}`);
|
||||
this.#data.splice(locationToAdd, 0, value);
|
||||
};
|
||||
|
||||
get size() {
|
||||
return this.#data.length;
|
||||
}
|
||||
|
||||
at(index) {
|
||||
return this.#data.at(index);
|
||||
}
|
||||
|
||||
*iter() {
|
||||
// console.log(JSON.stringify(this.#data))
|
||||
const size = this.#data.length;
|
||||
for (var i = 0; i < size; i++) {
|
||||
// if (i === size - 1) {
|
||||
// yield this.#data[i]
|
||||
// }
|
||||
yield this.#data[i];
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue