File Coverage

blib/lib/Graph/Easy/As_graph6.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition 8 8 100.0
subroutine 7 7 100.0
pod 0 1 0.0
total 50 51 98.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             package Graph::Easy::As_graph6;
19 2     2   124275 use 5.006; # Graph::Easy is 5.008 anyway
  2         9  
20 2     2   12 use strict;
  2         5  
  2         42  
21 2     2   10 use warnings;
  2         4  
  2         51  
22 2     2   1193 use Graph::Graph6;
  2         6  
  2         693  
23              
24             # uncomment this to run the ### lines
25             # use Smart::Comments;
26              
27             our $VERSION = 8;
28              
29              
30             package Graph::Easy;
31              
32             # In Graph::Easy 0.75 and 0.76, an undirected graph has_edge($v1,$v2) is
33             # true only of $v1,$v2 passed in the original edge order, not as $v2,$v1.
34             # That's probably a bug.
35             #
36             # To get the right result, _has_edge_either() is always used for undirected
37             # graphs.
38             #
39             # output $graph method
40             # ------ ------ ------
41             # graph6 undirected _has_edge_either() due to has_edge()
42             # graph6 directed _has_edge_either() write either way
43             # digraph6 undirected _has_edge_either() to write both ways
44             # digraph6 directed has_edge()
45             #
46             # For graph6 output, maybe some sort of Graph::Easy ->has_neighbour() could
47             # answer $v1,$v2 either way round for both directed and undirected graphs.
48             # Name has_neighbour() would correspond to Graph.pm neighbours(), so
49             # has_neighbour() would be whether a given $v2 is among the neighbours of
50             # $v1. (neighbours() are all adjacent vertices, for edges in either
51             # direction.)
52              
53             sub _has_edge_either {
54 39     39   67 my ($graph, $from, $to) = @_;
55 39   100     76 return $graph->has_edge($from,$to) || $graph->has_edge($to,$from);
56             }
57              
58             sub as_graph6 {
59 31     31 0 11081 my ($graph, %options) = @_;
60             ### Graph-Easy as_graph6() ...
61              
62 31         107 my @vertices = $graph->sorted_nodes('name');
63 31   100     1409 my $format = $options{'format'} || 'graph6';
64             ### $format
65              
66 31         50 my @edge_options;
67 31 100       69 if ($format eq 'sparse6') {
68 18         43 my @edges = $graph->edges; # Graph::Easy::Edge objects
69 18         311 my %name_to_num = map { $vertices[$_]->name => $_ } 0 .. $#vertices;
  64         283  
70 18         127 @edges = map { [map{$name_to_num{$_->name}} $_->nodes] } @edges;
  34         142  
  68         327  
71             ### @edges
72 18         120 @edge_options = (edge_aref => \@edges)
73              
74             } else {
75 13 100 100     37 my $has_edge_method = ($graph->is_directed && $format eq 'digraph6'
76             ? 'has_edge'
77             : \&_has_edge_either);
78             @edge_options
79             = (edge_predicate
80             => sub {
81 107     107   170 my ($from, $to) = @_;
82 107         235 return $graph->$has_edge_method($vertices[$from], $vertices[$to]);
83 13         1342 });
84             }
85              
86             Graph::Graph6::write_graph
87             (format => $format,
88 31         156 header => $options{'header'},
89             str_ref => \my $str,
90             num_vertices => scalar(@vertices),
91             @edge_options);
92             ### $str
93 31         325 return $str;
94             }
95              
96             1;
97             __END__