| 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
| |||||||||||
| 19 | 20 | ▶ | croak "Buffer overflow!" if exists $_[0]->{die_overflow}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 19 | shift @{$_[0]->{buf}}; | |||||||||
| 21 | 19 | ▶ | $_[0]->{tail}-- if $_[0]->{tail} > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 41 | push @{$_[0]->{buf}}, $_[1]; | |||||||||
| 25 | |||||||||||
| 26 | 41 | ▶ | $_[0]->{head}++ if $_[0]->{head} < $_[0]->{size}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 48 | 6 | ▶ | croak "Buffer size must be positive" unless $_[1] > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 4 | my %opts; | |||||||||
| 51 | 4 | ▶ | if (ref $_[2] eq "HASH") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | ▶ | $opts{die_overflow} = undef if $_[2]->{die_overflow}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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__ | ||||||||||