A circular buffer is one way in languages like C to implement typically a FIFO (First In First Out) queue.
Typically a fixed size hunk of memory is allocated and two pointers "chase" each other, the input and the output pointers. The C version involves pointer arithmetic and is a little complicated in the details, but is extremely performant. The Perl version is also very fast - not as fast as C, but much easier to write!

In Perl, a Perl array can be used to implement a FIFO buffer. A Perl array is different from arrays in C or other programming language's ideas of arrays in that it can change size. So to implement a FIFO queue in Perl, just push new entries onto the bottom of the Perl array and shift out "oldest" entries from the top. The size of the Perl array (and the indicies) will adjust themselves without you having to "move" anything. For the idea, this code is untested, but I figure should work...

my @fifo; my $MAX_FIFO = 53; #max size is optional sub add_entry { die "Cicular buffer overflow\n" if (@fifo >= $MAX_FIFO); my $entry = shift; #well, $_[0] is ok for syntax too #but I think this is more clear #these simple assignment statements are # not "expensive" push @fifo, $entry; } sub get_entry { return (shift @fifo); }
The Perl array also can implement essentially the equivalent of a 'C' doubly linked list. In Perl, this is an array of pointer to hash (an Array of Hash). In C this might be an array of struct and you couldn't change it without a lot of hassle or a linked list where you have to adjust many forward and backward pointers. But in Perl you can use splice() to insert or delete something from the middle of the array (or linked list 'C' analog)! WOW!

get_entry() above could be modified to "die" if the @fifo is empty. There are lots of other possibilities. I advise the OP to study the Perl functions: push, pop, shift, unshift and splice.


In reply to Re: Circular buffer by Marshall
in thread Circular buffer by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.