Ring buffers are used in performance critical situations, often in embedded code, where a fixed size chunk of memory is allocated and used as a queue for buffering data.

The usual implementation uses two pointers (indexes). Details vary a little, but by inspecting the pointers you can determine if the buffer is full (incrementing the input pointer would make it match the output pointer), empty (input pointer matches output pointer) or somewhere between. Very often the buffer size is a power of 2 to take advantage binary arithmetic operations to reduce code complexity and increase speed.

Except as an exercise there is no application I can think of where writing a ring buffer in Perl would be useful. The following may be of interest though:

use strict; use warnings; my $buffer = new (); print "Buffer has ", $buffer->count (), " values\n"; $buffer->push ($_) for 1 .. 200; print "Buffer has ", $buffer->count (), " values\n"; $buffer->pop () for 1 .. 100; print "Buffer has ", $buffer->count (), " values\n"; $buffer->push ($_ + 200) for 1 .. 150; print "Buffer has ", $buffer->count (), " values\n"; sub new { my @bufferArray; my $buffSize = 256; $#bufferArray = 256; # set buffer size return $buffer = bless { buffer => \@bufferArray, in => 0, out => 0, buffSize => $buffSize, }; } sub count { my ($self) = @_; my $used = $self->{in} - $self->{out}; $used += $self->{buffSize} if $used < 0; return $used; } sub pop { my ($self) = @_; die "Buffer underflow" if $self->{in} == $self->{out}; my $value = $self->{buffer}[$self->{out}]; $self->{out} = ($self->{out} + 1) % $self->{buffSize}; return $value; } sub push { my ($self, $value) = @_; die "Buffer overflow" if $self->count () + 1 == $self->{buffSize}; $self->{buffer}[$self->{in}] = $value; $self->{in} = ($self->{in} + 1) % $self->{buffSize}; }

Prints:

Buffer has 0 values Buffer has 200 values Buffer has 100 values Buffer has 250 values

Update: Fixed reporting error in push - thanks ikegami!


True laziness is hard work

In reply to Re: Linked lists in an array of hashes by GrandFather
in thread Linked lists in an array of hashes by biohisham

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.