HanoiExchange.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Hanoi.pm ↑ Summary TwinAlternateAreaTree.pm »
1 # Copyright 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::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=2.6
24  
25 use vars '$VERSION','@ISA';
26 $VERSION = 16;
27 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 sub _default_graph_maker {
34 require Graph;
35 Graph->new(@_);
36 }
37  
38 sub _vertex_names_func_digits {
39 my ($digits, $spindles) = @_;
40 return join('', reverse @$digits);
41 }
42 sub _vertex_names_func_integer {
43 my ($digits, $spindles) = @_;
44 my $ret = 0;
45 foreach my $digit (reverse @$digits) { # digits high to low
46 $ret = $ret*$spindles + $digit;
47 }
48 return $ret;
49 }
50  
51 sub init {
52 my ($self, %params) = @_;
53 ### HanoiExchange init: %params
54  
55 my $discs = delete($params{'discs'}) || 0;
56 my $spindles = delete($params{'spindles'}) || 3;
57 my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
58  
59 # this not documented yet ...
60 my $vertex_names = delete($params{'vertex_names'}) || 'integer';
61 my $vertex_name_func = $self->can("_vertex_names_func_$vertex_names")
62 || croak "Unrecognised vertex_names: ",$vertex_names;
63  
64 my $graph = $graph_maker->(%params);
65  
66 $graph->set_graph_attribute
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 my @t = (0) x $discs;
74 my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
75 T: for (;;) {
76 my $v = $vertex_name_func->(\@t, $spindles);
77 $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 my @t2;
83 if ($discs) {
84 @t2 = @t;
85 foreach my $to_digit (0 .. $t[0]-1) {
86 $t2[0] = $to_digit;
87 my $v2 = $vertex_name_func->(\@t2, $spindles);
88 $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 my @seen;
95 foreach my $d (1 .. $#t) { # disc number
96 next if $seen[$t[$d-1]]++; # $d-1 not on top of its spindle
97 next if $seen[$t[$d]]; # $d not on top of its spindle
98 next unless $t[$d-1] < $t[$d]; # one way so no duplicate edges
99 @t2 = @t;
100 ($t2[$d],$t2[$d-1]) = ($t2[$d-1],$t2[$d]); # exchange $d, $d-1
101 my $v2 = $vertex_name_func->(\@t2, $spindles);
102 $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 foreach my $pos (0 .. $#t) {
109 next T if ++$t[$pos] < $spindles;
110 $t[$pos] = 0;
111 }
112 last; # no more @t configurations
113 }
114 return $graph;
115 }
116  
117 Graph::Maker->add_factory_type('hanoi_exchange' => __PACKAGE__);
118 1;
119  
120 __END__
« Hanoi.pm ↑ Summary TwinAlternateAreaTree.pm »