mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-02 05:47:14 +02:00
fmt and lint
This commit is contained in:
+14
-21
@@ -28,16 +28,14 @@ abstract class BinHeap<T> {
|
||||
|
||||
/** Get the value of the root-most element of the heap, without changing the heap. */
|
||||
public peek(): T | undefined {
|
||||
if(this.data.length == 0)
|
||||
return undefined;
|
||||
if (this.data.length == 0) return undefined;
|
||||
|
||||
return this.data[0][1];
|
||||
}
|
||||
|
||||
/** Remove the root-most element of the heap and return the removed element's value. */
|
||||
public pop(): T | undefined {
|
||||
if(this.data.length == 0)
|
||||
return undefined;
|
||||
if (this.data.length == 0) return undefined;
|
||||
|
||||
const value = this.data[0][1];
|
||||
|
||||
@@ -52,9 +50,8 @@ abstract class BinHeap<T> {
|
||||
/** Change the weight of an element in the heap. */
|
||||
public changeWeight(predicate: (value: T) => boolean, weight: number): void {
|
||||
// Find first element with matching value, if any
|
||||
const i = this.data.findIndex(e => predicate(e[1]));
|
||||
if(i == -1)
|
||||
return;
|
||||
const i = this.data.findIndex((e) => predicate(e[1]));
|
||||
if (i == -1) return;
|
||||
|
||||
// Update that element's weight
|
||||
this.data[i][0] = weight;
|
||||
@@ -62,22 +59,22 @@ abstract class BinHeap<T> {
|
||||
// And re-heapify if needed
|
||||
const p = Math.floor((i - 1) / 2);
|
||||
|
||||
if(!this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) // Needs to shift root-wards?
|
||||
if (!this.heapOrderABeforeB(this.data[p][0], this.data[i][0]))
|
||||
// Needs to shift root-wards?
|
||||
this.heapifyUp(i);
|
||||
else // Try shifting deeper
|
||||
this.heapifyDown(i);
|
||||
// Try shifting deeper
|
||||
else this.heapifyDown(i);
|
||||
}
|
||||
|
||||
/** Restore heap condition, starting at index i and traveling towards root. */
|
||||
protected heapifyUp(i: number): void {
|
||||
// Swap the new element up towards root until it reaches root position or
|
||||
// settles under under a suitable parent
|
||||
while(i > 0) {
|
||||
while (i > 0) {
|
||||
const p = Math.floor((i - 1) / 2);
|
||||
|
||||
// Reached heap-ordered state already?
|
||||
if(this.heapOrderABeforeB(this.data[p][0], this.data[i][0]))
|
||||
break;
|
||||
if (this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) break;
|
||||
|
||||
// Swap
|
||||
const tmp = this.data[p];
|
||||
@@ -93,20 +90,17 @@ abstract class BinHeap<T> {
|
||||
protected heapifyDown(i: number): void {
|
||||
// Swap the shifted element down in the heap until it either reaches the
|
||||
// bottom layer or is in correct order relative to it's children
|
||||
while(i < this.data.length) {
|
||||
while (i < this.data.length) {
|
||||
const l = i * 2 + 1;
|
||||
const r = i * 2 + 2;
|
||||
let toSwap = i;
|
||||
|
||||
// Find which one of element i and it's children should be closest to root
|
||||
if(l < this.data.length && this.heapOrderABeforeB(this.data[l][0], this.data[toSwap][0]))
|
||||
toSwap = l;
|
||||
if(r < this.data.length && this.heapOrderABeforeB(this.data[r][0], this.data[toSwap][0]))
|
||||
toSwap = r;
|
||||
if (l < this.data.length && this.heapOrderABeforeB(this.data[l][0], this.data[toSwap][0])) toSwap = l;
|
||||
if (r < this.data.length && this.heapOrderABeforeB(this.data[r][0], this.data[toSwap][0])) toSwap = r;
|
||||
|
||||
// Already in order?
|
||||
if(i == toSwap)
|
||||
break;
|
||||
if (i == toSwap) break;
|
||||
|
||||
// Not in order. Swap child that should be closest to root up to 'i' and repeat
|
||||
const tmp = this.data[toSwap];
|
||||
@@ -124,7 +118,6 @@ abstract class BinHeap<T> {
|
||||
protected abstract heapOrderABeforeB(weightA: number, weightB: number): boolean;
|
||||
}
|
||||
|
||||
|
||||
/** Binary max-heap. */
|
||||
export class MaxHeap<T> extends BinHeap<T> {
|
||||
heapOrderABeforeB(weightA: number, weightB: number): boolean {
|
||||
|
||||
Reference in New Issue
Block a user