| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Role::TinyCommons::Collection::PickItems::Iterator; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
412976
|
use Role::Tiny; |
|
|
1
|
|
|
|
|
6829
|
|
|
|
1
|
|
|
|
|
8
|
|
|
4
|
1
|
|
|
1
|
|
845
|
use Role::Tiny::With; |
|
|
1
|
|
|
|
|
319
|
|
|
|
1
|
|
|
|
|
327
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
|
7
|
|
|
|
|
|
|
our $DATE = '2024-01-16'; # DATE |
|
8
|
|
|
|
|
|
|
our $DIST = 'Role-TinyCommons-Collection'; # DIST |
|
9
|
|
|
|
|
|
|
our $VERSION = '0.010'; # VERSION |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
requires 'get_next_item'; |
|
12
|
|
|
|
|
|
|
requires 'has_next_item'; |
|
13
|
|
|
|
|
|
|
requires 'reset_iterator'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
with 'Role::TinyCommons::Collection::PickItems'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub pick_items { |
|
18
|
0
|
|
|
0
|
0
|
|
my ($self, %args) = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
0
|
|
0
|
|
|
|
my $n = $args{n} || 1; |
|
21
|
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
my @items; |
|
23
|
0
|
|
|
|
|
|
my $i = -1; |
|
24
|
0
|
|
|
|
|
|
$self->reset_iterator; |
|
25
|
0
|
|
|
|
|
|
while ($self->has_next_item) { |
|
26
|
0
|
|
|
|
|
|
$i++; |
|
27
|
0
|
|
|
|
|
|
my $item = $self->get_next_item; |
|
28
|
0
|
0
|
|
|
|
|
if (@items < $n) { |
|
29
|
|
|
|
|
|
|
# we haven't reached $num_items, insert item to array in a random |
|
30
|
|
|
|
|
|
|
# position |
|
31
|
0
|
|
|
|
|
|
splice @items, rand(@items+1), 0, $item; |
|
32
|
|
|
|
|
|
|
} else { |
|
33
|
|
|
|
|
|
|
# we have reached $num_items, just replace an item randomly, using |
|
34
|
|
|
|
|
|
|
# algorithm from Learning Perl, slightly modified |
|
35
|
0
|
0
|
|
|
|
|
rand($i+1) < @items and splice @items, rand(@items), 1, $item; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
} |
|
38
|
0
|
|
|
|
|
|
@items; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
1; |
|
42
|
|
|
|
|
|
|
# ABSTRACT: Provide pick_items() that picks by iterating all items once |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
__END__ |