Browse Source

[cpp] Add optimized version of Vector. Add cpp.NativeArray.setSize

Hugh 11 years ago
parent
commit
925baff243
2 changed files with 32 additions and 10 deletions
  1. 4 0
      std/cpp/NativeArray.hx
  2. 28 10
      std/haxe/ds/Vector.hx

+ 4 - 0
std/cpp/NativeArray.hx

@@ -22,4 +22,8 @@ class NativeArray {
 	public static inline function memcmp<T>( inArrayA:Array<T>, inArrayB:Array<T>) : Int {
 		return untyped inArrayA.memcmp(inArrayB);
 	}
+
+	public static inline function setSize<T>( ioArray:Array<T>, inSize:Int) : Array<T> {
+		return untyped ioArray.__SetSizeExact(inSize);
+   }
 }

+ 28 - 10
std/haxe/ds/Vector.hx

@@ -21,6 +21,10 @@
  */
 package haxe.ds;
 
+#if cpp
+using cpp.NativeArray;
+#end
+
 private typedef VectorData<T> = #if flash10
 	flash.Vector<T>
 #elseif neko
@@ -75,7 +79,11 @@ abstract Vector<T>(VectorData<T>) {
 		unspecified.
 	**/
 	@:arrayAccess public inline function get(index:Int):Null<T> {
+		#if cpp
+		return this.unsafeGet(index);
+		#else
 		return this[index];
+		#end
 	}
 
 	/**
@@ -85,7 +93,11 @@ abstract Vector<T>(VectorData<T>) {
 		unspecified.
 	**/
 	@:arrayAccess public inline function set(index:Int, val:T):T {
+		#if cpp
+		return this.unsafeSet(index,val);
+		#else
 		return this[index] = val;
+		#end
 	}
 
 	/**
@@ -112,7 +124,7 @@ abstract Vector<T>(VectorData<T>) {
 		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, len:Int):Void
+	public static #if (cs || java || neko || cpp) inline #end function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void
 	{
 		#if neko
 			untyped __dollar__ablit(dest,destPos,src,srcPos,len);
@@ -120,6 +132,8 @@ abstract Vector<T>(VectorData<T>) {
 			java.lang.System.arraycopy(src, srcPos, dest, destPos, len);
 		#elseif cs
 			cs.system.Array.Copy(cast src, srcPos,cast dest, destPos, len);
+		#elseif cpp
+			dest.toData().blit(destPos,src.toData(), srcPos,len);
 		#else
 			for (i in 0...len)
 			{
@@ -131,16 +145,20 @@ abstract Vector<T>(VectorData<T>) {
 	/**
 		Creates a new Array, copy the content from the Vector to it, and returns it.
 	**/
-	public #if flash inline #end function toArray():Array<T> {
-		var a = new Array();
-		var len = length;
-		#if (cpp || neko)
-		// prealloc good size
-		if( len > 0 ) a[len - 1] = get(0);
+	public #if (flash || cpp) inline #end function toArray():Array<T> {
+		#if cpp
+			return this.copy();
+		#else
+			var a = new Array();
+			var len = length;
+			#if (neko)
+			// prealloc good size
+			if( len > 0 ) a[len - 1] = get(0);
+			#end
+			for( i in 0...len )
+				a[i] = get(i);
+			return a;
 		#end
-		for( i in 0...len )
-			a[i] = get(i);
-		return a;
 	}
 
 	/**