| 1 | package Tie::Scalar::RingBuffer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub _castbool ($) { $_[0] ? 1 : 0 } | ||||||||||||||||||||||||||
|
Subroutine
_castbool: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | sub TIESCALAR { | ||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=12 SCAR=25.8
| |||||||||||||||||||||||||||||
| 12 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 8 | my $list = shift; | |||||||||||||||||||||||||||
| 14 | 8 | ▶ | my $opts = shift || +{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | croak "Tie::Scalar::RingBuffer expects a listref" unless @_ == 0 && ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 0 and ref $list eq "ARRAY"
MC/DC0%0/2
@_ == 0 and ref $list eq "ARRAY"
@_ == 0
ref $list eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 0B= ref $list eq "ARRAY"
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | croak "Tie::Scalar::RingBuffer expects a hashref of options" unless ref($opts) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # validate options | ||||||||||||||||||||||||||||
| 20 | 8 | for (keys %$opts){ | |||||||||||||||||||||||||||
| 21 | 5 | ▶ | /^(?:start_offset|increment|random)$/ or croak "Unrecognized option '$_'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | if (exists($opts->{'increment'}) && exists($opts->{'random'})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $opts->{"increment"} and exists $opts->{"random"}
MC/DC0%0/2
exists $opts->{"increment"} and exists $opts->{"random"}
exists $opts->{"increment"}
exists $opts->{"random"}
Truth table66%2/3
A and B
A= exists $opts->{"increment"}B= exists $opts->{"random"}
| |||||||||||||||||||||||||||||
| 25 | 0 | croak "options 'increment' and 'random' are mutually exclusive" | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # set defaults | ||||||||||||||||||||||||||||
| 29 | 8 | my $self = bless +{ _list => $list, | |||||||||||||||||||||||||||
| 30 | _ix => 0, | ||||||||||||||||||||||||||||
| 31 | _last_ix => 0, | ||||||||||||||||||||||||||||
| 32 | _random => 0, | ||||||||||||||||||||||||||||
| 33 | _debug => 0, | ||||||||||||||||||||||||||||
| 34 | _incr => 1}, $class; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # apply options | ||||||||||||||||||||||||||||
| 37 | 8 | ▶ | if (exists $opts->{'random'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | $self->{_random} = _castbool ($opts->{'random'}); | |||||||||||||||||||||||||||
| 39 | 1 | $self->start_offset(int rand @{$self->{_list}}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 8 | ▶ | $self->start_offset ($opts->{'start_offset'}) if exists $opts->{'start_offset'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 8 | ▶ | $self->increment ($opts->{'increment'}) if exists $opts->{'increment'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 8 | $self | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub FETCH | ||||||||||||||||||||||||||||
| 48 | 83 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 49 | 83 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | 83 | ▶ | print ("FETCH(ix=",$self->{_ix},")\n") if $self->{_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 83 | my ($list,$ix) = ($self->{_list}, $self->{_ix}); | |||||||||||||||||||||||||||
| 52 | $self->{_ix} = $self->{_random} ? (rand @$list) | ||||||||||||||||||||||||||||
| 53 | 83 | ▶ | : ($ix + $self->{_incr}) % @$list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 83 | $self->{_last_ix} = $ix; | |||||||||||||||||||||||||||
| 55 | 83 | $list->[$ix]; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub redo () | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
redo: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | croak "redo expects an object" unless UNIVERSAL::isa($self, __PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->{_list}->[$self->{_last_ix}] | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub STORE | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 67 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 3 | ▶ | print("STORE(_last_ix=",$self->{_last_ix},")\n") if $self->{_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | $self->{_list}->[$self->{_last_ix}] = shift; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | ▶ | sub UNTIE { } | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub DESTROY { } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub start_offset { | ||||||||||||||||||||||||||
|
Subroutine
start_offset: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 77 | 3 | ▶ | croak "start_offset() expects an object" unless UNIVERSAL::isa($self,__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | if (@_){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | my $start_offset = shift; | |||||||||||||||||||||||||||
| 81 | 3 | ▶ | croak "start_offset() expects a numeric offset" unless defined($start_offset) && $start_offset =~ /^[+-]?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $start_offset and $start_offset =~ /^[+-]?\d+$/
MC/DC0%0/2
defined $start_offset and $start_offset =~ /^[+-]?\d+$/
defined $start_offset
$start_offset =~ /^[+-]?\d+$/
Truth table33%1/3
A and B
A= defined $start_offsetB= $start_offset =~ /^[+-]?\d+$/
| |||||||||||||||||||||||||||||
| 82 | 3 | $self->{_ix} = ($start_offset % scalar @{$self->{_list}}); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 3 | $self->{_ix}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub increment { | ||||||||||||||||||||||||||
|
Subroutine
increment: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | croak "increment() expects an object" unless UNIVERSAL::isa($self,__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if(@_){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | my $incr = shift; | |||||||||||||||||||||||||||
| 92 | 2 | ▶ | croak "increment() expects an integer" unless defined($incr) && $incr =~ /^[+-]?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $incr and $incr =~ /^[+-]?\d+$/
MC/DC0%0/2
defined $incr and $incr =~ /^[+-]?\d+$/
defined $incr
$incr =~ /^[+-]?\d+$/
Truth table33%1/3
A and B
A= defined $incrB= $incr =~ /^[+-]?\d+$/
| |||||||||||||||||||||||||||||
| 93 | 2 | $self->{_incr} = $incr; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 2 | $self->{_incr}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||