浏览代码

[lua] speed up int/string maps

Justin Donaldson 8 年之前
父节点
当前提交
478588c4c5
共有 2 个文件被更改,包括 53 次插入34 次删除
  1. 25 11
      std/lua/_std/haxe/ds/IntMap.hx
  2. 28 23
      std/lua/_std/haxe/ds/StringMap.hx

+ 25 - 11
std/lua/_std/haxe/ds/IntMap.hx

@@ -24,38 +24,52 @@ import lua.Lua;
 
 class IntMap<T> implements haxe.Constraints.IMap<Int,T> {
 
-	private var h : Dynamic;
+	private var h : lua.Table<Int,T>;
+	static var tnull : Dynamic = lua.Table.create();
 
 	public inline function new() : Void {
-		h = {};
+		h = lua.Table.create();
 	}
 
 	public inline function set( key : Int, value : T ) : Void {
-		 h[key] = value;
+		if (value == null){
+			h[key] = tnull;
+		} else {
+			h[key] = value;
+		}
 	}
 
 	public inline function get( key : Int ) : Null<T> {
-		return h[key];
+		var ret = h[key];
+		if (ret == tnull){
+			return null;
+		} else {
+			return ret;
+		}
 	}
 
 	public inline function exists( key : Int ) : Bool {
-		return Reflect.hasField(h,cast key);
+		return Lua.rawget(h,key) != null;
 	}
 
 	public function remove( key : Int ) : Bool {
-		if (!Reflect.hasField(h,cast key)) return false;
-		Reflect.deleteField(h, cast key);
-		return true;
+		if (Lua.rawget(h,key) == null){
+			return false;
+		} else {
+			Lua.rawset(h,key,null);
+			return true;
+		}
 	}
 
 	public function keys() : Iterator<Int> {
-		var cur = Reflect.fields(h).iterator();
+		var cur = Lua.next(h,null).index;
 		return {
 			next : function() {
-				var ret = cur.next();
+				var ret = cur;
+				cur = Lua.next(h,cur).index;
 				return cast ret;
 			},
-			hasNext : function() return cur.hasNext()
+			hasNext : function() return cur != null 
 		}
 	}
 

+ 28 - 23
std/lua/_std/haxe/ds/StringMap.hx

@@ -24,55 +24,60 @@ import lua.Lua;
 
 class StringMap<T> implements haxe.Constraints.IMap<String,T> {
 
-	private var k : Dynamic; // Is item exists
-	private var v : Dynamic; // Values table
+	private var h : lua.Table<String,T>;
+	static var tnull : Dynamic = lua.Table.create();
 
 	public inline function new() : Void {
-		// these need to be plain anonymous tables,
-		// so that we can set arbitrary string values.
-		v = untyped __lua__("{}");
-		k = untyped __lua__("{}");
+		h = lua.Table.create();
 	}
 
 	public inline function set( key : String, value : T ) : Void untyped {
-		untyped __lua__("{0}[{1}] = {2}", v, key, value);
-		untyped __lua__("{0}[{1}] = true", k, key);
+		if (value == null){
+			h[key] = tnull;
+		} else {
+			h[key] = value;
+		}
 	}
 
 	public inline function get( key : String ) : Null<T> untyped {
-		return untyped __lua__("{0}[{1}]", v, key);
+		var ret = h[key];
+		if (ret == tnull){
+			return null;
+		} else {
+			return ret;
+		}
 	}
 
 	public inline function exists( key : String ) : Bool untyped {
-		return untyped __lua__("({0}[{1}] or false)", k, key);
+		return Lua.rawget(h,key) != null;
 	}
 
 	public function remove( key : String ) : Bool untyped {
-		if (untyped __lua__("not {0}[{1}]", k, key)) return false;
-		untyped __lua__("{0}[{1}] = nil", v, key);
-		untyped __lua__("{0}[{1}] = nil", k, key);
-		return true;
+		if (Lua.rawget(h,key) == null){
+			return false;
+		} else {
+			Lua.rawset(h,key,null);
+			return true;
+		}
 	}
 
 	public function keys() : Iterator<String> {
-		var cur : Array<String> = [];
-		untyped __lua__("for _k,_v in pairs({1}) do
-			if(_v)then {0}:push(_k) end
-		end", cur, k);
+		var cur = Lua.next(h,null).index;
 		return {
 			next : function() {
-				var ret = cur.pop();
-				return ret;
+				var ret = cur;
+				cur = Lua.next(h,cur).index;
+				return cast ret;
 			},
-			hasNext : function() return cur.length > 0
+			hasNext : function() return cur != null 
 		}
 	}
 
 	public function iterator() : Iterator<T> {
 		var it = keys();
-		return  {
+		return untyped {
 			hasNext : function() return it.hasNext(),
-			next : function() return untyped __lua__("{0}[{1}]", v, it.next())
+			next : function() return h[it.next()]
 		};
 	}