Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.4 KiB

  1. package Tie::SubstrHash;
  2. =head1 NAME
  3. Tie::SubstrHash - Fixed-table-size, fixed-key-length hashing
  4. =head1 SYNOPSIS
  5. require Tie::SubstrHash;
  6. tie %myhash, 'Tie::SubstrHash', $key_len, $value_len, $table_size;
  7. =head1 DESCRIPTION
  8. The B<Tie::SubstrHash> package provides a hash-table-like interface to
  9. an array of determinate size, with constant key size and record size.
  10. Upon tying a new hash to this package, the developer must specify the
  11. size of the keys that will be used, the size of the value fields that the
  12. keys will index, and the size of the overall table (in terms of key-value
  13. pairs, not size in hard memory). I<These values will not change for the
  14. duration of the tied hash>. The newly-allocated hash table may now have
  15. data stored and retrieved. Efforts to store more than C<$table_size>
  16. elements will result in a fatal error, as will efforts to store a value
  17. not exactly C<$value_len> characters in length, or reference through a
  18. key not exactly C<$key_len> characters in length. While these constraints
  19. may seem excessive, the result is a hash table using much less internal
  20. memory than an equivalent freely-allocated hash table.
  21. =head1 CAVEATS
  22. Because the current implementation uses the table and key sizes for the
  23. hashing algorithm, there is no means by which to dynamically change the
  24. value of any of the initialization parameters.
  25. =cut
  26. use Carp;
  27. sub TIEHASH {
  28. my $pack = shift;
  29. my ($klen, $vlen, $tsize) = @_;
  30. my $rlen = 1 + $klen + $vlen;
  31. $tsize = findprime($tsize * 1.1); # Allow 10% empty.
  32. $self = bless ["\0", $klen, $vlen, $tsize, $rlen, 0, -1];
  33. $$self[0] x= $rlen * $tsize;
  34. $self;
  35. }
  36. sub FETCH {
  37. local($self,$key) = @_;
  38. local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
  39. &hashkey;
  40. for (;;) {
  41. $offset = $hash * $rlen;
  42. $record = substr($$self[0], $offset, $rlen);
  43. if (ord($record) == 0) {
  44. return undef;
  45. }
  46. elsif (ord($record) == 1) {
  47. }
  48. elsif (substr($record, 1, $klen) eq $key) {
  49. return substr($record, 1+$klen, $vlen);
  50. }
  51. &rehash;
  52. }
  53. }
  54. sub STORE {
  55. local($self,$key,$val) = @_;
  56. local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
  57. croak("Table is full") if $$self[5] == $tsize;
  58. croak(qq/Value "$val" is not $vlen characters long./)
  59. if length($val) != $vlen;
  60. my $writeoffset;
  61. &hashkey;
  62. for (;;) {
  63. $offset = $hash * $rlen;
  64. $record = substr($$self[0], $offset, $rlen);
  65. if (ord($record) == 0) {
  66. $record = "\2". $key . $val;
  67. die "panic" unless length($record) == $rlen;
  68. $writeoffset = $offset unless defined $writeoffset;
  69. substr($$self[0], $writeoffset, $rlen) = $record;
  70. ++$$self[5];
  71. return;
  72. }
  73. elsif (ord($record) == 1) {
  74. $writeoffset = $offset unless defined $writeoffset;
  75. }
  76. elsif (substr($record, 1, $klen) eq $key) {
  77. $record = "\2". $key . $val;
  78. die "panic" unless length($record) == $rlen;
  79. substr($$self[0], $offset, $rlen) = $record;
  80. return;
  81. }
  82. &rehash;
  83. }
  84. }
  85. sub DELETE {
  86. local($self,$key) = @_;
  87. local($klen, $vlen, $tsize, $rlen) = @$self[1..4];
  88. &hashkey;
  89. for (;;) {
  90. $offset = $hash * $rlen;
  91. $record = substr($$self[0], $offset, $rlen);
  92. if (ord($record) == 0) {
  93. return undef;
  94. }
  95. elsif (ord($record) == 1) {
  96. }
  97. elsif (substr($record, 1, $klen) eq $key) {
  98. substr($$self[0], $offset, 1) = "\1";
  99. return substr($record, 1+$klen, $vlen);
  100. --$$self[5];
  101. }
  102. &rehash;
  103. }
  104. }
  105. sub FIRSTKEY {
  106. local($self) = @_;
  107. $$self[6] = -1;
  108. &NEXTKEY;
  109. }
  110. sub NEXTKEY {
  111. local($self) = @_;
  112. local($klen, $vlen, $tsize, $rlen, $entries, $iterix) = @$self[1..6];
  113. for (++$iterix; $iterix < $tsize; ++$iterix) {
  114. next unless substr($$self[0], $iterix * $rlen, 1) eq "\2";
  115. $$self[6] = $iterix;
  116. return substr($$self[0], $iterix * $rlen + 1, $klen);
  117. }
  118. $$self[6] = -1;
  119. undef;
  120. }
  121. sub hashkey {
  122. croak(qq/Key "$key" is not $klen characters long.\n/)
  123. if length($key) != $klen;
  124. $hash = 2;
  125. for (unpack('C*', $key)) {
  126. $hash = $hash * 33 + $_;
  127. &_hashwrap if $hash >= 1e13;
  128. }
  129. &_hashwrap if $hash >= $tsize;
  130. $hash = 1 unless $hash;
  131. $hashbase = $hash;
  132. }
  133. sub _hashwrap {
  134. $hash -= int($hash / $tsize) * $tsize;
  135. }
  136. sub rehash {
  137. $hash += $hashbase;
  138. $hash -= $tsize if $hash >= $tsize;
  139. }
  140. sub findprime {
  141. use integer;
  142. my $num = shift;
  143. $num++ unless $num % 2;
  144. $max = int sqrt $num;
  145. NUM:
  146. for (;; $num += 2) {
  147. for ($i = 3; $i <= $max; $i += 2) {
  148. next NUM unless $num % $i;
  149. }
  150. return $num;
  151. }
  152. }
  153. 1;