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

Username:   Password: 
 RegisterRegister   
 Start of a vector class for turing
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericfourfour




PostPosted: Sat Nov 04, 2006 3:59 pm   Post subject: Start of a vector class for turing

If you are familiar with the vectors from other languages (such as c++ and java) you should already know what vectors are. If you don't know what they are, they are just a simplified way of handling arrays.

I have recently started making a vector object in Turing. It uses mainly the syntax from the java Vectors.

Here it is:
code:
class Vector
    export add, element_at, is_empty, set_element, size

    var elements : flexible array 0 .. -1 of ^anyclass
    var bound := -1
    var no_elements := true

    fcn bound_check (position : int) : int
        if position <= 0 then
            result 0
        elsif position >= bound then
            result bound
        end if
        result position
    end bound_check

    proc add (element : ^anyclass)
        bound += 1
        new elements, bound
        elements (bound) := element
        no_elements := false
    end add

    fcn element_at (position : int) : ^anyclass
        if bound = -1 then
            result nil
        end if
        result elements (bound_check (position))
    end element_at

    fcn is_empty () : boolean
        result no_elements
    end is_empty

    proc set_element (position : int, element : ^anyclass)
        elements (position) := element
    end set_element

    fcn size () : int
        result bound + 1
    end size
end Vector


I have not tested it extensively yet and it is very limited right now. However, it can be easily expanded upon.

Here is a sample program using the vector class.

code:
class Data
    export set_name, display

    var name : string

    proc set_name (new_name : string)
        name := new_name
    end set_name

    proc display ()
        put name ..
    end display
end Data

var data : array 0 .. 25 of ^Data
for i : 0 .. 25
    new Data, data (i)
    data (i) -> set_name (chr (ord ('a') + i))
end for

var vec : ^Vector
new Vector, vec

for i : 0 .. 25
    vec -> add (data (i))
end for

for i : 0 .. 25
    Data (vec -> element_at (i)).display ()
end for


It uses Turing's anyclass which is the parent class to all other classes. If you want a vector of types you will need to make a class for that type.

Eventually, it will have iterators, size limitations and much more. Comments, suggestions, improvements or even more functions would be appreciated.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: