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   11671 use 5.004;
  1         4  
20 1     1   5 use strict;
  1         2  
  1         23  
21 1     1   4 use Carp 'croak';
  1         2  
  1         38  
22 1     1   6 use Graph::Reader;
  1         2  
  1         47  
23              
24 1     1   7 use vars '@ISA','$VERSION';
  1         2  
  1         84  
25             @ISA = ('Graph::Reader');
26             $VERSION = 8;
27              
28 1     1   566 use Graph::Graph6;
  1         3  
  1         390  
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 134200 my ($self, $filename_or_fh) = @_;
40 6         64 require Graph;
41              
42 6         14 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   118 my ($n) = @_;
47 48         171 return sprintf '%0*d', $width, $n;
48 6   50     81 };
49              
50 6         16 my $graph;
51             my $check_countedged;
52 6 100       66 if (Graph::Graph6::read_graph
    50          
53             ((ref $filename_or_fh ? 'fh' : 'filename') => $filename_or_fh,
54             format_func => sub {
55 6     6   18 my ($format) = @_;
56 6         51 $graph = Graph->new(undirected => ($format ne 'digraph6'));
57 6         1777 $check_countedged = ($format eq 'sparse6');
58             },
59             num_vertices_func => sub {
60 6     6   14 my ($n) = @_;
61 6         16 $width = length($n-1);
62 6         26 foreach my $i (0 .. $n-1) {
63 20         690 my $name = $vertex_name_func->($i);
64 20         66 $graph->add_vertex($name);
65             }
66             },
67             edge_func => sub {
68 14     14   32 my ($from, $to) = @_;
69 14         28 $from = $vertex_name_func->($from);
70 14         28 $to = $vertex_name_func->($to);
71 14 100 100     54 if ($check_countedged && $graph->has_edge($from,$to)) {
72 2         45 $graph = _countedged_copy($graph);
73 2         5 undef $check_countedged;
74             }
75 14         217 $graph->add_edge($from,$to);
76             })) {
77 6         31 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   5 my ($graph) = @_;
87 2         6 my $new_graph = Graph->new (undirected => $graph->is_undirected,
88             countedged => 1);
89 2         412 $new_graph->add_vertices($graph->vertices);
90 2         278 $new_graph->add_edges($graph->edges);
91 2         426 return $new_graph;
92             }
93              
94             1;
95             __END__