| 1 | package WordListRole::RandomSeekPick; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||
| 4 | 1 | our $DATE = '2020-05-23'; # DATE | |||||||||||||||||||
| 5 | 1 | our $DIST = 'WordListRole-RandomSeekPick'; # DIST | |||||||||||||||||||
| 6 | 1 | our $VERSION = '0.004'; # VERSION | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Role::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub pick { | ||||||||||||||||||
|
Subroutine
pick: not called CC=11 SCAR=47.1
Pod
documented
| |||||||||||||||||||||
| 13 | # why is still this necesary? does Role::Tiny enforce strict? | ||||||||||||||||||||
| 14 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 0 | my ($self, $n, $allow_duplicates) = @_; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | $n = 1 if !defined $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 0 | ▶ | die "Please don't pick too many items" if $n >= 10_000; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | my $class = $self->{orig_class} || ref($self); | ||||||||||||||||||
|
Condition0%0/3
$self->{'orig_class'} || ref $self
MC/DC0%0/2
$self->{'orig_class'} || ref $self
$self->{'orig_class'}
ref $self
Truth table0%0/3
A || B
A= $self->{'orig_class'}B= ref $self
| |||||||||||||||||||||
| 22 | 0 | my $fh = \*{"$class\::DATA"}; | |||||||||||||||||||
| 23 | 0 | my $start = ${"$class\::DATA_POS"}; | |||||||||||||||||||
| 24 | 0 | my $end = -s $fh; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | my (%items, @items); | |||||||||||||||||||
| 27 | 0 | my $iter = 0; | |||||||||||||||||||
| 28 | 0 | while (1) { | |||||||||||||||||||
| 29 | 0 | ▶ | if ($allow_duplicates) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | ▶ | last if @items >= $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||
| 32 | 0 | ▶ | last if keys(%items) >= $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | seek $fh, $start + int(rand() * ($end-$start+1)), 0; | |||||||||||||||||||
| 36 | 0 | <$fh>; # skip the line fragment | |||||||||||||||||||
| 37 | 0 | ▶ | seek $fh,0,0 if eof $fh; # wrap if hit EOF | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | chomp(my $item = scalar <$fh>); # get the next line | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | if ($allow_duplicates) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | push @items, $item; | |||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||
| 43 | 0 | $items{$item}++; | |||||||||||||||||||
| 44 | 0 | ▶ | last if $iter++ > 50_000; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | if ($allow_duplicates) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | return @items; | |||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||
| 51 | 0 | return keys %items; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 1 | 1; | |||||||||||||||||||
| 56 | # ABSTRACT: Provide a pick() implementation that random-seeks DATA | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||