Browse Source

Added a non-allocating pair of iterators for UnsafeStringMap as well

SmerkyG 10 years ago
parent
commit
cd6b0eef52
1 changed files with 75 additions and 0 deletions
  1. 75 0
      std/flash/_std/haxe/ds/UnsafeStringMap.hx

+ 75 - 0
std/flash/_std/haxe/ds/UnsafeStringMap.hx

@@ -52,6 +52,10 @@ class UnsafeStringMap<T> implements haxe.Constraints.IMap<String,T> {
 		return true;
 		return true;
 	}
 	}
 
 
+	#if as3
+
+	// unoptimized version
+	
 	public function keys() : Iterator<String> {
 	public function keys() : Iterator<String> {
 		return untyped (__keys__(h)).iterator();
 		return untyped (__keys__(h)).iterator();
 	}
 	}
@@ -65,6 +69,18 @@ class UnsafeStringMap<T> implements haxe.Constraints.IMap<String,T> {
 		};
 		};
 	}
 	}
 
 
+	#else
+
+	public inline function keys() : Iterator<String> {
+		return new UnsafeStringMapKeysIterator(h);
+	}
+
+	public inline function iterator() : Iterator<T> {
+		return new UnsafeStringMapValuesIterator<T>(h);
+	}
+
+	#end
+
 	public function toString() : String {
 	public function toString() : String {
 		var s = new StringBuf();
 		var s = new StringBuf();
 		s.add("{");
 		s.add("{");
@@ -81,3 +97,62 @@ class UnsafeStringMap<T> implements haxe.Constraints.IMap<String,T> {
 	}
 	}
 
 
 }
 }
+
+#if !as3
+
+// this version uses __has_next__/__forin__ special SWF opcodes for iteration with no allocation
+
+@:allow(haxe.ds.UnsafeStringMap)
+private class UnsafeStringMapKeysIterator {
+	var h:flash.utils.Dictionary;
+	var index : Int;
+	var nextIndex : Int;
+
+	inline function new(h:flash.utils.Dictionary):Void {
+		this.h = h;
+		this.index = 0;
+		hasNext();
+	}
+
+	public inline function hasNext():Bool {
+		var h = h, index = index; // tmp vars required for __has_next
+		var n = untyped __has_next__(h, index);
+		this.nextIndex = index; // store next index
+		return n;
+	}
+
+	public inline function next():String {
+		var r : String = untyped __forin__(h, nextIndex);
+		index = nextIndex;
+		return r;
+	}
+
+}
+
+@:allow(haxe.ds.UnsafeStringMap)
+private class UnsafeStringMapValuesIterator<T> {
+	var h:flash.utils.Dictionary;
+	var index : Int;
+	var nextIndex : Int;
+
+	inline function new(h:flash.utils.Dictionary):Void {
+		this.h = h;
+		this.index = 0;
+		hasNext();
+	}
+
+	public inline function hasNext():Bool {
+		var h = h, index = index; // tmp vars required for __has_next
+		var n = untyped __has_next__(h, index);
+		this.nextIndex = index; // store next index
+		return n;
+	}
+
+	public inline function next():T {
+		var r = untyped __foreach__(h, nextIndex);
+		index = nextIndex;
+		return r;
+	}
+
+}
+#end