| 1 | package Algorithm::Dependency::Source::Invert; | ||
| 2 | # ABSTRACT: Logically invert a source | ||
| 3 | |||
| 4 | #pod =pod | ||
| 5 | #pod | ||
| 6 | #pod =head1 SYNOPSIS | ||
| 7 | #pod | ||
| 8 | #pod my $inverted = Algorithm::Dependency::Source::Invert->new( $source ); | ||
| 9 | #pod | ||
| 10 | #pod =head1 DESCRIPTION | ||
| 11 | #pod | ||
| 12 | #pod This class creates a source from another source, but with all dependencies | ||
| 13 | #pod reversed. | ||
| 14 | #pod | ||
| 15 | #pod =cut | ||
| 16 | |||
| 17 | use 5.005; | ||
| 18 | use strict; | ||
| 19 | use Params::Util '_INSTANCE'; | ||
| 20 | use Algorithm::Dependency::Source::HoA (); | ||
| 21 | |||
| 22 | our $VERSION = '1.112'; | ||
| 23 | our @ISA = 'Algorithm::Dependency::Source::HoA'; | ||
| 24 | |||
| 25 | |||
| 26 | ##################################################################### | ||
| 27 | # Constructor | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $class = shift; | ||
| 31 | my $source = _INSTANCE(shift, 'Algorithm::Dependency::Source') or return undef; | ||
| 32 | |||
| 33 | # Derive a HoA from the original source | ||
| 34 | my @items = $source->items; | ||
| 35 | my %hoa = map { $_->id => [ ] } @items; | ||
| 36 | foreach my $item ( @items ) { | ||
| 37 | my $id = $item->id; | ||
| 38 | my @deps = $item->depends; | ||
| 39 | foreach my $dep ( @deps ) { | ||
| 40 | push @{ $hoa{$dep} }, $id; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | # Hand off to the parent class | ||
| 45 | $class->SUPER::new( \%hoa ); | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | __END__ |