瀏覽代碼

[java/cs] Added Vector implementation. Added a [commented] version of the rest of the API to all platforms + unit tests

Caue Waneck 12 年之前
父節點
當前提交
38e78a199f
共有 4 個文件被更改,包括 172 次插入10 次删除
  1. 2 0
      gencs.ml
  2. 8 4
      genjava.ml
  3. 101 4
      std/haxe/ds/Vector.hx
  4. 61 2
      tests/unit/unitstd/haxe/ds/Vector.unit.hx

+ 2 - 0
gencs.ml

@@ -574,6 +574,8 @@ let configure gen =
       | TAbstract ({ a_path = ["cs"],"Out" },_)
       | TType ({ t_path = [],"Single" },[])
       | TAbstract ({ a_path = [],"Single" },[]) -> Some t
+      | TAbstract ({ a_impl = Some _ } as a, pl) ->
+          Some (gen.gfollow#run_f ( Codegen.get_underlying_type a pl) )
       | TAbstract( { a_path = ([], "EnumValue") }, _  )
       | TInst( { cl_path = ([], "EnumValue") }, _  ) -> Some t_dynamic
       | _ -> None);

+ 8 - 4
genjava.ml

@@ -680,7 +680,7 @@ let configure gen =
             if has_tdynamic params then List.map (fun _ -> t_dynamic) params else
               List.map (fun t ->
                 let f_t = gen.gfollow#run_f t in
-                match gen.gfollow#run_f t with
+                match f_t  with
                   | TEnum ({ e_path = ([], "Bool") }, [])
                   | TAbstract ({ a_path = ([], "Bool") },[])
                   | TInst ({ cl_path = ([],"Float") },[])
@@ -698,7 +698,9 @@ let configure gen =
                   | TType ({ t_path = ["java"],"Char16" },[])
                   | TAbstract ({ a_path = ["java"],"Char16" },[])
                   | TType ({ t_path = [],"Single" },[])
-                  | TAbstract ({ a_path = [],"Single" },[]) -> basic.tnull f_t
+                  | TAbstract ({ a_path = [],"Single" },[]) ->
+                      t_dynamic
+                      (*basic.tnull f_t*)
                   (*| TType ({ t_path = [], "Null"*)
                   | TInst (cl, ((_ :: _) as p)) ->
                     TInst(cl, List.map (fun _ -> t_dynamic) p)
@@ -745,6 +747,8 @@ let configure gen =
       | TType ({ t_path = [],"Single" },[])
       | TAbstract ({ a_path = [],"Single" },[])
       | TType ({ t_path = [],"Null" },[_]) -> Some t
+      | TAbstract ({ a_impl = Some _ } as a, pl) ->
+          Some (gen.gfollow#run_f ( Codegen.get_underlying_type a pl) )
 	    | TAbstract( { a_path = ([], "EnumValue") }, _ )
       | TInst( { cl_path = ([], "EnumValue") }, _  ) -> Some t_dynamic
       | _ -> None);
@@ -933,7 +937,7 @@ let configure gen =
   and path_param_s pos md path params =
       match params with
         | [] -> path_s_import pos path
-        | _ when has_tdynamic params -> path_s_import pos path
+        | _ when has_tdynamic (change_param_type md params) -> path_s_import pos path
         | _ -> sprintf "%s<%s>" (path_s_import pos path) (String.concat ", " (List.map (fun t -> param_t_s pos t) (change_param_type md params)))
   in
 
@@ -1816,7 +1820,7 @@ let configure gen =
   fun e ->
     match e.eexpr with
       | TArray(e1, e2) ->
-        ( match follow e1.etype with
+        ( match run_follow gen e1.etype with
           | TInst({ cl_path = (["java"], "NativeArray") }, _) -> false
           | _ -> true )
       | _ -> assert false

+ 101 - 4
std/haxe/ds/Vector.hx

@@ -25,6 +25,10 @@ private typedef VectorData<T> = #if flash10
 	flash.Vector<T>
 #elseif neko
 	neko.NativeArray<T>
+#elseif cs
+	cs.NativeArray<T>
+#elseif java
+	java.NativeArray<T>
 #else
 	Array<T>
 #end
@@ -53,13 +57,14 @@ abstract Vector<T>(VectorData<T>) {
 			this = untyped __dollar__amake(length);
 		#elseif js
 			this = untyped __new__(Array, length);
+		#elseif cs
+			this = new cs.NativeArray(length);
+		#elseif java
+			this = new java.NativeArray(length);
 		#else
 			this = [];
 			#if cpp
 				untyped this.__SetSize(length);
-			#elseif (java || cs)
-				//TODO optimize
-				this[length-1] = cast null;
 			#else
 				untyped this.length = length;
 			#end
@@ -86,19 +91,111 @@ abstract Vector<T>(VectorData<T>) {
 		return this[index] = val;
 	}
 
+	/**
+		Returns the value at index [index].
+
+		WARNING: On some platforms it may not perform any bounds check,
+		so accessing it out-of-bounds may cause an irrecoverable crash
+		or data corruption
+	**
+	public inline function unsafeGet(index:Int):T
+	{
+		#if cpp
+			return untyped this.__unsafeGet(index);
+		#else
+			return this[index];
+		#end
+	}*/
+
+	/**
+		Sets the value at index [index] to [val]
+
+		WARNING: On some platforms it may not perform any bounds check,
+		so accessing it out-of-bounds may cause an irrecoverable crash
+		or data corruption
+	**
+	public inline function unsafeSet(index:Int, val:T):Void
+	{
+		#if cpp
+			untyped this.__unsafeSet(index, val);
+		#else
+			this[index] = val;
+		#end
+	}*/
+
 	/**
 		Returns the length of [this] Vector.
 	**/
 	public var length(get, never):Int;
-	
+
 	inline function get_length():Int {
 		#if neko
 			return untyped __dollar__asize(this);
+		#elseif cs
+			return this.Length;
+		#elseif java
+			return this.length;
 		#else
 			return untyped this.length;
 		#end
 	}
 
+	/**
+		Copies [length] of elements from [src] Vector, beginning at [srcPos] to [dest] Vector, beginning at [destPos]
+
+		The results are unspecified if [length] results in out-of-bounds access, or if [src] or [dest] are null
+	**
+	public static #if (cs || java || neko) inline #end function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, length:Int):Void
+	{
+		#if neko
+			untyped __dollar__ablit(dst,dstPos,src,srcPos,length);
+		#elseif java
+			java.lang.System.arraycopy(src, srcPos, dest, destPos, length);
+		#elseif cs
+			cs.system.Array.Copy(cast src, srcPos,cast dest, destPos, length);
+		#else
+			for (i in 0...length)
+			{
+				dest[destPos + i] = src[srcPos + i];
+			}
+		#end
+	}*/
+
+	/**
+		Returns a Vector with the same elements as [this], and with size [targetSize].
+
+		WARNING: On some platforms, this function may modify [this] Vector, while on others it may return a copy
+	**
+	public #if (java || cs) inline #end function grow(targetSize:Int):Vector<T>
+	{
+		#if (neko || java || cs)
+			//because of type parameters' erasure, java must run this inlined
+			var len = #if cs this.Length #else this.length #end;
+			var ret = new Vector(targetSize), targetIdx = (len < targetSize) ? len : targetSize;
+			blit(cast this, 0, ret, 0, targetIdx);
+			return ret;
+		#elseif js
+			untyped this.length = targetSize;
+			return cast this;
+		#elseif flash
+			this.fixed = false;
+			this.length = targetSize;
+			this.fixed = true;
+			return cast this;
+		#elseif cpp
+			untyped this.__SetSize(targetSize);
+			return cast this;
+		#else
+			var len = this.length;
+			while (len > targetSize)
+			{
+				this.pop();
+			}
+			untyped this.length = targetSize;
+			return cast this;
+		#end
+	}*/
+
 	/**
 		Extracts the data of [this] Vector.
 

+ 61 - 2
tests/unit/unitstd/haxe/ds/Vector.unit.hx

@@ -31,7 +31,7 @@ vec.get(2) == vNullBool;
 // fromArray
 var arr = ["1", "2", "3"];
 var vec:haxe.ds.Vector<String> = haxe.ds.Vector.fromArrayCopy(arr);
-#if (!flash && !neko)
+#if (!flash && !neko && !cs && !java)
 arr != vec.toData();
 #end
 vec.length == 3;
@@ -59,4 +59,63 @@ vec2[2] == "3";
 vec2[1] = "4";
 vec2[1] == "4";
 vec2[0] += "a";
-vec2[0] = "1a";
+vec2[0] = "1a";
+
+/*
+// grow
+vec2 = vec2.grow(15);
+vec2.length == 15;
+(vec2[3] = "4") == "4";
+(vec2[4] = "5") == "5";
+vec2[0] == "1a";
+vec2[3] == "4";
+vec2[4] == "5";
+(vec2[10] = "11") == "11";
+vec2[10] == "11";
+
+vec2 = vec2.grow(2);
+vec2.length == 2;
+vec2[0] == "1a";
+vec2[1] == "4";
+
+// blit
+var vec3 = haxe.ds.Vector.fromArrayCopy([0,1,2,3,4,5,6]);
+var vec4 = new haxe.ds.Vector(5);
+
+haxe.ds.Vector.blit(vec3, 0, vec4, 1, 3);
+vec4[1] == 0;
+vec4[2] == 1;
+vec4[3] == 2;
+vec4[4] == vNullInt;
+vec4[0] == vNullInt;
+
+haxe.ds.Vector.blit(vec3, 0, vec4, 0, 5);
+vec4[0] == 0;
+vec4[1] == 1;
+vec4[2] == 2;
+vec4[3] == 3;
+vec4[4] == 4;
+
+haxe.ds.Vector.blit(vec4, 1, vec3, 0, 4);
+//vec3 should be [1,2,3,4,4,5,6]
+vec3[0] == 1;
+vec3[1] == 2;
+vec3[2] == 3;
+vec3[3] == 4;
+vec3[4] == 4;
+vec3[5] == 5;
+vec3[6] == 6;
+
+// unsafe get / set
+vec3.unsafeGet(0) == 1;
+vec3.unsafeGet(1) == 2;
+vec3.unsafeGet(2) == 3;
+vec3.unsafeGet(3) == 4;
+
+vec3.unsafeSet(0, 10);
+vec3[0] == 10;
+vec3.unsafeGet(0) == 10;
+vec3.unsafeGet(6) == 6;
+vec3.unsafeSet(6, 10);
+vec3.unsafeGet(6) == 10;
+*/