hash.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2007, Novell Inc.
  3. *
  4. * This program is licensed under the BSD license, read LICENSE.BSD
  5. * for further information
  6. */
  7. /*
  8. * hash.h
  9. * generic hash functions
  10. */
  11. #ifndef LIBSOLV_HASH_H
  12. #define LIBSOLV_HASH_H
  13. #include "pooltypes.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* value of a hash */
  18. typedef unsigned int Hashval;
  19. /* inside the hash table, Ids are stored. Hash maps: string -> hash -> Id */
  20. typedef Id *Hashtable;
  21. /* hash chain */
  22. #define HASHCHAIN_START 7
  23. #define HASHCHAIN_NEXT(h, hh, mask) (((h) + (hh)++) & (mask))
  24. /* very simple hash function
  25. * string -> hash
  26. */
  27. static inline Hashval
  28. strhash(const char *str)
  29. {
  30. Hashval r = 0;
  31. unsigned int c;
  32. while ((c = *(const unsigned char *)str++) != 0)
  33. r += (r << 3) + c;
  34. return r;
  35. }
  36. static inline Hashval
  37. strnhash(const char *str, unsigned len)
  38. {
  39. Hashval r = 0;
  40. unsigned int c;
  41. while (len-- && (c = *(const unsigned char *)str++) != 0)
  42. r += (r << 3) + c;
  43. return r;
  44. }
  45. static inline Hashval
  46. strhash_cont(const char *str, Hashval r)
  47. {
  48. unsigned int c;
  49. while ((c = *(const unsigned char *)str++) != 0)
  50. r += (r << 3) + c;
  51. return r;
  52. }
  53. /* hash for rel
  54. * rel -> hash
  55. */
  56. static inline Hashval
  57. relhash(Id name, Id evr, int flags)
  58. {
  59. return name + 7 * evr + 13 * flags;
  60. }
  61. /* compute bitmask for value
  62. * returns smallest (2^n-1) > 2 * num + 3
  63. *
  64. * used for Hashtable 'modulo' operation
  65. */
  66. static inline Hashval
  67. mkmask(unsigned int num)
  68. {
  69. num = num * 2 + 3;
  70. while (num & (num - 1))
  71. num &= num - 1;
  72. return num * 2 - 1;
  73. }
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif /* LIBSOLV_HASH_H */