File Coverage

blib/lib/Data/RingBuffer.pm
Criterion Covered Total %
statement 24 25 96.0
branch 9 12 75.0
condition n/a
subroutine 7 8 87.5
pod 4 4 100.0
total 44 49 89.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             #made by: KorG
3             # vim: sw=4 ts=4 et cc=79 :
4              
5             package Data::RingBuffer;
6              
7 2     2   135641 use 5.008;
  2         17  
8 2     2   10 use strict;
  2         4  
  2         48  
9 2     2   11 use warnings FATAL => 'all';
  2         3  
  2         70  
10 2     2   12 use Carp;
  2         2  
  2         738  
11             # use Exporter 'import';
12              
13             our $VERSION = '0.01';
14             $VERSION =~ tr/_//d;
15              
16             # Add an element to the buffer
17             sub push {
18 32 100   32 1 85 shift @{$_[0]->{buf}} if $_[0]->{head} == $_[0]->{size};
  16         23  
19              
20 32 50       57 $_[0]->{tail}-- if $_[0]->{tail} > 0;
21 32         37 push @{$_[0]->{buf}}, $_[1];
  32         51  
22              
23 32 100       58 $_[0]->{head}++ if $_[0]->{head} < $_[0]->{size};
24              
25 32         51 return $_[1];
26             }
27              
28             # Get all elements in the buffer
29             sub getall {
30 0     0 1 0 return $_[0]->{buf};
31             }
32              
33             # Get next element from the buffer
34             sub get {
35 17     17 1 4420 my $rb = $_[0];
36 17 100       50 return if $_[0]->{tail} >= $_[0]->{head};
37 16         42 return $_[0]->{buf}->[$_[0]->{tail}++];
38             }
39              
40             # OO ctor
41             # args: $size
42             sub new {
43             # Parse arguments
44 1 50   1 1 885 croak "Buffer size not defined" unless defined $_[1];
45 1 50       5 croak "Buffer size must be positive" unless $_[1] > 0;
46              
47 1         6 bless {
48             buf => [],
49             size => $_[1],
50             head => 0,
51             tail => 0,
52             }, $_[0];
53             }
54              
55             1; # End of Data::RingBuffer
56              
57             __END__