states-tmpl.cc 922 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <string.h>
  2. #include <iostream>
  3. #include "states-code.hh"
  4. static struct {
  5. char *name;
  6. char *abbr;
  7. int pop;
  8. } states[$NK] = {
  9. #include "states.dat.h"
  10. };
  11. static int T1[] = { $S1 };
  12. static int T2[] = { $S2 };
  13. static int G[] = { $G };
  14. static int hash_g (const char *key, const int *T)
  15. {
  16. int i, sum = 0;
  17. for (i = 0; key[i] != '\0'; i++) {
  18. sum += T[i] * key[i];
  19. sum %= $NG;
  20. }
  21. return G[sum];
  22. }
  23. static int perfect_hash (const char *key)
  24. {
  25. if (strlen (key) > $NS)
  26. return 0;
  27. return (hash_g (key, T1) + hash_g (key, T2)) % $NG;
  28. }
  29. State::State (const string abbr)
  30. {
  31. int hash_value = perfect_hash (abbr.c_str ());
  32. if (hash_value < $NK &&
  33. strcmp(abbr.c_str (), states[hash_value].abbr) == 0)
  34. {
  35. nam = states[hash_value].name;
  36. pop = states[hash_value].pop;
  37. }
  38. else
  39. {
  40. cerr << "'" << abbr << "' is not an abbreviation for a state\n";
  41. }
  42. }