Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created August 26, 2018 14:11
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/188e6fbc931d0534135b20f5c7cb530d to your computer and use it in GitHub Desktop.
Save AxGord/188e6fbc931d0534135b20f5c7cb530d to your computer and use it in GitHub Desktop.
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