Collection/PickItems/RandomSeekLines.pm

filter: Statementstmt 28.8% 13 / 45 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 66.6% 2 / 3 Podpod 0.0% 0 / 1 totaltotal 15.7% 15 / 95 CC 19 SCAR 58.4
CC
19
Cov
15%
pick_items
380.0
BEGIN
1.0
BEGIN
1.0
CRAP 343.9
↑ Summary Collection/PickItems/RandomPos.pm »
1 package Role::TinyCommons::Collection::PickItems::RandomSeekLines;
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 'fh';
12 # optionally depended methods
13 # fh_min_offset
14 # fh_max_offset
15 # cmp_items
16  
17 1 with 'Role::TinyCommons::Collection::PickItems';
18  
19 0 ▶sub pick_items {
Subroutine pick_items: not called CC=19 SCAR=59.4
Pod undocumented
20 0 my ($self, %args) = @_;
21 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
22 0 ▶ my $allow_resampling = defined $args{allow_resampling} ? $args{allow_resampling} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'allow_resampling'} ? : 0 0 0
23 0 ▶ my $max_attempts = $args{max_attempts} || 10_000;
Condition0%0/2
$args{'max_attempts'} || 10000
l!l
00
MC/DC0%0/1
$args{'max_attempts'} || 10000
$args{'max_attempts'}
Truth table0%0/2
A
A= $args{'max_attempts'}
Aresult
11
00
24  
25 0 my $fh = $self->fh;
26 0 ▶ my $fh_min_offset = $self->can('fh_min_offset') ? $self->fh_min_offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('fh_min_offset') ? : 0 0 0
27 0 ▶ my $fh_max_offset = $self->can('fh_max_offset') ? $self->fh_max_offset : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('fh_max_offset') ? : 0 0 0
28 0 ▶ unless (defined $fh_max_offset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh_max_offset) 0 0 0
29 0 ▶ my @st = stat($fh) or die "Can't stat filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = stat $fh or die "Can't stat filehandle: $!" 0 0 0
30 0 $fh_max_offset = $st[7]-1;
31 }
32 #print "D:fh_min_offset=$fh_min_offset, fh_max_offset=$fh_max_offset\n";
33  
34 0 ▶ return () unless $fh_max_offset >= 0 && $fh_max_offset > $fh_min_offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_max_offset >= 0 and $fh_max_offset > $fh_min_offset 0 0 0
Condition0%0/3
$fh_max_offset >= 0 and $fh_max_offset > $fh_min_offset
!ll&&!rl&&r
000
MC/DC0%0/2
$fh_max_offset >= 0 and $fh_max_offset > $fh_min_offset
$fh_max_offset >= 0 $fh_max_offset > $fh_min_offset
Truth table0%0/3
A and B
A= $fh_max_offset >= 0B= $fh_max_offset > $fh_min_offset
ABresult
0X0
100
111
35  
36 0 my @items;
37 my %used_pos;
38 0 my $attempts = 0;
39 PICK:
40 0 ▶ while (@items < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items < $n) 0 0 0
41 0 ▶ if ($attempts++ > $max_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts++ > $max_attempts) 0 0 0
42 0 warn "max_attempts exceeded, only picked ".scalar(@items)." out of $n";
43 0 last PICK;
44 }
45  
46 0 my ($line, $pos);
47 GET_RANDOM_LINE: {
48 0 my $pos0 = int(rand($fh_max_offset-$fh_min_offset+1)) + $fh_min_offset;
49 0 seek $fh, $pos0, 0; # XXX this is random *bytes*
50 0 ▶ if ($pos0 > $fh_min_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos0 > $fh_min_offset) 0 0 0
51 # discard partial line
52 0 <$fh>;
53 }
54 0 $line = <$fh>;
55 0 $pos = tell $fh;
56 0 ▶ die "Can't tell filehandle position: $!" if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
57 0 ▶ next PICK if !defined($line) || ($pos >= $fh_max_offset && $line !~ /\R\z/);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line or $pos >= $fh_max_offset and not $line =~ /\R\z/ 0 0 0
Condition0%0/6
not defined $line or $pos >= $fh_max_offset and not $line =~ /\R\z/
l!l&&r!l&&!r
000
$pos >= $fh_max_offset and not $line =~ /\R\z/
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $line or $pos >= $fh_max_offset and not $line =~ /\R\z/
not defined $line $pos >= $fh_max_offset not $line =~ /\R\z/
Truth table0%0/4
not(A) or B and not(C)
A= not defined $lineB= $pos >= $fh_max_offsetC= not $line =~ /\R\z/
ABCresult
1XX1
0111
00X0
0100
58 #print "D:line=<$line>\n";
59 0 chomp($line);
60 }
61  
62 0 ▶ unless ($allow_resampling) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allow_resampling) 0 0 0
63 0 ▶ next if $used_pos{$pos}++;
Branch: 1 branch
BranchTrueFalseTotal
if $used_pos{$pos}++ 0 0 0
64 }
65 0 push @items, $line;
66 }
67 0 @items;
68 }
69  
70 1 1;
71 # ABSTRACT: Provide pick_items() that picks items by random seeking lines in a (file)handle
72  
73 __END__
↑ Summary Collection/PickItems/RandomPos.pm »