Compare commits
2 Commits
2e0bda7a6e
...
28ec48cc85
| Author | SHA1 | Date | |
|---|---|---|---|
|
28ec48cc85
|
|||
|
4f2b6e7c53
|
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;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package lib.ui.elements;
|
||||
|
||||
class RootElement implements IUIElement {
|
||||
private var children:Array<IUIElement>;
|
||||
private var children:Array<IUIElement> = [];
|
||||
private final eventManager:UIEventManager = new UIEventManager();
|
||||
private var title:String = "";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user