Hanoi.pm

filter: Statementstmt 91.6% 66 / 72 Branchbran 72.7% 16 / 22 Conditioncond 52.3% 11 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 75.1% 103 / 137 CC 25 SCAR 31.2
CC
25
Cov
81%
init
30.8
_vertex_names_func_digits
2.0
_vertex_names_func_integer
2.0
CRAP 22.7
↑ Summary MostMaximumMatchingsTree.pm »
1 # Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2021 Kevin Ryde
2 #
3 # This file is part of Graph-Maker-Other.
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Graph-Maker-Other. See the file COPYING. If not, see
17 # <http://www.gnu.org/licenses/>.
18  
19 package Graph::Maker::Hanoi;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 $VERSION = 19;
27 1 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 19 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 19 require Graph;
35 19 Graph->new(@_);
36 }
37  
38 0 ▶sub _vertex_names_func_digits {
Subroutine _vertex_names_func_digits: not called CC=1 SCAR=6.9
39 0 my ($digits, $spindles) = @_;
40 0 return join('', reverse @$digits);
41 }
42 285 ▶sub _vertex_names_func_integer {
Subroutine _vertex_names_func_integer: called CC=2 SCAR=6.9
43 285 my ($digits, $spindles) = @_;
44 285 my $ret = 0;
45 285 foreach my $digit (reverse @$digits) { # digits high to low
46 1035 $ret = $ret*$spindles + $digit;
47 }
48 285 return $ret;
49 }
50  
51 19 ▶sub init {
Subroutine init: called CC=24 SCAR=34.3
Pod undocumented
52 19 my ($self, %params) = @_;
53 19 ▶ my $discs = delete($params{'discs'}) || 0;
Condition100%2/2
delete $params{'discs'} || 0
l!l
181
MC/DC100%1/1
delete $params{'discs'} || 0
delete $params{'discs'}
Truth table100%2/2
A
A= delete $params{'discs'}
Aresult
11
00
54 19 ▶ my $spindles = delete($params{'spindles'}) || 3;
Condition100%2/2
delete $params{'spindles'} || 3
l!l
154
MC/DC100%1/1
delete $params{'spindles'} || 3
delete $params{'spindles'}
Truth table100%2/2
A
A= delete $params{'spindles'}
Aresult
11
00
55 19 ▶ my $adjacency = delete($params{'adjacency'}) || 'any';
Condition50%1/2
delete $params{'adjacency'} || 'any'
l!l
019
MC/DC0%0/1
delete $params{'adjacency'} || 'any'
delete $params{'adjacency'}
Truth table50%1/2
A
A= delete $params{'adjacency'}
Aresult
11
00
56 19 ▶ my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::Hanoi::_default_graph_maker
l!l
019
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::Hanoi::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
Aresult
11
00
57  
58 # this not documented yet ...
59 19 ▶ my $vertex_names = delete($params{'vertex_names'}) || 'integer';
Condition50%1/2
delete $params{'vertex_names'} || 'integer'
l!l
019
MC/DC0%0/1
delete $params{'vertex_names'} || 'integer'
delete $params{'vertex_names'}
Truth table50%1/2
A
A= delete $params{'vertex_names'}
Aresult
11
00
60 19 ▶ my $vertex_name_func = $self->can("_vertex_names_func_$vertex_names")
Condition50%1/2
$self->can("_vertex_names_func_$vertex_names") || croak('Unrecognised vertex_names: ', $vertex_names)
l!l
190
MC/DC0%0/1
$self->can("_vertex_names_func_$vertex_names") || croak('Unrecognised vertex_names: ', $vertex_names)
$self->can("_vertex_names_func_$vertex_names")
Truth table50%1/2
A
A= $self->can("_vertex_names_func_$vertex_names")
Aresult
11
00
61 || croak "Unrecognised vertex_names: ",$vertex_names;
62  
63 19 my $graph = $graph_maker->(%params);
64 {
65 19 my $name = "Hanoi $discs";
66 19 ▶ if ($spindles != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spindles != 3) 15 4 19
67 15 $name .= " Discs, $spindles Spindles";
68 }
69 19 ▶ if ($adjacency ne 'any') {
Branch: 1 branch
BranchTrueFalseTotal
if ($adjacency ne "any") 0 19 19
70 0 $name .= ", \u$adjacency";
71 }
72 19 $graph->set_graph_attribute (name => $name);
73 }
74  
75 19 my $offset_limit;
76 19 ▶ if ($adjacency eq 'any' || $adjacency eq 'star') {
Branch: 2 branches
BranchTrueFalseTotal
if ($adjacency eq 'any' or $adjacency eq 'star') { } 19 0 19
elsif ($adjacency eq 'cyclic' or $adjacency eq 'linear') { } 0 0 0
Condition33%1/3
$adjacency eq "any" or $adjacency eq "star"
l!l&&r!l&&!r
1900
MC/DC0%0/2
$adjacency eq "any" or $adjacency eq "star"
$adjacency eq "any" $adjacency eq "star"
Truth table33%1/3
A or B
A= $adjacency eq "any"B= $adjacency eq "star"
ABresult
1X1
011
000
77 19 $offset_limit = $spindles - 1;
78 ▶ } elsif ($adjacency eq 'cyclic' || $adjacency eq 'linear') {
Condition0%0/3
$adjacency eq "cyclic" or $adjacency eq "linear"
l!l&&r!l&&!r
000
MC/DC0%0/2
$adjacency eq "cyclic" or $adjacency eq "linear"
$adjacency eq "cyclic" $adjacency eq "linear"
Truth table0%0/3
A or B
A= $adjacency eq "cyclic"B= $adjacency eq "linear"
ABresult
1X1
011
000
79 0 $offset_limit = 1;
80 } else {
81 0 croak "Unrecognised adjacency: ",$adjacency;
82 }
83 ### $offset_limit
84  
85 # $t[$d] is the spindle number (0 .. $spindles-1) which holds disc $d.
86 # $d = 0 is the smallest disc.
87 19 my @t = (0) x $discs;
88  
89 19 ▶ my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_directed ? : 13 6 19
90  
91 19 T: for (;;) {
92 169 my $v = $vertex_name_func->(\@t, $spindles);
93 169 $graph->add_vertex($v);
94 169 my @seen;
95 169 foreach my $d (0 .. $#t) { # disc number $d, smallest to biggest
96 633 my $from_digit = $t[$d];
97 633 ▶ next if $seen[$from_digit]++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen[$from_digit]++ 325 308 633
98 308 ▶ next if $from_digit && $adjacency eq 'star';
Branch: 1 branch
BranchTrueFalseTotal
if $from_digit and $adjacency eq "star" 0 308 308
Condition66%2/3
$from_digit and $adjacency eq "star"
!ll&&!rl&&r
1441640
MC/DC0%0/2
$from_digit and $adjacency eq "star"
$from_digit $adjacency eq "star"
Truth table66%2/3
A and B
A= $from_digitB= $adjacency eq "star"
ABresult
0X0
100
111
99  
100 308 foreach my $offset (1 .. $offset_limit) {
101 353 my $to_digit = $from_digit + $offset;
102 353 ▶ if ($adjacency eq 'cyclic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($adjacency eq 'cyclic') { } 0 353 353
103 0 $to_digit %= $spindles;
104 } else {
105 353 ▶ last if $to_digit >= $spindles;
Branch: 1 branch
BranchTrueFalseTotal
if $to_digit >= $spindles 164 189 353
106 }
107 189 ▶ next if $seen[$to_digit]; # smaller disc on other spindle
Branch: 1 branch
BranchTrueFalseTotal
if $seen[$to_digit] 73 116 189
108  
109 116 my @t2 = @t;
110 116 $t2[$d] = $to_digit;
111 116 my $v2 = $vertex_name_func->(\@t2, $spindles);
112 116 $graph->$add_edge($v, $v2);
113 ### edge: "pos=$d @t to @t2"
114 }
115 }
116  
117 # increment t ...
118 169 foreach my $pos (reverse 0 .. $#t) {
119 297 ▶ next T if ++$t[$pos] < $spindles;
Branch: 1 branch
BranchTrueFalseTotal
if ++$t[$pos] < $spindles 150 147 297
120 147 $t[$pos] = 0;
121 }
122 19 last; # no more @t configurations
123 }
124 19 return $graph;
125 }
126  
127 1 Graph::Maker->add_factory_type('hanoi' => __PACKAGE__);
128 1 1;
129  
130 __END__
↑ Summary MostMaximumMatchingsTree.pm »