-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopology.pm
98 lines (78 loc) · 2.17 KB
/
Topology.pm
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package Topology {
# models 4 nearest neighbors to position i,j forall i,j
my $adjacents = [ [ 0, -1], # left
[-1, 0], # top
[ 0, 1], # right
[+1, 0], # bottom
];
sub new {
my ($class, %attrs) = @_;
# initialize all elements of the matrix to cells O(N * M)
for my $i ( 0 .. $#{$attrs{field}} ) {
for my $j ( 0 .. $#{$attrs{field}->[$i]} ) {
# create new Cell
$attrs{field}->[$i]->[$j] =
Cell->new( x => $i,
y => $j,
elevation => $attrs{field}->[$i]->[$j],
);
}
}
# find sinks and set neighbors O(2 * N * M)
my @sinks;
for my $i ( 0 .. $#{$attrs{field}} ) {
for my $cell ( @{ $attrs{field}->[$i] } ) {
# get neighbors for this Cell
my @neighbors;
NEIGHBORS:
for my $adjacent ( @$adjacents ) {
my ($xmod, $ymod) = ($i + $adjacent->[0], $cell->y + $adjacent->[1]);
# x and y must be in the bounds of the matrix
next NEIGHBORS
if $xmod >= $attrs{rows} || $ymod >= $attrs{cols} || $xmod < 0 || $ymod < 0;
push @neighbors,
$attrs{field}->[$xmod]->[$ymod];
}
# set neighbors
$cell->set_neighbors(@neighbors);
# determine sinks
push @sinks, $cell
if $cell->is_sink;
}
}
# set sinks
$attrs{sinks} = \@sinks;
bless \%attrs, $class;
}
# accessor methods
sub field {
my $self = shift;
return $self->{field};
}
sub cell {
my ($self, $i, $j) = @_;
return $self->field->[$i]->[$j];
}
sub rows {
return shift->{rows};
}
sub cols {
return shift->{cols};
}
sub sinks {
return shift->{sinks};
}
# given an Array of Sinks, find the Basins in this field
# O(n)
sub basins {
my ($self) = @_;
my %basin;
my $basin_marker = 'A';
# determine how many cells eventually flow into this one
for my $sink ( @{ $self->sinks } ) {
$basin{$basin_marker++} = $sink->basin_size;
}
return %basin;
}
1;
} # end Topology