-
-
Save kemurphy/8b5d7722d2da8dce12f0 to your computer and use it in GitHub Desktop.
hic sunt leones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub findMinCycles { | |
# hic sunt leones | |
# sorry for the terse code, I promise it works, no need to touch it :) | |
my ($self, $v, $g, $i, $s) = @_; | |
if (exists $i->{$v}) { | |
my @c = @{$s}[$i->{$v}..$#$s]; | |
my $k = (sort { @{$g->{$c[$b]}}<=> @{$g->{$c[$a]}} } (0 .. $#c))[0]; | |
push @c, $v; | |
local $" = ' -> '; | |
$self->error("Dependency cycle detected: @c"); | |
$self->verbose("DEBUG: removing $c[$k] -> $c[$k+1]"); | |
my $e = $g->{$c[$k]}; | |
my $w = $c[$k+1]; | |
for my $j (0 .. $#$e) { | |
if ($w eq $e->[$j]) { | |
splice @$e, $j, 1; | |
last; | |
} | |
} | |
} elsif (defined $g->{$v}) { | |
push @$s, $v; | |
$i->{$v} = $#$s; | |
my @e = @{$g->{$v}}; | |
foreach my $w (@e) { | |
$self->findMinCycles($w, $g, $i, $s); | |
} | |
delete $i->{$v}; | |
pop @$s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment