| 1 | package Role::TinyCommons::Collection::PickItems::RandomPos; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use Role::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Role::Tiny::With; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 7 | 1 | our $DATE = '2024-01-16'; # DATE | |||||||||||
| 8 | 1 | our $DIST = 'Role-TinyCommons-Collection'; # DIST | |||||||||||
| 9 | 1 | our $VERSION = '0.010'; # VERSION | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | requires 'get_item_count'; | |||||||||||
| 12 | 1 | requires 'get_item_at_pos'; | |||||||||||
| 13 | |||||||||||||
| 14 | 1 | with 'Role::TinyCommons::Collection::PickItems'; | |||||||||||
| 15 | |||||||||||||
| 16 | 0 | ▶ | sub pick_items { | ||||||||||
|
Subroutine
pick_items: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||
| 17 | 0 | my ($self, %args) = @_; | |||||||||||
| 18 | |||||||||||||
| 19 | 0 | ▶ | my $n = $args{n} || 1; | ||||||||||
|
Condition0%0/2
$args{'n'} || 1
MC/DC0%0/1
$args{'n'} || 1
$args{'n'}
Truth table0%0/2
A
A= $args{'n'}
| |||||||||||||
| 20 | 0 | ▶ | my $allow_resampling = defined $args{allow_resampling} ? $args{allow_resampling} : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 0 | my $item_count = $self->get_item_count; | |||||||||||
| 22 | |||||||||||||
| 23 | # got an infinite collection? | ||||||||||||
| 24 | 0 | ▶ | if ($item_count == -1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 0 | warn "Got infinite item count (-1), setting item_count = 2**31 -1"; | |||||||||||
| 26 | 0 | $item_count = 2**31 - 1; | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | 0 | ▶ | $n = $item_count if $n > $item_count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | 0 | my @items; | |||||||||||
| 32 | my %used_pos; | ||||||||||||
| 33 | 0 | ▶ | while (@items < $n) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 0 | my $pos = int(rand() * $item_count); | |||||||||||
| 35 | 0 | ▶ | unless ($allow_resampling) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | ▶ | next if $used_pos{$pos}++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | 0 | push @items, $self->get_item_at_pos($pos); | |||||||||||
| 39 | } | ||||||||||||
| 40 | 0 | @items; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 1 | 1; | |||||||||||
| 44 | # ABSTRACT: Provide pick_items() that picks items by random positions | ||||||||||||
| 45 | |||||||||||||
| 46 | __END__ | ||||||||||||