| 1 | package Array::Pick::Scan; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 10 | 2 | our $DATE = '2022-05-21'; # DATE | |||||||||||||
| 11 | 2 | our $DIST = 'Array-Pick-Scan'; # DIST | |||||||||||||
| 12 | 2 | our $VERSION = '0.005'; # VERSION | |||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw(random_item pick); | |||||||||||||
| 15 | |||||||||||||||
| 16 | 203 | ▶ | sub random_item { | ||||||||||||
|
Subroutine
random_item: called CC=18 SCAR=34.8
Pod
documented
| |||||||||||||||
| 17 | 203 | my ($src, $num_items, $opts) = @_; | |||||||||||||
| 18 | 203 | my $ref = ref $src; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 203 | ▶ | $num_items //= 1; | ||||||||||||
|
Condition50%1/2
$num_items //= 1
MC/DC0%0/1
$num_items //= 1
$num_items
Truth table50%1/2
A
A= $num_items
| |||||||||||||||
| 21 | 203 | ▶ | $opts //= {}; | ||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 203 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 24 | 103 | my $ary = $src; | |||||||||||||
| 25 | 103 | my $ary_size = @$ary; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 103 | ▶ | if (!$ary_size) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 28 | 2 | return (); | |||||||||||||
| 29 | } elsif ($num_items == 1) { | ||||||||||||||
| 30 | 101 | my $idx = int(rand() * $ary_size); | |||||||||||||
| 31 | 101 | ▶ | return $opts->{pos} ? $idx : $ary->[$idx]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } else { | ||||||||||||||
| 33 | 0 | my @items; | |||||||||||||
| 34 | 0 | for my $i (0..$ary_size-1) { | |||||||||||||
| 35 | 0 | ▶ | if (@items < $num_items) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | # we haven't reached $num_items, insert item to array in a | ||||||||||||||
| 37 | # random position | ||||||||||||||
| 38 | 0 | my $idx = int(rand(@items+1)); | |||||||||||||
| 39 | 0 | ▶ | splice @items, $idx, 0, ($opts->{pos} ? $i : $ary->[$i]); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | } else { | ||||||||||||||
| 41 | # we have reached $num_items, just replace an item randomly, | ||||||||||||||
| 42 | # using algorithm from Learning Perl, slightly modified | ||||||||||||||
| 43 | 0 | ▶ | if (rand($i+1) < @items) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 0 | my $idx = int(rand(@items)); | |||||||||||||
| 45 | 0 | ▶ | splice @items, $idx, 1, ($opts->{pos} ? $i : $ary->[$i]); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | 0 | return @items; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | } elsif ($ref eq 'CODE') { | ||||||||||||||
| 52 | 100 | my $iter = $src; | |||||||||||||
| 53 | 100 | my @items; | |||||||||||||
| 54 | 100 | my $i = -1; | |||||||||||||
| 55 | 100 | ▶ | while (defined(my $item = $iter->())) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 1000 | $i++; | |||||||||||||
| 57 | 1000 | ▶ | if (@items < $num_items) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | # we haven't reached $num_items, insert item to array in a | ||||||||||||||
| 59 | # random position | ||||||||||||||
| 60 | 100 | my $idx = int(rand(@items+1)); | |||||||||||||
| 61 | 100 | ▶ | splice @items, $idx, 0, ($opts->{pos} ? $i : $item); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | } else { | ||||||||||||||
| 63 | # we have reached $num_items, just replace an item randomly, | ||||||||||||||
| 64 | # using algorithm from Learning Perl, slightly modified | ||||||||||||||
| 65 | 900 | ▶ | if (rand($i+1) < @items) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 184 | my $idx = int(rand(@items)); | |||||||||||||
| 67 | 184 | ▶ | splice @items, $idx, 1, $opts->{pos} ? $i : $item; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | 100 | return @items; | |||||||||||||
| 72 | } else { | ||||||||||||||
| 73 | 0 | die "Please specify arrayref or coderef iterator as source of items"; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | { | ||||||||||||||
| 78 | 2 | ▶ | no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 79 | 2 | *pick = \&random_item; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 2 | 1; | |||||||||||||
| 83 | # ABSTRACT: Pick random items from an array (or iterator), without duplicates | ||||||||||||||
| 84 | |||||||||||||||
| 85 | __END__ | ||||||||||||||