hash_table.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * libxlsxwriter
  3. *
  4. * Copyright 2014-2018, John McNamara, [email protected]. See LICENSE.txt.
  5. *
  6. * hash_table - Hash table functions for libxlsxwriter.
  7. *
  8. */
  9. #ifndef __LXW_HASH_TABLE_H__
  10. #define __LXW_HASH_TABLE_H__
  11. #include "common.h"
  12. /* Macro to loop over hash table elements in insertion order. */
  13. #define LXW_FOREACH_ORDERED(elem, hash_table) \
  14. STAILQ_FOREACH((elem), (hash_table)->order_list, lxw_hash_order_pointers)
  15. /* List declarations. */
  16. STAILQ_HEAD(lxw_hash_order_list, lxw_hash_element);
  17. SLIST_HEAD(lxw_hash_bucket_list, lxw_hash_element);
  18. /* LXW_HASH hash table struct. */
  19. typedef struct lxw_hash_table {
  20. uint32_t num_buckets;
  21. uint32_t used_buckets;
  22. uint32_t unique_count;
  23. uint8_t free_key;
  24. uint8_t free_value;
  25. struct lxw_hash_order_list *order_list;
  26. struct lxw_hash_bucket_list **buckets;
  27. } lxw_hash_table;
  28. /*
  29. * LXW_HASH table element struct.
  30. *
  31. * The hash elements contain pointers to allow them to be stored in
  32. * lists in the the hash table buckets and also pointers to track the
  33. * insertion order in a separate list.
  34. */
  35. typedef struct lxw_hash_element {
  36. void *key;
  37. void *value;
  38. STAILQ_ENTRY (lxw_hash_element) lxw_hash_order_pointers;
  39. SLIST_ENTRY (lxw_hash_element) lxw_hash_list_pointers;
  40. } lxw_hash_element;
  41. /* *INDENT-OFF* */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* *INDENT-ON* */
  46. lxw_hash_element *lxw_hash_key_exists(lxw_hash_table *lxw_hash, void *key,
  47. size_t key_len);
  48. lxw_hash_element *lxw_insert_hash_element(lxw_hash_table *lxw_hash, void *key,
  49. void *value, size_t key_len);
  50. lxw_hash_table *lxw_hash_new(uint32_t num_buckets, uint8_t free_key,
  51. uint8_t free_value);
  52. void lxw_hash_free(lxw_hash_table *lxw_hash);
  53. /* Declarations required for unit testing. */
  54. #ifdef TESTING
  55. #endif
  56. /* *INDENT-OFF* */
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. /* *INDENT-ON* */
  61. #endif /* __LXW_HASH_TABLE_H__ */