Created
August 26, 2018 14:10
-
-
Save AxGord/64580493308a6621bf132885b4434274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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