KnightGrid.pm

filter: Statementstmt 100% 64 / 64 Branchbran 87.5% 21 / 24 Conditioncond 92.3% 12 / 13 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 94.8% 110 / 116 CC 25 SCAR 26.0
CC
25
Cov
96%
init
20.0
_coordinates_to_vertex
4.1
_aref_any_nonzero
3.0
CRAP 13.5
« MostMaximumMatchingsTree.pm ↑ Summary FibonacciTree.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  
20 package Graph::Maker::KnightGrid;
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 22 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 22 require Graph;
35 22 Graph->new(@_);
36 }
37  
38 # last $dim runs fastest, per Graph::Maker::Grid
39 869 ▶sub _coordinates_to_vertex {
Subroutine _coordinates_to_vertex: called CC=4 SCAR=14.0
40 869 my ($c, $dims) = @_;
41 869 my $v = $c->[0];
42 869 ▶ die if $c->[0] >= $dims->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $c->[0] >= $dims->[0] 0 869 869
43 869 foreach my $i (1 .. $#$dims) {
44 1093 $v *= $dims->[$i];
45 1093 $v += $c->[$i];
46 1093 ▶ die if $c->[$i] >= $dims->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $c->[$i] >= $dims->[$i] 0 1093 1093
47 }
48 869 return $v+1;
49 }
50  
51 22 ▶sub init {
Subroutine init: called CC=20 SCAR=30.0
Pod undocumented
52 22 my ($self, %params) = @_;
53  
54 22 ▶ my $dims = delete($params{'dims'}) || [];
Condition100%2/2
delete $params{'dims'} || []
l!l
211
MC/DC100%1/1
delete $params{'dims'} || []
delete $params{'dims'}
Truth table100%2/2
A
A= delete $params{'dims'}
Aresult
11
00
55 22 my $cyclic = delete($params{'cyclic'});
56 22 ▶ my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::KnightGrid::_default_graph_maker
l!l
022
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::KnightGrid::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
Aresult
11
00
57  
58 ### KnightGrid ...
59 ### $dims
60  
61 22 my $graph = $graph_maker->(%params);
62  
63 22 ▶ $graph->set_graph_attribute(name =>
Branch: 2 branches
BranchTrueFalseTotal
@$dims ? : 18 4 22
$cyclic ? : 10 12 22
64 "Knight Grid "
65 . (@$dims ? join('x',@$dims) : 'empty')
66 . ($cyclic ? " cyclic" : ""));
67 22 ▶ unless (_aref_any_nonzero($dims)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_aref_any_nonzero($dims)) 4 18 22
68 ### all dims zero ...
69 4 return $graph;
70 }
71  
72 18 my @c = (0) x scalar(@$dims);
73 18 for (;;) {
74 85 my $v = _coordinates_to_vertex(\@c, $dims);
75 85 $graph->add_vertex($v);
76 ### at: join(',',@c)."=[$v]"
77  
78 85 foreach my $i (0 .. $#c) {
79 154 foreach my $j (0 .. $#c) {
80 308 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 154 154 308
81 154 foreach my $isign (1,-1) {
82 308 foreach my $jsign (1,-1) {
83 616 foreach my $offset (0, 1) {
84 1232 my @c2 = @c;
85 1232 $c2[$i] += (1+$offset)*$isign;
86 1232 $c2[$j] += (2-$offset)*$jsign;
87 1232 ▶ if ($cyclic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cyclic) { } 720 512 1232
88 720 $c2[$i] %= $dims->[$i];
89 720 $c2[$j] %= $dims->[$j];
90 } else {
91 512 ▶ next unless ($c2[$i] >= 0 && $c2[$i] < $dims->[$i]
Branch: 1 branch
BranchTrueFalseTotal
unless $c2[$i] >= 0 and $c2[$i] < $dims->[$i] and $c2[$j] >= 0 and $c2[$j] < $dims->[$j] 448 64 512
Condition100%9/9
$c2[$i] >= 0 and $c2[$i] < $dims->[$i] and $c2[$j] >= 0 and $c2[$j] < $dims->[$j]
!ll&&!rl&&r
3985064
$c2[$i] >= 0 and $c2[$i] < $dims->[$i] and $c2[$j] >= 0
!ll&&!rl&&r
34850114
$c2[$i] >= 0 and $c2[$i] < $dims->[$i]
!ll&&!rl&&r
174174164
MC/DC100%4/4
$c2[$i] >= 0 and $c2[$i] < $dims->[$i] and $c2[$j] >= 0 and $c2[$j] < $dims->[$j]
$c2[$i] >= 0 $c2[$i] < $dims->[$i] $c2[$j] >= 0 $c2[$j] < $dims->[$j]
Truth table100%5/5
A and B and C and D
A= $c2[$i] >= 0B= $c2[$i] < $dims->[$i]C= $c2[$j] >= 0D= $c2[$j] < $dims->[$j]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
92 && $c2[$j] >= 0 && $c2[$j] < $dims->[$j]);
93 }
94 784 my $v2 = _coordinates_to_vertex(\@c2, $dims);
95 ### edge: join(',',@c)."=[$v] to ".join(',',@c2)."=[$v2]"
96 784 ▶ unless ($graph->has_edge($v,$v2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($graph->has_edge($v, $v2)) 139 645 784
97 139 $graph->add_edge($v,$v2);
98 }
99 }
100 }
101 }
102 }
103 }
104  
105 # increment @c coordinates
106 85 for (my $i = 0; ; $i++) {
107 132 ▶ if ($i > $#$dims) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $dims->$#*) 18 114 132
108 18 return $graph;
109 }
110 114 ▶ if (++$c[$i] < $dims->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$c[$i] < $dims->[$i]) 67 47 114
111 67 last;
112 }
113 47 $c[$i] = 0;
114 }
115 }
116 }
117  
118 22 ▶sub _aref_any_nonzero {
Subroutine _aref_any_nonzero: called CC=3 SCAR=11.1
119 22 my ($aref) = @_;
120 22 foreach my $elem (@$aref) {
121 18 ▶ if ($elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 18 0 18
122 18 return 1;
123 }
124 }
125 4 return 0;
126 }
127  
128 1 Graph::Maker->add_factory_type('knight_grid' => __PACKAGE__);
129 1 1;
130  
131 __END__
« MostMaximumMatchingsTree.pm ↑ Summary FibonacciTree.pm »