2
0

sdbm_hash.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * sdbm - ndbm work-alike hashed database library
  18. * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
  19. * author: oz@nexus.yorku.ca
  20. * status: ex-public domain. keep it that way.
  21. *
  22. * hashing routine
  23. */
  24. #include "apr_sdbm.h"
  25. #include "sdbm_private.h"
  26. /*
  27. * polynomial conversion ignoring overflows
  28. * [this seems to work remarkably well, in fact better
  29. * then the ndbm hash function. Replace at your own risk]
  30. * use: 65599 nice.
  31. * 65587 even better.
  32. */
  33. long sdbm_hash(const char *str, int len)
  34. {
  35. register unsigned long n = 0;
  36. #define DUFF /* go ahead and use the loop-unrolled version */
  37. #ifdef DUFF
  38. #define HASHC n = *str++ + 65599 * n
  39. if (len > 0) {
  40. register int loop = (len + 8 - 1) >> 3;
  41. switch(len & (8 - 1)) {
  42. case 0: do {
  43. HASHC; case 7: HASHC;
  44. case 6: HASHC; case 5: HASHC;
  45. case 4: HASHC; case 3: HASHC;
  46. case 2: HASHC; case 1: HASHC;
  47. } while (--loop);
  48. }
  49. }
  50. #else
  51. while (len--)
  52. n = *str++ + 65599 * n;
  53. #endif
  54. return n;
  55. }