Array::Frugal - Arrays that re-use deleted indices


Array-Frugal documentation Contained in the Array-Frugal distribution.

Index


Code Index:

NAME

Top

Array::Frugal - Arrays that re-use deleted indices

SYNOPSIS

Top

  use Array::Frugal;
  my $stash = new Array::Frugal;
  $index = $stash->PUSH(34);
  print $stash->FETCH($index);  # prints 34;
  $stash->DELETE($index); # $index can be re-used now




DESCRIPTION

Top

Frugal as in memory use. Instead of continuing to count upwards toward MAXINT, when an element is deleted from a frugal array the index is available for re-use.

Currently new, PUSH, FETCH, STORE, and DELETE are all the methods that are defined, but this may become tieable in a future release.

HISTORY

Top

0.01

Original version;

SEE ALSO

Top

AUTHOR

Top

david l nicol, <davidnico@cpan.org>

COPYRIGHT AND LICENSE

Top


Array-Frugal documentation Contained in the Array-Frugal distribution.

package Array::Frugal;

use 5.000;
use strict;

use vars qw /$VERSION/;

$VERSION = '0.01';


# Preloaded methods go here.

sub new{
	bless [1,2,3,4,5,6,7,0];
}
sub PUSH{ # pop reuse stack or extend
	my $a = shift;
        my $i = $a->[0];
	if($i){
		$a->[0] = $a->[$i];
	}else{
		$i = ++$#$a; #extend
	};
	$a->[$i] = shift;
	$i;

}
sub FETCH{
	${$_[0]}[$_[1]];
}
sub STORE{
	${$_[0]}[$_[1]] = $_[2];

}
sub DELETE{  # stack index for reuse
	my $r = ${$_[0]}[$_[1]];
	${$_[0]}[$_[1]] = ${$_[0]}[0];
	${$_[0]}[0] = $_[1];
	$r;
};

1;
__END__
# Below is stub documentation for your module. You'd better edit it!