1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
package Array::Unique::Hash;
use strict;
use warnings;
our $VERSION = '0.09';
sub new {
TIEARRAY(@_);
}
sub TIEARRAY {
my $class = shift;
my $self = {
array => [],
hash => {},
};
bless $self, $class;
}
sub CLEAR {
my $self = shift;
$self->{array} = [];
$self->{hash} = {};
}
sub EXTEND {
my $self = shift;
#?
}
sub STORE {
my ($self, $index, $value) = @_;
$self->SPLICE($index, 1, $value);
}
sub FETCHSIZE {
my $self = shift;
return scalar @{$self->{array}};
}
sub FETCH {
my ($self, $index) = @_;
${$self->{array}}[$index];
}
sub STORESIZE {
my $self = shift;
my $size = shift;
# We cannot enlarge the array as the values would be undef
# But we can make it smaller
# if ($self->FETCHSIZE > $size) {
# $self->{->Splice($size);
# }
$#{$self->{array}} = $size;
return $size;
}
#sub EXISTS { exists $_[0]->[$_[1]] }
#sub DELETE { delete $_[0]->[$_[1]] }
#sub DESTROY
sub SPLICE {
my $self = shift;
my $offset = shift;
my $length = shift;
# reset length value to positive (this is done by the normal splicetoo)
if ($length < 0) {
#$length = @{$self->{array}} + $length;
$length = $self->FETCHSIZE + $length;
}
# reset offset to positive (this is done by the normal splice too)
if ($offset < 0) {
$offset += $self->FETCHSIZE;
}
# if ($offset > $self->FETCHSIZE) {
# $offset = $self->FETCHSIZE;
# }
# my @s = @{$self->{array}}[$offset..$offset+$length]; # the old values to be returned
my @original = splice @{$self->{array}}, $offset, $length, @_;
return @original;
}
sub PUSH {
my $self = shift;
$self->SPLICE($self->FETCHSIZE, 0, @_);
# while (my $value = shift) {
# $self->STORE($self->FETCHSIZE+1, $value);
# }
return $self->FETCHSIZE;
}
sub POP {
my $self = shift;
$self->SPLICE(-1);
}
sub SHIFT {
my $self = shift;
#($self->{array})[0];
$self->SPLICE(0,1);
}
sub UNSHIFT {
my $self = shift;
$self->SPLICE(0,0,@_);
}
1;
__END__
=pod
See Array::Unique for documentation
=cut
|