1 package Tie
::SubstrHash
;
5 Tie::SubstrHash - Fixed-table-size, fixed-key-length hashing
9 require Tie::SubstrHash;
11 tie %myhash, 'Tie::SubstrHash', $key_len, $value_len, $table_size;
15 The B<Tie::SubstrHash> package provides a hash-table-like interface to
16 an array of determinate size, with constant key size and record size.
18 Upon tying a new hash to this package, the developer must specify the
19 size of the keys that will be used, the size of the value fields that the
20 keys will index, and the size of the overall table (in terms of key-value
21 pairs, not size in hard memory). I<These values will not change for the
22 duration of the tied hash>. The newly-allocated hash table may now have
23 data stored and retrieved. Efforts to store more than C<$table_size>
24 elements will result in a fatal error, as will efforts to store a value
25 not exactly C<$value_len> characters in length, or reference through a
26 key not exactly C<$key_len> characters in length. While these constraints
27 may seem excessive, the result is a hash table using much less internal
28 memory than an equivalent freely-allocated hash table.
32 Because the current implementation uses the table and key sizes for the
33 hashing algorithm, there is no means by which to dynamically change the
34 value of any of the initialization parameters.
36 The hash does not support exists().
44 my ($klen, $vlen, $tsize) = @_;
45 my $rlen = 1 + $klen + $vlen;
47 findgteprime
($tsize * 1.1)]; # Allow 10% empty.
48 $self = bless ["\0", $klen, $vlen, $tsize, $rlen, 0, -1];
49 $$self[0] x
= $rlen * $tsize->[1];
55 $$self[0] = "\0" x
($$self[4] * $$self[3]->[1]);
61 local($self,$key) = @_;
62 local($klen, $vlen, $tsize, $rlen) = @
$self[1..4];
65 $offset = $hash * $rlen;
66 $record = substr($$self[0], $offset, $rlen);
67 if (ord($record) == 0) {
70 elsif (ord($record) == 1) {
72 elsif (substr($record, 1, $klen) eq $key) {
73 return substr($record, 1+$klen, $vlen);
80 local($self,$key,$val) = @_;
81 local($klen, $vlen, $tsize, $rlen) = @
$self[1..4];
82 croak
("Table is full ($tsize->[0] elements)") if $$self[5] > $tsize->[0];
83 croak
(qq/Value "$val" is not $vlen characters long/)
84 if length($val) != $vlen;
89 $offset = $hash * $rlen;
90 $record = substr($$self[0], $offset, $rlen);
91 if (ord($record) == 0) {
92 $record = "\2". $key . $val;
93 die "panic" unless length($record) == $rlen;
94 $writeoffset = $offset unless defined $writeoffset;
95 substr($$self[0], $writeoffset, $rlen) = $record;
99 elsif (ord($record) == 1) {
100 $writeoffset = $offset unless defined $writeoffset;
102 elsif (substr($record, 1, $klen) eq $key) {
103 $record = "\2". $key . $val;
104 die "panic" unless length($record) == $rlen;
105 substr($$self[0], $offset, $rlen) = $record;
113 local($self,$key) = @_;
114 local($klen, $vlen, $tsize, $rlen) = @
$self[1..4];
117 $offset = $hash * $rlen;
118 $record = substr($$self[0], $offset, $rlen);
119 if (ord($record) == 0) {
122 elsif (ord($record) == 1) {
124 elsif (substr($record, 1, $klen) eq $key) {
125 substr($$self[0], $offset, 1) = "\1";
126 return substr($record, 1+$klen, $vlen);
141 local($klen, $vlen, $tsize, $rlen, $entries, $iterix) = @
$self[1..6];
142 for (++$iterix; $iterix < $tsize->[1]; ++$iterix) {
143 next unless substr($$self[0], $iterix * $rlen, 1) eq "\2";
145 return substr($$self[0], $iterix * $rlen + 1, $klen);
152 croak
"Tie::SubstrHash does not support exists()";
156 croak
(qq/Key "$key" is not $klen characters long/)
157 if length($key) != $klen;
159 for (unpack('C*', $key)) {
160 $hash = $hash * 33 + $_;
161 &_hashwrap
if $hash >= 1e13
;
163 &_hashwrap
if $hash >= $tsize->[1];
164 $hash = 1 unless $hash;
169 $hash -= int($hash / $tsize->[1]) * $tsize->[1];
174 $hash -= $tsize->[1] if $hash >= $tsize->[1];
177 # using POSIX::ceil() would be too heavy, and not all platforms have it.
180 $num = int($num + 1) unless $num == int $num;
186 # http://www-groups.dcs.st-andrews.ac.uk/~history/HistTopics/Prime_numbers.html
189 sub findgteprime
{ # find the smallest prime integer greater than or equal to
192 my $num = ceil
(shift);
193 return 2 if $num <= 2;
195 $num++ unless $num % 2;
197 my $sqrtnum = int sqrt $num;
198 my $sqrtnumsquared = $sqrtnum * $sqrtnum;
202 if ($sqrtnumsquared < $num) {
204 $sqrtnumsquared = $sqrtnum * $sqrtnum;
206 for ($i = 3; $i <= $sqrtnum; $i += 2) {
207 next NUM
unless $num % $i;