Advertisement
Guest User

Hash functions for Speller CS50

a guest
Jun 16th, 2020
536
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SAMPLE HASH FUNCTIONS for SPELLER
  2. ----------------------
  3.  
  4. // Hashes word to a number
  5. unsigned int hash(const char *word)
  6. {
  7. // djb2 hash function source:http://www.cse.yorku.ca/~oz/hash.html
  8.  
  9. unsigned long x = 5381;
  10.  
  11. while (*word)
  12. {
  13. x += (x << 5) + tolower(*word);
  14. word++;
  15. }
  16.  
  17. return x % N;
  18. }
  19.  
  20. -------------------------------
  21.  
  22. // Hashes word to a number
  23. unsigned int hash(const char *word)
  24. {
  25.  
  26. unsigned int hashing = 3581;
  27. for (int i = 0, n = strlen(word); i < n; i++)
  28. {
  29. hashing = (hashing << 2) ^ tolower(word[i]);
  30. }
  31. return hashing % N;
  32.  
  33. }
  34.  
  35. ----------------------------------------------------
  36.  
  37. unsigned int hash(const char *word)
  38. {
  39. // 32-bit MurmurHash
  40.  
  41. unsigned int h = 3323198485;
  42. while (*word)
  43. {
  44. h ^= tolower(*word);
  45. h *= 0x5bd1e995;
  46. h ^= h >> 15;
  47. word++;
  48. }
  49. return h % N;
  50. }
  51.  
  52. -------------------------------
  53.  
  54. // Hashes word to a number
  55. unsigned int hash(const char *word)
  56. {
  57. // String Folding source: https://opendsa-server.cs.vt.edu/
  58.  
  59. unsigned int sum = 0;
  60. long factor = 1;
  61. int length = strlen(word);
  62.  
  63. for (int i = 0; i < length; i++)
  64. {
  65. factor = i % 4 == 0 ? 1 : factor << 8;
  66. sum += tolower(word[i]) * factor;
  67. }
  68. return sum % N;
  69. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement