Translating from C to Haxe
-
Hello, guys!
I'm translating some C code and I just faced this function:cvgQERef *cvgQERot(cvgQERef *edge) { return (edge->r < 3) ? edge+1 : edge-3; }
As you can see, he's returning a pointer to the memory address where that edge is. That edge is part of an array, of course.
My question is: can I do that with HaxeFlixel or should I workaround it (like passing the array as a parameter)?Thanks in advance!
-
You pass objects (array elements), and Haxe cares about reference counting and GC. But for optimization you can see how HaxeFlixel does pools with FlxPoint get() and put().
-
I'm not sure if I understood your answer correctly, but in case you're suggesting me to use a pool, I can't, because that function needs to return a specific edge. Thanks for your answer anyway.
(:
-
You don't need a pool. Basically, just have an
Array<Edge> a = new Array<Edge>()
(where Edge is your type) andreturn a[i]
. Or do you ask about linked lists?
-
I was trying to achieve is something similar to the original C function.
Which would be something like this:public function rot(edge:Edge):Edge { return (edge.r < 3) ? edge + 1 : edge - 3; } //where edge+1 is equivalent to arr[edgeIndex + 1] //somewhere else: var arr = new Array<Edge>(); arr.push(specificEdge1); arr.push(specificEdge2); arr.push(specificEdge3); arr.push(specificEdge4); //in case arr[2].r == 2, then specificEdge would be arr[3] var specificEdge = rot(arr[2]);
but apparently I can't do that in Haxe. So a workaround would be passing the array "arr" to the function, instead of just one edge.
-
Hmm, you could cook your own wrapper that includes array and index and acts similar to pointer.
-
Ok, that might work. Thanks @starry-abyss!