Collection/PickItems/RandomPos.pm

filter: Statementstmt 48.2% 14 / 29 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 66.6% 2 / 3 Podpod 0.0% 0 / 1 totaltotal 33.3% 16 / 48 CC 8 SCAR 40.6
CC
8
Cov
33%
pick_items
72.0
BEGIN
1.0
BEGIN
1.0
CRAP 57.8
« Collection/PickItems/RandomSeekLines.pm ↑ Summary Collection/PickItems/Iterator.pm »
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
l!l
00
MC/DC0%0/1
$args{'n'} || 1
$args{'n'}
Truth table0%0/2
A
A= $args{'n'}
Aresult
11
00
20 0 ▶ my $allow_resampling = defined $args{allow_resampling} ? $args{allow_resampling} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'allow_resampling'} ? : 0 0 0
21 0 my $item_count = $self->get_item_count;
22  
23 # got an infinite collection?
24 0 ▶ if ($item_count == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_count == -1) 0 0 0
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
BranchTrueFalseTotal
if $n > $item_count 0 0 0
30  
31 0 my @items;
32 my %used_pos;
33 0 ▶ while (@items < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items < $n) 0 0 0
34 0 my $pos = int(rand() * $item_count);
35 0 ▶ unless ($allow_resampling) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allow_resampling) 0 0 0
36 0 ▶ next if $used_pos{$pos}++;
Branch: 1 branch
BranchTrueFalseTotal
if $used_pos{$pos}++ 0 0 0
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__
« Collection/PickItems/RandomSeekLines.pm ↑ Summary Collection/PickItems/Iterator.pm »