UTHashTable.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #ifndef UTHASHTABLE_H
  3. #define UTHASHTABLE_H
  4. #include <stddef.h>
  5. #include "uthash.h"
  6. /* All node structs must begin with this (note that there is NO semicolon). */
  7. #define UTHashNode_HEAD UT_hash_handle hh;
  8. /* This file contains convenience macros and a standard struct for working with
  9. * uthash hash tables.
  10. *
  11. * The main purpose of this is for convenience of creating/freeing nodes. */
  12. struct _UTHashTable {
  13. void *uttable; /* The uthash table -- must start out as NULL. */
  14. void *nodes; /* Contiguous array of nodes. */
  15. size_t allocedNodeCount; /* Node count currently allocated for. */
  16. size_t nodeCount; /* Current node count. */
  17. size_t nodeSize; /* Size of each node. */
  18. };
  19. typedef struct _UTHashTable UTHashTable;
  20. /* Initiates a hash table to the default values. |table| should point to an
  21. * already allocated UTHashTable struct.
  22. *
  23. * If the |initialCount| argument in initHashTable is given, |nodes| is
  24. * allocated immediately to the maximum size and new nodes are simply slices of
  25. * that array. This can save calls to malloc if many nodes are to be added, and
  26. * the a reasonable maximum number is known ahead of time.
  27. *
  28. * If the node count goes over this maximum, or if |initialCount| is 0, the
  29. * array is dynamically reallocated to fit the size.
  30. */
  31. void initHashTable(UTHashTable *table, size_t initialCount, size_t nodeSize);
  32. /* Frees memory occupied by a UTHashTable's members.
  33. *
  34. * Note that this does NOT free memory for the UTHashTable pointed to by
  35. * |table| itself; if that was allocated on the heap, you must free() it
  36. * yourself after calling this. */
  37. void destroyHashTable(UTHashTable *table);
  38. /* Returns memory allocated for a new node. Responsibility for freeing this is
  39. * up to the destroyHashTable() macro; this should NOT be freed by the caller.
  40. *
  41. * This is intended to be used with a HASH_ADD() macro, e.g.:
  42. * {%
  43. * struct myNode *uttable = utHashTable->uttable;
  44. * struct myNode *node = getNewNode(utHashTable);
  45. * node->key = 42;
  46. * node->value = someValue;
  47. * HASH_ADD_INT(uttable, key, node);
  48. * utHashTable->uttable = uttable;
  49. * %}
  50. *
  51. * Or, use the UTHASHTABLE_ADD_INT or UTHASHTABLE_ADD_STR macros
  52. * for convenience (they are exactly equivalent):
  53. * {%
  54. * struct myNode *node = getNewNode(utHashTable);
  55. * node->key = 42;
  56. * node->value = someValue;
  57. * UTHASHTABLE_ADD_INT(utHashTable, key, node, struct myNode);
  58. * %}
  59. */
  60. void *getNewNode(UTHashTable *table);
  61. #define UTHASHTABLE_ADD_INT(tablePtr, keyName, node, nodeType) \
  62. do { \
  63. nodeType *uttable = (tablePtr)->uttable; \
  64. HASH_ADD_INT(uttable, keyName, node); \
  65. (tablePtr)->uttable = uttable; \
  66. } while (0)
  67. #define UTHASHTABLE_ADD_STR(tablePtr, keyName, node, nodeType) \
  68. do { \
  69. nodeType *uttable = (tablePtr)->uttable; \
  70. HASH_ADD_STR(uttable, keyName, node); \
  71. (tablePtr)->uttable = uttable; \
  72. } while (0)
  73. #endif /* MMHASHTABLE_H */