structures & records

new topic     » topic index » view thread      » older message » newer message

Hi, everybody,

while I certainly agree that just about any data structure can be
simulated in euphoria, sometimes it gets pretty ugly and cumbersome.
Native support for structures (or records, as they are called in
pascal) would no doubt enhance euphoria appeal to programmers
migrating, or even just taking temporary breaks from other, more
popular, mainstream languages. If that was combined with support for
tables (synonym for dictionaries, associative lists, etc), it would
surely make my life much easier, among others ;).

Returning to structures, the following is what I am occasionally
using. The module is not terribly sophisticated, but the resultant
code is fairly transparent, compact and reasonably fast. And because
the underlying field structure of each record is stored separately and
only once, the system is also quite space efficient.

And because both record structures and record instances are stered in
simple arrays, euphoria's print and get pair can be used to store and
retrieve them as sequences.

Just for illustration, I added a small example from an old pascal
book, together with its translation to euphoria. I think you will
agree it does not look too bad.

I hope someone will somehow find it useful, eventually...

jiri


--  records.e
--  jiri babor
--  jbabor at paradise.net.nz
--  01-08-12

----------------------------------------------------------------------
--  terminology
--      structures: records structures: sequences of field names
--      records: instances of structure: sequences of values,
--          each *prepended* with its structure index for tracking
----------------------------------------------------------------------

global sequence structures, records

global function structure(sequence fields)
    -- create structure with field names: sequence of strings
    -- return structure index

    structures = append(structures, fields)
    return length(structures)
end function

global function record(integer struct)
    -- create record: sequence of entries corresponding to fields
    -- each field is initialized to zero
    -- return record index

    integer n

    n = length(structures[struct])  -- number of structure fields
    records = append(records, struct & repeat(0, n))
    return length(records)
end function

global function record_sequence(integer struct, integer n)
    -- create sequence of records with n elements (record array)

    sequence r
    r = repeat(0, n)
    for i = 1 to n do r[i] = record(struct) end for
    return r
end function

global function fetch(integer rec, sequence field)
    -- given record index and field name, return value

    integer m,n

    m = records[rec][1]     -- structure index
    n = find(field, structures[m])
    return records[rec][n+1]
end function

global procedure set(integer rec, sequence field, object val)
    -- given record index and field name, replace current value
    -- with val

    integer m,n

    m = records[rec][1]     -- structure index
    n = find(field, structures[m])
    records[rec][n+1] = val
end procedure

-- initialize --------------------------------------------------------
structures = {}
records = {}



-- example -----------------------------------------------------------

pascal fragment:

type FlightData =   record
                        DepartureTime, ArrivalTime: real;
                        Terminal: char;
                        GateNumber: integer;
                        OnTime, Cancelled, SoldOut: boolean;
                    end;

OutboundFlights = array[1..100] of FlightData;
var United, TWA: outboundFlights;

writeln(United[48].DepartureTime);



translated into euphoria:

constant FlightData = structure({"Departure Time", "Arrival Time",
    "Terminal", "Gate Number", "On Time", "Cancelled", "Sold Out"})

constant United = record_sequence(FlightData, 100)
constant TWA = record_sequence(FlightData, 100)

? fetch(United[48], "Departure Time")

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu