| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Algorithm::Graphs::Reachable::Tiny; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 68821 | use 5.008; | 
|  | 2 |  |  |  |  | 14 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 69 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 10 | use Carp; | 
|  | 2 |  |  |  |  | 12 |  | 
|  | 2 |  |  |  |  | 153 |  | 
| 8 | 2 |  |  | 2 |  | 87 | use Exporter 'import'; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 754 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our @EXPORT_OK = qw(all_reachable); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub all_reachable { | 
| 16 | 36 | 50 |  | 36 | 1 | 2767 | @_ == 2 or croak("Need exactly two arguments!"); | 
| 17 | 36 |  |  |  |  | 77 | my ($graph, $nodes) = @_; | 
| 18 | 36 | 100 |  |  |  | 102 | my %visited = ref($nodes) eq "ARRAY" ? map {$_ => undef} @{$nodes} : %{$nodes}; | 
|  | 30 |  |  |  |  | 111 |  | 
|  | 18 |  |  |  |  | 41 |  | 
|  | 18 |  |  |  |  | 66 |  | 
| 19 | 36 | 100 |  |  |  | 130 | return {} unless %visited; | 
| 20 | 26 |  |  |  |  | 84 | my @queue = keys(%visited); | 
| 21 | 26 | 100 |  |  |  | 73 | if (ref($graph) eq 'HASH') { | 
|  |  | 50 |  |  |  |  |  | 
| 22 | 10 | 50 |  |  |  | 24 | return \%visited unless %{$graph}; | 
|  | 10 |  |  |  |  | 24 |  | 
| 23 | 10 |  |  |  |  | 28 | while (defined(my $v = shift(@queue))) { | 
| 24 | 62 | 100 |  |  |  | 135 | if (exists($graph->{$v})) { ## we need this if() to avoid autovivification! | 
| 25 | 40 |  |  |  |  | 62 | foreach my $s (keys(%{$graph->{$v}})) { | 
|  | 40 |  |  |  |  | 94 |  | 
| 26 | 50 | 100 |  |  |  | 104 | if (!exists($visited{$s})) { | 
| 27 | 42 |  |  |  |  | 68 | $visited{$s} = undef; | 
| 28 | 42 |  |  |  |  | 110 | push(@queue, $s); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  | elsif (ref($graph) eq 'ARRAY') { | 
| 35 | 16 | 50 |  |  |  | 25 | return \%visited unless @{$graph}; | 
|  | 16 |  |  |  |  | 38 |  | 
| 36 | 16 |  |  |  |  | 45 | while (defined(my $v = shift(@queue))) { | 
| 37 | 124 | 100 |  |  |  | 301 | if (defined($graph->[$v])) { | 
| 38 | 92 |  |  |  |  | 123 | foreach my $s (keys(%{$graph->[$v]})) { | 
|  | 92 |  |  |  |  | 229 |  | 
| 39 | 100 | 100 |  |  |  | 201 | if (!exists($visited{$s})) { | 
| 40 | 84 |  |  |  |  | 137 | $visited{$s} = undef; | 
| 41 | 84 |  |  |  |  | 215 | push(@queue, $s); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | } else { | 
| 47 | 0 |  |  |  |  | 0 | croak("Arg 1 must be an ARRAY or HASH reference"); | 
| 48 |  |  |  |  |  |  | } | 
| 49 | 26 |  |  |  |  | 132 | return \%visited; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | 1; # End of Algorithm::Graphs::Reachable::Tiny | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | __END__ |