blib/lib/Data/RingBuffer/Time.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 100% 63 / 63 CC 6 SCAR 7.5
CC
6
Cov
100%
getall
4.0
_find_index
3.0
BEGIN
1.0
CRAP 2.1
↑ Summary
1 #!/usr/bin/perl
2 #made by: KorG
3 # vim: sw=4 ts=4 et cc=79 :
4  
5 package Data::RingBuffer::Time;
6  
7 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Data::RingBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '0.02';
14 4 $VERSION =~ tr/_//d;
15  
16 4 ▶BEGIN { our @ISA = 'Data::RingBuffer' };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Add an element to the buffer
19 # args: $obj
20 70 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
21 70 $_[0]->[0]->push(time);
22 70 $_[0]->[1]->push($_[1]);
23 70 return $_[1];
24 }
25  
26 # (internal) find index of first element with time
27 7 ▶sub _find_index {
Subroutine _find_index: called CC=3 SCAR=11.0
28 7 my $idx = 0;
29 7 my $buf = $_[0]->[0]->{buf};
30 7 ▶ while ($idx <= $#$buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $buf->$#*) 30 2 32
31 30 ▶ return $idx if $buf->[$idx] > $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $buf->[$idx] > $_[1] 5 25 30
32 25 $idx++;
33 }
34  
35 2 return undef;
36 }
37  
38 # Get all elements in the buffer
39 # args: (optional) time
40 11 ▶sub getall {
Subroutine getall: called CC=4 SCAR=13.9
Pod documented
41 11 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 9 2 11
42 9 ▶ croak "Time must be positive" unless $_[1] >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] >= 0 1 7 8
43 7 my $idx = $_[0]->_find_index($_[1]);
44 7 ▶ return [] unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 2 5 7
45 5 my $buf = $_[0]->[1]->{buf};
46 5 return [@{$buf}[$idx .. $#$buf]];
47 }
48 2 return $_[0]->[1]->getall();
49 }
50  
51 # Get next element from the buffer
52 20 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
53 20 $_[0]->[0]->get(); # handle tail movement
54 20 $_[0]->[1]->get();
55 }
56  
57 # OO ctor
58 # args: $size
59 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60 8 bless [
61 Data::RingBuffer->new($_[1]), # time storage
62 Data::RingBuffer->new($_[1]), # obj storage
63 ], $_[0];
64 }
65  
66 4 1; # End of Data::RingBuffer::Time
67  
68 __END__
↑ Summary