| 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
MC/DC0%0/1
$args{'n'} || 1
$args{'n'}
Truth table0%0/2
A
A= $args{'n'}
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $allow_resampling = defined $args{allow_resampling} ? $args{allow_resampling} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $max_attempts = $args{max_attempts} || 10_000; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'max_attempts'} || 10000
MC/DC0%0/1
$args{'max_attempts'} || 10000
$args{'max_attempts'}
Truth table0%0/2
A
A= $args{'max_attempts'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $fh_max_offset = $self->can('fh_max_offset') ? $self->fh_max_offset : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | unless (defined $fh_max_offset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my @st = stat($fh) or die "Can't stat filehandle: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$fh_max_offset >= 0 and $fh_max_offset > $fh_min_offset
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
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my @items; | |||||||||||||||||||||||||||||||||||||||||
| 37 | my %used_pos; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $attempts = 0; | |||||||||||||||||||||||||||||||||||||||||
| 39 | PICK: | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | while (@items < $n) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ($attempts++ > $max_attempts) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | next PICK if !defined($line) || ($pos >= $fh_max_offset && $line !~ /\R\z/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $line or $pos >= $fh_max_offset and not $line =~ /\R\z/
$pos >= $fh_max_offset and not $line =~ /\R\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | #print "D:line=<$line>\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | chomp($line); | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ($allow_resampling) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | next if $used_pos{$pos}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||