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 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  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. exit(1);
  25. }
  26. unsigned long lh_ptr_hash(void *k)
  27. {
  28. /* CAW: refactored to be 64bit nice */
  29. return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
  30. }
  31. int lh_ptr_equal(void *k1, void *k2)
  32. {
  33. return (k1 == k2);
  34. }
  35. unsigned long lh_char_hash(void *k)
  36. {
  37. unsigned int h = 0;
  38. const char* data = k;
  39. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  40. return h;
  41. }
  42. int lh_char_equal(void *k1, void *k2)
  43. {
  44. return (strcmp((char*)k1, (char*)k2) == 0);
  45. }
  46. struct lh_table* lh_table_new(int size, char *name,
  47. lh_entry_free_fn *free_fn,
  48. lh_hash_fn *hash_fn,
  49. lh_equal_fn *equal_fn)
  50. {
  51. int i;
  52. struct lh_table *t;
  53. t = calloc(1, sizeof(struct lh_table));
  54. if(!t) lh_abort("lh_table_new: calloc failed\n");
  55. t->count = 0;
  56. t->size = size;
  57. t->name = name;
  58. t->table = calloc(size, sizeof(struct lh_entry));
  59. if(!t->table) lh_abort("lh_table_new: calloc failed\n");
  60. t->free_fn = free_fn;
  61. t->hash_fn = hash_fn;
  62. t->equal_fn = equal_fn;
  63. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  64. return t;
  65. }
  66. struct lh_table* lh_kchar_table_new(int size, char *name,
  67. lh_entry_free_fn *free_fn)
  68. {
  69. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  70. }
  71. struct lh_table* lh_kptr_table_new(int size, char *name,
  72. lh_entry_free_fn *free_fn)
  73. {
  74. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  75. }
  76. void lh_table_resize(struct lh_table *t, int new_size)
  77. {
  78. struct lh_table *new_t;
  79. struct lh_entry *ent;
  80. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  81. ent = t->head;
  82. while(ent) {
  83. lh_table_insert(new_t, ent->k, ent->v);
  84. ent = ent->next;
  85. }
  86. free(t->table);
  87. t->table = new_t->table;
  88. t->size = new_size;
  89. t->head = new_t->head;
  90. t->tail = new_t->tail;
  91. t->resizes++;
  92. free(new_t);
  93. }
  94. void lh_table_free(struct lh_table *t)
  95. {
  96. struct lh_entry *c;
  97. for(c = t->head; c != NULL; c = c->next) {
  98. if(t->free_fn) {
  99. t->free_fn(c);
  100. }
  101. }
  102. free(t->table);
  103. free(t);
  104. }
  105. int lh_table_insert(struct lh_table *t, void *k, void *v)
  106. {
  107. unsigned long h, n;
  108. t->inserts++;
  109. if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
  110. h = t->hash_fn(k);
  111. n = h % t->size;
  112. while( 1 ) {
  113. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  114. t->collisions++;
  115. if(++n == t->size) n = 0;
  116. }
  117. t->table[n].k = k;
  118. t->table[n].v = v;
  119. t->count++;
  120. if(t->head == NULL) {
  121. t->head = t->tail = &t->table[n];
  122. t->table[n].next = t->table[n].prev = NULL;
  123. } else {
  124. t->tail->next = &t->table[n];
  125. t->table[n].prev = t->tail;
  126. t->table[n].next = NULL;
  127. t->tail = &t->table[n];
  128. }
  129. return 0;
  130. }
  131. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
  132. {
  133. unsigned long h = t->hash_fn(k);
  134. unsigned long n = h % t->size;
  135. t->lookups++;
  136. while( 1 ) {
  137. if(t->table[n].k == LH_EMPTY) return NULL;
  138. if(t->table[n].k != LH_FREED &&
  139. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  140. if(++n == t->size) n = 0;
  141. }
  142. return NULL;
  143. }
  144. void* lh_table_lookup(struct lh_table *t, void *k)
  145. {
  146. struct lh_entry *e = lh_table_lookup_entry(t, k);
  147. if(e) return e->v;
  148. return NULL;
  149. }
  150. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  151. {
  152. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  153. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  154. if(n < 0) { return -2; }
  155. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  156. t->count--;
  157. if(t->free_fn) t->free_fn(e);
  158. t->table[n].v = NULL;
  159. t->table[n].k = LH_FREED;
  160. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  161. t->head = t->tail = NULL;
  162. } else if (t->head == &t->table[n]) {
  163. t->head->next->prev = NULL;
  164. t->head = t->head->next;
  165. } else if (t->tail == &t->table[n]) {
  166. t->tail->prev->next = NULL;
  167. t->tail = t->tail->prev;
  168. } else {
  169. t->table[n].prev->next = t->table[n].next;
  170. t->table[n].next->prev = t->table[n].prev;
  171. }
  172. t->table[n].next = t->table[n].prev = NULL;
  173. return 0;
  174. }
  175. int lh_table_delete(struct lh_table *t, void *k)
  176. {
  177. struct lh_entry *e = lh_table_lookup_entry(t, k);
  178. if(!e) return -1;
  179. return lh_table_delete_entry(t, e);
  180. }

No Description

Contributors (1)