Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Flexible function results?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cavetroll




PostPosted: Sun Oct 07, 2007 10:42 pm   Post subject: 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:
Turing:

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?
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sun Oct 07, 2007 11:51 pm   Post subject: Re: Flexible function results?

cavetroll wrote:
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




PostPosted: Mon Oct 08, 2007 1:04 am   Post subject: 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




PostPosted: Mon Oct 08, 2007 9:30 am   Post subject: 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:

code:

function foo (bar, baz : int) : array 1 .. 5 of int
    %stuff
end foo
cavetroll




PostPosted: Mon Oct 08, 2007 9:50 am   Post subject: 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




PostPosted: Mon Oct 08, 2007 10:06 am   Post subject: 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

Turing:

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




PostPosted: Mon Oct 08, 2007 11:19 am   Post subject: Re: Flexible function results?

If you make your own class like a100 did, you can do exactly what you wanted to originally.

Turing:

%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




PostPosted: Mon Oct 08, 2007 11:30 am   Post subject: 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 >_>
Sponsor
Sponsor
Sponsor
sponsor
cavetroll




PostPosted: Mon Oct 08, 2007 12:39 pm   Post subject: 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




PostPosted: Mon Oct 08, 2007 1:00 pm   Post subject: Re: Flexible function results?

If you want I have attached the whole, some what generic, FlexibleArray class.


F-Array class.t
 Description:

Download
 Filename:  F-Array class.t
 Filesize:  1.41 KB
 Downloaded:  67 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: