| 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
MC/DC100%1/1
delete $params{'discs'} || 0
delete $params{'discs'}
Truth table100%2/2
A
A= delete $params{'discs'}
| |||||||||||||||||||||||||||||||||
| 54 | 19 | ▶ | my $spindles = delete($params{'spindles'}) || 3; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'spindles'} || 3
MC/DC100%1/1
delete $params{'spindles'} || 3
delete $params{'spindles'}
Truth table100%2/2
A
A= delete $params{'spindles'}
| |||||||||||||||||||||||||||||||||
| 55 | 19 | ▶ | my $adjacency = delete($params{'adjacency'}) || 'any'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'adjacency'} || 'any'
MC/DC0%0/1
delete $params{'adjacency'} || 'any'
delete $params{'adjacency'}
Truth table50%1/2
A
A= delete $params{'adjacency'}
| |||||||||||||||||||||||||||||||||
| 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
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'}
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # this not documented yet ... | ||||||||||||||||||||||||||||||||
| 59 | 19 | ▶ | my $vertex_names = delete($params{'vertex_names'}) || 'integer'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'vertex_names'} || 'integer'
MC/DC0%0/1
delete $params{'vertex_names'} || 'integer'
delete $params{'vertex_names'}
Truth table50%1/2
A
A= delete $params{'vertex_names'}
| |||||||||||||||||||||||||||||||||
| 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)
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")
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 67 | 15 | $name .= " Discs, $spindles Spindles"; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 19 | ▶ | if ($adjacency ne 'any') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$adjacency eq "any" or $adjacency eq "star"
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"
| |||||||||||||||||||||||||||||||||
| 77 | 19 | $offset_limit = $spindles - 1; | |||||||||||||||||||||||||||||||
| 78 | ▶ | } elsif ($adjacency eq 'cyclic' || $adjacency eq 'linear') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$adjacency eq "cyclic" or $adjacency eq "linear"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 98 | 308 | ▶ | next if $from_digit && $adjacency eq 'star'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$from_digit and $adjacency eq "star"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 103 | 0 | $to_digit %= $spindles; | |||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||
| 105 | 353 | ▶ | last if $to_digit >= $spindles; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | 189 | ▶ | next if $seen[$to_digit]; # smaller disc on other spindle | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||