BulgarianSolitaire.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 95.8% 23 / 24 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 95.9% 119 / 124 CC 18 SCAR 18.8
CC
18
Cov
97%
init
12.0
_partition_next
5.0
_make_graph
2.0
CRAP 6.6
« BinomialTree.pm ↑ Summary Petersen.pm »
1 # Copyright 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::BulgarianSolitaire;
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 Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use List::Util 'sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 $VERSION = 19;
29 1 @ISA = ('Graph::Maker');
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34  
35 151 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
36 151 require Graph;
37 151 return Graph->new(@_);
38 }
39 151 ▶sub _make_graph {
Subroutine _make_graph: called CC=2 SCAR=7.1
40 151 my ($params) = @_;
41 151 ▶ my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::BulgarianSolitaire::_default_graph_maker
l!l
0151
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::BulgarianSolitaire::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
Aresult
11
00
42 151 return $graph_maker->(%$params);
43 }
44  
45 # $aref is an arrayref to a partition, with terms in ascending order.
46 # Eg. [1,3,3,5]
47 # Modify the contents of $aref to the lexicographically next partition.
48 # Return 1 if that is done, or return 0 if no more partitions.
49 # The first (lex smallest) partition is [1,1,...,1] and iteration can
50 # proceed from there
51 #
52 # partition last terms ..., x, y
53 # x increment +1 as long as x+1 <= y-1, which is x<=y-2
54 # otherwise x increment +y, to make one last term x+y
55 # after x += increment and y -= increment, remaining y is applied as terms
56 # x,x,...,x, r where r>=x (the new incremented x that is)
57 #
58 2471 ▶sub _partition_next {
Subroutine _partition_next: called CC=5 SCAR=16.1
59 2471 my ($aref) = @_;
60 ### _partition_next(): join(',',@$aref)
61  
62 2471 ▶ if (@$aref < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref < 2) 81 2390 2471
63 ### no more ...
64 81 return 0;
65 }
66 2390 my $y = pop @$aref;
67 2390 my $x = $aref->[-1];
68 2390 ▶ my $inc = ($x <= $y-2 ? 1 : $y);
Branch: 1 branch
BranchTrueFalseTotal
$x <= $y - 2 ? : 1253 1137 2390
69 2390 $aref->[-1] = ($x += $inc);
70 2390 ▶ while ($y -= $inc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y -= $inc) 1942 2390 4332
71 1942 ▶ $inc = ($y >= $x<<1 ? $x : $y);
Branch: 1 branch
BranchTrueFalseTotal
$y >= $x << 1 ? : 689 1253 1942
72 1942 push @$aref, $inc;
73 }
74 ### ret: join(',',@$aref)
75 2390 return 1;
76 }
77  
78 # start 1 1 1 1 1
79 # 1 0 1 0 1
80 # ending 1, decrement by last term add to prev
81 # 1 0 1 0 0
82 # ending 0, decrement by second last +1, and all 1s
83 #
84 6514 ▶sub _composition_next {
Subroutine _composition_next: called CC=2 SCAR=6.9
85 6514 my ($aref) = @_;
86 6514 ▶ if (@$aref < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref < 2) 84 6430 6514
87 ### no more ...
88 84 return 0;
89 }
90 6430 my $y = pop @$aref;
91 6430 $aref->[-1]++;
92 6430 push @$aref, (1) x ($y-1);
93 6430 return 1;
94 }
95  
96 # composition parameter, whether to push new term onto end of partition,
97 # otherwise unshift onto start
98 1 my %compositions_to_end = (0 => 1,
99 append => 1,
100 prepend => 0);
101  
102 151 ▶sub init {
Subroutine init: called CC=12 SCAR=24.9
Pod undocumented
103 151 my ($self, %params) = @_;
104 ### BulgarianSolitaire init ...
105  
106 151 ▶ my $N = delete($params{'N'}) || 0;
Condition100%2/2
delete $params{'N'} || 0
l!l
14011
MC/DC100%1/1
delete $params{'N'} || 0
delete $params{'N'}
Truth table100%2/2
A
A= delete $params{'N'}
Aresult
11
00
107 151 ▶ my $compositions = delete($params{'compositions'}) || 0;
Condition100%2/2
delete $params{'compositions'} || 0
l!l
7081
MC/DC100%1/1
delete $params{'compositions'} || 0
delete $params{'compositions'}
Truth table100%2/2
A
A= delete $params{'compositions'}
Aresult
11
00
108 151 ▶ my $no_self_loop = delete($params{'no_self_loop'}) || 0;
Condition100%2/2
delete $params{'no_self_loop'} || 0
l!l
37114
MC/DC100%1/1
delete $params{'no_self_loop'} || 0
delete $params{'no_self_loop'}
Truth table100%2/2
A
A= delete $params{'no_self_loop'}
Aresult
11
00
109  
110 # $end true if new term goes at the end of the list
111 151 my $end = $compositions_to_end{$compositions};
112 151 ▶ if (! defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end) 0 151 151
113 0 croak "Unrecognised compositions type: ", $compositions;
114 }
115  
116 151 my $graph = _make_graph(\%params);
117 151 ▶ $graph->set_graph_attribute
Branch: 1 branch
BranchTrueFalseTotal
$compositions ? : 70 81 151
118 (name => "Bulgarian Solitaire N=$N"
119 . ($compositions ? ", Compositions ".ucfirst($compositions) : ''));
120  
121 ### $N
122  
123 151 ▶ my $next = $compositions ? \&_composition_next : \&_partition_next;
Branch: 1 branch
BranchTrueFalseTotal
$compositions ? : 70 81 151
124 151 my @from = (1) x $N;
125 151 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
&$next(\@from) and do { do { my $from_str = join(",", @from); my(@to) = map({$_ - 1;} @from); splice @to, $end ? scalar @to : 0, 0, scalar @from; @to = grep({$_;} @to); unless ($compositions) { @to = sort {$a <=> $b;} @to; } ; my $to_str = join(",", @to); if ($no_self_loop and $from_str eq $to_str) { $graph->add_vertex($from_str); } else { $graph->add_edge($from_str, $to_str); } } } 8562 151 8713
126 8713 my $from_str = join(',',@from);
127  
128 8713 my @to = map {$_ - 1} @from; # decrement each term
129 8713 ▶ splice @to, $end ? scalar(@to) : 0, 0, scalar(@from); # new term
Branch: 1 branch
BranchTrueFalseTotal
$end ? : 5544 3169 8713
130 8713 @to = grep {$_} @to; # discard 0s
131 8713 ▶ unless ($compositions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compositions) 2471 6242 8713
132 # sort for partitions, no sort for compositions
133 2471 @to = sort {$a<=>$b} @to;
134 }
135 8713 my $to_str = join(',',@to);
136 ### $to_str
137  
138 8713 ▶ if ($no_self_loop && $from_str eq $to_str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_self_loop and $from_str eq $to_str) { } 19 8694 8713
Condition100%3/3
$no_self_loop and $from_str eq $to_str
!ll&&!rl&&r
6469222519
MC/DC100%2/2
$no_self_loop and $from_str eq $to_str
$no_self_loop $from_str eq $to_str
Truth table100%3/3
A and B
A= $no_self_loopB= $from_str eq $to_str
ABresult
0X0
100
111
139 19 $graph->add_vertex ($from_str);
140 } else {
141 8694 $graph->add_edge ($from_str, $to_str);
142 }
143 } while ($next->(\@from));
144  
145 ### total vertices: scalar $graph->vertices
146 ### total edges : scalar $graph->edges
147  
148 151 return $graph;
149 }
150  
151 1 Graph::Maker->add_factory_type('Bulgarian_solitaire' => __PACKAGE__);
152 1 1;
153  
154 __END__
« BinomialTree.pm ↑ Summary Petersen.pm »