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
|
3
|
|
|
3
|
|
200090
|
use 5.008; |
|
3
|
|
|
|
|
31
|
|
8
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
73
|
|
9
|
3
|
|
|
3
|
|
14
|
use warnings FATAL => 'all'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
101
|
|
10
|
3
|
|
|
3
|
|
26
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1418
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
13
|
|
|
|
|
|
|
$VERSION =~ tr/_//d; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Add an element to the buffer |
16
|
|
|
|
|
|
|
# args: $obj |
17
|
|
|
|
|
|
|
sub push { |
18
|
42
|
100
|
|
42
|
1
|
129
|
if ($_[0]->{head} == $_[0]->{size}) { |
19
|
20
|
100
|
|
|
|
147
|
croak "Buffer overflow!" if exists $_[0]->{die_overflow}; |
20
|
19
|
|
|
|
|
22
|
shift @{$_[0]->{buf}}; |
|
19
|
|
|
|
|
27
|
|
21
|
19
|
100
|
|
|
|
40
|
$_[0]->{tail}-- if $_[0]->{tail} > 0; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
41
|
|
|
|
|
53
|
push @{$_[0]->{buf}}, $_[1]; |
|
41
|
|
|
|
|
80
|
|
25
|
|
|
|
|
|
|
|
26
|
41
|
100
|
|
|
|
407
|
$_[0]->{head}++ if $_[0]->{head} < $_[0]->{size}; |
27
|
|
|
|
|
|
|
|
28
|
41
|
|
|
|
|
62
|
return $_[1]; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Get all elements in the buffer |
32
|
|
|
|
|
|
|
sub getall { |
33
|
1
|
|
|
1
|
1
|
11
|
return $_[0]->{buf}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Get next element from the buffer |
37
|
|
|
|
|
|
|
sub get { |
38
|
21
|
|
|
21
|
1
|
4536
|
my $rb = $_[0]; |
39
|
21
|
100
|
|
|
|
70
|
return if $_[0]->{tail} >= $_[0]->{head}; |
40
|
20
|
|
|
|
|
58
|
return $_[0]->{buf}->[$_[0]->{tail}++]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# OO ctor |
44
|
|
|
|
|
|
|
# args: $size, (optional) { %options } |
45
|
|
|
|
|
|
|
sub new { |
46
|
|
|
|
|
|
|
# Parse arguments |
47
|
7
|
100
|
|
7
|
1
|
3254
|
croak "Buffer size not defined" unless defined $_[1]; |
48
|
6
|
100
|
|
|
|
114
|
croak "Buffer size must be positive" unless $_[1] > 0; |
49
|
|
|
|
|
|
|
|
50
|
4
|
|
|
|
|
6
|
my %opts; |
51
|
4
|
100
|
|
|
|
15
|
if (ref $_[2] eq "HASH") { |
52
|
2
|
100
|
|
|
|
6
|
$opts{die_overflow} = undef if $_[2]->{die_overflow}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
bless { |
56
|
4
|
|
|
|
|
35
|
buf => [], |
57
|
|
|
|
|
|
|
size => $_[1], |
58
|
|
|
|
|
|
|
head => 0, |
59
|
|
|
|
|
|
|
tail => 0, |
60
|
|
|
|
|
|
|
%opts, |
61
|
|
|
|
|
|
|
}, $_[0]; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; # End of Data::RingBuffer |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
__END__ |