phash.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/perl
  2. ## --------------------------------------------------------------------------
  3. ##
  4. ## Copyright 1996-2009 the NASM Authors - All rights reserved.
  5. ##
  6. ## Redistribution and use in source and binary forms, with or without
  7. ## modification, are permitted provided that the following
  8. ## conditions are met:
  9. ##
  10. ## * Redistributions of source code must retain the above copyright
  11. ## notice, this list of conditions and the following disclaimer.
  12. ## * Redistributions in binary form must reproduce the above
  13. ## copyright notice, this list of conditions and the following
  14. ## disclaimer in the documentation and/or other materials provided
  15. ## with the distribution.
  16. ##
  17. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  18. ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  19. ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. ##
  31. ## --------------------------------------------------------------------------
  32. #
  33. # Perfect Minimal Hash Generator written in Perl, which produces
  34. # C output.
  35. #
  36. require 'phash.ph';
  37. #
  38. # Read input file
  39. #
  40. sub read_input() {
  41. my $key,$val;
  42. my %out;
  43. my $x = 0;
  44. while (defined($l = <STDIN>)) {
  45. chomp $l;
  46. $l =~ s/\s*(\#.*|)$//;
  47. next if ($l eq '');
  48. if ($l =~ /^([^=]+)\=([^=]+)$/) {
  49. $out{$1} = $2;
  50. $x = $2;
  51. } else {
  52. $out{$l} = $x;
  53. }
  54. $x++;
  55. }
  56. return %out;
  57. }
  58. #
  59. # Main program
  60. #
  61. sub main() {
  62. my $n;
  63. my %data;
  64. my @hashinfo;
  65. my $x, $i;
  66. %data = read_input();
  67. @hashinfo = gen_perfect_hash(\%data);
  68. if (!@hashinfo) {
  69. die "$0: no hash found\n";
  70. }
  71. verify_hash_table(\%data, \@hashinfo);
  72. ($n, $sv, $f1, $f2, $g) = @hashinfo;
  73. print "static int HASHNAME_fg1[$n] =\n";
  74. print "{\n";
  75. for ($i = 0; $i < $n; $i++) {
  76. print "\t", ${$g}[${$f1}[$i]], "\n";
  77. }
  78. print "};\n\n";
  79. print "static int HASHNAME_fg2[$n] =\n";
  80. print "{\n";
  81. for ($i = 0; $i < $n; $i++) {
  82. print "\t", ${$g}[${$f2}[$i]], "\n";
  83. }
  84. print "};\n\n";
  85. print "struct p_hash HASHNAME =\n";
  86. print "{\n";
  87. print "\t$n\n";
  88. print "\t$sv\n";
  89. print "\tHASHNAME_fg1,\n";
  90. print "\tHASHNAME_fg2,\n";
  91. print "};\n";
  92. }
  93. main();