| 1 | # Copyright 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 | |||||||||||||
| 20 | package Graph::Maker::RookGrid; | ||||||||||||
| 21 | 1 | ▶ | use 5.004; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||||
|
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 | 27 | ▶ | sub _default_graph_maker { | ||||||||||
|
Subroutine
_default_graph_maker: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 27 | require Graph; | |||||||||||
| 35 | 27 | Graph->new(@_); | |||||||||||
| 36 | } | ||||||||||||
| 37 | 27 | ▶ | sub _make_graph { | ||||||||||
|
Subroutine
_make_graph: called CC=2 SCAR=7.1
| |||||||||||||
| 38 | 27 | my ($self, %params) = @_; | |||||||||||
| 39 | 27 | ▶ | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||||||||||
|
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::RookGrid::_default_graph_maker
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::RookGrid::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
| |||||||||||||
| 40 | 27 | return $graph_maker->(%params); | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | # last $dim runs fastest, per Graph::Maker::Grid | ||||||||||||
| 44 | 416 | ▶ | sub _coordinates_to_vertex { | ||||||||||
|
Subroutine
_coordinates_to_vertex: called CC=4 SCAR=14.0
| |||||||||||||
| 45 | 416 | my ($c, $dims) = @_; | |||||||||||
| 46 | 416 | my $v = $c->[0]; | |||||||||||
| 47 | 416 | ▶ | die if $c->[0] >= $dims->[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 416 | foreach my $i (1 .. $#$dims) { | |||||||||||
| 49 | 865 | $v *= $dims->[$i]; | |||||||||||
| 50 | 865 | $v += $c->[$i]; | |||||||||||
| 51 | 865 | ▶ | die if $c->[$i] >= $dims->[$i]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | } | ||||||||||||
| 53 | 416 | return $v+1; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 27 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||
| 57 | 27 | my ($self, %params) = @_; | |||||||||||
| 58 | ### RookGrid ... | ||||||||||||
| 59 | |||||||||||||
| 60 | 27 | ▶ | my $dims = delete($params{'dims'}) || []; | ||||||||||
|
Condition100%2/2
delete $params{'dims'} || []
MC/DC100%1/1
delete $params{'dims'} || []
delete $params{'dims'}
Truth table100%2/2
A
A= delete $params{'dims'}
| |||||||||||||
| 61 | ### $dims | ||||||||||||
| 62 | 27 | my $graph = $self->_make_graph(%params); | |||||||||||
| 63 | |||||||||||||
| 64 | 27 | ▶ | $graph->set_graph_attribute(name => "Rook Grid " | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | . (@$dims ? join('x',@$dims) : 'empty')); | ||||||||||||
| 66 | 27 | ▶ | unless (_aref_any_nonzero($dims)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | ### all dims zero ... | ||||||||||||
| 68 | 2 | return $graph; | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 25 | ▶ | my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 25 | my @c = (0) x scalar(@$dims); | |||||||||||
| 73 | 25 | for (;;) { | |||||||||||
| 74 | 148 | my $v = _coordinates_to_vertex(\@c, $dims); | |||||||||||
| 75 | 148 | $graph->add_vertex($v); | |||||||||||
| 76 | ### at: join(',',@c)."=[$v]" | ||||||||||||
| 77 | |||||||||||||
| 78 | # each coordinate i, each greater value in that coordinate | ||||||||||||
| 79 | 148 | foreach my $i (0 .. $#c) { | |||||||||||
| 80 | 430 | foreach my $i_to ($c[$i]+1 .. $dims->[$i]-1) { | |||||||||||
| 81 | 268 | my @c2 = @c; | |||||||||||
| 82 | 268 | $c2[$i] = $i_to; | |||||||||||
| 83 | 268 | my $v2 = _coordinates_to_vertex(\@c2, $dims); | |||||||||||
| 84 | ### edge: join(',',@c)."=[$v] to ".join(',',@c2)."=[$v2]" | ||||||||||||
| 85 | 268 | $graph->$add_edge($v,$v2); | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | # increment @c coordinates | ||||||||||||
| 90 | 148 | for (my $i = 0; ; $i++) { | |||||||||||
| 91 | 294 | ▶ | if ($i > $#$dims) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 25 | return $graph; | |||||||||||
| 93 | } | ||||||||||||
| 94 | 269 | ▶ | if (++$c[$i] < $dims->[$i]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 123 | last; | |||||||||||
| 96 | } | ||||||||||||
| 97 | 146 | $c[$i] = 0; | |||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 27 | ▶ | sub _aref_any_nonzero { | ||||||||||
|
Subroutine
_aref_any_nonzero: called CC=3 SCAR=11.1
| |||||||||||||
| 103 | 27 | my ($aref) = @_; | |||||||||||
| 104 | 27 | foreach my $elem (@$aref) { | |||||||||||
| 105 | 25 | ▶ | if ($elem) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 25 | return 1; | |||||||||||
| 107 | } | ||||||||||||
| 108 | } | ||||||||||||
| 109 | 2 | return 0; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 1 | Graph::Maker->add_factory_type('rook_grid' => __PACKAGE__); | |||||||||||
| 113 | 1 | 1; | |||||||||||
| 114 | |||||||||||||
| 115 | __END__ | ||||||||||||