| 1 | package List::Util::WeightedRoundRobin; | ||
| 2 | |||
| 3 | $VERSION = 0.4; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | |||
| 11 | my $self = {}; | ||
| 12 | bless $self, $class; | ||
| 13 | |||
| 14 | return( $self ); | ||
| 15 | }; | ||
| 16 | |||
| 17 | |||
| 18 | sub create_weighted_list { | ||
| 19 | my $self = shift; | ||
| 20 | my $sources = shift; | ||
| 21 | |||
| 22 | my $weighted_list = []; | ||
| 23 | |||
| 24 | # The weighting of one source is a list | ||
| 25 | # containing only that source | ||
| 26 | if( @{$sources} == 1 ) { | ||
| 27 | $weighted_list = [ $sources->[0]->{name} ]; | ||
| 28 | return( $weighted_list ); | ||
| 29 | }; | ||
| 30 | |||
| 31 | $sources = $self->_reduce_and_sort_weightings( $sources ); | ||
| 32 | |||
| 33 | foreach my $source ( @{$sources} ) { | ||
| 34 | my $total_weight = scalar @{$weighted_list}; | ||
| 35 | my $frequency = $total_weight / $source->{weight}; | ||
| 36 | |||
| 37 | # If we haven't yet added elements, add all of the first source | ||
| 38 | unless( $total_weight ) { | ||
| 39 | for( my $count = 0; $count < $source->{weight}; $count++ ) { | ||
| 40 | push @{$weighted_list}, $source->{name}; | ||
| 41 | }; | ||
| 42 | next; | ||
| 43 | }; | ||
| 44 | |||
| 45 | for( my $count = $source->{weight}; $count > 0; $count-- ) { | ||
| 46 | my $tmp = sprintf( "%.f", $count * $frequency ); | ||
| 47 | splice( @{$weighted_list}, $tmp, 0, $source->{name} ); | ||
| 48 | }; | ||
| 49 | |||
| 50 | }; | ||
| 51 | |||
| 52 | return( $weighted_list ); | ||
| 53 | }; | ||
| 54 | |||
| 55 | |||
| 56 | sub _reduce_and_sort_weightings { | ||
| 57 | my $self = shift; | ||
| 58 | my $sources = shift; | ||
| 59 | |||
| 60 | my @weights = (); | ||
| 61 | |||
| 62 | foreach my $source ( @{$sources} ) { | ||
| 63 | push @weights, $source->{weight}; | ||
| 64 | }; | ||
| 65 | |||
| 66 | my $common_factor = multigcf( @weights ); | ||
| 67 | |||
| 68 | my $sorted_sources = []; | ||
| 69 | |||
| 70 | foreach my $source ( sort sort_weights_descending(@{$sources}) ) { | ||
| 71 | $source->{weight} /= $common_factor; | ||
| 72 | push @{$sorted_sources}, $source; | ||
| 73 | }; | ||
| 74 | |||
| 75 | return( $sorted_sources ); | ||
| 76 | }; | ||
| 77 | |||
| 78 | |||
| 79 | sub sort_weights_descending { $a->{weight} <=> $b->{weight}; }; | ||
| 80 | |||
| 81 | |||
| 82 | # Taken from: http://www.perlmonks.org/?node=greatest%20common%20factor | ||
| 83 | sub gcf { | ||
| 84 | my ($x, $y) = @_; | ||
| 85 | ($x, $y) = ($y, $x % $y) while $y; | ||
| 86 | return $x; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub multigcf { | ||
| 90 | my $x = shift; | ||
| 91 | $x = gcf($x, shift) while @_; | ||
| 92 | return $x; | ||
| 93 | }; | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | =head1 NAME | ||
| 98 | |||
| 99 | List::Util::WeightedRoundRobin - Creates a list based on weighted input | ||
| 100 | |||
| 101 | =head1 SYNOPSIS | ||
| 102 | |||
| 103 | my $list = [ | ||
| 104 | { | ||
| 105 | name => 'jingle', | ||
| 106 | weight => 6, | ||
| 107 | }, | ||
| 108 | { | ||
| 109 | name => 'bells', | ||
| 110 | weight => 2, | ||
| 111 | }, | ||
| 112 | ]; | ||
| 113 | |||
| 114 | my $WeightedList = List::Util::WeightedRoundRobin->new(); | ||
| 115 | my $weighted_list = $WeightedList->create_weighted_list( $list ); | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | C<List::Util::WeightedRoundRobin> is a utility for creating a weighted list | ||
| 120 | based on the input and associated weights. | ||
| 121 | |||
| 122 | =head1 METHOD | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =head2 new | ||
| 127 | |||
| 128 | Constructs a new C<List::Util::WeightedRoundRobin> and returns it. Takes no | ||
| 129 | arguments. | ||
| 130 | |||
| 131 | =head2 create_weighted_list | ||
| 132 | |||
| 133 | Takes an array reference as an argument. The array reference must contain | ||
| 134 | hash entries which have a 'name' and 'weight' key. | ||
| 135 | |||
| 136 | If the sources are valid and a weighted list has been created, the method will | ||
| 137 | return a weighted list. In the case of an error, the returned list will be | ||
| 138 | empty. | ||
| 139 | |||
| 140 | =head1 AUTHOR | ||
| 141 | |||
| 142 | Alistair Francis, http://search.cpan.org/~friffin/ | ||
| 143 | |||
| 144 | =head1 COPYRIGHT AND LICENSE | ||
| 145 | |||
| 146 | This library is free software; you can redistribute it and/or modify | ||
| 147 | it under the same terms as Perl itself, either Perl version 5.10 or, | ||
| 148 | at your option, any later version of Perl 5 you may have available. | ||
| 149 | |||
| 150 | =cut |