package Array2D;
use strict;
use Carp;
use constant sizeof_int => length pack 'i', 1337;
sub new {
my $class = shift;
@_ == 2 or croak
"must call ${class}::new with x, y dimensions";
my ($dx, $dy) = @_;
$dx > 0 && $dy > 0
or croak "${class}::new : illegal x, y dimensions";
bless {
dx => $dx,
dy => $dy,
buf => "\0" x ($dx * $dy * sizeof_int)
}, $class;
}
sub get {
my $ary = shift;
my $class = ref $ary;
@_ == 2 or croak
"must call ${class}::get with x, y positions";
my ($x, $y) = @_;
my ($dx, $dy) = @{$ary}{qw/dx dy/};
$x >= 0 && $y >= 0 && $x < $dx && $y < $dy
or croak "${class}::get : subscripts out of range";
unpack 'i', substr (
$ary>{buf},
($x + $y * $dx) * sizeof_int,
sizeof_int );
}
sub set {
my $ary = shift;
my $class = ref $ary;
@_ == 3 or croak
"must call ${class}::get with x, y positions and new value";
my ($x, $y, $val) = @_;
my ($dx, $dy) = @{$ary}{qw/dx dy/};
$x >= 0 && $y >= 0 && $x < $dx && $y < $dy
or croak "${class}::set : subscripts out of range";
substr (
$ary>{buf},
($x + $y * $dx) * sizeof_int,
sizeof_int,
pack 'i', $val
);
();
}
1;
