http://qs321.pair.com?node_id=684352


in reply to Re^4: Data Structures
in thread Data Structures

OK

I use Moose

First I made the class holding the whole structure. I called it Seismic.

use strict; package Seismic; package Seismic::Station; use Moose; has 'Easting' => (isa => 'Int', is => 'rw', required => 1); has 'Northing' => (isa => 'Int', is => 'rw', required => 1); has 'Elevation' => (isa => 'Int', is => 'rw', required => 1); has 'Id' => (isa => 'Str', is => 'rw', required => 1); package Seismic::Line; use Moose; use MooseX::AttributeHelpers; has 'Length' => (isa => 'Int', is => 'rw'); has 'GroupInterval' => (isa => 'Int', is => 'rw'); has 'Id' => (isa => 'Str', is => 'rw', req +uired => 1); has 'Stations' => ( metaclass => 'Collection::Hash', is => 'rw', isa => 'HashRef[Seismic::Station]', default => sub { {} }, provides => { exists => 'station_exists', keys => 'station_ids', get => 'get_station', set => 'add_station', count => 'count_stations', delete => 'delete_station', clear => 'delete_all_stations', }, ); package Seismic::Grid; use Moose; has 'Lines' => ( metaclass => 'Collection::Hash', is => 'rw', isa => 'HashRef[Seismic::Line]', default => sub { {} }, provides => { exists => 'line_exists', keys => 'line_ids', get => 'get_line', set => 'add_line', count => 'count_lines', delete => 'delete_line', clear => 'delete_all_lines', }, ); 1;

And this is the program:

use strict; use Seismic; my $grid = Seismic::Grid->new(); for my $line (1 .. 1000) { my $lineobject = Seismic::Line->new( { Id => $line, Length => int(1000 * rand(90)), GroupInterval => int( 100 * rand(90)), } ); $grid->add_line($lineobject->Id, $lineobject); for my $station (1 .. 10) { my $stationobject = Seismic::Station->new( { Id => $line . '_' . $station, Northing => int(1e6 * rand(90)), Easting => int(1e6 * rand(90)), Elevation => int( 10 * rand(90)), } ); $lineobject->add_station($stationobject->Id, $stationobject); } } print "line station position elev.\n"; for my$line_id ($grid->line_ids) { my $line = $grid->get_line($line_id); for my $station_id ($line->station_ids) { my $station = $line->get_station($station_id); printf "%08d %-8s %08dN%08dW %4d\n", $line->Id, $station->Id, $station->Northing, $station->Easting, $station->Elevation; } }
Everything is stored in one $grid object which consists of $line objects which hold the $station objects.

Direct access to a station and its attributes is easy:

$grid->get_line(257)->get_station('257_5')->Elevation;
Of course, behind the scenes, it is all hashes and arrays and hashes of hashes and ... . And of course it is much slower than programming these data structures directly, but then you don't get these nice accessors, mutators, type-checking, default values, ...

PS: I did not implement all the fields in all the objects, just enough --I hope-- to show it works.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James