TwinAlternateAreaTree.pm

filter: Statementstmt 76.1% 51 / 67 Branchbran 62.5% 10 / 16 Conditioncond 66.6% 8 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 1 totaltotal 71.0% 81 / 114 CC 16 SCAR 21.8
CC
16
Cov
73%
_vertex_name_func_xy
20.0
init
12.0
_make_graph
2.0
CRAP 8.8
« Keller.pm ↑ Summary BinaryBeanstalk.pm »
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 package Graph::Maker::TwinAlternateAreaTree;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp 'croak';
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 53 ▶sub _default_graph_maker {
Subroutine _default_graph_maker: called CC=1 SCAR=0.0
34 53 require Graph;
35 53 Graph->new(@_);
36 }
37 53 ▶sub _make_graph {
Subroutine _make_graph: called CC=2 SCAR=7.1
38 53 my ($params) = @_;
39 53 ▶ my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::TwinAlternateAreaTree::_default_graph_maker
l!l
053
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::TwinAlternateAreaTree::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
Aresult
11
00
40 53 return $graph_maker->(%$params);
41 }
42 247 ▶sub _add_edge_reverse {
Subroutine _add_edge_reverse: called CC=1 SCAR=0.0
43 247 my ($graph, $u, $v) = @_;
44 247 $graph->add_edge($v,$u); # reverse
45 }
46 1 my %add_edge_method = (smaller => \&_add_edge_reverse,
47 bigger => 'add_edge',
48 both => 'add_cycle');
49  
50 3033 ▶sub _vertex_name_func_decimal {
Subroutine _vertex_name_func_decimal: called CC=1 SCAR=0.0
51 3033 my ($v) = @_;
52 3033 return $v;
53 }
54 0 ▶sub _vertex_name_func_binary {
Subroutine _vertex_name_func_binary: not called CC=1 SCAR=6.9
55 0 my ($v) = @_;
56 0 return sprintf '%b', $v;
57 }
58 0 ▶sub _vertex_name_func_xy {
Subroutine _vertex_name_func_xy: not called CC=4 SCAR=30.0
59 0 my ($v) = @_;
60 0 my $x = 0;
61 0 my $y = 0;
62 0 my $bit = 1;
63 0 ▶ while ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
64 0 ▶ if ($v & 1) { $x |= $bit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v & 1) 0 0 0
65 0 $v >>= 1;
66 0 ▶ if ($v & 1) { $y |= $bit; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v & 1) 0 0 0
67 0 $v >>= 1;
68 0 $bit <<= 1;
69 }
70 0 $x = $y-$x;
71 0 return "$x,$y";
72 }
73 53 ▶sub init {
Subroutine init: called CC=12 SCAR=24.9
Pod undocumented
74 53 my ($self, %params) = @_;
75  
76 53 ▶ my $level = delete($params{level}) || 0;
Condition100%2/2
delete $params{'level'} || 0
l!l
467
MC/DC100%1/1
delete $params{'level'} || 0
delete $params{'level'}
Truth table100%2/2
A
A= delete $params{'level'}
Aresult
11
00
77 53 ▶ my $direction_type = delete($params{'direction_type'}) || 'both';
Condition100%2/2
delete $params{'direction_type'} || 'both'
l!l
1637
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
78  
79 # undocumented parameter
80 53 ▶ my $vertex_name_type = delete($params{'vertex_name_type'}) || 'decimal';
Condition50%1/2
delete $params{'vertex_name_type'} || 'decimal'
l!l
053
MC/DC0%0/1
delete $params{'vertex_name_type'} || 'decimal'
delete $params{'vertex_name_type'}
Truth table50%1/2
A
A= delete $params{'vertex_name_type'}
Aresult
11
00
81 53 ▶ my $vertex_names_func = $self->can("_vertex_name_func_$vertex_name_type")
Condition50%1/2
$self->can("_vertex_name_func_$vertex_name_type") || croak('Unrecognised vertex_name_type: ', $vertex_name_type)
l!l
530
MC/DC0%0/1
$self->can("_vertex_name_func_$vertex_name_type") || croak('Unrecognised vertex_name_type: ', $vertex_name_type)
$self->can("_vertex_name_func_$vertex_name_type")
Truth table50%1/2
A
A= $self->can("_vertex_name_func_$vertex_name_type")
Aresult
11
00
82 || croak 'Unrecognised vertex_name_type: ',$vertex_name_type;
83  
84 ### $level
85 ### $vertex_name_type
86  
87 53 my $graph = _make_graph(\%params);
88 53 $graph->set_graph_attribute(name => "Twin Alternate Area Tree $level");
89 53 $graph->set_graph_attribute(vertex_name_type => $vertex_name_type);
90 53 $graph->add_vertex($vertex_names_func->(0));
91 my $add_edge = ($graph->is_undirected ? 'add_edge'
92 53 ▶ : ($add_edge_method{$direction_type}
Branch: 1 branch
BranchTrueFalseTotal
$graph->is_undirected ? : 18 35 53
Condition50%1/2
$add_edge_method{$direction_type} or croak("Unrecognised direction_type ", $direction_type)
l!l
350
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
93 || croak "Unrecognised direction_type ",$direction_type));
94  
95 53 V: foreach my $v (0 .. 2**$level-1) {
96 ### $v
97  
98 # v = ...1
99 # edge from = ...0
100 1543 ▶ if ($v & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v & 1) 768 775 1543
101 ### horizontal ...
102 768 my $from = $v ^ 1;
103 768 $graph->$add_edge($vertex_names_func->($from),
104 $vertex_names_func->($v));
105 }
106  
107 # v = ...00 11...11
108 # \-----/ zero or more low 1 bits
109 # to = ...11 00...00
110 #
111 1543 my $bit = 1;
112 1543 for (my $pos = 1; ; $pos++, $bit<<=1) {
113 2987 ▶ $pos < $level or next V;
Branch: 1 branch
BranchTrueFalseTotal
$pos < $level or next V 2888 99 2987
114 2888 ▶ next if $v & $bit; # still all 1s
Branch: 1 branch
BranchTrueFalseTotal
if $v & $bit 1444 1444 2888
115 1444 ▶ unless ($v & ($bit<<1)) { # must be 00 11..11
Branch: 1 branch
BranchTrueFalseTotal
unless ($v & $bit << 1) 722 722 1444
116 722 my $to = $v+1+($bit<<1);
117 722 $graph->$add_edge($vertex_names_func->($v),
118 $vertex_names_func->($to));
119 }
120 1444 last;
121 }
122 }
123 53 return $graph;
124 }
125  
126 1 Graph::Maker->add_factory_type('twin_alternate_area_tree' => __PACKAGE__);
127 1 1;
128 __END__
« Keller.pm ↑ Summary BinaryBeanstalk.pm »