| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::Sandy::WeightedRaffle; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Weighted raffle interface. |
|
3
|
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
47
|
use App::Sandy::Base 'class'; |
|
|
6
|
|
|
|
|
18
|
|
|
|
6
|
|
|
|
|
53
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
with 'App::Sandy::Role::BSearch'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.22'; # 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
|
20
|
|
|
20
|
0
|
40
|
my $self = shift; |
|
47
|
|
|
|
|
|
|
|
|
48
|
20
|
|
|
|
|
560
|
my $weights = $self->weights; |
|
49
|
20
|
|
|
|
|
605
|
my $keys = $self->keys; |
|
50
|
|
|
|
|
|
|
|
|
51
|
20
|
50
|
|
|
|
515
|
if (scalar(@$weights) != scalar(@$keys)) { |
|
52
|
0
|
|
|
|
|
0
|
croak "Number of weights must be equal to the number of keys"; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _build_num_weights { |
|
57
|
4
|
|
|
4
|
|
25
|
my $self = shift; |
|
58
|
4
|
|
|
|
|
208
|
my $weights = $self->_weights; |
|
59
|
4
|
|
|
|
|
190
|
return scalar @$weights; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _build_max_weight { |
|
63
|
4
|
|
|
4
|
|
41
|
my $self = shift; |
|
64
|
4
|
|
|
|
|
276
|
my $weights = $self->_weights; |
|
65
|
4
|
|
|
|
|
132
|
return $weights->[-1]{up}; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _build_weights { |
|
69
|
4
|
|
|
4
|
|
40
|
my $self = shift; |
|
70
|
4
|
|
|
|
|
181
|
my $weights = $self->weights; |
|
71
|
|
|
|
|
|
|
|
|
72
|
4
|
|
|
|
|
54
|
my @weights_offset; |
|
73
|
4
|
|
|
|
|
38
|
my $left = 0; |
|
74
|
|
|
|
|
|
|
|
|
75
|
4
|
|
|
|
|
71
|
for (my $i = 0; $i < @$weights; $i++) { |
|
76
|
20
|
|
|
|
|
203
|
my %weight = ( |
|
77
|
|
|
|
|
|
|
down => $left, |
|
78
|
|
|
|
|
|
|
up => $left + $weights->[$i] - 1 |
|
79
|
|
|
|
|
|
|
); |
|
80
|
|
|
|
|
|
|
|
|
81
|
20
|
|
|
|
|
42
|
$left += $weights->[$i]; |
|
82
|
20
|
|
|
|
|
93
|
push @weights_offset => \%weight; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
4
|
|
|
|
|
147
|
return \@weights_offset; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub weighted_raffle { |
|
89
|
1710
|
|
|
1710
|
0
|
2890
|
my $self = shift; |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Raffle between 0 and max weight |
|
92
|
1710
|
|
|
|
|
49281
|
my $range = int(rand($self->_max_weight + 1)); |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Look for the index where the range is |
|
95
|
1710
|
|
|
|
|
48417
|
my $index = $self->with_bsearch($range, $self->_weights, |
|
96
|
|
|
|
|
|
|
$self->_num_weights, \&_cmp); |
|
97
|
|
|
|
|
|
|
|
|
98
|
1710
|
50
|
|
|
|
4303
|
if (not defined $index) { |
|
99
|
0
|
|
|
|
|
0
|
croak "Random index not found at range = $range"; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Do it! |
|
103
|
1710
|
|
|
|
|
77301
|
return $self->_get_key($index); |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _cmp { |
|
107
|
|
|
|
|
|
|
# State the function to compare at bsearch |
|
108
|
3638
|
|
|
3638
|
|
6434
|
my ($range, $weight) = @_; |
|
109
|
|
|
|
|
|
|
|
|
110
|
3638
|
100
|
100
|
|
|
14693
|
if ($range >= $weight->{down} && $range <= $weight->{up}) { |
|
|
|
100
|
|
|
|
|
|
|
111
|
1710
|
|
|
|
|
3989
|
return 0; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
elsif ($range > $weight->{down}) { |
|
114
|
1391
|
|
|
|
|
3559
|
return 1; |
|
115
|
|
|
|
|
|
|
} else { |
|
116
|
537
|
|
|
|
|
1418
|
return -1; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=pod |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=encoding UTF-8 |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 NAME |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
App::Sandy::WeightedRaffle - Weighted raffle interface. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 VERSION |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
version 0.22 |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 AUTHORS |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over 4 |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item * |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Thiago L. A. Miller <tmiller@mochsl.org.br> |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item * |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
J. Leonel Buzzo <lbuzzo@mochsl.org.br> |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Felipe R. C. dos Santos <fsantos@mochsl.org.br> |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Helena B. Conceição <hconceicao@mochsl.org.br> |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Gabriela Guardia <gguardia@mochsl.org.br> |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Fernanda Orpinelli <forpinelli@mochsl.org.br> |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Pedro A. F. Galante <pgalante@mochsl.org.br> |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This software is Copyright (c) 2018 by Teaching and Research Institute from SÃrio-Libanês Hospital. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This is free software, licensed under: |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |