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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <stddef.h>
  17. #include <limits.h>
  18. #include "linkhash.h"
  19. void lh_abort(const char *msg, ...)
  20. {
  21. va_list ap;
  22. va_start(ap, msg);
  23. vprintf(msg, ap);
  24. va_end(ap);
  25. exit(1);
  26. }
  27. unsigned long lh_ptr_hash(const void *k)
  28. {
  29. /* CAW: refactored to be 64bit nice */
  30. return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
  31. }
  32. int lh_ptr_equal(const void *k1, const void *k2)
  33. {
  34. return (k1 == k2);
  35. }
  36. unsigned long lh_char_hash(const void *k)
  37. {
  38. unsigned int h = 0;
  39. const char* data = (const char*)k;
  40. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  41. return h;
  42. }
  43. int lh_char_equal(const void *k1, const void *k2)
  44. {
  45. return (strcmp((const char*)k1, (const char*)k2) == 0);
  46. }
  47. struct lh_table* lh_table_new(int size, const char *name,
  48. lh_entry_free_fn *free_fn,
  49. lh_hash_fn *hash_fn,
  50. lh_equal_fn *equal_fn)
  51. {
  52. int i;
  53. struct lh_table *t;
  54. t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
  55. if(!t) lh_abort("lh_table_new: calloc failed\n");
  56. t->count = 0;
  57. t->size = size;
  58. t->name = name;
  59. t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
  60. if(!t->table) lh_abort("lh_table_new: calloc failed\n");
  61. t->free_fn = free_fn;
  62. t->hash_fn = hash_fn;
  63. t->equal_fn = equal_fn;
  64. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  65. return t;
  66. }
  67. struct lh_table* lh_kchar_table_new(int size, const char *name,
  68. lh_entry_free_fn *free_fn)
  69. {
  70. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  71. }
  72. struct lh_table* lh_kptr_table_new(int size, const char *name,
  73. lh_entry_free_fn *free_fn)
  74. {
  75. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  76. }
  77. void lh_table_resize(struct lh_table *t, int new_size)
  78. {
  79. struct lh_table *new_t;
  80. struct lh_entry *ent;
  81. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  82. ent = t->head;
  83. while(ent) {
  84. lh_table_insert(new_t, ent->k, ent->v);
  85. ent = ent->next;
  86. }
  87. free(t->table);
  88. t->table = new_t->table;
  89. t->size = new_size;
  90. t->head = new_t->head;
  91. t->tail = new_t->tail;
  92. t->resizes++;
  93. free(new_t);
  94. }
  95. void lh_table_free(struct lh_table *t)
  96. {
  97. struct lh_entry *c;
  98. for(c = t->head; c != NULL; c = c->next) {
  99. if(t->free_fn) {
  100. t->free_fn(c);
  101. }
  102. }
  103. free(t->table);
  104. free(t);
  105. }
  106. int lh_table_insert(struct lh_table *t, void *k, const void *v)
  107. {
  108. unsigned long h, n;
  109. t->inserts++;
  110. if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2);
  111. h = t->hash_fn(k);
  112. n = h % t->size;
  113. while( 1 ) {
  114. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  115. t->collisions++;
  116. if(++n == t->size) n = 0;
  117. }
  118. t->table[n].k = k;
  119. t->table[n].v = v;
  120. t->count++;
  121. if(t->head == NULL) {
  122. t->head = t->tail = &t->table[n];
  123. t->table[n].next = t->table[n].prev = NULL;
  124. } else {
  125. t->tail->next = &t->table[n];
  126. t->table[n].prev = t->tail;
  127. t->table[n].next = NULL;
  128. t->tail = &t->table[n];
  129. }
  130. return 0;
  131. }
  132. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
  133. {
  134. unsigned long h = t->hash_fn(k);
  135. unsigned long n = h % t->size;
  136. int count = 0;
  137. t->lookups++;
  138. while( count < t->size ) {
  139. if(t->table[n].k == LH_EMPTY) return NULL;
  140. if(t->table[n].k != LH_FREED &&
  141. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  142. if(++n == t->size) n = 0;
  143. count++;
  144. }
  145. return NULL;
  146. }
  147. const void* lh_table_lookup(struct lh_table *t, const void *k)
  148. {
  149. void *result;
  150. lh_table_lookup_ex(t, k, &result);
  151. return result;
  152. }
  153. json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
  154. {
  155. struct lh_entry *e = lh_table_lookup_entry(t, k);
  156. if (e != NULL) {
  157. if (v != NULL) *v = (void *)e->v;
  158. return TRUE; /* key found */
  159. }
  160. if (v != NULL) *v = NULL;
  161. return FALSE; /* key not found */
  162. }
  163. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  164. {
  165. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  166. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  167. if(n < 0) { return -2; }
  168. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  169. t->count--;
  170. if(t->free_fn) t->free_fn(e);
  171. t->table[n].v = NULL;
  172. t->table[n].k = LH_FREED;
  173. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  174. t->head = t->tail = NULL;
  175. } else if (t->head == &t->table[n]) {
  176. t->head->next->prev = NULL;
  177. t->head = t->head->next;
  178. } else if (t->tail == &t->table[n]) {
  179. t->tail->prev->next = NULL;
  180. t->tail = t->tail->prev;
  181. } else {
  182. t->table[n].prev->next = t->table[n].next;
  183. t->table[n].next->prev = t->table[n].prev;
  184. }
  185. t->table[n].next = t->table[n].prev = NULL;
  186. return 0;
  187. }
  188. int lh_table_delete(struct lh_table *t, const void *k)
  189. {
  190. struct lh_entry *e = lh_table_lookup_entry(t, k);
  191. if(!e) return -1;
  192. return lh_table_delete_entry(t, e);
  193. }