Graph/Graph6.pm

filter: Statementstmt 90.1% 302 / 335 Branchbran 81.9% 182 / 222 Conditioncond 72.0% 49 / 68 MC/DCmcdc 57.5% 23 / 40 Subroutinesub 100% 24 / 24 Podpod 100% 2 / 2 totaltotal 84.2% 582 / 691 SCAR 53.5
CC
144
Cov
85%
write_graph
68.4
__ANON__
39.4
__ANON__
23.6
CRAP 210.1
↑ Summary Graph/Writer/Graph6.pm »
1 # Copyright 2015, 2016, 2017, 2018 Kevin Ryde
2 #
3 # This file is part of Graph-Graph6.
4 #
5 # Graph-Graph6 is free software; you can redistribute it and/or modify it under
6 # 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 # Graph-Graph6 is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Graph-Graph6. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Graph::Graph6;
19 9 ▶use 5.006; # for 3-arg open
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 our @ISA = ('Exporter');
27 9 our @EXPORT_OK = ('read_graph','write_graph',
28 'HEADER_GRAPH6','HEADER_SPARSE6','HEADER_DIGRAPH6');
29  
30 9 our $VERSION = 8;
31  
32 # uncomment this to run the ### lines
33 # use Smart::Comments;
34  
35  
36 9 ▶use constant HEADER_GRAPH6 => '>>graph6<<';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use constant HEADER_SPARSE6 => '>>sparse6<<';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 9 ▶use constant HEADER_DIGRAPH6 => '>>digraph6<<';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 sub _read_header {
41 2 ▶ my ($fh, $str) = @_;
Subroutine _read_header: called CC=8 SCAR=27.7
42 2 for (;;) {
43 5 my $s2 = getc $fh;
44 5 ▶ if (! defined $str) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 0 5 5
45  
46 5 $str .= $s2;
47 5 ▶ if ($str eq substr(HEADER_GRAPH6, 0, length($str))) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str eq substr('>>graph6<<', 0, length $str)) { } 3 2 5
elsif ($str eq substr('>>sparse6<<', 0, length $str)) { } 0 2 2
elsif ($str eq substr('>>digraph6<<', 0, length $str)) { } 0 2 2
48 3 ▶ if (length($str) == length(HEADER_GRAPH6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str == 10) 0 3 3
49 ### header: $str
50 # $format = 'graph6';
51 0 return;
52 }
53  
54 } elsif ($str eq substr(HEADER_SPARSE6, 0, length($str))) {
55 0 ▶ if (length($str) == length(HEADER_SPARSE6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str == 11) 0 0 0
56 ### header: $str
57 # $format = 'sparse6';
58 0 return;
59 }
60  
61 } elsif ($str eq substr(HEADER_DIGRAPH6, 0, length($str))) {
62 0 ▶ if (length($str) == length(HEADER_DIGRAPH6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str == 12) 0 0 0
63 ### header: $str
64 # $format = 'digraph6';
65 0 return;
66 }
67 } else {
68 2 return $str;
69 }
70 }
71 }
72  
73 sub read_graph {
74 43 ▶ my %options = @_;
Subroutine read_graph: called CC=5 SCAR=16.3
Pod documented
75  
76 43 my $fh = $options{'fh'};
77 43 ▶ if (defined $options{'str'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"str"}) 32 11 43
78 32 require IO::String;
79 32 $fh = IO::String->new($options{'str'});
80 }
81  
82 43 my $skip_newlines = 1;
83 43 my $allow_header = 1;
84 43 my $format = 'graph6';
85 43 my $initial = 1;
86 43 my $error;
87  
88 # Return: byte 0 to 63
89 # or -1 and $error=undef if end of file
90 # or -1 and $error=string if something bad
91 my $read_byte = sub {
92 117 ▶ for (;;) {
Subroutine __ANON__: called CC=16 SCAR=28.1
93 152 my $str;
94 152 my $len = read($fh, $str, 1);
95 152 ▶ if (! defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 0 152 152
96 0 $error = "Error reading: $!";
97 0 return -1;
98 }
99 ### read byte: $str
100  
101 152 ▶ if ($skip_newlines && $str eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_newlines and $str eq "\n") 9 143 152
Condition100%3/3
$skip_newlines and $str eq "\n"
!ll&&!rl&&r
101429
MC/DC100%2/2
$skip_newlines and $str eq "\n"
$skip_newlines $str eq "\n"
Truth table100%3/3
A and B
A= $skip_newlinesB= $str eq "\n"
ABresult
0X0
100
111
102 # secret undocumented skipping of newlines, so skip blank lines
103 # rather than reckoning one newline as immediate end of file
104 ### skip initial newline ...
105 9 next;
106 }
107 143 $skip_newlines = 0;
108  
109 143 ▶ if ($allow_header && $str eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_header and $str eq ">") 2 141 143
Condition100%3/3
$allow_header and $str eq ">"
!ll&&!rl&&r
101402
MC/DC100%2/2
$allow_header and $str eq ">"
$allow_header $str eq ">"
Truth table100%3/3
A and B
A= $allow_headerB= $str eq ">"
ABresult
0X0
100
111
110 2 $str = _read_header($fh, $str);
111 2 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 2 0 2
112 2 $error = "Incomplete header: $str";
113 2 return -1;
114 }
115 0 $allow_header = 0;
116 0 next;
117 }
118 141 $allow_header = 0;
119  
120 141 my $n = ord($str) - 63;
121 141 ▶ if ($n >= 0 && $n <= 63) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 0 and $n <= 63) 96 45 141
Condition66%2/3
$n >= 0 and $n <= 63
!ll&&!rl&&r
45096
MC/DC50%1/2
$n >= 0 and $n <= 63
$n >= 0 $n <= 63
Truth table66%2/3
A and B
A= $n >= 0B= $n <= 63
ABresult
0X0
100
111
122 96 return $n;
123 }
124  
125 45 ▶ if ($str eq '' || $str eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "" or $str eq "\n") 12 33 45
Condition100%3/3
$str eq "" or $str eq "\n"
l!l&&r!l&&!r
6633
MC/DC100%2/2
$str eq "" or $str eq "\n"
$str eq "" $str eq "\n"
Truth table100%3/3
A or B
A= $str eq ""B= $str eq "\n"
ABresult
1X1
011
000
126 ### end of file or end of line ...
127 12 return -1;
128 }
129  
130 33 ▶ if ($initial && $str eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial and $str eq "&") 3 30 33
Condition100%3/3
$initial and $str eq "&"
!ll&&!rl&&r
4263
MC/DC100%2/2
$initial and $str eq "&"
$initial $str eq "&"
Truth table100%3/3
A and B
A= $initialB= $str eq "&"
ABresult
0X0
100
111
131 3 $format = 'digraph6';
132 ### $format
133 3 $initial = 0;
134 3 next;
135 }
136 30 ▶ if ($initial && $str eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial and $str eq ":") 23 7 30
Condition100%3/3
$initial and $str eq ":"
!ll&&!rl&&r
4323
MC/DC100%2/2
$initial and $str eq ":"
$initial $str eq ":"
Truth table100%3/3
A and B
A= $initialB= $str eq ":"
ABresult
0X0
100
111
137 23 $format = 'sparse6';
138 ### $format
139 23 $initial = 0;
140 23 next;
141 }
142 7 ▶ if ($str eq "\r") {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "\r") 0 7 7
143 ### skip CR ...
144 0 next;
145 }
146  
147 7 $error = "Unrecognised character: $str";
148 7 return -1;
149 }
150 43 };
151  
152 # Return: number 0 to 2^36-1
153 # -1 and $error=undef if end of file before any part of number
154 # -1 and $error if something bad, including partial number
155 my $read_number = sub {
156 42 ▶ my $n = $read_byte->();
Subroutine __ANON__: called CC=8 SCAR=31.6
157 42 $initial = 0;
158 42 ▶ if ($n <= 62) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 62) 41 1 42
159 41 return $n;
160 }
161 1 $n = $read_byte->();
162 1 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 1 0 1
163 1 ▶ $error ||= "Unexpected EOF";
Condition50%1/2
$error ||= "Unexpected EOF"
l!l
01
MC/DC0%0/1
$error ||= "Unexpected EOF"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
164 1 return -1;
165 }
166 0 my $len;
167 0 ▶ if ($n <= 62) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 62) { } 0 0 0
168 0 $len = 2;
169 } else {
170 0 $n = 0;
171 0 $len = 6;
172 }
173 0 foreach (1 .. $len) {
174 0 my $n2 = $read_byte->();
175 0 ▶ if ($n2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 < 0) 0 0 0
176 0 ▶ $error ||= "Unexpected EOF";
Condition0%0/2
$error ||= "Unexpected EOF"
l!l
00
MC/DC0%0/1
$error ||= "Unexpected EOF"
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
177 0 return -1;
178 }
179 0 $n = ($n << 6) + $n2;
180 }
181 0 return $n;
182 43 };
183  
184 # Return true if good.
185 # Return false and $error=string if something bad.
186 # Return false and $error=undef if EOF.
187 my $read = sub {
188  
189 43 ▶ if (! defined $fh) {
Subroutine __ANON__: called CC=38 SCAR=36.7
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 8 35 43
190 8 ▶ if (defined(my $filename = $options{'filename'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = $options{"filename"})) 8 0 8
191 open $fh, '<', $filename
192 8 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or do { $error = "Cannot open file ${filename}: $!"; return } 1 7 8
193 1 $error = "Cannot open file $filename: $!";
194 1 return;
195 };
196 }
197 }
198  
199 42 my $num_vertices = $read_number->();
200 ### $num_vertices
201  
202 42 ▶ if (my $format_func = $options{'format_func'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format_func = $options{"format_func"}) 13 29 42
203 13 $format_func->($format);
204 }
205 42 ▶ if (my $format_ref = $options{'format_ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format_ref = $options{"format_ref"}) 0 42 42
206 0 $$format_ref = $format;
207 }
208  
209 42 ▶ if ($num_vertices < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_vertices < 0) 11 31 42
210 11 return; # eof or possible error
211 }
212 31 ▶ if (my $num_vertices_func = $options{'num_vertices_func'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num_vertices_func = $options{"num_vertices_func"}) 18 13 31
213 18 $num_vertices_func->($num_vertices);
214 }
215 31 ▶ if (my $num_vertices_ref = $options{'num_vertices_ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num_vertices_ref = $options{"num_vertices_ref"}) 12 19 31
216 12 $$num_vertices_ref = $num_vertices;
217 }
218  
219 31 my $edge_func = $options{'edge_func'};
220 31 my $edge_aref = $options{'edge_aref'};
221 31 ▶ if ($edge_aref) { @$edge_aref = (); }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_aref) 11 20 31
222  
223 ### $format
224 31 ▶ if ($format eq 'sparse6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'sparse6') { } 22 9 31
225 ### sparse6 ...
226 22 my $v = 0;
227  
228 # number of bits required to represent $num_vertices - 1
229 22 my $width = 0;
230 22 ▶ while (($num_vertices-1) >> $width) { $width++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($num_vertices - 1 >> $width) 48 22 70
231  
232 22 my $bits = 0;
233 22 my $n = 0;
234 22 my $mask = (1 << $width) - 1;
235  
236 22 ▶ while ($v < $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < $num_vertices) 93 12 105
237 93 ▶ if ($bits < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits < 1) 40 53 93
238 40 $n = $read_byte->();
239 40 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 8 32 40
240 ### end n ...
241 ### $error
242 8 return ! defined $error;
243 }
244 32 $bits = 6;
245 }
246 85 $bits--;
247 85 my $b = ($n >> $bits) & 1; # first bit from $n
248 85 $v += $b; # propagate possible taintedness of $n,$b to $v
249 ### $b
250 ### to v: $v
251  
252 85 ▶ while ($bits < $width) { # fill $n,$bits to >= $width many bits
Branch: 1 branch
BranchTrueFalseTotal
if ($bits < $width) 19 83 102
253 19 my $n2 = $read_byte->();
254 19 ▶ if ($n2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 < 0) 2 17 19
255 ### end n2 ...
256 ### $error
257 2 return ! defined $error;
258 }
259 17 $bits += 6;
260 17 $n <<= 6;
261 17 $n |= $n2;
262 }
263 83 $bits -= $width;
264 83 my $x = ($n >> $bits) & $mask;
265 ### $x
266  
267 83 ▶ if ($x > $v) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x > $v) { } 19 64 83
elsif ($v < $num_vertices) { } 52 12 64
268 ### set v: $x
269 19 $v = $x;
270 } elsif ($v < $num_vertices) { # padding can make v>n-1
271 ### edge: "$x - $v"
272 52 ▶ if ($edge_func) { $edge_func->($x, $v); }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_func) 25 27 52
273 52 ▶ if ($edge_aref) { push @$edge_aref, [$x, $v]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_aref) 26 26 52
274 }
275 }
276 ### end ...
277  
278 } else {
279 ### graph6 or digraph6 ...
280 9 my $n;
281 my $mask;
282 9 my $from;
283 9 my $to;
284 my $output_edge = sub {
285 67 ▶ if ($n & $mask) {
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if ($n & $mask) 22 45 67
286 22 my $taint0 = $n & 0;
287 22 my $from_taint = $from + $taint0;
288 22 my $to_taint = $to + $taint0;
289 22 ▶ if ($edge_func) { $edge_func->( $from_taint, $to_taint); }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_func) 17 5 22
290 22 ▶ if ($edge_aref) { push @$edge_aref, [$from_taint, $to_taint]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($edge_aref) 1 21 22
291 }
292 9 };
293  
294 9 ▶ if ($format eq 'graph6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'graph6') { } 8 1 9
295 # graph6 goes by columns of "to" within which "from" runs 0 though to-1
296 # first column is to=1
297 8 $from = 0;
298 8 $to = 1;
299 8 ▶ while ($to < $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < $num_vertices) 10 8 18
300 10 ▶ if (($n = $read_byte->()) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = &$read_byte()) < 0) 0 10 10
301 0 ▶ $error ||= "Unexpected EOF"; # end of file is not ok
Condition0%0/2
$error ||= "Unexpected EOF"
l!l
00
MC/DC0%0/1
$error ||= "Unexpected EOF"
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
302 0 return;
303 }
304 10 ▶ for ($mask = 1 << 5; $mask != 0; $mask >>= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask != 0) 42 4 46
305 42 $output_edge->();
306 42 $from++;
307 42 ▶ if ($from >= $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from >= $to) 18 24 42
308 18 $to++;
309 18 ▶ last unless $to < $num_vertices;
Branch: 1 branch
BranchTrueFalseTotal
unless $to < $num_vertices 6 12 18
310 12 $from = 0;
311 }
312 }
313 }
314 } else {
315 # graph6 goes by rows of "from", within which "to" runs 0 to n-1
316 1 $from = 0;
317 1 $to = 0;
318 1 ▶ while ($from < $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from < $num_vertices) 5 1 6
319 5 ▶ if (($n = $read_byte->()) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = &$read_byte()) < 0) 0 5 5
320 0 ▶ $error ||= "Unexpected EOF"; # end of file is not ok
Condition0%0/2
$error ||= "Unexpected EOF"
l!l
00
MC/DC0%0/1
$error ||= "Unexpected EOF"
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
321 0 return;
322 }
323 5 ▶ for ($mask = 1 << 5; $mask != 0; $mask >>= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask != 0) 25 4 29
324 25 $output_edge->();
325 25 $to++;
326 25 ▶ if ($to >= $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to >= $num_vertices) 5 20 25
327 5 $from++;
328 5 ▶ last unless $from < $num_vertices;
Branch: 1 branch
BranchTrueFalseTotal
unless $from < $num_vertices 1 4 5
329 4 $to = 0;
330 }
331 }
332 }
333 }
334  
335 # read \n or \r\n, so can take successive graphs from file handle
336 9 for (;;) {
337 10 my $str;
338 10 my $len = read($fh, $str, 1);
339 10 ▶ if (! defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 0 10 10
340 0 $error = "Error reading: $!";
341 0 last;
342 }
343 10 ▶ if ($str eq "\r") {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "\r") 1 9 10
344 1 next; # skip CR in case reading MS-DOS file as bytes
345 }
346 9 ▶ if ($str eq '' || $str eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "" or $str eq "\n") 9 0 9
Condition66%2/3
$str eq "" or $str eq "\n"
l!l&&r!l&&!r
360
MC/DC0%0/2
$str eq "" or $str eq "\n"
$str eq "" $str eq "\n"
Truth table66%2/3
A or B
A= $str eq ""B= $str eq "\n"
ABresult
1X1
011
000
347 9 last; # EOF or EOL, good
348 }
349 }
350 }
351  
352 21 return 1;
353 43 };
354  
355  
356 43 ▶ if ($read->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$read()) 30 13 43
357 30 return 1; # successful read
358 }
359 13 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 11 2 13
360 ### $error
361 11 ▶ my $error_func = $options{'error_func'} || \&Carp::croak;
Condition100%2/2
$options{'error_func'} || \&Carp::croak
l!l
101
MC/DC100%1/1
$options{'error_func'} || \&Carp::croak
$options{'error_func'}
Truth table100%2/2
A
A= $options{'error_func'}
Aresult
11
00
362 11 $error_func->($error);
363 10 return undef;
364 }
365 2 return 0; # EOF
366 }
367  
368 #------------------------------------------------------------------------------
369  
370 # For internal use.
371 # Biggest shift is by (6-1)*6 = 30 bits, so ok in 32-bit Perls circa 5.8 and
372 # earlier (where counts were taken modulo 32, not full value).
373 sub _number_to_string {
374 55 ▶ my ($n) = @_;
Subroutine _number_to_string: called CC=4 SCAR=13.9
375 55 my $str;
376 my $bitpos;
377 55 ▶ if ($n > 258047) { # binary 0b_111110_111111_111111 octal 0767777
Branch: 2 branches
BranchTrueFalseTotal
if ($n > 258047) { } 7 48 55
elsif ($n > 62) { } 1 47 48
378 7 $str = '~~';
379 7 $bitpos = (6-1)*6;
380 } elsif ($n > 62) {
381 1 $str = '~';
382 1 $bitpos = (3-1)*6;
383 } else {
384 47 $str = '';
385 47 $bitpos = 0;
386 }
387 55 ▶ do { # big endian, high to low
Branch: 1 branch
BranchTrueFalseTotal
($bitpos -= 6) >= 0 and do { do { $str .= chr(($n >> $bitpos & 63) + 63) } } 37 55 92
388 92 $str .= chr( (($n >> $bitpos) & 0x3F) + 63 );
389 } while (($bitpos-=6) >= 0);
390 55 return $str;
391 }
392  
393 sub _edges_iterator_none {
394 2 ▶ return;
Subroutine _edges_iterator_none: called CC=1 SCAR=0.0
395 }
396 sub _edge_predicate_none {
397 1 ▶ return 0;
Subroutine _edge_predicate_none: called CC=1 SCAR=0.0
398 }
399  
400 sub write_graph {
401 46 ▶ my %options = @_;
Subroutine write_graph: called CC=60 SCAR=42.2
Pod documented
402 ### %options
403  
404 46 my $fh = $options{'fh'};
405 46 ▶ if (! $fh
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh and defined(my $str_ref = $options{"str_ref"})) 44 2 46
Condition66%2/3
not $fh and defined(my $str_ref = $options{"str_ref"})
!ll&&!rl&&r
0244
MC/DC50%1/2
not $fh and defined(my $str_ref = $options{"str_ref"})
not $fh defined(my $str_ref = $options{"str_ref"})
Truth table66%2/3
not(A) and B
A= not $fhB= defined(my $str_ref = $options{"str_ref"})
ABresult
0X0
100
111
406 && defined(my $str_ref = $options{'str_ref'})) {
407 ### str_ref ...
408 44 require IO::String;
409 44 $fh = IO::String->new($$str_ref);
410 }
411 46 ▶ if (! $fh
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh and defined(my $filename = $options{"filename"})) 2 44 46
Condition66%2/3
not $fh and defined(my $filename = $options{"filename"})
!ll&&!rl&&r
4402
MC/DC50%1/2
not $fh and defined(my $filename = $options{"filename"})
not $fh defined(my $filename = $options{"filename"})
Truth table66%2/3
not(A) and B
A= not $fhB= defined(my $filename = $options{"filename"})
ABresult
0X0
100
111
412 && defined(my $filename = $options{'filename'})) {
413 ### $filename
414 2 ▶ open $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $filename or return 0 0 2 2
415 or return 0;
416 }
417  
418 46 my $format = $options{'format'};
419 46 ▶ if (! defined $format) { $format = 'graph6'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format) 8 38 46
420  
421 46 my $num_vertices = $options{'num_vertices'};
422 46 ▶ if (! defined $num_vertices
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $num_vertices and my $edge_aref = $options{"edge_aref"}) 2 44 46
Condition66%2/3
not defined $num_vertices and my $edge_aref = $options{"edge_aref"}
!ll&&!rl&&r
4402
MC/DC50%1/2
not defined $num_vertices and my $edge_aref = $options{"edge_aref"}
not defined $num_vertices my $edge_aref = $options{"edge_aref"}
Truth table66%2/3
not(A) and B
A= not defined $num_verticesB= my $edge_aref = $options{"edge_aref"}
ABresult
0X0
100
111
423 && (my $edge_aref = $options{'edge_aref'})) {
424 # from maximum in edge_aref
425 2 $num_vertices = -1;
426 2 foreach my $edge (@$edge_aref) {
427 2 $num_vertices = max($num_vertices, @$edge);
428 }
429 2 $num_vertices += 1;
430 }
431 46 ▶ if (! defined $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num_vertices) 0 46 46
432 0 croak 'Missing num_vertices';
433 }
434 ### $num_vertices
435  
436 print $fh
437 46 ▶ ($options{'header'} ? ">>$format<<" : ()),
Branch: 4 branches
BranchTrueFalseTotal
print $fh $options{"header"} ? ">>$format<<" : (), $format eq "sparse6" ? ":" : ($format eq "digraph6" ? "&" : ()), _number_to_string($num_vertices) or return 0 0 46 46
$options{'header'} ? : 6 40 46
$format eq 'sparse6' ? : 22 24 46
$format eq 'digraph6' ? : 9 15 24
438 ($format eq 'sparse6' ? ':'
439 : $format eq 'digraph6' ? '&'
440 : ()),
441 _number_to_string($num_vertices)
442 or return 0;
443  
444 46 my $bitpos = 5;
445 46 my $word = 0;
446 my $put_bit = sub {
447 462 ▶ my ($bit) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
448 462 $word |= $bit << $bitpos;
449 462 ▶ if ($bitpos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitpos > 0) { } 385 77 462
450 385 $bitpos--;
451 } else {
452 77 ▶ print $fh chr($word + 63) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
print $fh chr $word + 63 or return 0 0 77 77
453 77 $bitpos = 5;
454 77 $word = 0;
455 }
456 462 return 1;
457 46 };
458  
459 46 ▶ if ($format eq 'sparse6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'sparse6') { } 22 24 46
460 22 my $edge_iterator;
461  
462 22 ▶ if (my $edge_aref = $options{'edge_aref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $edge_aref = $options{"edge_aref"}) 19 3 22
463 ### edge_aref ...
464 # swap to [from <= to]
465 19 ▶ my @edges = map { $_->[0] > $_->[1]
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] > $_->[1] ? : 2 36 38
466 ? [ $_->[1], $_->[0] ]
467 : $_
468 } @$edge_aref;
469 # sort to ascending "to", and within those ascending "from"
470 19 ▶ @edges = sort { ($a->[1] <=> $b->[1]) || ($a->[0] <=> $b->[0]) } @edges;
Branch: 1 branch
BranchTrueFalseTotal
$Graph::Graph6::a->[1] <=> $Graph::Graph6::b->[1] or $Graph::Graph6::a->[0] <=> $Graph::Graph6::b->[0] 13 15 28
471 $edge_iterator = sub {
472 57 ▶ return @{(shift @edges) || []};
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
shift @edges or [] 19 38 57
473 19 };
474 }
475  
476 22 ▶ if (! $edge_iterator
Branch: 1 branch
BranchTrueFalseTotal
if (not $edge_iterator and my $edge_predicate = $options{"edge_predicate"}) 1 21 22
Condition100%3/3
not $edge_iterator and my $edge_predicate = $options{"edge_predicate"}
!ll&&!rl&&r
1921
MC/DC100%2/2
not $edge_iterator and my $edge_predicate = $options{"edge_predicate"}
not $edge_iterator my $edge_predicate = $options{"edge_predicate"}
Truth table100%3/3
not(A) and B
A= not $edge_iteratorB= my $edge_predicate = $options{"edge_predicate"}
ABresult
0X0
100
111
477 && (my $edge_predicate = $options{'edge_predicate'})) {
478 ### edge_predicate ...
479 1 my $from = 0;
480 1 my $to = -1;
481 $edge_iterator = sub {
482 2 ▶ for (;;) {
Subroutine __ANON__: called CC=4 SCAR=13.9
483 497 $from++;
484 497 ▶ if ($from > $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from > $to) 32 465 497
485 32 $to++;
486 32 ▶ if ($to >= $num_vertices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to >= $num_vertices) 1 31 32
487 1 return;
488 }
489 31 $from = 0;
490 }
491 496 ▶ if ($edge_predicate->($from,$to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$edge_predicate($from, $to)) 1 495 496
492 1 return ($from,$to);
493 }
494 }
495 1 };
496 }
497  
498 22 ▶ $edge_iterator ||= \&_edges_iterator_none;
Condition100%2/2
$edge_iterator ||= \&Graph::Graph6::_edges_iterator_none
l!l
202
MC/DC100%1/1
$edge_iterator ||= \&Graph::Graph6::_edges_iterator_none
$edge_iterator
Truth table100%2/2
A
A= $edge_iterator
Aresult
11
00
499  
500 # $width = number of bits required to represent $num_vertices - 1
501 22 my $width = 0;
502 22 ▶ if ($num_vertices > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_vertices > 0) 21 1 22
503 21 ▶ while (($num_vertices-1) >> $width) { $width++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($num_vertices - 1 >> $width) 45 21 66
504 }
505 ### $width
506  
507 my $put_n = sub {
508 44 ▶ my ($n) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
509 44 ▶ for (my $i = $width-1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 104 44 148
510 104 ▶ $put_bit->(($n >> $i) & 1) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit($n >> $i & 1) or return 0 0 104 104
511 }
512 44 return 1;
513 22 };
514  
515 # When doing a "set v" for a new to >= v+2, the b[i] bit can be either 0
516 # or 1. When 1 it means v++ increment, and the x[i]=to is still >v so
517 # set v. The code here follows the nauty tools ntos6() and emits b[i]=1.
518  
519 22 my $v = 0;
520 22 ▶ while (my ($from, $to) = $edge_iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$edge_iterator()) 39 22 61
521 ### edge: "$from $to"
522  
523 39 ▶ if ($to == $v + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to == $v + 1) { } 20 19 39
524 ### increment v ...
525 20 ▶ $put_bit->(1) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(1) or return 0 0 20 20
526  
527 } else {
528 19 ▶ if ($to != $v) { # $to >= $v+2
Branch: 1 branch
BranchTrueFalseTotal
if ($to != $v) 5 14 19
529 ### set v ...
530 5 ▶ ($put_bit->(1) # set v done with b[i]=1
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(1) and &$put_n($to) or return 0 0 5 5
Condition33%1/3
&$put_bit(1) and &$put_n($to)
!ll&&!rl&&r
005
MC/DC0%0/2
&$put_bit(1) and &$put_n($to)
&$put_bit(1) &$put_n($to)
Truth table33%1/3
A and B
A= &$put_bit(1)B= &$put_n($to)
ABresult
0X0
100
111
531 && $put_n->($to))
532 or return 0;
533 }
534 19 ▶ $put_bit->(0) or return 0; # v unchanged
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(0) or return 0 0 19 19
535 }
536 ### write: $from
537 39 ▶ $put_n->($from) or return 0; # edge ($from, $v)
Branch: 1 branch
BranchTrueFalseTotal
&$put_n($from) or return 0 0 39 39
538  
539 39 $v = $to;
540 }
541  
542 22 ▶ if ($bitpos != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitpos != 5) 15 7 22
543 ### pad: $bitpos+1
544 ### $v
545  
546 # Rule for padding so not to look like self-loop n-1 to n-1.
547 # There are $bitpos+1 many bits to pad.
548 # b[i]=0 bit if num_vertices = 2,4,8,16 so width=1,2,3,4
549 # and pad >= width+1
550 # and edge involving n-2 so final v=n-2
551 # 0 111 is set v=n-1 provided prev <= n-2
552 # 1 111 is a v+1 and edge n-1,v which is n-1,n out of range
553 15 ▶ if (($width >= 1 && $width <= 4)
Branch: 1 branch
BranchTrueFalseTotal
if ($width >= 1 and $width <= 4 and $num_vertices == 1 << $width and $bitpos >= $width and $v == $num_vertices - 2) 0 15 15
Condition58%7/12
$width >= 1 and $width <= 4 and $num_vertices == 1 << $width and $bitpos >= $width and $v == $num_vertices - 2
!ll&&!rl&&r
1140
$width >= 1 and $width <= 4 and $num_vertices == 1 << $width and $bitpos >= $width
!ll&&!rl&&r
1104
$width >= 1 and $width <= 4 and $num_vertices == 1 << $width
!ll&&!rl&&r
0114
$width >= 1 and $width <= 4
!ll&&!rl&&r
0015
MC/DC0%0/5
$width >= 1 and $width <= 4 and $num_vertices == 1 << $width and $bitpos >= $width and $v == $num_vertices - 2
$width >= 1 $width <= 4 $num_vertices == 1 << $width $bitpos >= $width $v == $num_vertices - 2
Truth table33%2/6
A and B and C and D and E
A= $width >= 1B= $width <= 4C= $num_vertices == 1 << $widthD= $bitpos >= $widthE= $v == $num_vertices - 2
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
554 && $num_vertices == (1 << $width) # 1,2,4,8
555 && $bitpos >= $width # room for final b[i] and x[i]
556 && $v == $num_vertices - 2) {
557 ### pad 0 ...
558 0 ▶ $put_bit->(0) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(0) or return 0 0 0 0
559 }
560  
561 ### pad with 1s: $bitpos
562 15 ▶ until ($bitpos == 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bitpos == 5) 50 15 65
563 50 ▶ $put_bit->(1) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(1) or return 0 0 50 50
564 }
565 }
566  
567 } else {
568 24 my $edge_predicate = $options{'edge_predicate'};
569  
570 24 ▶ if (! $edge_predicate
Branch: 1 branch
BranchTrueFalseTotal
if (not $edge_predicate and my $edge_aref = $options{"edge_aref"}) 7 17 24
Condition100%3/3
not $edge_predicate and my $edge_aref = $options{"edge_aref"}
!ll&&!rl&&r
1437
MC/DC100%2/2
not $edge_predicate and my $edge_aref = $options{"edge_aref"}
not $edge_predicate my $edge_aref = $options{"edge_aref"}
Truth table100%3/3
not(A) and B
A= not $edge_predicateB= my $edge_aref = $options{"edge_aref"}
ABresult
0X0
100
111
571 && (my $edge_aref = $options{'edge_aref'})) {
572 ### edge_predicate from edge_aref ...
573 7 my %edge_hash;
574 7 foreach my $edge (@$edge_aref) {
575 17 my ($from, $to) = @$edge;
576 17 ▶ if ($from > $to && $format eq 'graph6') { ($from,$to) = ($to,$from); }
Branch: 1 branch
BranchTrueFalseTotal
if ($from > $to and $format eq "graph6") 2 15 17
Condition100%3/3
$from > $to and $format eq "graph6"
!ll&&!rl&&r
1232
MC/DC100%2/2
$from > $to and $format eq "graph6"
$from > $to $format eq "graph6"
Truth table100%3/3
A and B
A= $from > $toB= $format eq "graph6"
ABresult
0X0
100
111
577 17 $edge_hash{$from}->{$to} = undef;
578 }
579 $edge_predicate = sub {
580 65 ▶ my ($from, $to) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
581 65 return exists $edge_hash{$from}->{$to};
582 7 };
583 }
584  
585 24 ▶ $edge_predicate ||= \&_edge_predicate_none;
Condition100%2/2
$edge_predicate ||= \&Graph::Graph6::_edge_predicate_none
l!l
213
MC/DC100%1/1
$edge_predicate ||= \&Graph::Graph6::_edge_predicate_none
$edge_predicate
Truth table100%2/2
A
A= $edge_predicate
Aresult
11
00
586  
587 24 ▶ if ($format eq 'graph6') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'graph6') { } 15 9 24
elsif ($format eq 'digraph6') { } 9 0 9
588 15 foreach my $to (1 .. $num_vertices-1) {
589 30 foreach my $from (0 .. $to-1) {
590 59 ▶ $put_bit->($edge_predicate->($from,$to) ? 1 : 0) or return 0;
Branch: 2 branches
BranchTrueFalseTotal
&$put_bit(&$edge_predicate($from, $to) ? 1 : 0) or return 0 0 59 59
&$edge_predicate($from, $to) ? : 20 39 59
591 }
592 }
593 } elsif ($format eq 'digraph6') {
594 9 foreach my $from (0 .. $num_vertices-1) {
595 33 foreach my $to (0 .. $num_vertices-1) {
596 129 ▶ $put_bit->($edge_predicate->($from,$to) ? 1 : 0) or return 0;
Branch: 2 branches
BranchTrueFalseTotal
&$put_bit(&$edge_predicate($from, $to) ? 1 : 0) or return 0 0 129 129
&$edge_predicate($from, $to) ? : 20 109 129
597 }
598 }
599 } else {
600 0 croak 'Unrecognised format: ',$format;
601 }
602  
603 24 ▶ until ($bitpos == 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bitpos == 5) 76 24 100
604 76 ▶ $put_bit->(0) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$put_bit(0) or return 0 0 76 76
605 }
606 }
607  
608 46 ▶ print $fh "\n" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
print $fh "\n" or return 0 0 46 46
609 46 return 1;
610 }
611  
612 # if (! $edge_predicate
613 # && (my $edge_matrix = $options{'edge_matrix'})) {
614 # $edge_predicate = sub {
615 # my ($from, $to) = @_;
616 # return $edge_matrix->[$from]->[$to];
617 # };
618 # }
619  
620 9 1;
621 __END__
↑ Summary Graph/Writer/Graph6.pm »