hash.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #ifndef ISL_HASH_H
  10. #define ISL_HASH_H
  11. #include <stdlib.h>
  12. #include <isl/stdint.h>
  13. #include <isl/ctx.h>
  14. #if defined(__cplusplus)
  15. extern "C" {
  16. #endif
  17. #define isl_hash_init() (2166136261u)
  18. #define isl_hash_byte(h,b) do { \
  19. h *= 16777619; \
  20. h ^= b; \
  21. } while(0)
  22. #define isl_hash_hash(h,h2) \
  23. do { \
  24. isl_hash_byte(h, (h2) & 0xFF); \
  25. isl_hash_byte(h, ((h2) >> 8) & 0xFF); \
  26. isl_hash_byte(h, ((h2) >> 16) & 0xFF); \
  27. isl_hash_byte(h, ((h2) >> 24) & 0xFF); \
  28. } while(0)
  29. #define isl_hash_bits(h,bits) \
  30. ((bits) == 32) ? (h) : \
  31. ((bits) >= 16) ? \
  32. ((h) >> (bits)) ^ ((h) & (((uint32_t)1 << (bits)) - 1)) : \
  33. (((h) >> (bits)) ^ (h)) & (((uint32_t)1 << (bits)) - 1)
  34. uint32_t isl_hash_string(uint32_t hash, const char *s);
  35. uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len);
  36. #define isl_hash_builtin(h,l) isl_hash_mem(h, &l, sizeof(l))
  37. struct isl_hash_table_entry
  38. {
  39. uint32_t hash;
  40. void *data;
  41. };
  42. struct isl_hash_table {
  43. int bits;
  44. int n;
  45. struct isl_hash_table_entry *entries;
  46. };
  47. struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size);
  48. void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table);
  49. int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
  50. int min_size);
  51. void isl_hash_table_clear(struct isl_hash_table *table);
  52. extern struct isl_hash_table_entry *isl_hash_table_entry_none;
  53. struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
  54. struct isl_hash_table *table,
  55. uint32_t key_hash,
  56. isl_bool (*eq)(const void *entry, const void *val),
  57. const void *val, int reserve);
  58. isl_stat isl_hash_table_foreach(isl_ctx *ctx, struct isl_hash_table *table,
  59. isl_stat (*fn)(void **entry, void *user), void *user);
  60. void isl_hash_table_remove(struct isl_ctx *ctx,
  61. struct isl_hash_table *table,
  62. struct isl_hash_table_entry *entry);
  63. #if defined(__cplusplus)
  64. }
  65. #endif
  66. #endif