| 1 | package Role::TinyCommons::Collection::PickItems::Iterator; | ||||||||||||
| 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_next_item'; | |||||||||||
| 12 | 1 | requires 'has_next_item'; | |||||||||||
| 13 | 1 | requires 'reset_iterator'; | |||||||||||
| 14 | |||||||||||||
| 15 | 1 | with 'Role::TinyCommons::Collection::PickItems'; | |||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | sub pick_items { | ||||||||||
|
Subroutine
pick_items: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 18 | 0 | my ($self, %args) = @_; | |||||||||||
| 19 | |||||||||||||
| 20 | 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'}
| |||||||||||||
| 21 | |||||||||||||
| 22 | 0 | my @items; | |||||||||||
| 23 | 0 | my $i = -1; | |||||||||||
| 24 | 0 | $self->reset_iterator; | |||||||||||
| 25 | 0 | ▶ | while ($self->has_next_item) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 0 | $i++; | |||||||||||
| 27 | 0 | my $item = $self->get_next_item; | |||||||||||
| 28 | 0 | ▶ | if (@items < $n) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | # we haven't reached $num_items, insert item to array in a random | ||||||||||||
| 30 | # position | ||||||||||||
| 31 | 0 | splice @items, rand(@items+1), 0, $item; | |||||||||||
| 32 | } else { | ||||||||||||
| 33 | # we have reached $num_items, just replace an item randomly, using | ||||||||||||
| 34 | # algorithm from Learning Perl, slightly modified | ||||||||||||
| 35 | 0 | ▶ | rand($i+1) < @items and splice @items, rand(@items), 1, $item; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | } | ||||||||||||
| 37 | } | ||||||||||||
| 38 | 0 | @items; | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | 1; | |||||||||||
| 42 | # ABSTRACT: Provide pick_items() that picks by iterating all items once | ||||||||||||
| 43 | |||||||||||||
| 44 | __END__ | ||||||||||||