]> dxcluster.net Git - spider.git/blob - perl/RingBuf.pm
fix error in broadcast_route_pc9x
[spider.git] / perl / RingBuf.pm
1 #
2 # Finite size ring buffer creation and access routines
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 use strict;
10
11 package RingBuf;
12
13 use vars qw($VERSION $BRANCH);
14 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
15 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
16 $main::build += $VERSION;
17 $main::branch += $BRANCH;
18
19
20 sub new
21 {
22         my $pkg = shift;
23         my $size = shift;
24
25         # 0 - size
26         # 1 - lth
27         # 2 - end
28         # 3 - start
29         # 4 - pos
30         # 5 - buffer []
31         return bless [$size+1, 0, 0, 0, 0, []], (ref $pkg || $pkg);
32 }
33
34 sub write
35 {
36         my $self = shift;
37
38         $self->[5]->[$self->[2]++] = shift;
39         $self->[2] = 0 if $self->[2] >= $self->[0];
40         $self->[1]++ if $self->[1] < $self->[0];
41         if ($self->[1] == $self->[0] && $self->[2] == $self->[3]) {
42                 $self->[3] = $self->[2]+1;
43                 $self->[3] = 0 if $self->[3] >= $self->[0]; 
44         }
45 }
46
47 sub read
48 {
49         my $self = shift;
50         return unless $self->[1];
51         my $r;
52         
53         if ($self->[4] != $self->[2]) {
54                 $r = $self->[5]->[$self->[4]++];
55                 $self->[4] = 0 if $self->[4] >= $self->[0];
56         }
57         return $r;
58 }
59
60 sub rewind
61 {
62         my $self = shift;
63         $self->[4] = $self->[3];
64 }
65
66 sub lth
67 {
68         my $self = shift;
69         return $self->[1];
70 }
71
72 sub readall
73 {
74         my $self = shift;
75         my @out;
76         
77         $self->rewind;
78         while (my $r = $self->read) {
79                 push @out, $r;
80         }
81         return @out;
82 }
83 1;