states-tmpl.c 656 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <string.h>
  2. #include "states-code.h"
  3. static int T1[] = { $S1 };
  4. static int T2[] = { $S2 };
  5. static int G[] = { $G };
  6. static char *K[] = { $K };
  7. static int hash_g (const char *key, const int *T)
  8. {
  9. int i, sum = 0;
  10. for (i = 0; key[i] != '\0'; i++) {
  11. sum += T[i] * key[i];
  12. sum %= $NG;
  13. }
  14. return G[sum];
  15. }
  16. static int perfect_hash (const char *key)
  17. {
  18. if (strlen (key) > $NS)
  19. return 0;
  20. return (hash_g (key, T1) + hash_g (key, T2)) % $NG;
  21. }
  22. int has_key (const char *abbr)
  23. {
  24. int hash_value = perfect_hash (abbr);
  25. if (hash_value < $NK && strcmp(abbr, K[hash_value]) == 0)
  26. return hash_value;
  27. return -1;
  28. }