Johnson.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 16 / 16 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 97.1% 102 / 105 CC 16 SCAR 15.6
CC
16
Cov
99%
init
8.0
_sorted_arefs_count_same
5.0
_N_K_subsets
4.0
CRAP 4.8
« GosperIsland.pm ↑ Summary Circulant.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::Johnson;
20 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 $VERSION = 19;
26 2 @ISA = ('Graph::Maker');
27  
28 # uncomment this to run the ### lines
29 # use Smart::Comments;
30  
31  
32 59 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
33 59 require Graph;
34 59 return Graph->new(@_);
35 }
36 59 ▶sub _make_graph {
Subroutine _make_graph: called CC=2 SCAR=7.1
37 59 my ($params) = @_;
38 59 ▶ my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::Johnson::_default_graph_maker
l!l
059
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::Johnson::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
Aresult
11
00
39 59 return $graph_maker->(%$params);
40 }
41  
42 # $aref and $bref are arrayrefs of integers sorted in ascending order.
43 # Return the number of integers common to both arrays.
44 1305 ▶sub _sorted_arefs_count_same {
Subroutine _sorted_arefs_count_same: called CC=5 SCAR=16.1
45 1305 my ($aref, $bref) = @_;
46 1305 my $i = 0;
47 1305 my $j = 0;
48 1305 my $count = 0;
49 1305 ▶ while ($i <= $#$aref && $j <= $#$bref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $aref->$#* and $j <= $bref->$#*) 5561 1305 6866
Condition100%3/3
$i <= $aref->$#* and $j <= $bref->$#*
!ll&&!rl&&r
11761295561
MC/DC100%2/2
$i <= $aref->$#* and $j <= $bref->$#*
$i <= $aref->$#* $j <= $bref->$#*
Truth table100%3/3
A and B
A= $i <= $aref->$#*B= $j <= $bref->$#*
ABresult
0X0
100
111
50 5561 ▶ if ($aref->[$i] == $bref->[$j]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aref->[$i] == $bref->[$j]) { } 2632 2929 5561
elsif ($aref->[$i] < $bref->[$j]) { } 1794 1135 2929
51 2632 $count++;
52 2632 $i++;
53 2632 $j++;
54 } elsif ($aref->[$i] < $bref->[$j]) {
55 1794 $i++;
56 } else {
57 1135 $j++;
58 }
59 }
60 1305 return $count;
61 }
62  
63 # Return a list of arrayrefs which contain all the $K element subsets of
64 # integers 1 to $N. The integers in each subset are in ascending order.
65 # The order of the subsets themselves is unspecified.
66 #
67 59 ▶sub _N_K_subsets {
Subroutine _N_K_subsets: called CC=4 SCAR=13.9
68 59 my ($N, $K) = @_;
69  
70 # $pos is 0 to $K-1
71 # $upto[$pos] to maximum $N-($K-1)+$pos
72 # the top $upto[$K-1] runs to maximum $N-($K-1)+($K-1) = $N
73 # so $upto[$pos] <= $N-$K+1+$pos
74 59 my @ret;
75 59 my @upto = (0);
76 59 my $pos = 0;
77 59 my $limit = $N-$K+1;
78 59 for (;;) {
79 ### @upto
80 ### $pos
81 680 ▶ if (++$upto[$pos] > $limit+$pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$upto[$pos] > $limit + $pos) { } 229 451 680
82 # backtrack
83 229 ▶ if (--$pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$pos < 0) 59 170 229
84 59 last;
85 }
86 } else {
87 451 ▶ if (++$pos >= $K) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$pos >= $K) { } 281 170 451
88 ### subset: "@upto"
89 281 push @ret, [@upto];
90 281 $pos--;
91 } else {
92 170 $upto[$pos] = $upto[$pos-1];
93 }
94 }
95 }
96 59 return @ret;
97 }
98  
99  
100 23 ▶sub init {
Subroutine init: called CC=8 SCAR=20.8
Pod undocumented
101 23 my ($self, %params) = @_;
102  
103 23 ▶ my $N = delete($params{'N'}) || 0;
Condition100%2/2
delete $params{'N'} || 0
l!l
221
MC/DC100%1/1
delete $params{'N'} || 0
delete $params{'N'}
Truth table100%2/2
A
A= delete $params{'N'}
Aresult
11
00
104 23 ▶ my $K = delete($params{'K'}) || 0;
Condition100%2/2
delete $params{'K'} || 0
l!l
221
MC/DC100%1/1
delete $params{'K'} || 0
delete $params{'K'}
Truth table100%2/2
A
A= delete $params{'K'}
Aresult
11
00
105 ### $N
106 ### $K
107 23 my $graph = _make_graph(\%params);
108  
109 23 $graph->set_graph_attribute (name => "Johnson $N,$K");
110  
111 23 my @vertices = _N_K_subsets($N,$K);
112 23 foreach my $v (@vertices) {
113 88 $graph->add_vertex(join(',',@$v));
114 }
115  
116 23 ▶ my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_directed ? : 12 11 23
117 23 foreach my $i_from (0 .. $#vertices-1) {
118 65 my $from = $vertices[$i_from];
119 65 foreach my $i_to ($i_from+1 .. $#vertices) {
120 155 my $to = $vertices[$i_to];
121 ### consider: "from=".join(',',@$from)." to=".join(',',@$to)
122  
123 155 my $count = _sorted_arefs_count_same($from, $to);
124 ### $count
125 155 ▶ if ($count == $K - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $K - 1) 152 3 155
126 152 my $v_from = join(',',@$from);
127 152 my $v_to = join(',',@$to);
128 ### edge: "$v_from to $v_to"
129 152 $graph->$add_edge($v_from, $v_to);
130 }
131 }
132 }
133 23 return $graph;
134 }
135  
136 2 Graph::Maker->add_factory_type('Johnson' => __PACKAGE__);
137 2 1;
138  
139 __END__
« GosperIsland.pm ↑ Summary Circulant.pm »