소스 검색

optimize python Vector (no bound checks), add vector iteration test

frabbit 11 년 전
부모
커밋
980a1af814
4개의 변경된 파일29개의 추가작업 그리고 12개의 파일을 삭제
  1. 11 4
      std/haxe/ds/Vector.hx
  2. 4 6
      std/python/_std/Array.hx
  3. 2 2
      std/python/internal/ArrayImpl.hx
  4. 12 0
      tests/unit/unitstd/haxe/ds/Vector.unit.hx

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

@@ -67,10 +67,7 @@ abstract Vector<T>(VectorData<T>) {
 		#elseif cpp
 			this = untyped (new Array<T>()).__SetSizeExact(length);
 		#elseif python
-			this = new Array();
-			for (i in 0...length) {
-				this[i] = null;
-			}
+			this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
 		#else
 			this = [];
 			untyped this.length = length;
@@ -86,6 +83,8 @@ abstract Vector<T>(VectorData<T>) {
 	@:arrayAccess public inline function get(index:Int):Null<T> {
 		#if cpp
 		return this.unsafeGet(index);
+		#elseif python
+		return python.internal.ArrayImpl.unsafeGet(this, index);
 		#else
 		return this[index];
 		#end
@@ -100,6 +99,8 @@ abstract Vector<T>(VectorData<T>) {
 	@:arrayAccess public inline function set(index:Int, val:T):T {
 		#if cpp
 		return this.unsafeSet(index,val);
+		#elseif python
+		return python.internal.ArrayImpl.unsafeSet(this, index, val);
 		#else
 		return this[index] = val;
 		#end
@@ -155,6 +156,8 @@ abstract Vector<T>(VectorData<T>) {
 	public #if (flash || cpp) inline #end function toArray():Array<T> {
 		#if cpp
 			return this.copy();
+		#elseif python
+			return this.copy();
 		#else
 			var a = new Array();
 			var len = length;
@@ -199,10 +202,14 @@ abstract Vector<T>(VectorData<T>) {
 	**/
 	#if as3 @:extern #end
 	static public inline function fromArrayCopy<T>(array:Array<T>):Vector<T> {
+		#if python
+		return cast array.copy();
+		#else
 		// TODO: Optimize this for flash (and others?)
 		var vec = new Vector<T>(array.length);
 		for (i in 0...array.length)
 			vec.set(i, array[i]);
 		return vec;
+		#end
 	}
 }

+ 4 - 6
std/python/_std/Array.hx

@@ -115,8 +115,6 @@ extern class Array<T> implements ArrayAccess<T> extends ArrayImpl {
 		return ArrayImpl.filter(this,f);
 	}
 
-
-
 	@:keep private inline function _get(idx:Int):T
 	{
 		return ArrayImpl._get(this, idx);
@@ -127,14 +125,14 @@ extern class Array<T> implements ArrayAccess<T> extends ArrayImpl {
 		return ArrayImpl._set(this, idx,val);
 	}
 
-	@:keep private inline function __unsafe_get(idx:Int):T
+	@:keep private inline function unsafeGet(idx:Int):T
 	{
-		return ArrayImpl.__unsafe_get(this, idx);
+		return ArrayImpl.unsafeGet(this, idx);
 	}
 
-	@:keep private inline function __unsafe_set(idx:Int, val:T):T
+	@:keep private inline function unsafeSet(idx:Int, val:T):T
 	{
-		return ArrayImpl.__unsafe_set(this, idx,val);
+		return ArrayImpl.unsafeSet(this, idx,val);
 	}
 
 	@:noCompletion private function __iter__ ():NativeIterator<T>;

+ 2 - 2
std/python/internal/ArrayImpl.hx

@@ -155,11 +155,11 @@ class ArrayImpl {
 		return v;
 	}
 
-	private static inline function __unsafe_get<T>(x:Array<T>,idx:Int):T {
+	public static inline function unsafeGet<T>(x:Array<T>,idx:Int):T {
 		return Syntax.arrayAccess(x, idx);
 	}
 
-	private static inline function __unsafe_set<T>(x:Array<T>,idx:Int, val:T):T {
+	public static inline function unsafeSet<T>(x:Array<T>,idx:Int, val:T):T {
 		Syntax.assign(Syntax.arrayAccess(x, idx), val);
 		return val;
 	}

+ 12 - 0
tests/unit/unitstd/haxe/ds/Vector.unit.hx

@@ -88,3 +88,15 @@ vec3[3] == 4;
 vec3[4] == 4;
 vec3[5] == 5;
 vec3[6] == 6;
+
+// test iteration
+
+var vec1 = new haxe.ds.Vector(2);
+vec1[0] = 1;
+vec1[1] = 2;
+var res = 0;
+for (e in vec1) {
+	res += e;
+}
+eq(3, res);
+