1 package Tie::SubstrHash;
7 Tie::SubstrHash - Fixed-table-size, fixed-key-length hashing
11 require Tie::SubstrHash;
13 tie %myhash, 'Tie::SubstrHash', $key_len, $value_len, $table_size;
17 The B<Tie::SubstrHash> package provides a hash-table-like interface to
18 an array of determinate size, with constant key size and record size.
20 Upon tying a new hash to this package, the developer must specify the
21 size of the keys that will be used, the size of the value fields that the
22 keys will index, and the size of the overall table (in terms of key-value
23 pairs, not size in hard memory). I<These values will not change for the
24 duration of the tied hash>. The newly-allocated hash table may now have
25 data stored and retrieved. Efforts to store more than C<$table_size>
26 elements will result in a fatal error, as will efforts to store a value
27 not exactly C<$value_len> characters in length, or reference through a
28 key not exactly C<$key_len> characters in length. While these constraints
29 may seem excessive, the result is a hash table using much less internal
30 memory than an equivalent freely-allocated hash table.
34 Because the current implementation uses the table and key sizes for the
35 hashing algorithm, there is no means by which to dynamically change the
36 value of any of the initialization parameters.
38 The hash does not support exists().
46 my ($klen, $vlen, $tsize) = @_;
47 my $rlen = 1 + $klen + $vlen;
49 findgteprime($tsize * 1.1)]; # Allow 10% empty.
50 $self = bless ["\0", $klen, $vlen, $tsize, $rlen, 0, -1];
51 $$self[0] x= $rlen * $tsize->[1];
57 $$self[0] = "\0" x ($$self[4] * $$self[3]->[1]);
63 local($self,$key) = @_;
64 local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
67 $offset = $hash * $rlen;
68 $record = substr($$self[0], $offset, $rlen);
69 if (ord($record) == 0) {
72 elsif (ord($record) == 1) {
74 elsif (substr($record, 1, $klen) eq $key) {
75 return substr($record, 1+$klen, $vlen);
82 local($self,$key,$val) = @_;
83 local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
84 croak("Table is full ($tsize->[0] elements)") if $$self[5] > $tsize->[0];
85 croak(qq/Value "$val" is not $vlen characters long/)
86 if length($val) != $vlen;
91 $offset = $hash * $rlen;
92 $record = substr($$self[0], $offset, $rlen);
93 if (ord($record) == 0) {
94 $record = "\2". $key . $val;
95 die "panic" unless length($record) == $rlen;
96 $writeoffset = $offset unless defined $writeoffset;
97 substr($$self[0], $writeoffset, $rlen) = $record;
101 elsif (ord($record) == 1) {
102 $writeoffset = $offset unless defined $writeoffset;
104 elsif (substr($record, 1, $klen) eq $key) {
105 $record = "\2". $key . $val;
106 die "panic" unless length($record) == $rlen;
107 substr($$self[0], $offset, $rlen) = $record;
115 local($self,$key) = @_;
116 local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
119 $offset = $hash * $rlen;
120 $record = substr($$self[0], $offset, $rlen);
121 if (ord($record) == 0) {
124 elsif (ord($record) == 1) {
126 elsif (substr($record, 1, $klen) eq $key) {
127 substr($$self[0], $offset, 1) = "\1";
128 return substr($record, 1+$klen, $vlen);
143 local($klen, $vlen, $tsize, $rlen, $entries, $iterix) = @$self[1..6];
144 for (++$iterix; $iterix < $tsize->[1]; ++$iterix) {
145 next unless substr($$self[0], $iterix * $rlen, 1) eq "\2";
147 return substr($$self[0], $iterix * $rlen + 1, $klen);
154 croak "Tie::SubstrHash does not support exists()";
158 croak(qq/Key "$key" is not $klen characters long/)
159 if length($key) != $klen;
161 for (unpack('C*', $key)) {
162 $hash = $hash * 33 + $_;
163 &_hashwrap if $hash >= 1e13;
165 &_hashwrap if $hash >= $tsize->[1];
166 $hash = 1 unless $hash;
171 $hash -= int($hash / $tsize->[1]) * $tsize->[1];
176 $hash -= $tsize->[1] if $hash >= $tsize->[1];
179 # using POSIX::ceil() would be too heavy, and not all platforms have it.
182 $num = int($num + 1) unless $num == int $num;
188 # http://www-groups.dcs.st-andrews.ac.uk/~history/HistTopics/Prime_numbers.html
191 sub findgteprime { # find the smallest prime integer greater than or equal to
194 my $num = ceil(shift);
195 return 2 if $num <= 2;
197 $num++ unless $num % 2;
199 my $sqrtnum = int sqrt $num;
200 my $sqrtnumsquared = $sqrtnum * $sqrtnum;
204 if ($sqrtnumsquared < $num) {
206 $sqrtnumsquared = $sqrtnum * $sqrtnum;
208 for ($i = 3; $i <= $sqrtnum; $i += 2) {
209 next NUM unless $num % $i;