BinomialTree.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« BiStar.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::BinomialTree;
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 Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=2.6
23 use Carp 'croak';
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 parent => \&_add_edge_reverse,
48 bigger => 'add_edge',
49 child => 'add_edge',
50 both => 'add_cycle');
51  
52 sub _count_1bits {
53 my ($n) = @_;
54 my $ret = 0;
55 while ($n) { $ret += $n&1; $n >>= 1; }
56 return $ret;
57 }
58  
59 sub init {
60 my ($self, %params) = @_;
61  
62 my $N = delete($params{'N'});
63 my $order = delete($params{'order'});
64 my $direction_type = delete($params{'direction_type'}) || 'both';
65 my $coordinate_type = delete($params{'coordinate_type'}) || '';
66  
67 my $graph = _make_graph(\%params);
68 my $limit;
69 if (! defined $N) {
70 $limit = 2**$order - 1;
71 $graph->set_graph_attribute (name => "Binomial Tree, Order $order");
72 } else {
73 $limit = $N-1;
74 if ($N <= 0) {
75 $graph->set_graph_attribute (name => "Binomial Tree, Empty");
76 } else {
77 $graph->set_graph_attribute (name => "Binomial Tree, 0 to $limit");
78 }
79 }
80  
81 ### $limit
82 if ($limit >= 0) {
83 $graph->add_vertex(0);
84 my $add_edge = ($graph->is_undirected ? 'add_edge'
85 : ($add_edge_method{$direction_type}
86 || croak "Unrecognised direction_type ",$direction_type));
87  
88 foreach my $i (1 .. $limit) {
89 my $parent = $i & ~($i ^ ($i-1)); # clear lowest 1-bit
90 ### edge: "$parent down to $i"
91 $graph->$add_edge($parent, $i);
92 }
93 }
94  
95 if ($coordinate_type eq 'across') {
96 foreach my $i (0 .. $limit) {
97 $graph->set_vertex_attribute($i, x => $i>>1);
98 $graph->set_vertex_attribute($i, y => - _count_1bits($i));
99 }
100 }
101 return $graph;
102 }
103  
104 Graph::Maker->add_factory_type('binomial_tree' => __PACKAGE__);
105 1;
106  
107 __END__
« BiStar.pm ↑ Summary Circulant.pm »