File Coverage

blib/lib/Graph/Writer/Graph6.pm
Criterion Covered Total %
statement 38 38 100.0
branch 4 4 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod n/a
total 57 57 100.0


line stmt bran cond sub pod time code
1             # Copyright 2015, 2016, 2017 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 .
17              
18              
19             # ENHANCE-ME: Maybe an append option to append to a $filename instead of
20             # re-write it. Perhaps
21              
22              
23             package Graph::Writer::Graph6;
24 2     2   11304 use 5.004;
  2         6  
25 2     2   10 use strict;
  2         6  
  2         40  
26 2     2   1155 use Graph::Graph6;
  2         5  
  2         92  
27 2     2   17 use Graph::Writer;
  2         3  
  2         61  
28              
29 2     2   11 use vars '@ISA','$VERSION';
  2         3  
  2         882  
30             @ISA = ('Graph::Writer');
31             $VERSION = 8;
32              
33              
34             sub _init {
35 20     20   30057 my ($self,%param) = @_;
36 20         65 $self->SUPER::_init();
37 20         117 %$self = (format => 'graph6',
38             %$self,
39             %param);
40             }
41              
42             # $graph is a Graph.pm object
43             # return true if there is an edge either direction between $v1 and $v2
44             sub _has_edge_either_directed {
45 7     7   14 my ($graph, $v1, $v2) = @_;
46 7   100     19 return ($graph->has_edge($v1,$v2) || $graph->has_edge($v2,$v1));
47             }
48              
49             sub _write_graph {
50 22     22   2429 my ($self, $graph, $fh) = @_;
51              
52 22         84 my @vertices = sort $graph->vertices;
53 22         1256 my @edge_options;
54 22         45 my $format = $self->{'format'};
55 22 100       50 if ($format eq 'sparse6') {
56 8         24 my @edges = $graph->edges; # [ $from_name, $to_name ]
57             ### @edges
58              
59             # as [$from,$to] numbers
60 8         733 my %vertex_to_num = map { $vertices[$_] => $_ } 0 .. $#vertices;
  28         62  
61 8         18 @edges = map { my $from = $vertex_to_num{$_->[0]};
  17         26  
62 17         23 my $to = $vertex_to_num{$_->[1]};
63 17         35 [$from, $to]
64             } @edges;
65 8         25 @edge_options = (edge_aref => \@edges);
66             } else {
67 14 100 100     38 my $has_edge_either = ($graph->is_directed && $format ne 'digraph6'
68             ? \&_has_edge_either_directed
69              
70             # graph undirected, or directed and format digraph
71             : 'has_edge');
72             @edge_options
73             = (edge_predicate => sub {
74 76     76   120 my ($from, $to) = @_;
75 76         180 return $graph->$has_edge_either($vertices[$from], $vertices[$to]);
76 14         186 });
77             }
78              
79             Graph::Graph6::write_graph
80             (format => $format,
81 22         105 header => $self->{'header'},
82             fh => $fh,
83             num_vertices => scalar(@vertices),
84             @edge_options);
85 22         91 return 1;
86             }
87              
88             1;
89             __END__