| 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
| |||||||||||
| 31 | 30 | ▶ | return $idx if $buf->[$idx] > $_[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 42 | 9 | ▶ | croak "Time must be positive" unless $_[1] >= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 7 | my $idx = $_[0]->_find_index($_[1]); | |||||||||
| 44 | 7 | ▶ | return [] unless defined $idx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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__ | ||||||||||