$my_ordered_hash{$old_key} = $newvalue;
would be implemented as:
sub STORE
{
my( $this, $key, $val ) = @_;
if ( exists( $this->{hash}->{$key} ) )
{
$this->{hash}->{$key} = $val;
}
else
{ $this->{hash}->{$key} = $val;
push @{$this->{order}}, $key;
}
}
...since replacing a value for an existing key should not
(in this case) change its order. if you did want this
behaviour ie STOREing a value *always* appends to the end,
then the performance of STOREing with this implementation
gets a little ugly -- O(n), for the array scan.
the only really messy problem with this implementation is
DELETEing - since that requires array scanning and
splicing.
is there any other complications? |