Computer Science Canada

Multidimensional flexible arrays using classes

Author:  NikG [ Sun Sep 10, 2006 1:50 am ]
Post subject:  Multidimensional flexible arrays using classes

I've been trying to figure out a solution to Turing's inability to resize multidimensional flexible arrays for my rts game. One of the posts on this site suggested using classes, so I decided to try it:
code:
class yAxis
    export setUpper, setY, Y

    var fullY : flexible array 1 .. 0 of int

    proc setUpper (MaxY : int)
        new fullY, MaxY
    end setUpper

    proc setY (which, what : int)
        fullY (which) := what
    end setY

    fcn Y (which : int) : int
        result fullY (which)
    end Y
end yAxis

var X : flexible array 1 .. 0 of ^yAxis

%test: 10x10 array
new X, 10
for i : 1 .. 10
    new X (i)
    X (i) -> setUpper (10)
    for j : 1 .. 10
        X (i) -> setY (j, i * j)
    end for
end for

for i : 1 .. 10
    for j : 1 .. 10
        put X (i) -> Y(j):4 ..
    end for
    put ""
end for

new X, 0

This works, and resizing it simply involves the following:
code:
%test: 20x20 array
new X, 20
for i : 1 .. 20
    new X (i)
    X (i) -> setUpper (20)
end for

The only problem with this is that I'm not sure about how well it can remember the old values when you resize it.

Hope this helps those of you trying to solve this problem.

Author:  Clayton [ Sun Sep 10, 2006 8:53 am ]
Post subject: 

Interesting... its a fairly round about way of doing it but it gets the job done (even if it is tedious Razz), although i would have to say, dont get into the habit of:

code:

new X (i)


for a pointer to reference an object, all turing is doing here is assuming what object you want, and while you only have one here, its not so bad, but its just a general bad coding practice.

Author:  NikG [ Sun Sep 10, 2006 12:48 pm ]
Post subject: 

Right you are Freakman. Guess I was just being lazy...

For those of you who don't know, the correct way should be:
code:
new yAxis, X (i)


: