| 1 | # Copyright 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::BinaryBeanstalk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 147 | ▶ | sub _default_graph_maker { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_default_graph_maker: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 147 | require Graph; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 147 | Graph->new(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 147 | ▶ | sub _make_graph { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_graph: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 147 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 147 | ▶ | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'graph_maker'} || \&Graph::Maker::BinaryBeanstalk::_default_graph_maker
MC/DC0%0/1
delete $params{'graph_maker'} || \&Graph::Maker::BinaryBeanstalk::_default_graph_maker
delete $params{'graph_maker'}
Truth table50%1/2
A
A= delete $params{'graph_maker'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 147 | return $graph_maker->(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 103 | ▶ | sub _add_edge_reverse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_edge_reverse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 103 | my ($graph, $u, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 103 | $graph->add_edge($v,$u); # reverse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my %add_edge_method = (smaller => \&_add_edge_reverse, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | parent => \&_add_edge_reverse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | bigger => 'add_edge', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | child => 'add_edge', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | both => 'add_cycle'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 147 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 147 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 147 | my $height = delete($params{'height'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 147 | my $N = delete($params{'N'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 147 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 147 | my $graph = $self->_make_graph(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 147 | ▶ | if ((defined $height && $height > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
defined $height and $height > 0 or defined $N and $N > 0
defined $height and $height > 0
defined $N and $N > 0
MC/DC75%3/4
defined $height and $height > 0 or defined $N and $N > 0
defined $height
$height > 0
defined $N
$N > 0
Truth table57%4/7
A and B or C and D
A= defined $heightB= $height > 0C= defined $ND= $N > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | || (defined $N && $N > 0)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 135 | $graph->add_vertex(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | my $add_edge = ($graph->is_undirected ? 'add_edge' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 135 | ▶ | : $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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | || croak "Unrecognised direction_type ",$direction_type); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 135 | my $row_start = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 135 | my $v = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 135 | my $h = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 135 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1209 | my $parent = $v - _count_1_bits($v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | ### at: "$v parent $parent h=$h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1209 | ▶ | if ($parent >= $row_start) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 680 | $row_start = $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 680 | $h++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 680 | ▶ | if (defined $height && $h > $height) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $height and $h > $height
MC/DC100%2/2
defined $height and $h > $height
defined $height
$h > $height
Truth table100%3/3
A and B
A= defined $heightB= $h > $height
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # stop for height limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 50 | $graph->set_graph_attribute(name=>"Binary Beanstalk, Height $height"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 50 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1159 | ▶ | if (defined $N && $v >= $N) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $N and $v >= $N
MC/DC100%2/2
defined $N and $v >= $N
defined $N
$v >= $N
Truth table100%3/3
A and B
A= defined $NB= $v >= $N
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # stop for N limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 85 | $graph->set_graph_attribute (name => "Binary Beanstalk, $N Vertices"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 85 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1074 | $graph->$add_edge($parent, $v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1074 | $v++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 12 | $graph->set_graph_attribute (name => "Binary Beanstalk, Empty"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 147 | return $graph; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1209 | ▶ | sub _count_1_bits { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_count_1_bits: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1209 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1209 | my $count = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1209 | ▶ | while ($n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3676 | $count += ($n & 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3676 | $n >>= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1209 | return $count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | Graph::Maker->add_factory_type('binary_beanstalk' => __PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||