hash_table.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef _HASH_TABLE_H_
  2. #define _HASH_TABLE_H_
  3. #define HASH_TABLE_STRINGS BIT0
  4. #define HASH_TABLE_INTS BIT1
  5. #define HASH_TABLE_MIXED BIT2
  6. #define HASH_TABLE_NORESIZE BIT3
  7. /* Turns a key into an unsigned int. */
  8. typedef unsigned int (*hash_table_hash_alg)(const void *key);
  9. /* Returns -1, 0, or 1 if left is <, =, or > than right. */
  10. typedef int (*hash_table_cmp_alg)(const void *left, const void *right);
  11. typedef int (*hash_table_node_func)(const void *key, void *data, void *param);
  12. typedef struct hash_table_entry_b {
  13. struct hash_table_entry_b *next;
  14. const void *key;
  15. void *data;
  16. unsigned int hash;
  17. } hash_table_entry_t;
  18. typedef struct {
  19. int len;
  20. hash_table_entry_t *head;
  21. } hash_table_row_t;
  22. typedef struct hash_table_b {
  23. int flags;
  24. int max_rows;
  25. int cells_in_use;
  26. hash_table_hash_alg hash;
  27. hash_table_cmp_alg cmp;
  28. hash_table_row_t *rows;
  29. } hash_table_t;
  30. hash_table_t *hash_table_create(hash_table_hash_alg alg, hash_table_cmp_alg cmp, int nrows, int flags);
  31. int hash_table_delete(hash_table_t *ht);
  32. int hash_table_check_resize(hash_table_t *ht);
  33. int hash_table_resize(hash_table_t *ht, int nrows);
  34. int hash_table_insert(hash_table_t *ht, const void *key, void *data);
  35. int hash_table_replace(hash_table_t *ht, const void *key, void *data);
  36. int hash_table_find(hash_table_t *ht, const void *key, void *dataptr);
  37. int hash_table_remove(hash_table_t *ht, const void *key, void *dataptr);
  38. int hash_table_walk(hash_table_t *ht, hash_table_node_func callback, void *param);
  39. #endif /* !_HASH_TABLE_H_ */