HanoiExchange.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 100% 14 / 14 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 90.0% 90 / 100 CC 17 SCAR 23.7
CC
17
Cov
94%
init
16.0
_vertex_names_func_digits
2.0
_vertex_names_func_integer
2.0
CRAP 10.7
« FibonacciTree.pm ↑ Summary Keller.pm »
1 # Copyright 2021, 2022 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::HanoiExchange;
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 34 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 34 require Graph;
35 34 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 1665 ▶sub _vertex_names_func_integer {
Subroutine _vertex_names_func_integer: called CC=2 SCAR=6.9
43 1665 my ($digits, $spindles) = @_;
44 1665 my $ret = 0;
45 1665 foreach my $digit (reverse @$digits) { # digits high to low
46 6775 $ret = $ret*$spindles + $digit;
47 }
48 1665 return $ret;
49 }
50  
51 34 ▶sub init {
Subroutine init: called CC=16 SCAR=27.7
Pod undocumented
52 34 my ($self, %params) = @_;
53 ### HanoiExchange init: %params
54  
55 34 ▶ my $discs = delete($params{'discs'}) || 0;
Condition100%2/2
delete $params{'discs'} || 0
l!l
259
MC/DC100%1/1
delete $params{'discs'} || 0
delete $params{'discs'}
Truth table100%2/2
A
A= delete $params{'discs'}
Aresult
11
00
56 34 ▶ my $spindles = delete($params{'spindles'}) || 3;
Condition100%2/2
delete $params{'spindles'} || 3
l!l
1816
MC/DC100%1/1
delete $params{'spindles'} || 3
delete $params{'spindles'}
Truth table100%2/2
A
A= delete $params{'spindles'}
Aresult
11
00
57 34 ▶ my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::HanoiExchange::_default_graph_maker
l!l
034
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::HanoiExchange::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
Aresult
11
00
58  
59 # this not documented yet ...
60 34 ▶ my $vertex_names = delete($params{'vertex_names'}) || 'integer';
Condition50%1/2
delete $params{'vertex_names'} || 'integer'
l!l
034
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
61 34 ▶ 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
340
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
62 || croak "Unrecognised vertex_names: ",$vertex_names;
63  
64 34 my $graph = $graph_maker->(%params);
65  
66 34 ▶ $graph->set_graph_attribute
Branch: 1 branch
BranchTrueFalseTotal
$spindles == 3 ? : 17 17 34
67 (name =>
68 "Hanoi Exchange $discs"
69 . ($spindles == 3 ? '' : " Discs, $spindles Spindles"));
70  
71 # $t[$d] is the spindle number (0 .. $spindles-1) which holds disc $d.
72 # $d = 0 is the smallest disc.
73 34 my @t = (0) x $discs;
74 34 ▶ my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_directed ? : 20 14 34
75 34 T: for (;;) {
76 500 my $v = $vertex_name_func->(\@t, $spindles);
77 500 $graph->add_vertex($vertex_name_func->(\@t, $spindles));
78 ### from: "v= ".join(',',@t)
79  
80 # Smallest disc moves anywhere, other than its current spindle.
81 # To smaller spindle number so no duplicate edges.
82 500 my @t2;
83 500 ▶ if ($discs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discs) 491 9 500
84 491 @t2 = @t;
85 491 foreach my $to_digit (0 .. $t[0]-1) {
86 456 $t2[0] = $to_digit;
87 456 my $v2 = $vertex_name_func->(\@t2, $spindles);
88 456 $graph->$add_edge($v, $v2);
89 ### smallest move: "$t[0] to $t2[0] edge $v -- $v2"
90 }
91 }
92  
93 # $seen[$spindle] true when $spindle has one of the 0 .. $d-1 discs
94 500 my @seen;
95 500 foreach my $d (1 .. $#t) { # disc number
96 1505 ▶ next if $seen[$t[$d-1]]++; # $d-1 not on top of its spindle
Branch: 1 branch
BranchTrueFalseTotal
if $seen[$t[$d - 1]]++ 531 974 1505
97 974 ▶ next if $seen[$t[$d]]; # $d not on top of its spindle
Branch: 1 branch
BranchTrueFalseTotal
if $seen[$t[$d]] 556 418 974
98 418 ▶ next unless $t[$d-1] < $t[$d]; # one way so no duplicate edges
Branch: 1 branch
BranchTrueFalseTotal
unless $t[$d - 1] < $t[$d] 209 209 418
99 209 @t2 = @t;
100 209 ($t2[$d],$t2[$d-1]) = ($t2[$d-1],$t2[$d]); # exchange $d, $d-1
101 209 my $v2 = $vertex_name_func->(\@t2, $spindles);
102 209 $graph->$add_edge($v, $v2);
103 ### exchange: "pos=$d to vec ".join(',',@t2)." edge $v -- $v2"
104 }
105 ### final seen: @seen
106  
107 # increment t vector ...
108 500 foreach my $pos (0 .. $#t) {
109 740 ▶ next T if ++$t[$pos] < $spindles;
Branch: 1 branch
BranchTrueFalseTotal
if ++$t[$pos] < $spindles 466 274 740
110 274 $t[$pos] = 0;
111 }
112 34 last; # no more @t configurations
113 }
114 34 return $graph;
115 }
116  
117 1 Graph::Maker->add_factory_type('hanoi_exchange' => __PACKAGE__);
118 1 1;
119  
120 __END__
« FibonacciTree.pm ↑ Summary Keller.pm »