Skip to content

Commit

Permalink
Adapt inline IntMap iterator from HaxeFoundation#8806
Browse files Browse the repository at this point in the history
  • Loading branch information
basro committed Jul 1, 2020
1 parent 228d695 commit 2256a8b
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions std/js/_std/haxe/ds/IntMap.hx
Expand Up @@ -22,6 +22,28 @@

package haxe.ds;

private class IntMapIterator<T> {
var map:IntMap<T>;
var keys:Array<Int>;
var index:Int;
var count:Int;

public inline function new(map:IntMap<T>, keys:Array<Int>) {
this.map = map;
this.keys = keys;
this.index = 0;
this.count = keys.length;
}

public inline function hasNext() {
return index < count;
}

public inline function next() {
return map.get(keys[index++]);
}
}

@:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
private var h:Dynamic;

Expand Down Expand Up @@ -49,23 +71,17 @@ package haxe.ds;
}

public function keys():Iterator<Int> {
return arrayKeys().iterator();
}

inline function arrayKeys():Array<Int> {
var a = [];
js.Syntax.code("for( var key in {0} ) if({0}.hasOwnProperty(key)) {1}.push(key | 0)", h, a);
return a.iterator();
return a;
}

public function iterator():Iterator<T> {
return untyped {
ref: h,
it: keys(),
hasNext: function() {
return __this__.it.hasNext();
},
next: function() {
var i = __this__.it.next();
return __this__.ref[i];
}
};
public inline function iterator():Iterator<T> {
return new IntMapIterator(this, arrayKeys());
}

@:runtime public inline function keyValueIterator():KeyValueIterator<Int, T> {
Expand Down

0 comments on commit 2256a8b

Please sign in to comment.