| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Copyright 2015, 2016, 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::KnightGrid; |
|
21
|
1
|
|
|
1
|
|
813
|
use 5.004; |
|
|
1
|
|
|
|
|
4
|
|
|
22
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
17
|
|
|
23
|
1
|
|
|
1
|
|
110
|
use Graph::Maker; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use vars '$VERSION','@ISA'; |
|
26
|
|
|
|
|
|
|
$VERSION = 7; |
|
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
|
|
|
|
|
|
|
### KnightGrid ... |
|
59
|
|
|
|
|
|
|
### $dims |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $graph = $graph_maker->(%params); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$graph->set_graph_attribute(name => "Knight 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 $j (0 .. $#c) { |
|
78
|
|
|
|
|
|
|
next if $i == $j; |
|
79
|
|
|
|
|
|
|
foreach my $isign (1,-1) { |
|
80
|
|
|
|
|
|
|
foreach my $jsign (1,-1) { |
|
81
|
|
|
|
|
|
|
foreach my $offset (0, 1) { |
|
82
|
|
|
|
|
|
|
my @c2 = @c; |
|
83
|
|
|
|
|
|
|
$c2[$i] += (1+$offset)*$isign; |
|
84
|
|
|
|
|
|
|
$c2[$j] += (2-$offset)*$jsign; |
|
85
|
|
|
|
|
|
|
if ($cyclic) { |
|
86
|
|
|
|
|
|
|
$c2[$i] %= $dims->[$i]; |
|
87
|
|
|
|
|
|
|
$c2[$j] %= $dims->[$j]; |
|
88
|
|
|
|
|
|
|
} else { |
|
89
|
|
|
|
|
|
|
next unless ($c2[$i] >= 0 && $c2[$i] < $dims->[$i] |
|
90
|
|
|
|
|
|
|
&& $c2[$j] >= 0 && $c2[$j] < $dims->[$j]); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
my $v2 = _coordinates_to_vertex(\@c2, $dims); |
|
93
|
|
|
|
|
|
|
### edge: join(',',@c)."=[$v] to ".join(',',@c2)."=[$v2]" |
|
94
|
|
|
|
|
|
|
unless ($graph->has_edge($v,$v2)) { |
|
95
|
|
|
|
|
|
|
$graph->add_edge($v,$v2); |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# increment @c coordinates |
|
104
|
|
|
|
|
|
|
for (my $i = 0; ; $i++) { |
|
105
|
|
|
|
|
|
|
if ($i > $#$dims) { |
|
106
|
|
|
|
|
|
|
return $graph; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
if (++$c[$i] < $dims->[$i]) { |
|
109
|
|
|
|
|
|
|
last; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
$c[$i] = 0; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _aref_any_nonzero { |
|
117
|
|
|
|
|
|
|
my ($aref) = @_; |
|
118
|
|
|
|
|
|
|
foreach my $elem (@$aref) { |
|
119
|
|
|
|
|
|
|
if ($elem) { |
|
120
|
|
|
|
|
|
|
return 1; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
return 0; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Graph::Maker->add_factory_type('knight_grid' => __PACKAGE__); |
|
127
|
|
|
|
|
|
|
1; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |