FibonacciTree.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
« Crown.pm ↑ Summary GosperIsland.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::FibonacciTree;
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 = 17;
27 @ISA = ('Graph::Maker');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32 sub _default_graph_maker {
33 require Graph;
34 Graph->new(@_);
35 }
36 sub _make_graph {
37 my ($self, %params) = @_;
38 my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker;
39 return $graph_maker->(%params);
40 }
41 sub _add_edge_reverse {
42 my ($graph, $u, $v) = @_;
43 $graph->add_edge($v,$u); # reverse
44 }
45 my %add_edge_method = (smaller => \&_add_edge_reverse,
46 parent => \&_add_edge_reverse,
47 bigger => 'add_edge',
48 child => 'add_edge',
49 both => 'add_cycle');
50  
51 sub init {
52 my ($self, %params) = @_;
53  
54 my $height = delete($params{'height'}) || 0;
55 my $series_reduced = delete($params{'series_reduced'}) ? 1 : 0;
56 my $leaf_reduced = delete $params{'leaf_reduced'};
57 my $direction_type = delete($params{'direction_type'}) || 'both';
58  
59 ### FibonacciTree ...
60 ### $height
61 ### %params
62  
63 my $graph = $self->_make_graph(%params);
64  
65 $graph->set_graph_attribute
66 (name => "Fibonacci Tree height $height"
67 . ($series_reduced && $leaf_reduced ? ', series and leaf reduced'
68 : $series_reduced ? ', series reduced'
69 : $leaf_reduced ? ', leaf reduced'
70 : ''));
71  
72 if ($height > 0) {
73 $graph->add_vertex(1);
74  
75 my @pending_n = (1);
76 my @pending_depth = (1);
77 my @pending_type = (0); # left
78 my $upto = 1;
79 my $add_edge = ($graph->is_undirected ? 'add_edge'
80 : $add_edge_method{$direction_type}
81 || croak "Unrecognised direction_type ",$direction_type);
82  
83 my $add = sub {
84 my ($parent) = @_;
85 my $n = ++$upto;
86 $graph->$add_edge($parent, $n);
87 return $n;
88 };
89  
90 foreach (2 .. $height) {
91 ### at: "row $_"
92 ### pending_n: join(',',@pending_n)
93 ### pending_depth: join(',',@pending_depth)
94 ### pending_type: join(',',@pending_type)
95  
96 my @new_pending_n;
97 my @new_pending_depth;
98 my @new_pending_type;
99  
100 while (@pending_n) {
101 my $parent = shift @pending_n;
102 my $depth = shift @pending_depth;
103 my $type = shift @pending_type;
104 ### under: "parent=$parent depth=$depth type=$type"
105  
106 next if $depth >= $height;
107 $depth++;
108  
109 # 0 0
110 # / \ / \
111 # d+1 0 1 d+1 0 0 d+2
112 # / \ | / \
113 # 0 1 0 d+2 0 0
114  
115 # left child
116 {
117 ### left to: "depth=$depth"
118 push @new_pending_n, $add->($parent);
119 push @new_pending_depth, $depth;
120 push @new_pending_type, 0; # non-delay
121 }
122  
123 if ($type == 0) {
124 if ($series_reduced && $depth < $height) {
125 # series reduced, step down to depth+2
126 $depth++;
127 } elsif ($leaf_reduced && $depth == $height) {
128 # leaf reduced, no node
129 next;
130 } else {
131 $type = 1;
132 }
133 } else {
134 # only single child under a delay
135 next;
136 }
137  
138 ### right to: "depth=$depth"
139 push @new_pending_n, $add->($parent);
140 push @new_pending_depth, $depth;
141 push @new_pending_type, $type; # right
142 }
143  
144 @pending_n = @new_pending_n;
145 @pending_depth = @new_pending_depth;
146 @pending_type = @new_pending_type;
147 }
148 }
149 return $graph;
150 }
151  
152 Graph::Maker->add_factory_type('fibonacci_tree' => __PACKAGE__);
153 1;
154  
155 __END__
« Crown.pm ↑ Summary GosperIsland.pm »