Thread Probleme Größe eines Array of Arrays (10 answers)
Opened by gmafx at 2012-05-25 11:16

topeg
 2012-05-25 13:41
#158601 #158601
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ein kleines Modul, das eine Matrix von Zahlenwerten als String verwaltet.
Mit unpack pack werden die Werte gelesen und geschrieben.
Darum muss zu Anfang angeben werden welches Format die Zahlen haben sollen. zudem hat die Matrix eine feste Größe, damit die Position des Wertes im String berechnet werden kann.
bin_matrix.pm :
more (20.8kb):
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package bin_matrix;
use strict;
use warnings;

my %types=(
  c => 1,
  C => 1,
  s => 2,
  S => 2,
  l => 4,
  L => 4,
  f => 4,
  d => 8,
);

sub new
{
  my $class=shift;
  my $size_x=shift;
  my $size_y=shift;
  my $type=shift // 'l';
  return undef unless($size_x and $size_y);
  return undef unless($type and exists($types{$type}));
  my $self = bless({
      matrix => '',
      sx     => $size_x,
      sy     => $size_y,
      type   => $type,
    },$class);
  $self->init();
  return $self;
}

sub init
{
  my $self=shift;
  my $val=shift // 0;
  $self->{matrix}=pack($self->{type},$val) x ($self->{sx}*$self->{sy});
}

sub get
{
  my $self=shift;
  my $px=shift // 0;
  my $py=shift // 0;

  $px-= $self->{sx}     while($px >= $self->{sx});
  $py-= $self->{sy}     while($py >= $self->{sy});
  $px = $self->{sx}-$px while($px <  0);
  $py = $self->{sy}-$py while($py <  0);

  my $s=$types{$self->{type}};
  my $pos=($px*$s*$self->{sx})+($py*$s);
  my $v=substr($self->{matrix},$pos,$s);
  return unpack($self->{type},$v);
}

sub set
{
  my $self=shift;
  my $px=shift // 0;
  my $py=shift // 0;
  my $val=shift // return undef;

  $px-= $self->{sx}     while($px >= $self->{sx});
  $py-= $self->{sy}     while($py >= $self->{sy});
  $px = $self->{sx}-$px while($px <  0);
  $py = $self->{sy}-$py while($py <  0);

  $val=pack($self->{type},$val);
  my $s=length($val);
  my $pos=($px*$s*$self->{sx})+($py*$s);

  substr($self->{matrix},$pos,$s,$val);

  return 1;
}

1;


Verwenung:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl
use strict;
use warnings;
use bin_matrix;

my $m=bin_matrix->new(10,10,'L');

for my $x (0..9)
{
  for my $y (0..9)
  {
    my $v=int(rand(2**32));
    $m->set($x,$y,$v);
  }
}

for my $x (0..9)
{
  for my $y (0..9)
  {
    my $v=$m->get($x,$y);
    print "($x,$y) = $v\n";
  }
}


Wenn du viele Komplexe Berechnungen auf zweidimensionale Matrizen anwendest kann es sinnvoll eine Grafikbibliothek zu nutzen. Nicht nur das ein Bild eine 2D Matrix mit bis zu vier Werten pro Position ist. Eine Bibliothek hat auch einige Nützliche Funktionen die man zum Transformieren von solchen Matrizen nutzen kann.

View full thread Probleme Größe eines Array of Arrays