| 1 | package Algorithm::Dependency::Ordered; | ||||||||||
| 2 | # ABSTRACT: Implements an ordered dependency hierarchy | ||||||||||
| 3 | |||||||||||
| 4 | #pod =pod | ||||||||||
| 5 | #pod | ||||||||||
| 6 | #pod =head1 DESCRIPTION | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod Algorithm::Dependency::Ordered implements the most common variety of | ||||||||||
| 9 | #pod L<Algorithm::Dependency>, the one in which the dependencies of an item must | ||||||||||
| 10 | #pod be acted upon before the item itself can be acted upon. | ||||||||||
| 11 | #pod | ||||||||||
| 12 | #pod In use and semantics, this should be used in exactly the same way as for the | ||||||||||
| 13 | #pod main parent class. Please note that the output of the C<depends> method is | ||||||||||
| 14 | #pod NOT changed, as the order of the depends is not assumed to be important. | ||||||||||
| 15 | #pod Only the output of the C<schedule> method is modified to ensure the correct | ||||||||||
| 16 | #pod order. | ||||||||||
| 17 | #pod | ||||||||||
| 18 | #pod For API details, see L<Algorithm::Dependency>. | ||||||||||
| 19 | #pod | ||||||||||
| 20 | #pod =cut | ||||||||||
| 21 | |||||||||||
| 22 | 4 | ▶ | use 5.005; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 4 | ▶ | use Algorithm::Dependency (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 4 | our $VERSION = '1.112'; | |||||||||
| 27 | 4 | our @ISA = 'Algorithm::Dependency'; | |||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | 48 | ▶ | sub schedule { | ||||||||
|
Subroutine
schedule: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||
| 31 | 48 | my $self = shift; | |||||||||
| 32 | 48 | my $source = $self->{source}; | |||||||||
| 33 | 48 | ▶ | my @items = @_ or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 48 | ▶ | return undef if grep { ! $source->item($_) } @items; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | # The actual items to select will be the same as for the unordered | ||||||||||
| 37 | # version, so we can simplify the algorithm greatly by using the | ||||||||||
| 38 | # normal unordered ->schedule method to get the starting list. | ||||||||||
| 39 | 48 | my $rv = $self->SUPER::schedule( @items ); | |||||||||
| 40 | 48 | ▶ | my @queue = $rv ? @$rv : return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | # Get a working copy of the selected index | ||||||||||
| 43 | 47 | my %selected = %{ $self->{selected} }; | |||||||||
| 44 | |||||||||||
| 45 | # If at any time we check every item in the stack without finding | ||||||||||
| 46 | # a suitable candidate for addition to the schedule, we have found | ||||||||||
| 47 | # a circular reference error. We need to create a marker to track this. | ||||||||||
| 48 | 47 | my $error_marker = ''; | |||||||||
| 49 | |||||||||||
| 50 | # Begin the processing loop | ||||||||||
| 51 | 47 | my @schedule = (); | |||||||||
| 52 | 47 | ▶ | while ( my $id = shift @queue ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | # Have we checked every item in the stack? | ||||||||||
| 54 | 135 | ▶ | return undef if $id eq $error_marker; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | # Are there any un-met dependencies | ||||||||||
| 57 | 135 | ▶ | my $Item = $self->{source}->item($id) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 135 | my @missing = grep { ! $selected{$_} } $Item->depends; | |||||||||
| 59 | |||||||||||
| 60 | # Remove orphans if we are ignoring them | ||||||||||
| 61 | 135 | ▶ | if ( $self->{ignore_orphans} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 1 | @missing = grep { $self->{source}->item($_) } @missing; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 135 | ▶ | if ( @missing ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | # Set the error marker if not already | ||||||||||
| 67 | 41 | ▶ | $error_marker = $id unless $error_marker; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | # Add the id back to the end of the queue | ||||||||||
| 70 | 41 | push @queue, $id; | |||||||||
| 71 | 41 | next; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | # All dependencies have been met. Add the item to the schedule and | ||||||||||
| 75 | # to the selected index, and clear the error marker. | ||||||||||
| 76 | 94 | push @schedule, $id; | |||||||||
| 77 | 94 | $selected{$id} = 1; | |||||||||
| 78 | 94 | $error_marker = ''; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | # All items have been added | ||||||||||
| 82 | 47 | \@schedule; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 4 | 1; | |||||||||
| 86 | |||||||||||
| 87 | __END__ | ||||||||||