| 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=0.0
| |||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||
|
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 | 241 | ▶ | sub _default_graph_maker { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_default_graph_maker: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 33 | 241 | require Graph; | |||||||||||||||||||||||||||||||||||
| 34 | 241 | Graph->new(@_); | |||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||
| 36 | 241 | ▶ | sub _make_graph { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_make_graph: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||
| 37 | 241 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||
| 38 | 241 | ▶ | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::FibonacciTree::_default_graph_maker
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::FibonacciTree::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
| |||||||||||||||||||||||||||||||||||||
| 39 | 241 | return $graph_maker->(%params); | |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | 1184 | ▶ | sub _add_edge_reverse { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_add_edge_reverse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 42 | 1184 | my ($graph, $u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 43 | 1184 | $graph->add_edge($v,$u); # reverse | |||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||
| 45 | 1 | 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 | 241 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=19 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 52 | 241 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | 241 | ▶ | my $height = delete($params{'height'}) || 0; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'height'} || 0
MC/DC100%1/1
delete $params{'height'} || 0
delete $params{'height'}
Truth table100%2/2
A
A= delete $params{'height'}
| |||||||||||||||||||||||||||||||||||||
| 55 | 241 | ▶ | my $series_reduced = delete($params{'series_reduced'}) ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | 241 | my $leaf_reduced = delete $params{'leaf_reduced'}; | |||||||||||||||||||||||||||||||||||
| 57 | 241 | ▶ | my $direction_type = delete($params{'direction_type'}) || 'both'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params{'direction_type'} || 'both'
MC/DC100%1/1
delete $params{'direction_type'} || 'both'
delete $params{'direction_type'}
Truth table100%2/2
A
A= delete $params{'direction_type'}
| |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | ### FibonacciTree ... | ||||||||||||||||||||||||||||||||||||
| 60 | ### $height | ||||||||||||||||||||||||||||||||||||
| 61 | ### %params | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 241 | my $graph = $self->_make_graph(%params); | |||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | 241 | ▶ | $graph->set_graph_attribute | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$series_reduced and $leaf_reduced
MC/DC100%2/2
$series_reduced and $leaf_reduced
$series_reduced
$leaf_reduced
Truth table100%3/3
A and B
A= $series_reducedB= $leaf_reduced
| |||||||||||||||||||||||||||||||||||||
| 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 | 241 | ▶ | if ($height > 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 215 | $graph->add_vertex(1); | |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 215 | my @pending_n = (1); | |||||||||||||||||||||||||||||||||||
| 76 | 215 | my @pending_depth = (1); | |||||||||||||||||||||||||||||||||||
| 77 | 215 | my @pending_type = (0); # left | |||||||||||||||||||||||||||||||||||
| 78 | 215 | my $upto = 1; | |||||||||||||||||||||||||||||||||||
| 79 | my $add_edge = ($graph->is_undirected ? 'add_edge' | ||||||||||||||||||||||||||||||||||||
| 80 | 215 | ▶ | : $add_edge_method{$direction_type} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$add_edge_method{$direction_type} or croak("Unrecognised direction_type ", $direction_type)
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}
| |||||||||||||||||||||||||||||||||||||
| 81 | || croak "Unrecognised direction_type ",$direction_type); | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 7211 | ▶ | my $add = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 84 | 7211 | my ($parent) = @_; | |||||||||||||||||||||||||||||||||||
| 85 | 7211 | my $n = ++$upto; | |||||||||||||||||||||||||||||||||||
| 86 | 7211 | $graph->$add_edge($parent, $n); | |||||||||||||||||||||||||||||||||||
| 87 | 7211 | return $n; | |||||||||||||||||||||||||||||||||||
| 88 | 215 | }; | |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 215 | 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 | 797 | my @new_pending_n; | |||||||||||||||||||||||||||||||||||
| 97 | my @new_pending_depth; | ||||||||||||||||||||||||||||||||||||
| 98 | 797 | my @new_pending_type; | |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | 797 | ▶ | while (@pending_n) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | 6039 | my $parent = shift @pending_n; | |||||||||||||||||||||||||||||||||||
| 102 | 6039 | my $depth = shift @pending_depth; | |||||||||||||||||||||||||||||||||||
| 103 | 6039 | my $type = shift @pending_type; | |||||||||||||||||||||||||||||||||||
| 104 | ### under: "parent=$parent depth=$depth type=$type" | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 6039 | ▶ | next if $depth >= $height; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 107 | 4903 | $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 | 4903 | push @new_pending_n, $add->($parent); | |||||||||||||||||||||||||||||||||||
| 119 | 4903 | push @new_pending_depth, $depth; | |||||||||||||||||||||||||||||||||||
| 120 | 4903 | push @new_pending_type, 0; # non-delay | |||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 4903 | ▶ | if ($type == 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | 3773 | ▶ | if ($series_reduced && $depth < $height) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$series_reduced and $depth < $height
MC/DC100%2/2
$series_reduced and $depth < $height
$series_reduced
$depth < $height
Truth table100%3/3
A and B
A= $series_reducedB= $depth < $height
| |||||||||||||||||||||||||||||||||||||
| 125 | # series reduced, step down to depth+2 | ||||||||||||||||||||||||||||||||||||
| 126 | 1126 | $depth++; | |||||||||||||||||||||||||||||||||||
| 127 | ▶ | } elsif ($leaf_reduced && $depth == $height) { | |||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$leaf_reduced and $depth == $height
MC/DC100%2/2
$leaf_reduced and $depth == $height
$leaf_reduced
$depth == $height
Truth table100%3/3
A and B
A= $leaf_reducedB= $depth == $height
| |||||||||||||||||||||||||||||||||||||
| 128 | # leaf reduced, no node | ||||||||||||||||||||||||||||||||||||
| 129 | 1465 | next; | |||||||||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||||||||||
| 131 | 1182 | $type = 1; | |||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||||||||||
| 134 | # only single child under a delay | ||||||||||||||||||||||||||||||||||||
| 135 | 1130 | next; | |||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | ### right to: "depth=$depth" | ||||||||||||||||||||||||||||||||||||
| 139 | 2308 | push @new_pending_n, $add->($parent); | |||||||||||||||||||||||||||||||||||
| 140 | 2308 | push @new_pending_depth, $depth; | |||||||||||||||||||||||||||||||||||
| 141 | 2308 | push @new_pending_type, $type; # right | |||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 797 | @pending_n = @new_pending_n; | |||||||||||||||||||||||||||||||||||
| 145 | 797 | @pending_depth = @new_pending_depth; | |||||||||||||||||||||||||||||||||||
| 146 | 797 | @pending_type = @new_pending_type; | |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||
| 149 | 241 | return $graph; | |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | 1 | Graph::Maker->add_factory_type('fibonacci_tree' => __PACKAGE__); | |||||||||||||||||||||||||||||||||||
| 153 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||||||||||