| 1 | package Algorithm::C3; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 66 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=26 SCAR=32.6
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 66 | my ($root, $parent_fetcher, $cache) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 66 | ▶ | $cache ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$cache ||= {}
MC/DC100%1/1
$cache ||= {}
$cache
Truth table100%2/2
A
A= $cache
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 66 | my @STACK; # stack for simulating recursion | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 66 | my $pfetcher_is_coderef = ref($parent_fetcher) eq 'CODE'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 66 | ▶ | unless ($pfetcher_is_coderef or $root->can($parent_fetcher)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pfetcher_is_coderef or $root->can($parent_fetcher)
MC/DC100%2/2
$pfetcher_is_coderef or $root->can($parent_fetcher)
$pfetcher_is_coderef
$root->can($parent_fetcher)
Truth table100%3/3
A or B
A= $pfetcher_is_coderefB= $root->can($parent_fetcher)
| |||||||||||||||||||||||||||||
| 20 | 1 | confess "Could not find method $parent_fetcher in $root"; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 65 | my $current_root = $root; | |||||||||||||||||||||||||||
| 24 | 65 | my $current_parents = [ $root->$parent_fetcher ]; | |||||||||||||||||||||||||||
| 25 | 65 | my $recurse_mergeout = []; | |||||||||||||||||||||||||||
| 26 | 65 | my $i = 0; | |||||||||||||||||||||||||||
| 27 | 65 | my %seen = ( $root => 1 ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 65 | my ($new_root, $mergeout, %tails); | |||||||||||||||||||||||||||
| 30 | 65 | while(1) { | |||||||||||||||||||||||||||
| 31 | 794 | ▶ | if($i < @$current_parents) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 387 | $new_root = $current_parents->[$i++]; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 387 | ▶ | if($seen{$new_root}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 8 | my @isastack; | |||||||||||||||||||||||||||
| 36 | my $reached; | ||||||||||||||||||||||||||||
| 37 | 8 | ▶ | for(my $i = 0; $i < $#STACK; $i += 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 27 | ▶ | if($reached || ($reached = ($STACK[$i] eq $new_root))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$reached or $reached = $STACK[$i] eq $new_root
MC/DC100%2/2
$reached or $reached = $STACK[$i] eq $new_root
$reached
$reached = $STACK[$i] eq $new_root
Truth table100%3/3
A or B
A= $reachedB= $reached = $STACK[$i] eq $new_root
| |||||||||||||||||||||||||||||
| 39 | 14 | push(@isastack, $STACK[$i]); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 8 | my $isastack = join(q{ -> }, @isastack, $current_root, $new_root); | |||||||||||||||||||||||||||
| 43 | 8 | die "Infinite loop detected in parents of '$root': $isastack"; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 379 | $seen{$new_root} = 1; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 379 | ▶ | unless ($pfetcher_is_coderef or $new_root->can($parent_fetcher)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pfetcher_is_coderef or $new_root->can($parent_fetcher)
MC/DC100%2/2
$pfetcher_is_coderef or $new_root->can($parent_fetcher)
$pfetcher_is_coderef
$new_root->can($parent_fetcher)
Truth table100%3/3
A or B
A= $pfetcher_is_coderefB= $new_root->can($parent_fetcher)
| |||||||||||||||||||||||||||||
| 48 | 1 | confess "Could not find method $parent_fetcher in $new_root"; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 378 | push(@STACK, $current_root, $current_parents, $recurse_mergeout, $i); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 378 | $current_root = $new_root; | |||||||||||||||||||||||||||
| 54 | 378 | ▶ | $current_parents = $cache->{pfetch}->{$current_root} ||= [ $current_root->$parent_fetcher ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$cache->{"pfetch"}{$current_root} ||= [$current_root->$parent_fetcher]
MC/DC100%1/1
$cache->{"pfetch"}{$current_root} ||= [$current_root->$parent_fetcher]
$cache->{"pfetch"}{$current_root}
Truth table100%2/2
A
A= $cache->{"pfetch"}{$current_root}
| |||||||||||||||||||||||||||||
| 55 | 378 | $recurse_mergeout = []; | |||||||||||||||||||||||||||
| 56 | 378 | $i = 0; | |||||||||||||||||||||||||||
| 57 | 378 | next; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 407 | $seen{$current_root} = 0; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 407 | ▶ | $mergeout = $cache->{merge}->{$current_root} ||= do { | ||||||||||||||||||||||||||
|
Condition100%3/3
$cache->{"merge"}{$current_root} ||= do {
my(@seqs) = map({[@$_];} @$recurse_mergeout);
push @seqs, [@$current_parents] if @$current_parents;
foreach my $seq (@seqs) {
++$tails{$seq->[$_]} foreach (1 .. $seq->$#*);
};
my(@res) = $current_root;
while (1) {
my($cand, $winner);
foreach $_ (@seqs) {
next unless @$_;
if (not $winner) {
$cand = $_->[0];
next if $tails{$cand};
push @res, $winner = $cand;
shift @$_;
--$tails{$_->[0]} if @$_;
}
elsif ($_->[0] eq $winner) {
shift @$_;
--$tails{$_->[0]} if @$_;
};
};
last unless $cand;
die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner;
};
\@res
}
MC/DC100%2/2
$cache->{"merge"}{$current_root} ||= do {
my(@seqs) = map({[@$_];} @$recurse_mergeout);
push @seqs, [@$current_parents] if @$current_parents;
foreach my $seq (@seqs) {
++$tails{$seq->[$_]} foreach (1 .. $seq->$#*);
};
my(@res) = $current_root;
while (1) {
my($cand, $winner);
foreach $_ (@seqs) {
next unless @$_;
if (not $winner) {
$cand = $_->[0];
next if $tails{$cand};
push @res, $winner = $cand;
shift @$_;
--$tails{$_->[0]} if @$_;
}
elsif ($_->[0] eq $winner) {
shift @$_;
--$tails{$_->[0]} if @$_;
};
};
last unless $cand;
die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner;
};
\@res
}
$cache->{"merge"}{$current_root}
do {
my(@seqs) = map({[@$_];} @$recurse_mergeout);
push @seqs, [@$current_parents] if @$current_parents;
foreach my $seq (@seqs) {
++$tails{$seq->[$_]} foreach (1 .. $seq->$#*);
};
my(@res) = $current_root;
while (1) {
my($cand, $winner);
foreach $_ (@seqs) {
next unless @$_;
if (not $winner) {
$cand = $_->[0];
next if $tails{$cand};
push @res, $winner = $cand;
shift @$_;
--$tails{$_->[0]} if @$_;
}
elsif ($_->[0] eq $winner) {
shift @$_;
--$tails{$_->[0]} if @$_;
};
};
last unless $cand;
die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner;
};
\@res
}
Truth table100%3/3
A ||= B
A= $cache->{"merge"}{$current_root}B= do {
my(@seqs) = map({[@$_];} @$recurse_mergeout);
push @seqs, [@$current_parents] if @$current_parents;
foreach my $seq (@seqs) {
++$tails{$seq->[$_]} foreach (1 .. $seq->$#*);
};
my(@res) = $current_root;
while (1) {
my($cand, $winner);
foreach $_ (@seqs) {
next unless @$_;
if (not $winner) {
$cand = $_->[0];
next if $tails{$cand};
push @res, $winner = $cand;
shift @$_;
--$tails{$_->[0]} if @$_;
}
elsif ($_->[0] eq $winner) {
shift @$_;
--$tails{$_->[0]} if @$_;
};
};
last unless $cand;
die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner;
};
\@res
}
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # This do-block is the code formerly known as the function | ||||||||||||||||||||||||||||
| 65 | # that was a perl-port of the python code at | ||||||||||||||||||||||||||||
| 66 | # http://www.python.org/2.3/mro.html :) | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Initial set (make sure everything is copied - it will be modded) | ||||||||||||||||||||||||||||
| 69 | 221 | my @seqs = map { [@$_] } @$recurse_mergeout; | |||||||||||||||||||||||||||
| 70 | 221 | ▶ | push(@seqs, [@$current_parents]) if @$current_parents; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Construct the tail-checking hash (actually, it's cheaper and still | ||||||||||||||||||||||||||||
| 73 | # correct to re-use it throughout this function) | ||||||||||||||||||||||||||||
| 74 | 221 | foreach my $seq (@seqs) { | |||||||||||||||||||||||||||
| 75 | 339 | $tails{$seq->[$_]}++ for (1..$#$seq); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 221 | my @res = ( $current_root ); | |||||||||||||||||||||||||||
| 79 | 221 | while (1) { | |||||||||||||||||||||||||||
| 80 | 750 | my $cand; | |||||||||||||||||||||||||||
| 81 | my $winner; | ||||||||||||||||||||||||||||
| 82 | 750 | foreach (@seqs) { | |||||||||||||||||||||||||||
| 83 | 1879 | ▶ | next if !@$_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1129 | ▶ | if(!$winner) { # looking for a winner | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 601 | $cand = $_->[0]; # seq head is candidate | |||||||||||||||||||||||||||
| 86 | 601 | ▶ | next if $tails{$cand}; # he loses if in %tails | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Handy warn to give a output like the ones on | ||||||||||||||||||||||||||||
| 89 | # http://www.python.org/download/releases/2.3/mro/ | ||||||||||||||||||||||||||||
| 90 | #warn " = " . join(' + ', @res) . " + merge([" . join('] [', map { join(', ', @$_) } grep { @$_ } @seqs) . "])\n"; | ||||||||||||||||||||||||||||
| 91 | 529 | push @res => $winner = $cand; | |||||||||||||||||||||||||||
| 92 | 529 | shift @$_; # strip off our winner | |||||||||||||||||||||||||||
| 93 | 529 | ▶ | $tails{$_->[0]}-- if @$_; # keep %tails sane | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | elsif($_->[0] eq $winner) { | ||||||||||||||||||||||||||||
| 96 | 296 | shift @$_; # strip off our winner | |||||||||||||||||||||||||||
| 97 | 296 | ▶ | $tails{$_->[0]}-- if @$_; # keep %tails sane | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Handy warn to give a output like the ones on | ||||||||||||||||||||||||||||
| 102 | # http://www.python.org/download/releases/2.3/mro/ | ||||||||||||||||||||||||||||
| 103 | #warn " = " . join(' + ', @res) . "\n" if !$cand; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 750 | ▶ | last if !$cand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 530 | ▶ | die q{Inconsistent hierarchy found while merging '} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | . $current_root . qq{':\n\t} | ||||||||||||||||||||||||||||
| 108 | . qq{current merge results [\n\t\t} | ||||||||||||||||||||||||||||
| 109 | . (join ",\n\t\t" => @res) | ||||||||||||||||||||||||||||
| 110 | . qq{\n\t]\n\t} . qq{merging failed on '$cand'\n} | ||||||||||||||||||||||||||||
| 111 | if !$winner; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 220 | \@res; | |||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 406 | ▶ | return @$mergeout if !@STACK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 351 | $i = pop(@STACK); | |||||||||||||||||||||||||||
| 119 | 351 | $recurse_mergeout = pop(@STACK); | |||||||||||||||||||||||||||
| 120 | 351 | $current_parents = pop(@STACK); | |||||||||||||||||||||||||||
| 121 | 351 | $current_root = pop(@STACK); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 351 | push(@$recurse_mergeout, $mergeout); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 11 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||