/hash.h

https://bitbucket.org/shadwick/alchemy · C Header · 37 lines · 22 code · 6 blank · 9 comment · 0 complexity · 30d6efa3bccdb7d999c7eda99b4ad656 MD5 · raw file

  1. #ifndef INC_HASH_H
  2. #define INC_HASH_H
  3. /* simple hashtable
  4. * David Kaplan <david[at]2of1.org>, 2011
  5. * Modified since by Erik Shadwick.
  6. *
  7. * I found this simple yet functional hash table on 2of1.org in 2011. However,
  8. * the site appears to be down for now; but either way, as stated above, it
  9. * was written by David Kaplan originally.
  10. * I've modified it by adding ht_init, ht_end, and ht_iterate.
  11. */
  12. #define HT_KEY_LEN 30
  13. struct ht_node {
  14. void *val;
  15. char key[HT_KEY_LEN + 1];
  16. struct ht_node *nxt;
  17. };
  18. typedef struct ht {
  19. struct ht_node **tbl;
  20. int size;
  21. } HT;
  22. HT *ht_create(int size); /* allocate hashtable mem */
  23. void ht_init(HT *ht, int size); /* initialize pre-alloc'd HT */
  24. void ht_destroy(HT *ht); /* free hashtable mem */
  25. void ht_end(HT *ht); /* deinitialize pre-alloc'd HT */
  26. void *ht_get(HT *ht, const char *key); /* retrieve entry */
  27. void ht_put(HT *ht, const char *key, void *val); /* store entry */
  28. void ht_remove(HT *ht, const char *key); /* remove entry */
  29. void ht_clear(HT *ht);
  30. struct ht_node *ht_iterate(HT *ht, struct ht_node *prev);
  31. #endif