Browse Source

start working on Vector improvements (see #3888)

Simon Krajewski 10 years ago
parent
commit
af80e22b30
2 changed files with 140 additions and 0 deletions
  1. 81 0
      std/haxe/ds/Vector.hx
  2. 59 0
      tests/unit/src/unitstd/haxe/ds/Vector.unit.hx

+ 81 - 0
std/haxe/ds/Vector.hx

@@ -219,4 +219,85 @@ abstract Vector<T>(VectorData<T>) {
 		return vec;
 		#end
 	}
+
+	/**
+		Returns a shallow copy of `this` Vector.
+
+		The elements are not copied and retain their identity, so
+		`a[i] == a.copy()[i]` is true for any valid `i`. However,
+		`a == a.copy()` is always false.
+	**/
+	public inline function copy<T>():Vector<T> {
+		var r = new Vector<T>(length);
+		Vector.blit(cast this, 0, r, 0, length);
+		return r;
+	}
+
+	/**
+		Returns a string representation of `this` Vector, with `sep` separating
+		each element.
+
+		The result of this operation is equal to `Std.string(this[0]) + sep +
+		Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1])`
+
+		If `this` Vector has length 0, the result is the empty String `""`.
+		If `this` has exactly one element, the result is equal to a call to
+		`Std.string(this[0])`.
+
+		If `sep` is null, the result is unspecified.
+	**/
+	public inline function join<T>(sep:String):String {
+		#if (flash||cpp)
+		return this.join(sep);
+		#else
+		var b = new StringBuf();
+		var i = 0;
+		var len = length;
+		for(i in 0...len) {
+			b.add( Std.string(get(i)) );
+			if(i < len-1) {
+				b.add(sep);
+			}
+		}
+		return b.toString();
+		#end
+	}
+
+	/**
+		Creates a new Vector by applying function `f` to all elements of `this`.
+
+		The order of elements is preserved.
+
+		If `f` is null, the result is unspecified.
+	**/
+	//public function map<S>(f:T->S):Vector<S> {
+		//var r = new Vector<S>(length);
+		//var i = 0;
+		//var len = length;
+		//for(i in 0...len) {
+			//var v = f(get(i));
+			//r.set(i, v);
+		//}
+		//return r;
+	//}
+
+	/**
+		Sorts `this` Vector according to the comparison function `f`, where
+		`f(x,y)` returns 0 if x == y, a positive Int if x > y and a
+		negative Int if x < y.
+
+		This operation modifies `this` Vector in place.
+
+		The sort operation is not guaranteed to be stable, which means that the
+		order of equal elements may not be retained.
+
+		If `f` is null, the result is unspecified.
+	**/
+	public inline function sort<T>(f:T->T->Int):Void {
+		#if (neko || cs || java)
+		throw "not yet supported";
+		#else
+		this.sort(f);
+		#end
+	}
 }

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

@@ -100,3 +100,62 @@ for (e in vec1) {
 }
 eq(3, res);
 
+// copy
+
+var i0 = new IntWrap(1);
+var i1 = new IntWrap(1);
+var i2 = new IntWrap(5);
+
+var vec = new haxe.ds.Vector(3);
+vec[0] = i0;
+vec[1] = i1;
+vec[2] = i2;
+var vec2 = vec.copy();
+f(vec == vec2);
+vec[0] == vec2[0];
+vec[1] == vec2[1];
+vec[2] == vec2[2];
+
+// join
+
+var vec = new haxe.ds.Vector(0);
+vec.join(",") == "";
+
+var vec = new haxe.ds.Vector(1);
+vec.join(",") == "null";
+
+var vec = new haxe.ds.Vector(2);
+vec.join(",") == "null,null";
+
+var vec = new haxe.ds.Vector(2);
+vec[0] = "foo";
+vec[1] = "bar";
+vec.join(", ") == "foo, bar";
+
+
+// map
+
+//var vec = new haxe.ds.Vector(0);
+//vec.map(function(i) return throw false);
+//
+//var vec = new haxe.ds.Vector(2);
+//vec[0] = 12;
+//vec[1] = 13;
+//var vec2 = vec.map(function(i) return "value: " +i);
+//vec2[0] == "value: 12";
+//vec2[1] == "value: 13";
+
+// sort
+
+#if !(neko || cs || java)
+var vec = new haxe.ds.Vector(4);
+vec[0] = 99;
+vec[1] = 101;
+vec[2] = -12;
+vec[3] = 0;
+vec.sort(Reflect.compare);
+vec[0] == -12;
+vec[1] == 0;
+vec[2] == 99;
+vec[3] == 101;
+#end