added PriorityQueue for later use
This commit is contained in:
parent
4f2b6e7c53
commit
28ec48cc85
69
src/lib/PriorityQueue.hx
Normal file
69
src/lib/PriorityQueue.hx
Normal file
@ -0,0 +1,69 @@
|
||||
package lib;
|
||||
|
||||
using Lambda;
|
||||
|
||||
abstract PriorityQueue<T>(Array<{prio:Float, val:T}>) {
|
||||
public inline function new() {
|
||||
this = [];
|
||||
}
|
||||
|
||||
public function containsElement(element:T):Bool {
|
||||
return this.exists((e) -> e.val == element);
|
||||
}
|
||||
|
||||
public function isEmpty():Bool {
|
||||
return this.length == 0;
|
||||
}
|
||||
|
||||
public function insert(e:T, prio:Float) {
|
||||
this.push({prio: prio, val: e});
|
||||
bubbleUp(this.length - 1);
|
||||
}
|
||||
|
||||
public function extractMin():Null<T> {
|
||||
if (this.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
swap(0, this.length - 1);
|
||||
var minElement = this.pop();
|
||||
|
||||
bubbleDown(0);
|
||||
|
||||
return minElement.val;
|
||||
}
|
||||
|
||||
private function bubbleUp(index:Int) {
|
||||
var parentIndex:Int = Math.floor((index - 1) / 2);
|
||||
|
||||
if (index > 0 && this[index].prio < this[parentIndex].prio) {
|
||||
swap(index, parentIndex);
|
||||
bubbleUp(parentIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private function bubbleDown(index:Int) {
|
||||
var leftIndex = 2 * index + 1;
|
||||
var rightIndex = 2 * index + 2;
|
||||
var smallest = index;
|
||||
|
||||
if (leftIndex < this.length && this[leftIndex].prio < this[smallest].prio) {
|
||||
smallest = leftIndex;
|
||||
}
|
||||
|
||||
if (rightIndex < this.length && this[rightIndex].prio < this[smallest].prio) {
|
||||
smallest = rightIndex;
|
||||
}
|
||||
|
||||
if (smallest != index) {
|
||||
swap(index, smallest);
|
||||
bubbleDown(smallest);
|
||||
}
|
||||
}
|
||||
|
||||
private function swap(i:Int, j:Int) {
|
||||
var tmp = this[i];
|
||||
this[i] = this[j];
|
||||
this[j] = tmp;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user