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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * $Id: linkhash.c,v 1.3 2005/06/14 22:41:51 mclark Exp $
  3. *
  4. * Copyright Metaparadigm Pte. Ltd. 2004.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public (LGPL)
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details: http://www.gnu.org/
  16. *
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdarg.h>
  23. #include <stddef.h>
  24. #include <limits.h>
  25. #include "linkhash.h"
  26. void lh_abort(const char *msg, ...)
  27. {
  28. va_list ap;
  29. va_start(ap, msg);
  30. vprintf(msg, ap);
  31. exit(1);
  32. }
  33. unsigned long lh_ptr_hash(void *k)
  34. {
  35. /* CAW: refactored to be 64bit nice */
  36. return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
  37. }
  38. int lh_ptr_equal(void *k1, void *k2)
  39. {
  40. return (k1 == k2);
  41. }
  42. unsigned long lh_char_hash(void *k)
  43. {
  44. unsigned int h = 0;
  45. const char* data = k;
  46. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  47. return h;
  48. }
  49. int lh_char_equal(void *k1, void *k2)
  50. {
  51. return (strcmp((char*)k1, (char*)k2) == 0);
  52. }
  53. struct lh_table* lh_table_new(int size, char *name,
  54. lh_entry_free_fn *free_fn,
  55. lh_hash_fn *hash_fn,
  56. lh_equal_fn *equal_fn)
  57. {
  58. int i;
  59. struct lh_table *t;
  60. t = calloc(1, sizeof(struct lh_table));
  61. if(!t) lh_abort("lh_table_new: calloc failed\n");
  62. t->count = 0;
  63. t->size = size;
  64. t->name = name;
  65. t->table = calloc(size, sizeof(struct lh_entry));
  66. if(!t->table) lh_abort("lh_table_new: calloc failed\n");
  67. t->free_fn = free_fn;
  68. t->hash_fn = hash_fn;
  69. t->equal_fn = equal_fn;
  70. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  71. return t;
  72. }
  73. struct lh_table* lh_kchar_table_new(int size, char *name,
  74. lh_entry_free_fn *free_fn)
  75. {
  76. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  77. }
  78. struct lh_table* lh_kptr_table_new(int size, char *name,
  79. lh_entry_free_fn *free_fn)
  80. {
  81. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  82. }
  83. void lh_table_resize(struct lh_table *t, int new_size)
  84. {
  85. struct lh_table *new_t;
  86. struct lh_entry *ent;
  87. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  88. ent = t->head;
  89. while(ent) {
  90. lh_table_insert(new_t, ent->k, ent->v);
  91. ent = ent->next;
  92. }
  93. free(t->table);
  94. t->table = new_t->table;
  95. t->size = new_size;
  96. t->head = new_t->head;
  97. t->tail = new_t->tail;
  98. t->resizes++;
  99. free(new_t);
  100. }
  101. void lh_table_free(struct lh_table *t)
  102. {
  103. struct lh_entry *c;
  104. for(c = t->head; c != NULL; c = c->next) {
  105. if(t->free_fn) {
  106. t->free_fn(c);
  107. }
  108. }
  109. free(t->table);
  110. free(t);
  111. }
  112. int lh_table_insert(struct lh_table *t, void *k, void *v)
  113. {
  114. unsigned long h, n;
  115. t->inserts++;
  116. if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
  117. h = t->hash_fn(k);
  118. n = h % t->size;
  119. while( 1 ) {
  120. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  121. t->collisions++;
  122. if(++n == t->size) n = 0;
  123. }
  124. t->table[n].k = k;
  125. t->table[n].v = v;
  126. t->count++;
  127. if(t->head == NULL) {
  128. t->head = t->tail = &t->table[n];
  129. t->table[n].next = t->table[n].prev = NULL;
  130. } else {
  131. t->tail->next = &t->table[n];
  132. t->table[n].prev = t->tail;
  133. t->table[n].next = NULL;
  134. t->tail = &t->table[n];
  135. }
  136. return 0;
  137. }
  138. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
  139. {
  140. unsigned long h = t->hash_fn(k);
  141. unsigned long n = h % t->size;
  142. t->lookups++;
  143. while( 1 ) {
  144. if(t->table[n].k == LH_EMPTY) return NULL;
  145. if(t->table[n].k != LH_FREED &&
  146. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  147. if(++n == t->size) n = 0;
  148. }
  149. return NULL;
  150. }
  151. void* lh_table_lookup(struct lh_table *t, void *k)
  152. {
  153. struct lh_entry *e = lh_table_lookup_entry(t, k);
  154. if(e) return e->v;
  155. return NULL;
  156. }
  157. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  158. {
  159. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  160. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  161. if(n < 0) { return -2; }
  162. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  163. t->count--;
  164. if(t->free_fn) t->free_fn(e);
  165. t->table[n].v = NULL;
  166. t->table[n].k = LH_FREED;
  167. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  168. t->head = t->tail = NULL;
  169. } else if (t->head == &t->table[n]) {
  170. t->head->next->prev = NULL;
  171. t->head = t->head->next;
  172. } else if (t->tail == &t->table[n]) {
  173. t->tail->prev->next = NULL;
  174. t->tail = t->tail->prev;
  175. } else {
  176. t->table[n].prev->next = t->table[n].next;
  177. t->table[n].next->prev = t->table[n].prev;
  178. }
  179. t->table[n].next = t->table[n].prev = NULL;
  180. return 0;
  181. }
  182. int lh_table_delete(struct lh_table *t, void *k)
  183. {
  184. struct lh_entry *e = lh_table_lookup_entry(t, k);
  185. if(!e) return -1;
  186. return lh_table_delete_entry(t, e);
  187. }

No Description

Contributors (1)