This is basically a wrap-around array model with bounds checking; however, an address of an element can never be out of bounds, since the model folds all indices back into the model's valid address space.
# ------------------------------------------------------- # Ring.pm : memory ring model # Author : Velaki (velaki@aol.com) # Date : 09 July 2004 # Version : 1.0 # History : # # 1.0 09 July 2004 -- added init routines <Auth> # 0.2 09 July 2004 -- converted set/get to cell <Auth> # 0.1 08 July 2004 -- initial version <Auth> # # ------------------------------------------------------- package Ring; require Exporter; @ISA = qw/Exporter/; @EXPORT = qw//; @EXPORT_OK = qw//; $VERSION = 1.0; use strict; my $DEFAULT_SIZE = 10; # Default ring size for memory ring. # ------------------------------------------------------- # Name : new # Desc : create a new memory ring # # Input : size to make memory ring, or none # Output: new memory ring segment # sub new { my $this = shift; my $class = ref($this) || $this; my $size = shift || $DEFAULT_SIZE; my $self = { SIZE => $size, RING => [] }; bless $self, $class; $self->init; return $self; } # ------------------------------------------------------- # ------------------------------------------------------- # Name : init # Desc : blank the ring memory # # Input : none # Output: none # sub init { my $self = shift; $self->cell($_) = 0 for (0 .. $self->size); } # ------------------------------------------------------- # ------------------------------------------------------- # Name : size # Desc : get the size of the ring # # Input : none # Output: the size of the ring # sub size { my $self = shift; return $self->{SIZE}; } # ------------------------------------------------------- # ------------------------------------------------------- # Name : cell # Desc : get or set the value of a ring cell # # Input : address to be set/retrieved # Output: the current value at the address # sub cell : lvalue { # making it an lvalue means # we can set it during # retrival, e.g. # $c->cell(3) = 10; # print $c->cell(3); # prints: 10 my $self = shift; my $addr = shift; # the address is normalized before being used $self->{RING}[$self->naddr($addr)]; } # ------------------------------------------------------- # ------------------------------------------------------- # Name : naddr # Desc : Normalize ring address to a positive number. # # Input : signed integer address # Output: unsigned address guaranteed to fit in ring # sub naddr { my $self = shift; my $addr = shift; return $addr % $self->size; } # ------------------------------------------------------- 1; # end of module

In reply to Ring Memory by Velaki

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.