the code
import openfl.Vector;
class TypedArray<T> {
/**
*
* @param array
* @return
*/
public static inline function ofArray<T>(array:Array<T>):TypedArray<T> {
var result:TypedArray<T> = new TypedArray();
for (item in array) {
result.push(item);
}
return result;
}
var vector:Vector<T>;
public var length (get, null):Int;
/**
*
*/
public function new() {
vector = new Vector();
}
/**
*
* @param i
* @return
*/
public inline function get(i:Int):T {
return vector[i];
}
/**
*
* @param i
* @param v
*/
public inline function set(i:Int, v:T):Void {
trace("v="+v+",i="+i); //i=0,v=121
vector.set(i,v);
trace(vector[i]); //0
trace(vector.length); //0
vector[i] = v;
trace(vector[i]); //0
trace(vector.length); //0
vector.push(v);
trace(vector[i]); //0
trace(vector.length); //0
}
/**
*
* @param v
* @return
*/
public inline function push(v:T):Int {
return vector.push(v);
}
/**
*
* @return
*/
private inline function get_length():Int {
trace("into get_length");
return vector.length;
}
}
Any problem the code?
Why i can’t set value?
Thank your help?
Sean
Does this work?
public inline function set(i:Int, v:T):Void {
while(i >= vector.length) {
vector.push(v);
}
vector[i] = v;
}
Let’s do some more experiments.
1:
var vector:Vector<Int> = new Vector<Int>();
vector.push(1);
vector[0] = 128;
trace(vector); //Should be "128"
2:
var vector:Vector<Int> = [1, 2, 3];
vector[0] = 128;
trace(vector); //Should be "128, 2, 3"
If these work normally, the problem lies in your class. If these fail, the problem lies elsewhere.
Isn’t Vector has a fixed size thats what makes it different from Array and List as its much faster by having fixed size?
what I know when writing
var vector:Vector<T> = new Vector<T>();
while create a vector of size zero and since you can’t change its size but u can set values so what I think you should make it
var vector:Vector<T> = new Vector<T>(size);
then u can set values using the square braces like that
vector[0] = new T();
or even by using the set function but u must have the size set at the beginning so the code will be like that
vector.set(0, new T());
but if you need push function and pop use Array Instead like the following code
var array:Array<T> = new Array<T>();
arrray.push(new T());
Hope that helps here is a link to the vectors in the API http://api.haxe.org/haxe/ds/Vector.html
Your problem that u didn’t define the length of the vector while initialization
That’s the haxe.ds.Vector
version. The openfl.Vector
version can vary in size unless you explicitly specify that it shouldn’t.
My code is haxe2 and nme,use nme.vector.
It is work fine in haxe2 & nme,
but convect to haxe3&openfl.vector, it isn’t work now.
Check my class now.
When you run the experiments that I mentioned above, what happened? Tell us what they traced.
Finally, it is work.
the code
public function new() {
vector1 = new Vector<Float>();
}
public inline function set(i:Int, v:Float):Void {
vector1 = new Vector<Float>();
vector1[i] = v;
}
But i don’t know the reason.
It is so strange, “new” twice.
That isn’t the solution you want. If you make a new vector, it’ll forget all the data in the old one.
How about, instead of trying to write your own class, you use Vector
directly?
typedef TypedArray<T> = openfl.Vector<T>;