| 1 | package App::Sandy::WeightedRaffle; | ||
| 2 | # ABSTRACT: Weighted raffle interface. | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | with 'App::Sandy::Role::BSearch'; | ||
| 7 | |||
| 8 | our $VERSION = '0.25'; # VERSION | ||
| 9 | |||
| 10 | has 'keys' => ( | ||
| 11 | traits => ['Array'], | ||
| 12 | is => 'ro', | ||
| 13 | isa => 'ArrayRef', | ||
| 14 | required => 1, | ||
| 15 | handles => { _get_key => 'get' } | ||
| 16 | ); | ||
| 17 | |||
| 18 | has 'weights' => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'ArrayRef[My:IntGt0]', | ||
| 21 | required => 1 | ||
| 22 | ); | ||
| 23 | |||
| 24 | has '_weights' => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'My:Weights', | ||
| 27 | builder => '_build_weights', | ||
| 28 | lazy_build => 1 | ||
| 29 | ); | ||
| 30 | |||
| 31 | has '_num_weights' => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'My:IntGt0', | ||
| 34 | builder => '_build_num_weights', | ||
| 35 | lazy_build => 1 | ||
| 36 | ); | ||
| 37 | |||
| 38 | has '_max_weight' => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => 'My:IntGe0', | ||
| 41 | builder => '_build_max_weight', | ||
| 42 | lazy_build => 1 | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub BUILD { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | my $weights = $self->weights; | ||
| 49 | my $keys = $self->keys; | ||
| 50 | |||
| 51 | if (scalar(@$weights) != scalar(@$keys)) { | ||
| 52 | croak "Number of weights must be equal to the number of keys"; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _build_num_weights { | ||
| 57 | my $self = shift; | ||
| 58 | my $weights = $self->_weights; | ||
| 59 | return scalar @$weights; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _build_max_weight { | ||
| 63 | my $self = shift; | ||
| 64 | my $weights = $self->_weights; | ||
| 65 | return $weights->[-1]{up}; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _build_weights { | ||
| 69 | my $self = shift; | ||
| 70 | my $weights = $self->weights; | ||
| 71 | |||
| 72 | my @weights_offset; | ||
| 73 | my $left = 0; | ||
| 74 | |||
| 75 | for (my $i = 0; $i < @$weights; $i++) { | ||
| 76 | my %weight = ( | ||
| 77 | down => $left, | ||
| 78 | up => $left + $weights->[$i] - 1 | ||
| 79 | ); | ||
| 80 | |||
| 81 | $left += $weights->[$i]; | ||
| 82 | push @weights_offset => \%weight; | ||
| 83 | } | ||
| 84 | |||
| 85 | return \@weights_offset; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub weighted_raffle { | ||
| 89 | my ($self, $rng) = @_; | ||
| 90 | |||
| 91 | # Raffle between 0 and max weight | ||
| 92 | my $range = $rng->get_n($self->_max_weight + 1); | ||
| 93 | |||
| 94 | # Look for the index where the range is | ||
| 95 | my $index = $self->with_bsearch($range, $self->_weights, | ||
| 96 | $self->_num_weights, \&_cmp); | ||
| 97 | |||
| 98 | if (not defined $index) { | ||
| 99 | croak "Random index not found at range = $range"; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Do it! | ||
| 103 | return $self->_get_key($index); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _cmp { | ||
| 107 | # State the function to compare at bsearch | ||
| 108 | my ($range, $weight) = @_; | ||
| 109 | |||
| 110 | if ($range >= $weight->{down} && $range <= $weight->{up}) { | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | elsif ($range > $weight->{down}) { | ||
| 114 | return 1; | ||
| 115 | } else { | ||
| 116 | return -1; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | __END__ | ||