File Coverage

blib/lib/Graph/MoreUtils/Replace.pm
Criterion Covered Total %
statement 9 21 42.8
branch 0 8 0.0
condition 0 6 0.0
subroutine 3 4 75.0
pod 0 1 0.0
total 12 40 30.0


line stmt bran cond sub pod time code
1             package Graph::MoreUtils::Replace;
2              
3             # ABSTRACT: Replace one on more vertices with a given one.
4             our $VERSION = '0.3.0'; # VERSION
5              
6 8     8   97 use strict;
  8         20  
  8         393  
7 8     8   80 use warnings;
  8         15  
  8         562  
8              
9 8     8   5379 use Set::Object qw( set );
  8         83174  
  8         2474  
10              
11             sub replace
12             {
13 0     0 0   my( $graph, $new, @old ) = @_;
14              
15 0           $graph->add_vertex( $new );
16              
17 0           my $old = set( @old );
18 0 0 0       for my $edge (grep { ($old->has( $_->[0] ) && !$old->has( $_->[1] )) ||
  0   0        
19             ($old->has( $_->[1] ) && !$old->has( $_->[0] )) }
20             $graph->edges) {
21 0 0         my( $vertex, $neighbour ) = $old->has( $edge->[0] ) ? @$edge : reverse @$edge;
22 0 0         next if $graph->has_edge( $new, $neighbour );
23 0           $graph->add_edge( $new, $neighbour );
24 0 0         next unless $graph->has_edge_attributes( @$edge );
25 0           $graph->set_edge_attributes( $new, $neighbour, $graph->get_edge_attributes( @$edge ) );
26             }
27 0           $graph->delete_vertices( @old );
28              
29 0           return $graph;
30             }
31              
32             1;