You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkhash.c 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <stddef.h>
  18. #include <limits.h>
  19. #include "linkhash.h"
  20. void lh_abort(const char *msg, ...)
  21. {
  22. va_list ap;
  23. va_start(ap, msg);
  24. vprintf(msg, ap);
  25. va_end(ap);
  26. exit(1);
  27. }
  28. unsigned long lh_ptr_hash(const void *k)
  29. {
  30. /* CAW: refactored to be 64bit nice */
  31. return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
  32. }
  33. int lh_ptr_equal(const void *k1, const void *k2)
  34. {
  35. return (k1 == k2);
  36. }
  37. unsigned long lh_char_hash(const void *k)
  38. {
  39. unsigned int h = 0;
  40. const char* data = (const char*)k;
  41. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  42. return h;
  43. }
  44. int lh_char_equal(const void *k1, const void *k2)
  45. {
  46. return (strcmp((const char*)k1, (const char*)k2) == 0);
  47. }
  48. struct lh_table* lh_table_new(int size, const char *name,
  49. lh_entry_free_fn *free_fn,
  50. lh_hash_fn *hash_fn,
  51. lh_equal_fn *equal_fn)
  52. {
  53. int i;
  54. struct lh_table *t;
  55. /* Allocate space for elements to avoid divisions by zero. */
  56. assert(size > 0);
  57. t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
  58. if(!t) lh_abort("lh_table_new: calloc failed\n");
  59. t->count = 0;
  60. t->size = size;
  61. t->name = name;
  62. t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
  63. if(!t->table) lh_abort("lh_table_new: calloc failed\n");
  64. t->free_fn = free_fn;
  65. t->hash_fn = hash_fn;
  66. t->equal_fn = equal_fn;
  67. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  68. return t;
  69. }
  70. struct lh_table* lh_kchar_table_new(int size, const char *name,
  71. lh_entry_free_fn *free_fn)
  72. {
  73. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  74. }
  75. struct lh_table* lh_kptr_table_new(int size, const char *name,
  76. lh_entry_free_fn *free_fn)
  77. {
  78. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  79. }
  80. void lh_table_resize(struct lh_table *t, int new_size)
  81. {
  82. struct lh_table *new_t;
  83. struct lh_entry *ent;
  84. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  85. ent = t->head;
  86. while(ent) {
  87. lh_table_insert(new_t, ent->k, ent->v);
  88. ent = ent->next;
  89. }
  90. free(t->table);
  91. t->table = new_t->table;
  92. t->size = new_size;
  93. t->head = new_t->head;
  94. t->tail = new_t->tail;
  95. t->resizes++;
  96. free(new_t);
  97. }
  98. void lh_table_free(struct lh_table *t)
  99. {
  100. struct lh_entry *c;
  101. for(c = t->head; c != NULL; c = c->next) {
  102. if(t->free_fn) {
  103. t->free_fn(c);
  104. }
  105. }
  106. free(t->table);
  107. free(t);
  108. }
  109. int lh_table_insert(struct lh_table *t, void *k, const void *v)
  110. {
  111. unsigned long h, n;
  112. t->inserts++;
  113. if (t->count >= t->size * LH_LOAD_FACTOR) {
  114. /* Avoid signed integer overflow with large tables. */
  115. int new_size = (t->size > INT_MAX / 2) ? INT_MAX : (t->size * 2);
  116. if (t->size == INT_MAX)
  117. return -1;
  118. lh_table_resize(t, new_size);
  119. }
  120. h = t->hash_fn(k);
  121. n = h % t->size;
  122. while( 1 ) {
  123. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  124. t->collisions++;
  125. if ((int)++n == t->size) n = 0;
  126. }
  127. t->table[n].k = k;
  128. t->table[n].v = v;
  129. t->count++;
  130. if(t->head == NULL) {
  131. t->head = t->tail = &t->table[n];
  132. t->table[n].next = t->table[n].prev = NULL;
  133. } else {
  134. t->tail->next = &t->table[n];
  135. t->table[n].prev = t->tail;
  136. t->table[n].next = NULL;
  137. t->tail = &t->table[n];
  138. }
  139. return 0;
  140. }
  141. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
  142. {
  143. unsigned long h = t->hash_fn(k);
  144. unsigned long n = h % t->size;
  145. int count = 0;
  146. t->lookups++;
  147. while( count < t->size ) {
  148. if(t->table[n].k == LH_EMPTY) return NULL;
  149. if(t->table[n].k != LH_FREED &&
  150. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  151. if ((int)++n == t->size) n = 0;
  152. count++;
  153. }
  154. return NULL;
  155. }
  156. const void* lh_table_lookup(struct lh_table *t, const void *k)
  157. {
  158. void *result;
  159. lh_table_lookup_ex(t, k, &result);
  160. return result;
  161. }
  162. json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
  163. {
  164. struct lh_entry *e = lh_table_lookup_entry(t, k);
  165. if (e != NULL) {
  166. if (v != NULL) *v = (void *)e->v;
  167. return TRUE; /* key found */
  168. }
  169. if (v != NULL) *v = NULL;
  170. return FALSE; /* key not found */
  171. }
  172. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  173. {
  174. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  175. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  176. if(n < 0) { return -2; }
  177. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  178. t->count--;
  179. if(t->free_fn) t->free_fn(e);
  180. t->table[n].v = NULL;
  181. t->table[n].k = LH_FREED;
  182. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  183. t->head = t->tail = NULL;
  184. } else if (t->head == &t->table[n]) {
  185. t->head->next->prev = NULL;
  186. t->head = t->head->next;
  187. } else if (t->tail == &t->table[n]) {
  188. t->tail->prev->next = NULL;
  189. t->tail = t->tail->prev;
  190. } else {
  191. t->table[n].prev->next = t->table[n].next;
  192. t->table[n].next->prev = t->table[n].prev;
  193. }
  194. t->table[n].next = t->table[n].prev = NULL;
  195. return 0;
  196. }
  197. int lh_table_delete(struct lh_table *t, const void *k)
  198. {
  199. struct lh_entry *e = lh_table_lookup_entry(t, k);
  200. if(!e) return -1;
  201. return lh_table_delete_entry(t, e);
  202. }
  203. int lh_table_length(struct lh_table *t)
  204. {
  205. return t->count;
  206. }