| 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::TwindragonAreaTree; | ||
| 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 = 17; | ||
| 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 init { | ||
| 51 | my ($self, %params) = @_; | ||
| 52 | |||
| 53 | my $level = delete($params{level}) || 0; | ||
| 54 | my $direction_type = delete($params{'direction_type'}) || 'both'; | ||
| 55 | ### $level | ||
| 56 | |||
| 57 | my $graph = _make_graph(\%params); | ||
| 58 | $graph->set_graph_attribute(name => "Twindragon Area Tree $level"); | ||
| 59 | $graph->add_vertex(0); | ||
| 60 | my $add_edge = ($graph->is_undirected ? 'add_edge' | ||
| 61 | : ($add_edge_method{$direction_type} | ||
| 62 | || croak "Unrecognised direction_type ",$direction_type)); | ||
| 63 | |||
| 64 | V: foreach my $v (0 .. 2**$level-1) { | ||
| 65 | # ...1 edge from ...0 | ||
| 66 | if ($v & 1) { | ||
| 67 | my $from = $v ^ 1; | ||
| 68 | $graph->$add_edge($from, $v); # smaller to bigger | ||
| 69 | } | ||
| 70 | |||
| 71 | # ...10 11...11 | ||
| 72 | # \-----/ zero or more low 1 bits | ||
| 73 | # edge to | ||
| 74 | # ...01 00...00 | ||
| 75 | # | ||
| 76 | my $bit = 1; | ||
| 77 | for (my $pos = 1; ; $pos++) { | ||
| 78 | $pos < $level or next V; | ||
| 79 | $v & $bit or last; | ||
| 80 | $bit <<= 1; | ||
| 81 | } | ||
| 82 | $bit <<= 1; | ||
| 83 | if ($v & $bit) { | ||
| 84 | my $from = $v-$bit+1; | ||
| 85 | $graph->$add_edge($from, $v); # smaller to bigger | ||
| 86 | } | ||
| 87 | } | ||
| 88 | return $graph; | ||
| 89 | } | ||
| 90 | |||
| 91 | Graph::Maker->add_factory_type('twindragon_area_tree' => __PACKAGE__); | ||
| 92 | 1; | ||
| 93 | __END__ | ||