TwinAlternateAreaTree.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« HanoiExchange.pm ↑ Summary TwindragonAreaTree.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=2.6
24  
25 use vars '$VERSION','@ISA';
26 $VERSION = 16;
27 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 sub _default_graph_maker {
34 require Graph;
35 Graph->new(@_);
36 }
37 sub _make_graph {
38 my ($params) = @_;
39 my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
40 return $graph_maker->(%$params);
41 }
42 sub _add_edge_reverse {
43 my ($graph, $u, $v) = @_;
44 $graph->add_edge($v,$u); # reverse
45 }
46 my %add_edge_method = (smaller => \&_add_edge_reverse,
47 bigger => 'add_edge',
48 both => 'add_cycle');
49  
50 sub _vertex_name_func_decimal {
51 my ($v) = @_;
52 return $v;
53 }
54 sub _vertex_name_func_binary {
55 my ($v) = @_;
56 return sprintf '%b', $v;
57 }
58 sub _vertex_name_func_xy {
59 my ($v) = @_;
60 my $x = 0;
61 my $y = 0;
62 my $bit = 1;
63 while ($v) {
64 if ($v & 1) { $x |= $bit; }
65 $v >>= 1;
66 if ($v & 1) { $y |= $bit; }
67 $v >>= 1;
68 $bit <<= 1;
69 }
70 $x = $y-$x;
71 return "$x,$y";
72 }
73 sub init {
74 my ($self, %params) = @_;
75  
76 my $level = delete($params{level}) || 0;
77 my $direction_type = delete($params{'direction_type'}) || 'both';
78  
79 # undocumented parameter
80 my $vertex_name_type = delete($params{'vertex_name_type'}) || 'decimal';
81 my $vertex_names_func = $self->can("_vertex_name_func_$vertex_name_type")
82 || croak 'Unrecognised vertex_name_type: ',$vertex_name_type;
83  
84 ### $level
85 ### $vertex_name_type
86  
87 my $graph = _make_graph(\%params);
88 $graph->set_graph_attribute(name => "Twin Alternate Area Tree $level");
89 $graph->set_graph_attribute(vertex_name_type => $vertex_name_type);
90 $graph->add_vertex($vertex_names_func->(0));
91 my $add_edge = ($graph->is_undirected ? 'add_edge'
92 : ($add_edge_method{$direction_type}
93 || croak "Unrecognised direction_type ",$direction_type));
94  
95 V: foreach my $v (0 .. 2**$level-1) {
96 ### $v
97  
98 # v = ...1
99 # edge from = ...0
100 if ($v & 1) {
101 ### horizontal ...
102 my $from = $v ^ 1;
103 $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 my $bit = 1;
112 for (my $pos = 1; ; $pos++, $bit<<=1) {
113 $pos < $level or next V;
114 next if $v & $bit; # still all 1s
115 unless ($v & ($bit<<1)) { # must be 00 11..11
116 my $to = $v+1+($bit<<1);
117 $graph->$add_edge($vertex_names_func->($v),
118 $vertex_names_func->($to));
119 }
120 last;
121 }
122 }
123 return $graph;
124 }
125  
126 Graph::Maker->add_factory_type('twin_alternate_area_tree' => __PACKAGE__);
127 1;
128 __END__
« HanoiExchange.pm ↑ Summary TwindragonAreaTree.pm »