BinomialTree.pm

filter: Statementstmt 84.2% 48 / 57 Branchbran 75.0% 9 / 12 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 78.2% 72 / 92 CC 13 SCAR 19.1
CC
13
Cov
81%
init
11.4
_count_1bits
6.0
_make_graph
2.0
CRAP 6.7
« NoughtsAndCrosses.pm ↑ Summary BulgarianSolitaire.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::BinomialTree;
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 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 $VERSION = 19;
27 2 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 291 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 291 require Graph;
35 291 Graph->new(@_);
36 }
37 291 ▶sub _make_graph {
Subroutine _make_graph: called CC=2 SCAR=7.1
38 291 my ($params) = @_;
39 291 ▶ my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::BinomialTree::_default_graph_maker
l!l
0291
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::BinomialTree::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
Aresult
11
00
40 291 return $graph_maker->(%$params);
41 }
42 882 ▶sub _add_edge_reverse {
Subroutine _add_edge_reverse: called CC=1 SCAR=0.0
43 882 my ($graph, $u, $v) = @_;
44 882 $graph->add_edge($v,$u); # reverse
45 }
46 2 my %add_edge_method = (smaller => \&_add_edge_reverse,
47 parent => \&_add_edge_reverse,
48 bigger => 'add_edge',
49 child => 'add_edge',
50 both => 'add_cycle');
51  
52 0 ▶sub _count_1bits {
Subroutine _count_1bits: not called CC=2 SCAR=17.9
53 0 my ($n) = @_;
54 0 my $ret = 0;
55 0 ▶ while ($n) { $ret += $n&1; $n >>= 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
56 0 return $ret;
57 }
58  
59 291 ▶sub init {
Subroutine init: called CC=11 SCAR=24.3
Pod undocumented
60 291 my ($self, %params) = @_;
61  
62 291 my $N = delete($params{'N'});
63 291 my $order = delete($params{'order'});
64 291 ▶ my $direction_type = delete($params{'direction_type'}) || 'both';
Condition100%2/2
delete $params{'direction_type'} || 'both'
l!l
111180
MC/DC100%1/1
delete $params{'direction_type'} || 'both'
delete $params{'direction_type'}
Truth table100%2/2
A
A= delete $params{'direction_type'}
Aresult
11
00
65 291 ▶ my $coordinate_type = delete($params{'coordinate_type'}) || '';
Condition50%1/2
delete $params{'coordinate_type'} || ''
l!l
0291
MC/DC0%0/1
delete $params{'coordinate_type'} || ''
delete $params{'coordinate_type'}
Truth table50%1/2
A
A= delete $params{'coordinate_type'}
Aresult
11
00
66  
67 291 my $graph = _make_graph(\%params);
68 291 my $limit;
69 291 ▶ if (! defined $N) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $N) { } 52 239 291
70 52 $limit = 2**$order - 1;
71 52 $graph->set_graph_attribute (name => "Binomial Tree, Order $order");
72 } else {
73 239 $limit = $N-1;
74 239 ▶ if ($N <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N <= 0) { } 8 231 239
75 8 $graph->set_graph_attribute (name => "Binomial Tree, Empty");
76 } else {
77 231 $graph->set_graph_attribute (name => "Binomial Tree, 0 to $limit");
78 }
79 }
80  
81 ### $limit
82 291 ▶ if ($limit >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit >= 0) 283 8 291
83 283 $graph->add_vertex(0);
84 my $add_edge = ($graph->is_undirected ? 'add_edge'
85 283 ▶ : ($add_edge_method{$direction_type}
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_undirected ? : 123 160 283
Condition50%1/2
$add_edge_method{$direction_type} or croak("Unrecognised direction_type ", $direction_type)
l!l
1600
MC/DC0%0/1
$add_edge_method{$direction_type} or croak("Unrecognised direction_type ", $direction_type)
$add_edge_method{$direction_type}
Truth table50%1/2
A
A= $add_edge_method{$direction_type}
Aresult
11
00
86 || croak "Unrecognised direction_type ",$direction_type));
87  
88 283 foreach my $i (1 .. $limit) {
89 5742 my $parent = $i & ~($i ^ ($i-1)); # clear lowest 1-bit
90 ### edge: "$parent down to $i"
91 5742 $graph->$add_edge($parent, $i);
92 }
93 }
94  
95 291 ▶ if ($coordinate_type eq 'across') {
Branch: 1 branch
BranchTrueFalseTotal
if ($coordinate_type eq "across") 0 291 291
96 0 foreach my $i (0 .. $limit) {
97 0 $graph->set_vertex_attribute($i, x => $i>>1);
98 0 $graph->set_vertex_attribute($i, y => - _count_1bits($i));
99 }
100 }
101 291 return $graph;
102 }
103  
104 2 Graph::Maker->add_factory_type('binomial_tree' => __PACKAGE__);
105 2 1;
106  
107 __END__
« NoughtsAndCrosses.pm ↑ Summary BulgarianSolitaire.pm »