blib/lib/Data/RingBuffer.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 63 / 63 CC 10 SCAR 12.4
CC
10
Cov
100%
push
5.0
new
5.0
get
2.0
CRAP 3.5
↑ Summary
1 #!/usr/bin/perl
2 #made by: KorG
3 # vim: sw=4 ts=4 et cc=79 :
4  
5 package Data::RingBuffer;
6  
7 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.02';
13 3 $VERSION =~ tr/_//d;
14  
15 # Add an element to the buffer
16 # args: $obj
17 42 ▶sub push {
Subroutine push: called CC=5 SCAR=16.1
Pod documented
18 42 ▶ if ($_[0]->{head} == $_[0]->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"head"} == $_[0]{"size"}) 20 22 42
19 20 ▶ croak "Buffer overflow!" if exists $_[0]->{die_overflow};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"die_overflow"} 1 19 20
20 19 shift @{$_[0]->{buf}};
21 19 ▶ $_[0]->{tail}-- if $_[0]->{tail} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"tail"} > 0 2 17 19
22 }
23  
24 41 push @{$_[0]->{buf}}, $_[1];
25  
26 41 ▶ $_[0]->{head}++ if $_[0]->{head} < $_[0]->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"head"} < $_[0]{"size"} 22 19 41
27  
28 41 return $_[1];
29 }
30  
31 # Get all elements in the buffer
32 1 ▶sub getall {
Subroutine getall: called CC=1 SCAR=0.0
Pod documented
33 1 return $_[0]->{buf};
34 }
35  
36 # Get next element from the buffer
37 21 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
38 21 my $rb = $_[0];
39 21 ▶ return if $_[0]->{tail} >= $_[0]->{head};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"tail"} >= $_[0]{"head"} 1 20 21
40 20 return $_[0]->{buf}->[$_[0]->{tail}++];
41 }
42  
43 # OO ctor
44 # args: $size, (optional) { %options }
45 7 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
46 # Parse arguments
47 7 ▶ croak "Buffer size not defined" unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 1 6 7
48 6 ▶ croak "Buffer size must be positive" unless $_[1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] > 0 1 4 5
49  
50 4 my %opts;
51 4 ▶ if (ref $_[2] eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2] eq "HASH") 2 2 4
52 2 ▶ $opts{die_overflow} = undef if $_[2]->{die_overflow};
Branch: 1 branch
BranchTrueFalseTotal
if $_[2]{"die_overflow"} 1 1 2
53 }
54  
55 bless {
56 4 buf => [],
57 size => $_[1],
58 head => 0,
59 tail => 0,
60 %opts,
61 }, $_[0];
62 }
63  
64 3 1; # End of Data::RingBuffer
65  
66 __END__
↑ Summary