
-----------------------------------
cavetroll
Sun Oct 07, 2007 10:42 pm

Flexible function results?
-----------------------------------
I am trying to create a pathfinding algorithm (A* to be exact) using a function that will return an array which holds the positions of the steps to take. Is it possible to do something like this:

fcn Astar(start,finish : coords) : flexible array 1 .. 1
%code
end Astar

Not exact code just example.

What I want is for it to get larger every time and then assign the value to another flexible array outside the function. Any way to do this or I'm i gonna just have to rewrite some of my code?

-----------------------------------
richcash
Sun Oct 07, 2007 11:51 pm

Re: Flexible function results?
-----------------------------------
What I want is for it to get larger every time and then assign the value to another flexible array outside the function.
From what I can recall, you can not assign flexible arrays in Turing (even if you use another flexible array).

To my knowledge, there is also no way of having a function return a flexible array. So I think you'll have to rewrite your code (or write your own array class).

-----------------------------------
Euphoracle
Mon Oct 08, 2007 1:04 am

RE:Flexible function results?
-----------------------------------
The one thing I really hated about turing, is you can't actually use the flexible arrays for anything useful.  The only way you can do it in turing is to create the array outside the procedure and update it externally.  Turing doesn't allow you to do ANY reflection at all, so you're VERY limitted as to how you can do this.

-----------------------------------
Clayton
Mon Oct 08, 2007 9:30 am

RE:Flexible function results?
-----------------------------------
Unfortunately, the only kind of array you can return from a function in Turing is a static array with known bounds at compile-time. This means, the only way to return an array, is something clunky like this:


function foo (bar, baz : int) : array 1 .. 5 of int
    %stuff
end foo


-----------------------------------
cavetroll
Mon Oct 08, 2007 9:50 am

RE:Flexible function results?
-----------------------------------
Oh well geuss I'm gonna have to recode my program. Thats ok though because my computer crashed and I lost some of it.

Anyway thanks for the responses

-----------------------------------
Saad
Mon Oct 08, 2007 10:06 am

Re: Flexible function results?
-----------------------------------
What you can do wrap the flexible array in a class, and have member functions which allow you to add/remove stuff from the array


class FlexibleArray
    export add, getn
    var data : flexible array 1 .. 0 of int
    proc add (moo : int)
        new data, upper (data) + 1
        data (upper (data)) := moo
    end add
    fcn getn (n : int) : int
        result data (n)
    end getn
end FlexibleArray

proc manipulate (foo : ^FlexibleArray)
    for i : 1 .. 4
        foo -> add (i)
    end for
    for i : 1 .. 4
        put foo -> getn (i)
    end for
end manipulate

var foo : ^FlexibleArray
new foo
manipulate (foo)
free foo



Thats just a start, you can figure out the rest

-----------------------------------
richcash
Mon Oct 08, 2007 11:19 am

Re: Flexible function results?
-----------------------------------
If you make your own class like a100 did, you can do exactly what you wanted to originally.


%place a100's class here

var outsider : ^FlexibleArray
new FlexibleArray, outsider

fcn Astar () : ^FlexibleArray
   var arr : ^FlexibleArray
   new FlexibleArray, arr
   arr -> add (2)
   arr -> add (3)
   result arr
end Astar

outsider := Astar ()


*code untested

-----------------------------------
Euphoracle
Mon Oct 08, 2007 11:30 am

RE:Flexible function results?
-----------------------------------
Awrwrwrwr, I didn't know you could do that! D:  That would have saved me so much hackyness in my final project last year >_>

-----------------------------------
cavetroll
Mon Oct 08, 2007 12:39 pm

RE:Flexible function results?
-----------------------------------
Wow you guys saved me so much recoding. Thank you so much. I'll make sure to put both of you in the credits.

-----------------------------------
Saad
Mon Oct 08, 2007 1:00 pm

Re: Flexible function results?
-----------------------------------
If you want I have attached the whole, some what generic, FlexibleArray class.
