Fast.pm

filter: Statementstmt 85.9% 104 / 121 Branchbran 58.3% 14 / 24 Conditioncond 82.3% 14 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 95.0% 19 / 20 Podpod 66.6% 8 / 12 totaltotal 80.9% 166 / 205 CC 27 SCAR 22.3
CC
27
Cov
81%
recursive_dijkstra
42.0
dijkstra_worker
10.0
del_vertex
5.0
CRAP 9.3
↑ Summary Fast/GraphPM.pm »
1 package Graph::Fast;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = "0.02";
8  
9 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Hash::PriorityQueue;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
16 5 my ($class, %args) = @_;
17 5 ▶ my $queue_maker = exists($args{queue_maker}) ? $args{queue_maker} : sub { Hash::PriorityQueue->new() };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'queue_maker'} ? : 0 5 5
18 5 return bless({
19 vertices => {},
20 edges => [],
21 _queue_maker => $queue_maker,
22 }, $class);
23 }
24  
25 21 ▶sub count_edges {
Subroutine count_edges: called CC=1 SCAR=0.0
Pod documented
26 21 my ($self) = @_;
27 21 return scalar(@{$self->{edges}});
28 }
29  
30 21 ▶sub count_vertices {
Subroutine count_vertices: called CC=1 SCAR=0.0
Pod documented
31 21 my ($self) = @_;
32 21 return scalar(keys(%{$self->{vertices}}));
33 }
34  
35 15 ▶sub add_vertex {
Subroutine add_vertex: called CC=2 SCAR=7.0
Pod documented
36 15 my ($self, $name) = @_;
37  
38 15 ▶ if (!exists($self->{vertices}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"vertices"}{$name}) 15 0 15
39 15 $self->{vertices}->{$name} = { name => $name, edges_in => {}, edges_out => {} };
40 }
41 15 return $self->{vertices}->{$name};
42 }
43  
44 6 ▶sub del_vertex {
Subroutine del_vertex: called CC=5 SCAR=16.1
Pod documented
45 6 my ($self, $name) = @_;
46  
47 6 ▶ if (exists($self->{vertices}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"vertices"}{$name}) 6 0 6
48 6 ▶ @{$self->{edges}} = grep { $_->{from} ne $name and $_->{to} ne $name } @{$self->{edges}};
Condition100%3/3
$_->{'from'} ne $name && $_->{'to'} ne $name
!ll&&!rl&&r
4811
MC/DC100%2/2
$_->{'from'} ne $name && $_->{'to'} ne $name
$_->{'from'} ne $name $_->{'to'} ne $name
Truth table100%3/3
A && B
A= $_->{'from'} ne $nameB= $_->{'to'} ne $name
ABresult
0X0
100
111
49 6 foreach my $in_edge (keys %{$self->{vertices}->{$name}->{edges_in}}) {
50 8 delete($self->{vertices}->{$in_edge}->{edges_out}->{$name});
51 }
52 6 foreach my $out_edge (keys %{$self->{vertices}->{$name}->{edges_out}}) {
53 4 delete($self->{vertices}->{$out_edge}->{edges_in}->{$name});
54 }
55 6 delete($self->{vertices}->{$name});
56 }
57 }
58  
59 9 ▶sub dijkstra_worker {
Subroutine dijkstra_worker: called CC=10 SCAR=23.0
Pod undocumented
60 9 my ($self, $from, $to) = @_;
61  
62 9 my $vert = $self->{vertices};
63 9 my $suboptimal = $self->{_queue_maker}->();
64 9 $suboptimal->insert($_, $self->{d_suboptimal}->{$_}) foreach (keys(%{$self->{d_suboptimal}}));
65 9 $self->{d_dist}->{$_} = -1 foreach (@{$self->{d_unvisited}});
66 9 $self->{d_dist}->{$from} = 0;
67  
68 9 while (1) {
69 # find the smallest unvisited node
70 31 ▶ my $current = $suboptimal->pop() // last;
Condition100%2/2
$suboptimal->pop // (last)
l!l
229
MC/DC100%1/1
$suboptimal->pop // (last)
$suboptimal->pop
Truth table100%2/2
A
A= $suboptimal->pop
Aresult
11
00
71  
72 # update all neighbors
73 22 foreach my $edge (values %{$vert->{$current}->{edges_out}}) {
74 19 ▶ if (($self->{d_dist}->{$edge->{to}} == -1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"d_dist"}{$edge->{"to"}} == -1 or $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}) 16 3 19
Condition100%3/3
$self->{"d_dist"}{$edge->{"to"}} == -1 or $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
l!l&&r!l&&!r
1423
MC/DC100%2/2
$self->{"d_dist"}{$edge->{"to"}} == -1 or $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
$self->{"d_dist"}{$edge->{"to"}} == -1 $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
Truth table100%3/3
A or B
A= $self->{"d_dist"}{$edge->{"to"}} == -1B= $self->{"d_dist"}{$edge->{"to"}} > $self->{"d_dist"}{$current} + $edge->{"weight"}
ABresult
1X1
011
000
75 ($self->{d_dist}->{$edge->{to}} > ($self->{d_dist}->{$current} + $edge->{weight}) )) {
76 $suboptimal->update(
77 $edge->{to},
78 $self->{d_dist}->{$edge->{to}} = $self->{d_dist}->{$current} + $edge->{weight}
79 16 );
80 }
81 }
82 }
83  
84 # trace the path from the destination to the start
85 9 my @path = ();
86 9 my $current = $to;
87 9 ▶ NODE: while ($current ne $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current ne $from) 11 5 16
88 11 foreach my $edge (values %{$vert->{$current}->{edges_in}}) {
89 13 ▶ if ($self->{d_dist}->{$current} == $self->{d_dist}->{$edge->{from}} + $edge->{weight}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"d_dist"}{$current} == $self->{"d_dist"}{$edge->{"from"}} + $edge->{"weight"}) 7 6 13
90 7 $current = $edge->{from};
91 7 unshift(@path, $edge);
92 7 next NODE;
93 }
94 }
95 # getting here means we found no predecessor - there is none.
96 # so there's no path.
97 4 return ();
98 }
99  
100 5 return @path;
101 }
102  
103 9 ▶sub dijkstra_first {
Subroutine dijkstra_first: called CC=1 SCAR=0.0
Pod undocumented
104 9 my ($self, $from, $to) = @_;
105 9 $self->{d_from} = $from;
106 9 $self->{d_dist} = {};
107 9 $self->{d_unvisited} = [ grep { $_ ne $from } keys(%{$self->{vertices}}) ];
108 9 $self->{d_suboptimal} = { $from => 0 };
109  
110 9 dijkstra_worker($self, $from, $to);
111 }
112  
113 5 ▶sub dijkstra_continue {
Subroutine dijkstra_continue: called CC=1 SCAR=0.0
Pod undocumented
114 5 my ($self, $from, $to, $del_to) = @_;
115 # instead of reinitializing, it should invoke the worker after initializing
116 # to a state that assumes that an edge to $del_to has just been deleted.
117 5 goto &dijkstra_first;
118 }
119  
120 9 ▶sub dijkstra {
Subroutine dijkstra: called CC=3 SCAR=11.0
Pod documented
121 9 my ($self, $from, $to, $del_to) = @_;
122 9 ▶ if (!defined($self->{d_from}) or $self->{d_from} ne $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'d_from'} or $self->{'d_from'} ne $from) { } 4 5 9
Condition100%3/3
not defined $self->{"d_from"} or $self->{"d_from"} ne $from
l!l&&r!l&&!r
135
MC/DC100%2/2
not defined $self->{"d_from"} or $self->{"d_from"} ne $from
not defined $self->{"d_from"} $self->{"d_from"} ne $from
Truth table100%3/3
not(A) or B
A= not defined $self->{"d_from"}B= $self->{"d_from"} ne $from
ABresult
1X1
011
000
123 4 goto &dijkstra_first;
124 } else {
125 5 goto &dijkstra_continue;
126 }
127 }
128  
129 0 ▶sub recursive_dijkstra {
Subroutine recursive_dijkstra: not called CC=6 SCAR=37.4
Pod undocumented
130 0 my ($self, $from, $to, $level, $del_to) = @_;
131 0 my @d = ([ $self->dijkstra($from, $to, $del_to) ]);
132  
133 0 ▶ if (!defined($d[0]->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d[0][0]) 0 0 0
134 0 return ();
135 }
136  
137 0 ▶ if ($level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level > 0) 0 0 0
138 0 foreach (0..(@{$d[0]}-1)) {
139 # from copies of the graph, remove one edge from the result path,
140 # and continue finding paths on that tree.
141 0 my $ffffuuuu = $self->{_queue_maker};
142 0 $self->{_queue_maker} = "omg";
143 0 my $g2 = dclone($self);
144 0 $g2->{_queue_maker} = $self->{_queue_maker} = $ffffuuuu;
145  
146 0 $g2->del_edge($d[0]->[$_]->{from}, $d[0]->[$_]->{to});
147 0 my @new = $g2->recursive_dijkstra($from, $to, $level - 1, $d[0]->[$_]->{to});
148  
149 # add all new paths, unless they are already present in the result set
150 0 foreach my $n (@new) {
151 0 ▶ push(@d, $n) unless (grep { $n ~~ $_ } @d);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$n ~~ $_;} @d 0 0 0
152 }
153 }
154 }
155  
156 0 @d;
157 }
158  
159 31 ▶sub add_edge {
Subroutine add_edge: called CC=4 SCAR=14.6
Pod documented
160 31 my ($self, $from, $to, $weight, $user_data) = @_;
161 31 $self->del_edge($from => $to);
162  
163 31 ▶ my $edge = { from => $from, to => $to, weight => $weight, (defined($user_data) ? (user_data => $user_data) : ()) };
Branch: 1 branch
BranchTrueFalseTotal
defined $user_data ? : 0 31 31
164  
165 31 push(@{$self->{edges}}, $edge);
166 31 ▶ ($self->{vertices}->{$from} // $self->add_vertex($from))->{edges_out}->{$to} = $edge;
Condition33%1/3
$self->{"vertices"}{$from} // $self->add_vertex($from)
l!l&&r!l&&!r
3100
MC/DC0%0/2
$self->{"vertices"}{$from} // $self->add_vertex($from)
$self->{"vertices"}{$from} $self->add_vertex($from)
Truth table33%1/3
A // B
A= $self->{"vertices"}{$from}B= $self->add_vertex($from)
ABresult
1X1
011
000
167 31 ▶ ($self->{vertices}->{$to } // $self->add_vertex($to ))->{edges_in }->{$from} = $edge;
Condition66%2/3
$self->{"vertices"}{$to} // $self->add_vertex($to)
l!l&&r!l&&!r
16150
MC/DC0%0/2
$self->{"vertices"}{$to} // $self->add_vertex($to)
$self->{"vertices"}{$to} $self->add_vertex($to)
Truth table66%2/3
A // B
A= $self->{"vertices"}{$to}B= $self->add_vertex($to)
ABresult
1X1
011
000
168 }
169  
170 33 ▶sub del_edge {
Subroutine del_edge: called CC=2 SCAR=6.9
Pod documented
171 33 my ($self, $from, $to) = @_;
172  
173 # find the edge. assume it only exists once -> only delete the first.
174 # while we're at it, delete the edge from the source vertex...
175 33 my $e = $self->{vertices}->{$from}->{edges_out}->{$to};
176 33 ▶ return undef if (!defined($e));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 31 2 33
177 2 delete($self->{vertices}->{$from}->{edges_out}->{$to});
178  
179 # now search it in the destination vertex' list, delete it there
180 # also only delete the first matching one here (though now there
181 # shouldn't be any duplicates at all because now we're matching the
182 # actual edge, not just its endpoints like above.
183 2 delete($self->{vertices}->{$to}->{edges_in}->{$from});
184  
185 # and remove it from the graph's vertex list
186 2 @{$self->{edges}} = grep { $_ != $e } @{$self->{edges}}
187 }
188  
189 6 1;
190  
191 __END__
↑ Summary Fast/GraphPM.pm »