| 1 | # Copyright 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 | |||
| 20 | package Graph::Maker::RookGrid; | ||
| 21 | 1 | ▶ | use 5.004; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use strict; |
|
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 = 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 ($self, %params) = @_; | ||
| 39 | my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; | ||
| 40 | return $graph_maker->(%params); | ||
| 41 | } | ||
| 42 | |||
| 43 | # last $dim runs fastest, per Graph::Maker::Grid | ||
| 44 | sub _coordinates_to_vertex { | ||
| 45 | my ($c, $dims) = @_; | ||
| 46 | my $v = $c->[0]; | ||
| 47 | die if $c->[0] >= $dims->[0]; | ||
| 48 | foreach my $i (1 .. $#$dims) { | ||
| 49 | $v *= $dims->[$i]; | ||
| 50 | $v += $c->[$i]; | ||
| 51 | die if $c->[$i] >= $dims->[$i]; | ||
| 52 | } | ||
| 53 | return $v+1; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub init { | ||
| 57 | my ($self, %params) = @_; | ||
| 58 | ### RookGrid ... | ||
| 59 | |||
| 60 | my $dims = delete($params{'dims'}) || []; | ||
| 61 | ### $dims | ||
| 62 | my $graph = $self->_make_graph(%params); | ||
| 63 | |||
| 64 | $graph->set_graph_attribute(name => "Rook Grid " | ||
| 65 | . (@$dims ? join('x',@$dims) : 'empty')); | ||
| 66 | unless (_aref_any_nonzero($dims)) { | ||
| 67 | ### all dims zero ... | ||
| 68 | return $graph; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge'); | ||
| 72 | my @c = (0) x scalar(@$dims); | ||
| 73 | for (;;) { | ||
| 74 | my $v = _coordinates_to_vertex(\@c, $dims); | ||
| 75 | $graph->add_vertex($v); | ||
| 76 | ### at: join(',',@c)."=[$v]" | ||
| 77 | |||
| 78 | # each coordinate i, each greater value in that coordinate | ||
| 79 | foreach my $i (0 .. $#c) { | ||
| 80 | foreach my $i_to ($c[$i]+1 .. $dims->[$i]-1) { | ||
| 81 | my @c2 = @c; | ||
| 82 | $c2[$i] = $i_to; | ||
| 83 | my $v2 = _coordinates_to_vertex(\@c2, $dims); | ||
| 84 | ### edge: join(',',@c)."=[$v] to ".join(',',@c2)."=[$v2]" | ||
| 85 | $graph->$add_edge($v,$v2); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # increment @c coordinates | ||
| 90 | for (my $i = 0; ; $i++) { | ||
| 91 | if ($i > $#$dims) { | ||
| 92 | return $graph; | ||
| 93 | } | ||
| 94 | if (++$c[$i] < $dims->[$i]) { | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | $c[$i] = 0; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _aref_any_nonzero { | ||
| 103 | my ($aref) = @_; | ||
| 104 | foreach my $elem (@$aref) { | ||
| 105 | if ($elem) { | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | return 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | Graph::Maker->add_factory_type('rook_grid' => __PACKAGE__); | ||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ | ||