Hanoi.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
« ExcessConfigurations.pm ↑ Summary HanoiExchange.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=2.6
24  
25 use vars '$VERSION','@ISA';
26 $VERSION = 17;
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 my $discs = delete($params{'discs'}) || 0;
54 my $spindles = delete($params{'spindles'}) || 3;
55 my $adjacency = delete($params{'adjacency'}) || 'any';
56 my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
57  
58 # this not documented yet ...
59 my $vertex_names = delete($params{'vertex_names'}) || 'integer';
60 my $vertex_name_func = $self->can("_vertex_names_func_$vertex_names")
61 || croak "Unrecognised vertex_names: ",$vertex_names;
62  
63 my $graph = $graph_maker->(%params);
64 {
65 my $name = "Hanoi $discs";
66 if ($spindles != 3) {
67 $name .= " Discs, $spindles Spindles";
68 }
69 if ($adjacency ne 'any') {
70 $name .= ", \u$adjacency";
71 }
72 $graph->set_graph_attribute (name => $name);
73 }
74  
75 my $offset_limit;
76 if ($adjacency eq 'any' || $adjacency eq 'star') {
77 $offset_limit = $spindles - 1;
78 } elsif ($adjacency eq 'cyclic' || $adjacency eq 'linear') {
79 $offset_limit = 1;
80 } else {
81 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 my @t = (0) x $discs;
88  
89 my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
90  
91 T: for (;;) {
92 my $v = $vertex_name_func->(\@t, $spindles);
93 $graph->add_vertex($v);
94 my @seen;
95 foreach my $d (0 .. $#t) { # disc number $d, smallest to biggest
96 my $from_digit = $t[$d];
97 next if $seen[$from_digit]++;
98 next if $from_digit && $adjacency eq 'star';
99  
100 foreach my $offset (1 .. $offset_limit) {
101 my $to_digit = $from_digit + $offset;
102 if ($adjacency eq 'cyclic') {
103 $to_digit %= $spindles;
104 } else {
105 last if $to_digit >= $spindles;
106 }
107 next if $seen[$to_digit]; # smaller disc on other spindle
108  
109 my @t2 = @t;
110 $t2[$d] = $to_digit;
111 my $v2 = $vertex_name_func->(\@t2, $spindles);
112 $graph->$add_edge($v, $v2);
113 ### edge: "pos=$d @t to @t2"
114 }
115 }
116  
117 # increment t ...
118 foreach my $pos (reverse 0 .. $#t) {
119 next T if ++$t[$pos] < $spindles;
120 $t[$pos] = 0;
121 }
122 last; # no more @t configurations
123 }
124 return $graph;
125 }
126  
127 Graph::Maker->add_factory_type('hanoi' => __PACKAGE__);
128 1;
129  
130 __END__
« ExcessConfigurations.pm ↑ Summary HanoiExchange.pm »