X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTie%2FSubstrHash.pm;h=4b18a58e122cc62cbfe03173151773400c78b445;hb=19799a22062ef658e4ac543ea06fa9193323512a;hp=6250e7384875f0439aaa4989c6b8c4f3a33c523a;hpb=399f14a194513745fd160c0e4e8f6f7f718779cf;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Tie/SubstrHash.pm b/lib/Tie/SubstrHash.pm index 6250e73..4b18a58 100644 --- a/lib/Tie/SubstrHash.pm +++ b/lib/Tie/SubstrHash.pm @@ -1,4 +1,40 @@ -package SubstrHash; +package Tie::SubstrHash; + +=head1 NAME + +Tie::SubstrHash - Fixed-table-size, fixed-key-length hashing + +=head1 SYNOPSIS + + require Tie::SubstrHash; + + tie %myhash, 'Tie::SubstrHash', $key_len, $value_len, $table_size; + +=head1 DESCRIPTION + +The B package provides a hash-table-like interface to +an array of determinate size, with constant key size and record size. + +Upon tying a new hash to this package, the developer must specify the +size of the keys that will be used, the size of the value fields that the +keys will index, and the size of the overall table (in terms of key-value +pairs, not size in hard memory). I. The newly-allocated hash table may now have +data stored and retrieved. Efforts to store more than C<$table_size> +elements will result in a fatal error, as will efforts to store a value +not exactly C<$value_len> characters in length, or reference through a +key not exactly C<$key_len> characters in length. While these constraints +may seem excessive, the result is a hash table using much less internal +memory than an equivalent freely-allocated hash table. + +=head1 CAVEATS + +Because the current implementation uses the table and key sizes for the +hashing algorithm, there is no means by which to dynamically change the +value of any of the initialization parameters. + +=cut + use Carp; sub TIEHASH { @@ -33,7 +69,7 @@ sub FETCH { sub STORE { local($self,$key,$val) = @_; local($klen, $vlen, $tsize, $rlen) = @$self[1..4]; - croak("Table is full") if $self[5] == $tsize; + croak("Table is full") if $$self[5] == $tsize; croak(qq/Value "$val" is not $vlen characters long./) if length($val) != $vlen; my $writeoffset; @@ -108,13 +144,17 @@ sub hashkey { $hash = 2; for (unpack('C*', $key)) { $hash = $hash * 33 + $_; + &_hashwrap if $hash >= 1e13; } - $hash = $hash - int($hash / $tsize) * $tsize - if $hash >= $tsize; + &_hashwrap if $hash >= $tsize; $hash = 1 unless $hash; $hashbase = $hash; } +sub _hashwrap { + $hash -= int($hash / $tsize) * $tsize; +} + sub rehash { $hash += $hashbase; $hash -= $tsize if $hash >= $tsize;