Created
August 26, 2018 14:11
-
-
Save AxGord/188e6fbc931d0534135b20f5c7cb530d 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 SmartArray([1, 2, 3]); | |
for (e in a) { | |
trace(e); | |
if (e == 2) a.remove(1); | |
} | |
} | |
} | |
abstract SmartArray({a: Array<Int>, it: SmartArrayIterator}) { | |
inline public function new(arr:Array<Int>) this = {a: arr, it: new SmartArrayIterator(arr)}; | |
inline public function iterator():SmartArrayIterator return this.it.reset(); | |
inline public function remove(x:Int):Bool { | |
var i = this.a.indexOf(x); | |
if (i == -1) return false; | |
if (i < this.it.i) this.it.i--; | |
this.a.splice(i, 1); | |
return true; | |
} | |
} | |
class SmartArrayIterator { | |
var a:Array<Int>; | |
public var i:Int = 0; | |
inline public function new(a:Array<Int>) this.a = a; | |
inline public function hasNext():Bool return i < a.length; | |
inline public function next():Int return a[i++]; | |
inline public function reset():SmartArrayIterator { | |
i = 0; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment