| 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 <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Graph::Easy::Parser::Graph6; | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use 5.006; # Graph::Easy is 5.008 anyway | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Graph::Graph6; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Graph::Easy::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | our $VERSION = 8; | |||||||||||||||||||||||||||
| 26 | 1 | our @ISA = ('Graph::Easy::Parser'); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 29 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub _default_vertex_name_func { | ||||||||||||||||||||||||||||
| 33 | 43 | ▶ | my ($n, $num_vertices) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_default_vertex_name_func: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 43 | return sprintf '%0*d', length($num_vertices-1), $n; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | sub _init { | ||||||||||||||||||||||||||||
| 37 | 7 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # this undocumented yet ... | ||||||||||||||||||||||||||||
| 40 | 7 | ▶ | $self->{'vertex_name_func'} = delete $args->{'vertex_name_func'} | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args->{'vertex_name_func'} || \&Graph::Easy::Parser::Graph6::_default_vertex_name_func
MC/DC100%1/1
delete $args->{'vertex_name_func'} || \&Graph::Easy::Parser::Graph6::_default_vertex_name_func
delete $args->{'vertex_name_func'}
Truth table100%2/2
A
A= delete $args->{'vertex_name_func'}
| |||||||||||||||||||||||||||||
| 41 | || \&_default_vertex_name_func; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | return $self->SUPER::_init($args); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub from_file { | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | my ($self, $filename_or_fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | return _read_graph6($self, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | (ref $filename_or_fh | ||||||||||||||||||||||||||||
| 50 | ? (fh => $filename_or_fh) | ||||||||||||||||||||||||||||
| 51 | : (filename => $filename_or_fh))); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | sub from_text { | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | my ($self, $str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 5 | return _read_graph6($self, str => $str); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub _read_graph6 { | ||||||||||||||||||||||||||||
| 59 | 7 | ▶ | my ($self, @options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read_graph6: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | if (! ref $self) { $self = $self->new; } # class method | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 7 | $self->reset; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | my $graph = $self->{'_graph'}; | |||||||||||||||||||||||||||
| 65 | 7 | my $num_vertices; | |||||||||||||||||||||||||||
| 66 | 7 | my $vertex_name_func = $self->{'vertex_name_func'}; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my $ret = Graph::Graph6::read_graph | ||||||||||||||||||||||||||||
| 69 | (@options, | ||||||||||||||||||||||||||||
| 70 | format_func => sub { | ||||||||||||||||||||||||||||
| 71 | 7 | ▶ | my ($format) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 72 | 7 | ▶ | unless ($format eq 'digraph6') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 7 | $graph->set_attribute (type => 'undirected'); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | num_vertices_func => sub { | ||||||||||||||||||||||||||||
| 77 | 5 | ▶ | my ($n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | ### num_vertices_func(): $n | ||||||||||||||||||||||||||||
| 79 | 5 | $num_vertices = $n; | |||||||||||||||||||||||||||
| 80 | 5 | foreach my $i (0 .. $n-1) { | |||||||||||||||||||||||||||
| 81 | 32 | my $name = $vertex_name_func->($i,$num_vertices); | |||||||||||||||||||||||||||
| 82 | 32 | $graph->add_node($name); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | edge_func => sub { | ||||||||||||||||||||||||||||
| 86 | 13 | ▶ | my ($from, $to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | ### edge_func() ... | ||||||||||||||||||||||||||||
| 88 | ### $from | ||||||||||||||||||||||||||||
| 89 | ### $to | ||||||||||||||||||||||||||||
| 90 | 13 | $graph->add_edge($vertex_name_func->($from,$num_vertices), | |||||||||||||||||||||||||||
| 91 | $vertex_name_func->($to, $num_vertices)); | ||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | error_func => sub { | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->error(@_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 7 | }); | |||||||||||||||||||||||||||
| 96 | ### $ret | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | if (defined $ret && $ret == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ret and $ret == 0
MC/DC100%2/2
defined $ret and $ret == 0
defined $ret
$ret == 0
Truth table100%3/3
A and B
A= defined $retB= $ret == 0
| |||||||||||||||||||||||||||||
| 99 | 1 | return undef; # EOF | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # When fatal_errors is false Graph::Easy::Parser returns a partial graph, | ||||||||||||||||||||||||||||
| 103 | # though its docs suggest undef. Try to follow its behaviour. | ||||||||||||||||||||||||||||
| 104 | 6 | return $graph; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||