line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 2017 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
|
|
|
|
|
|
|
# . |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
package Graph::Maker::RookGrid; |
21
|
1
|
|
|
1
|
|
644
|
use 5.004; |
|
1
|
|
|
|
|
3
|
|
22
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
23
|
1
|
|
|
1
|
|
107
|
use Graph::Maker; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use vars '$VERSION','@ISA'; |
26
|
|
|
|
|
|
|
$VERSION = 8; |
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
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# last $dim runs fastest, per Graph::Maker::Grid |
39
|
|
|
|
|
|
|
sub _coordinates_to_vertex { |
40
|
|
|
|
|
|
|
my ($c, $dims) = @_; |
41
|
|
|
|
|
|
|
my $v = $c->[0]; |
42
|
|
|
|
|
|
|
die if $c->[0] >= $dims->[0]; |
43
|
|
|
|
|
|
|
foreach my $i (1 .. $#$dims) { |
44
|
|
|
|
|
|
|
$v *= $dims->[$i]; |
45
|
|
|
|
|
|
|
$v += $c->[$i]; |
46
|
|
|
|
|
|
|
die if $c->[$i] >= $dims->[$i]; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
return $v+1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub init { |
52
|
|
|
|
|
|
|
my ($self, %params) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $dims = delete($params{'dims'}) || []; |
55
|
|
|
|
|
|
|
my $cyclic = delete($params{'cyclic'}); |
56
|
|
|
|
|
|
|
my $graph_maker = delete($params{'graph_maker'}) || \&_default_graph_maker; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
### RookGrid ... |
59
|
|
|
|
|
|
|
### $dims |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $graph = $graph_maker->(%params); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$graph->set_graph_attribute(name => "Rook Grid " |
64
|
|
|
|
|
|
|
. (@$dims ? join('x',@$dims) : 'empty')); |
65
|
|
|
|
|
|
|
unless (_aref_any_nonzero($dims)) { |
66
|
|
|
|
|
|
|
### all dims zero ... |
67
|
|
|
|
|
|
|
return $graph; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my @c = (0) x scalar(@$dims); |
71
|
|
|
|
|
|
|
for (;;) { |
72
|
|
|
|
|
|
|
my $v = _coordinates_to_vertex(\@c, $dims); |
73
|
|
|
|
|
|
|
$graph->add_vertex($v); |
74
|
|
|
|
|
|
|
### at: join(',',@c)."=[$v]" |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
foreach my $i (0 .. $#c) { |
77
|
|
|
|
|
|
|
foreach my $i_to ($c[$i]+1 .. $dims->[$i]-1) { |
78
|
|
|
|
|
|
|
my @c2 = @c; |
79
|
|
|
|
|
|
|
$c2[$i] = $i_to; |
80
|
|
|
|
|
|
|
if ($cyclic) { |
81
|
|
|
|
|
|
|
$c2[$i] %= $dims->[$i]; |
82
|
|
|
|
|
|
|
} |
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__ |