dtstrhash.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <assert.h>
  2. #include <cdt/dthdr.h>
  3. #include <limits.h>
  4. #include <string.h>
  5. /* Hashing a string into an unsigned integer.
  6. ** The basic method is to continuously accumulate bytes and multiply
  7. ** with some given prime. The length n of the string is added last.
  8. ** The recurrent equation is like this:
  9. ** h[k] = (h[k-1] + bytes)*prime for 0 <= k < n
  10. ** h[n] = (h[n-1] + n)*prime
  11. ** The prime is chosen to have a good distribution of 1-bits so that
  12. ** the multiplication will distribute the bits in the accumulator well.
  13. ** The below code accumulates 2 bytes at a time for speed.
  14. **
  15. ** Written by Kiem-Phong Vo (02/28/03)
  16. */
  17. unsigned dtstrhash(void *args, int n) {
  18. unsigned h = 0;
  19. unsigned char *s = args;
  20. if(n <= 0)
  21. { for(; *s != 0; s += s[1] ? 2 : 1)
  22. h = (h + ((unsigned)s[0] << 8u) + (unsigned)s[1]) * DT_PRIME;
  23. assert(strlen(args) <= INT_MAX);
  24. n = (int)(s - (unsigned char*)args);
  25. }
  26. else
  27. { unsigned char* ends;
  28. for(ends = s+n-1; s < ends; s += 2)
  29. h = (h + ((unsigned)s[0] << 8u) + (unsigned)s[1]) * DT_PRIME;
  30. if(s <= ends)
  31. h = (h + ((unsigned)s[0] << 8u)) * DT_PRIME;
  32. }
  33. assert(n >= 0);
  34. return (h + (unsigned)n) * DT_PRIME;
  35. }