Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created August 26, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/64580493308a6621bf132885b4434274 to your computer and use it in GitHub Desktop.
Save AxGord/64580493308a6621bf132885b4434274 to your computer and use it in GitHub Desktop.
class Test {
static function main() {
var a = new Priority();
a.add(3, 2);
a.add(1, 1);
trace(a.a);
}
}
class Priority {
public var a:Array<Int> = [];
var m:Map<Int, Int> = new Map();
public function new() {}
public function add(e:Int, priority:Int):Void {
if (a.indexOf(e) != -1) return;
var count:Int = 0;
var pos:Int = 0;
for (k in m.keys()) {
if (k < priority) pos += m[k];
if (k == priority) count = m[k];
}
a.insert(pos, e);
m[priority] = count + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment