hash_table.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*****************************************************************************
  2. * hash_table - Hash table functions for libxlsxwriter.
  3. *
  4. * Used in conjunction with the libxlsxwriter library.
  5. *
  6. * Copyright 2014-2018, John McNamara, [email protected]. See LICENSE.txt.
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include "xlsxwriter/hash_table.h"
  14. /*
  15. * Calculate the hash key using the FNV function. See:
  16. * http://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
  17. */
  18. STATIC size_t
  19. _generate_hash_key(void *data, size_t data_len, size_t num_buckets)
  20. {
  21. unsigned char *p = data;
  22. size_t hash = 2166136261U;
  23. size_t i;
  24. for (i = 0; i < data_len; i++)
  25. hash = (hash * 16777619) ^ p[i];
  26. return hash % num_buckets;
  27. }
  28. /*
  29. * Check if an element exists in the hash table and return a pointer
  30. * to it if it does.
  31. */
  32. lxw_hash_element *
  33. lxw_hash_key_exists(lxw_hash_table *lxw_hash, void *key, size_t key_len)
  34. {
  35. size_t hash_key = _generate_hash_key(key, key_len, lxw_hash->num_buckets);
  36. struct lxw_hash_bucket_list *list;
  37. lxw_hash_element *element;
  38. if (!lxw_hash->buckets[hash_key]) {
  39. /* The key isn't in the LXW_HASH hash table. */
  40. return NULL;
  41. }
  42. else {
  43. /* The key is already in the table or there is a hash collision. */
  44. list = lxw_hash->buckets[hash_key];
  45. /* Iterate over the keys in the bucket's linked list. */
  46. SLIST_FOREACH(element, list, lxw_hash_list_pointers) {
  47. if (memcmp(element->key, key, key_len) == 0) {
  48. /* The key already exists in the table. */
  49. return element;
  50. }
  51. }
  52. /* Key doesn't exist in the list so this is a hash collision. */
  53. return NULL;
  54. }
  55. }
  56. /*
  57. * Insert or update a value in the LXW_HASH table based on a key
  58. * and return a pointer to the new or updated element.
  59. */
  60. lxw_hash_element *
  61. lxw_insert_hash_element(lxw_hash_table *lxw_hash, void *key, void *value,
  62. size_t key_len)
  63. {
  64. size_t hash_key = _generate_hash_key(key, key_len, lxw_hash->num_buckets);
  65. struct lxw_hash_bucket_list *list = NULL;
  66. lxw_hash_element *element = NULL;
  67. if (!lxw_hash->buckets[hash_key]) {
  68. /* The key isn't in the LXW_HASH hash table. */
  69. /* Create a linked list in the bucket to hold the lxw_hash keys. */
  70. list = calloc(1, sizeof(struct lxw_hash_bucket_list));
  71. GOTO_LABEL_ON_MEM_ERROR(list, mem_error1);
  72. /* Initialize the bucket linked list. */
  73. SLIST_INIT(list);
  74. /* Create an lxw_hash element to add to the linked list. */
  75. element = calloc(1, sizeof(lxw_hash_element));
  76. GOTO_LABEL_ON_MEM_ERROR(element, mem_error1);
  77. /* Store the key and value. */
  78. element->key = key;
  79. element->value = value;
  80. /* Add the lxw_hash element to the bucket's linked list. */
  81. SLIST_INSERT_HEAD(list, element, lxw_hash_list_pointers);
  82. /* Also add it to the insertion order linked list. */
  83. STAILQ_INSERT_TAIL(lxw_hash->order_list, element,
  84. lxw_hash_order_pointers);
  85. /* Store the bucket list at the hash index. */
  86. lxw_hash->buckets[hash_key] = list;
  87. lxw_hash->used_buckets++;
  88. lxw_hash->unique_count++;
  89. return element;
  90. }
  91. else {
  92. /* The key is already in the table or there is a hash collision. */
  93. list = lxw_hash->buckets[hash_key];
  94. /* Iterate over the keys in the bucket's linked list. */
  95. SLIST_FOREACH(element, list, lxw_hash_list_pointers) {
  96. if (memcmp(element->key, key, key_len) == 0) {
  97. /* The key already exists in the table. Update the value. */
  98. if (lxw_hash->free_value)
  99. free(element->value);
  100. element->value = value;
  101. return element;
  102. }
  103. }
  104. /* Key doesn't exist in the list so this is a hash collision.
  105. * Create an lxw_hash element to add to the linked list. */
  106. element = calloc(1, sizeof(lxw_hash_element));
  107. GOTO_LABEL_ON_MEM_ERROR(element, mem_error2);
  108. /* Store the key and value. */
  109. element->key = key;
  110. element->value = value;
  111. /* Add the lxw_hash element to the bucket linked list. */
  112. SLIST_INSERT_HEAD(list, element, lxw_hash_list_pointers);
  113. /* Also add it to the insertion order linked list. */
  114. STAILQ_INSERT_TAIL(lxw_hash->order_list, element,
  115. lxw_hash_order_pointers);
  116. lxw_hash->unique_count++;
  117. return element;
  118. }
  119. mem_error1:
  120. free(list);
  121. mem_error2:
  122. free(element);
  123. return NULL;
  124. }
  125. /*
  126. * Create a new LXW_HASH hash table object.
  127. */
  128. lxw_hash_table *
  129. lxw_hash_new(uint32_t num_buckets, uint8_t free_key, uint8_t free_value)
  130. {
  131. /* Create the new hash table. */
  132. lxw_hash_table *lxw_hash = calloc(1, sizeof(lxw_hash_table));
  133. RETURN_ON_MEM_ERROR(lxw_hash, NULL);
  134. lxw_hash->free_key = free_key;
  135. lxw_hash->free_value = free_value;
  136. /* Add the lxw_hash element buckets. */
  137. lxw_hash->buckets =
  138. calloc(num_buckets, sizeof(struct lxw_hash_bucket_list *));
  139. GOTO_LABEL_ON_MEM_ERROR(lxw_hash->buckets, mem_error);
  140. /* Add a list for tracking the insertion order. */
  141. lxw_hash->order_list = calloc(1, sizeof(struct lxw_hash_order_list));
  142. GOTO_LABEL_ON_MEM_ERROR(lxw_hash->order_list, mem_error);
  143. /* Initialize the order list. */
  144. STAILQ_INIT(lxw_hash->order_list);
  145. /* Store the number of buckets to calculate the load factor. */
  146. lxw_hash->num_buckets = num_buckets;
  147. return lxw_hash;
  148. mem_error:
  149. lxw_hash_free(lxw_hash);
  150. return NULL;
  151. }
  152. /*
  153. * Free the LXW_HASH hash table object.
  154. */
  155. void
  156. lxw_hash_free(lxw_hash_table *lxw_hash)
  157. {
  158. size_t i;
  159. lxw_hash_element *element;
  160. lxw_hash_element *element_temp;
  161. if (!lxw_hash)
  162. return;
  163. /* Free the lxw_hash_elements and data using the ordered linked list. */
  164. if (lxw_hash->order_list) {
  165. STAILQ_FOREACH_SAFE(element, lxw_hash->order_list,
  166. lxw_hash_order_pointers, element_temp) {
  167. if (lxw_hash->free_key)
  168. free(element->key);
  169. if (lxw_hash->free_value)
  170. free(element->value);
  171. free(element);
  172. }
  173. }
  174. /* Free the buckets from the hash table. */
  175. for (i = 0; i < lxw_hash->num_buckets; i++) {
  176. free(lxw_hash->buckets[i]);
  177. }
  178. free(lxw_hash->order_list);
  179. free(lxw_hash->buckets);
  180. free(lxw_hash);
  181. }