File Coverage

blib/lib/Graph/Reader/Graph6.pm
Criterion Covered Total %
statement 46 47 97.8
branch 5 6 83.3
condition 4 5 80.0
subroutine 12 12 100.0
pod 1 1 100.0
total 68 71 95.7


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             package Graph::Reader::Graph6;
19 1     1   8987 use 5.004;
  1         4  
20 1     1   5 use strict;
  1         2  
  1         19  
21 1     1   5 use Carp 'croak';
  1         2  
  1         31  
22 1     1   5 use Graph::Reader;
  1         2  
  1         27  
23              
24 1     1   5 use vars '@ISA','$VERSION';
  1         2  
  1         82  
25             @ISA = ('Graph::Reader');
26             $VERSION = 7;
27              
28 1     1   705 use Graph::Graph6;
  1         3  
  1         274  
29              
30              
31             # Not using the base class read_graph() since want Graph->new to follow the
32             # directed-ness of the format read.
33             #
34             # This misses the Graph::Reader::read_graph() warn() on unable to open file,
35             # but think something in the return would be better than a warn message
36             # anyway.
37             #
38             sub read_graph {
39 6     6 1 4053 my ($self, $filename_or_fh) = @_;
40 6         33 require Graph;
41              
42 6         12 my $width = 1;
43             # this vertex_name_func not a documented feature yet ...
44             my $vertex_name_func = $self->{'vertex_name_func'}
45             || sub {
46 48     48   74 my ($n) = @_;
47 48         117 return sprintf '%0*d', $width, $n;
48 6   50     48 };
49              
50 6         13 my $graph;
51             my $check_countedged;
52 6 100       40 if (Graph::Graph6::read_graph
    50          
53             ((ref $filename_or_fh ? 'fh' : 'filename') => $filename_or_fh,
54             format_func => sub {
55 6     6   13 my ($format) = @_;
56 6         34 $graph = Graph->new(undirected => ($format ne 'digraph6'));
57 6         1197 $check_countedged = ($format eq 'sparse6');
58             },
59             num_vertices_func => sub {
60 6     6   11 my ($n) = @_;
61 6         12 $width = length($n-1);
62 6         15 foreach my $i (0 .. $n-1) {
63 20         518 my $name = $vertex_name_func->($i);
64 20         51 $graph->add_vertex($name);
65             }
66             },
67             edge_func => sub {
68 14     14   23 my ($from, $to) = @_;
69 14         24 $from = $vertex_name_func->($from);
70 14         27 $to = $vertex_name_func->($to);
71 14 100 100     44 if ($check_countedged && $graph->has_edge($from,$to)) {
72 2         47 $graph = _countedged_copy($graph);
73 2         12 undef $check_countedged;
74             }
75 14         196 $graph->add_edge($from,$to);
76             })) {
77 6         20 return $graph;
78             } else {
79 0         0 return 0;
80             }
81             }
82              
83             # $graph is a Graph.pm.
84             # Return a copy which is countedged.
85             sub _countedged_copy {
86 2     2   3 my ($graph) = @_;
87 2         5 my $new_graph = Graph->new (undirected => $graph->is_undirected,
88             countedged => 1);
89 2         343 $new_graph->add_vertices($graph->vertices);
90 2         226 $new_graph->add_edges($graph->edges);
91 2         363 return $new_graph;
92             }
93              
94             1;
95             __END__