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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 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 = 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 = 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 = 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 * 0.66) 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. t->lookups++;
  137. while( 1 ) {
  138. if(t->table[n].k == LH_EMPTY) return NULL;
  139. if(t->table[n].k != LH_FREED &&
  140. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  141. if(++n == t->size) n = 0;
  142. }
  143. return NULL;
  144. }
  145. const void* lh_table_lookup(struct lh_table *t, const void *k)
  146. {
  147. struct lh_entry *e = lh_table_lookup_entry(t, k);
  148. if(e) return e->v;
  149. return NULL;
  150. }
  151. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  152. {
  153. ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
  154. /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
  155. if(n < 0) { return -2; }
  156. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
  157. t->count--;
  158. if(t->free_fn) t->free_fn(e);
  159. t->table[n].v = NULL;
  160. t->table[n].k = LH_FREED;
  161. if(t->tail == &t->table[n] && t->head == &t->table[n]) {
  162. t->head = t->tail = NULL;
  163. } else if (t->head == &t->table[n]) {
  164. t->head->next->prev = NULL;
  165. t->head = t->head->next;
  166. } else if (t->tail == &t->table[n]) {
  167. t->tail->prev->next = NULL;
  168. t->tail = t->tail->prev;
  169. } else {
  170. t->table[n].prev->next = t->table[n].next;
  171. t->table[n].next->prev = t->table[n].prev;
  172. }
  173. t->table[n].next = t->table[n].prev = NULL;
  174. return 0;
  175. }
  176. int lh_table_delete(struct lh_table *t, const void *k)
  177. {
  178. struct lh_entry *e = lh_table_lookup_entry(t, k);
  179. if(!e) return -1;
  180. return lh_table_delete_entry(t, e);
  181. }

No Description

Contributors (1)