2
0
Simon Krajewski 12 жил өмнө
parent
commit
5e45a1db19

+ 104 - 0
std/haxe/Vector.hx

@@ -0,0 +1,104 @@
+/*
+ * Copyright (C)2005-2013 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package haxe;
+
+/**
+	A Vector is a storage of fixed size. It can be faster than Array on some
+	targets, and is never slower.
+**/
+abstract Vector(#if flash9
+	flash.Vector<T>
+#elseif neko
+	neko.NativeArray<T>
+#else
+	Array<T>
+#end)<T> {
+	/**
+		Creates a new Vector of length [length].
+		
+		Initially [this] Vector contains [length] neutral elements:
+			- always null on dynamic targets
+			- 0, 0.0 or false for Int, Float and Bool respectively on static
+			targets
+			- null for other types on static targets
+			
+		If [length] is less than or equal to 0, an exception is thrown.
+	**/
+	public inline function new(length:Int) {
+		if (length <= 0)
+			throw "Invalid length, must be >= 1";
+		#if flash9
+			this = new flash.Vector<T>(length, true);
+		#elseif neko
+			this = untyped __dollar__amake(length);
+		#else
+			this = [];
+			#if cpp
+				untyped this.__SetSize(length);
+			#else
+				untyped this.length = length;
+			#end
+		#end
+	}
+
+	/**
+		Returns the value at index [index].
+		
+		If [index] is negative or exceeds [this].length, null is returned.
+	**/
+    public inline function get(index:Int):Null<T> {
+		#if (flash9 || cpp || js || java)
+		return index < 0 || index >= this.length ? null : this[index];
+		#else
+		return this[index];
+		#end
+	}
+	
+	/**
+		Sets the value at index [index] to [val].
+		
+		If [index] is negative or exceeds [this].length, the result is
+		unspecified.
+	**/
+    public inline function set(index:Int, val:T):T {
+		return this[index] = val;
+	}
+	
+	/**
+		Returns the length of [this] Vector.
+	**/
+    public inline function length():Int {
+		#if neko
+			return untyped __dollar__asize(this);
+		#else
+			return untyped this.length;
+		#end
+	}
+	
+	@:from static public inline function fromArray(arr:Array<T>) {
+		// TODO: Optimize this for flash (and others?)
+		var vec = new Vector<T>(arr.length);
+		for (i in 0...arr.length)
+			vec.set(i, arr[i]);
+		return vec;
+	}
+}

+ 46 - 0
tests/unit/unitstd/haxe/Vector.unit.hx

@@ -0,0 +1,46 @@
+var vec = new haxe.Vector(3);
+var vNullInt = #if (flash9 || cpp || java || cs) 0 #else null #end;
+var vNullBool = #if (flash9 || cpp || java || cs) false #else null #end;
+var vNullFloat = #if (flash9 || cpp || java || cs) 0.0 #else null #end;
+
+vec.length() == 3;
+vec.get(0) == vNullInt;
+vec.get(1) == vNullInt;
+vec.get(2) == vNullInt;
+vec.set(1, 2);
+vec.length() == 3;
+vec.get(0) == vNullInt;
+vec.get(1) == 2;
+vec.get(2) == vNullInt;
+
+// out of bounds
+vec.get( -1) == null;
+vec.get(4) == null;
+
+// float init
+var vec = new haxe.Vector<Float>(3);
+vec.get( -1) == null;
+vec.get(0) == vNullFloat;
+vec.get(1) == vNullFloat;
+vec.get(2) == vNullFloat;
+vec.get(3) == null;
+
+// bool init
+var vec = new haxe.Vector<Bool>(3);
+vec.get( -1) == null;
+vec.get(0) == vNullBool;
+vec.get(1) == vNullBool;
+vec.get(2) == vNullBool;
+vec.get(3) == null;
+
+// fromArray
+var vec:haxe.Vector<String> = ["1", "2", "3"];
+vec.length() == 3;
+vec.get(0) == "1";
+vec.get(1) == "2";
+vec.get(2) == "3";
+
+// objects
+var tpl = new haxe.Template("foo");
+var vec:haxe.Vector<haxe.Template> = [tpl];
+tpl == vec.get(0);