| 1 | package Algorithm::Dependency::MapReduce; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Algorithm::Dependency::MapReduce - A Map/Reduce implementation for Alg:Dep graphs | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use Params::Util qw{ _CODE }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use Algorithm::Dependency (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | $VERSION = '0.03'; | |||||||||
| 26 | 2 | @ISA = 'Algorithm::Dependency'; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||
| 30 | 1 | my $class = shift; | |||||||||
| 31 | 1 | my %args = @_; | |||||||||
| 32 | |||||||||||
| 33 | # Check params | ||||||||||
| 34 | 1 | ▶ | unless ( _CODE($args{'map'}) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | Carp::croak("The 'map' param is not a CODE reference"); | |||||||||
| 36 | } | ||||||||||
| 37 | 1 | ▶ | unless ( _CODE($args{'reduce'}) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | Carp::croak("The 'reduce' param is not a CODE reference"); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | # Hand off to the parent constructor | ||||||||||
| 42 | 1 | my $self = $class->SUPER::new(@_); | |||||||||
| 43 | |||||||||||
| 44 | # Add the extra attributes | ||||||||||
| 45 | 1 | $self->{'map'} = $args{'map'}; | |||||||||
| 46 | 1 | $self->{'reduce'} = $args{'reduce'}; | |||||||||
| 47 | |||||||||||
| 48 | 1 | return $self; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 6 | ▶ | sub mapreduce { | ||||||||
|
Subroutine
mapreduce: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 52 | 6 | my $self = shift; | |||||||||
| 53 | |||||||||||
| 54 | # Fetch the dependencies for the provided params | ||||||||||
| 55 | 6 | my $schedule = $self->schedule(@_); | |||||||||
| 56 | |||||||||||
| 57 | # Handle the special cases | ||||||||||
| 58 | 6 | ▶ | if ( @$schedule == 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | # Empty list | ||||||||||
| 60 | 0 | return undef; | |||||||||
| 61 | } | ||||||||||
| 62 | 6 | ▶ | if ( @$schedule == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | # Single element, just map it and return | ||||||||||
| 64 | 4 | return $self->{'map'}->( $self, $schedule->[0] ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # Map the first two elements and prime the reduction | ||||||||||
| 68 | my $result = $self->{'reduce'}->( $self, | ||||||||||
| 69 | scalar($self->{'map'}->( $self, shift(@$schedule) )), | ||||||||||
| 70 | 2 | scalar($self->{'map'}->( $self, shift(@$schedule) )), | |||||||||
| 71 | ); | ||||||||||
| 72 | |||||||||||
| 73 | # Process the remaining elements | ||||||||||
| 74 | 2 | ▶ | while ( @$schedule ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | $result = $self->{'reduce'}->( $self, | ||||||||||
| 76 | $result, | ||||||||||
| 77 | 1 | scalar($self->{'map'}->( $self, shift(@$schedule) )), | |||||||||
| 78 | ); | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 2 | return $result; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 2 | 1; | |||||||||
| 85 | |||||||||||
| 86 | =pod | ||||||||||
| 87 | |||||||||||
| 88 | =head1 SUPPORT | ||||||||||
| 89 | |||||||||||
| 90 | Bugs should be submitted via the CPAN bug tracker, located at | ||||||||||
| 91 | |||||||||||
| 92 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Dependency-MapReduce> | ||||||||||
| 93 | |||||||||||
| 94 | For general comments, contact the author. | ||||||||||
| 95 | |||||||||||
| 96 | =head1 AUTHOR | ||||||||||
| 97 | |||||||||||
| 98 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 99 | |||||||||||
| 100 | =head1 SEE ALSO | ||||||||||
| 101 | |||||||||||
| 102 | L<Algorithm::Dependency> | ||||||||||
| 103 | |||||||||||
| 104 | =head1 COPYRIGHT | ||||||||||
| 105 | |||||||||||
| 106 | Copyright 2009 Adam Kennedy. | ||||||||||
| 107 | |||||||||||
| 108 | This program is free software; you can redistribute | ||||||||||
| 109 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 110 | |||||||||||
| 111 | The full text of the license can be found in the | ||||||||||
| 112 | LICENSE file included with this module. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||