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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * $Id: linkhash.c,v 1.2 2004/07/21 01:24:33 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 <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include "linkhash.h"
  23. void lh_abort(const char *msg, ...)
  24. {
  25. va_list ap;
  26. va_start(ap, msg);
  27. vprintf(msg, ap);
  28. exit(1);
  29. }
  30. unsigned long lh_ptr_hash(void *k)
  31. {
  32. return ((long)k * LH_PRIME) >> 4;
  33. }
  34. int lh_ptr_equal(void *k1, void *k2)
  35. {
  36. return (k1 == k2);
  37. }
  38. unsigned long lh_char_hash(void *k)
  39. {
  40. unsigned int h = 0;
  41. const char* data = k;
  42. while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
  43. return h;
  44. }
  45. int lh_char_equal(void *k1, void *k2)
  46. {
  47. return (strcmp((char*)k1, (char*)k2) == 0);
  48. }
  49. struct lh_table* lh_table_new(int size, char *name,
  50. lh_entry_free_fn *free_fn,
  51. lh_hash_fn *hash_fn,
  52. lh_equal_fn *equal_fn)
  53. {
  54. int i;
  55. struct lh_table *t;
  56. t = calloc(1, sizeof(struct lh_table));
  57. if(!t) lh_abort("lh_table_new: calloc failed\n");
  58. t->count = 0;
  59. t->size = size;
  60. t->name = name;
  61. t->table = calloc(size, sizeof(struct lh_entry));
  62. if(!t->table) lh_abort("lh_table_new: calloc failed\n");
  63. t->free_fn = free_fn;
  64. t->hash_fn = hash_fn;
  65. t->equal_fn = equal_fn;
  66. for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
  67. return t;
  68. }
  69. struct lh_table* lh_kchar_table_new(int size, char *name,
  70. lh_entry_free_fn *free_fn)
  71. {
  72. return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
  73. }
  74. struct lh_table* lh_kptr_table_new(int size, char *name,
  75. lh_entry_free_fn *free_fn)
  76. {
  77. return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
  78. }
  79. void lh_table_resize(struct lh_table *t, int new_size)
  80. {
  81. struct lh_table *new_t;
  82. struct lh_entry *ent;
  83. new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
  84. ent = t->head;
  85. while(ent) {
  86. lh_table_insert(new_t, ent->k, ent->v);
  87. ent = ent->next;
  88. }
  89. free(t->table);
  90. t->table = new_t->table;
  91. t->size = new_size;
  92. t->head = new_t->head;
  93. t->tail = new_t->tail;
  94. t->resizes++;
  95. free(new_t);
  96. }
  97. void lh_table_free(struct lh_table *t)
  98. {
  99. struct lh_entry *c;
  100. for(c = t->head; c != NULL; c = c->next) {
  101. if(t->free_fn) {
  102. t->free_fn(c);
  103. }
  104. }
  105. free(t->table);
  106. free(t);
  107. }
  108. int lh_table_insert(struct lh_table *t, void *k, void *v)
  109. {
  110. unsigned long h, n;
  111. t->inserts++;
  112. if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
  113. h = t->hash_fn(k);
  114. n = h % t->size;
  115. while( 1 ) {
  116. if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
  117. t->collisions++;
  118. if(++n == t->size) n = 0;
  119. }
  120. t->table[n].k = k;
  121. t->table[n].v = v;
  122. t->count++;
  123. if(t->head == NULL) {
  124. t->head = t->tail = &t->table[n];
  125. t->table[n].next = t->table[n].prev = NULL;
  126. } else {
  127. t->tail->next = &t->table[n];
  128. t->table[n].prev = t->tail;
  129. t->table[n].next = NULL;
  130. t->tail = &t->table[n];
  131. }
  132. return 0;
  133. }
  134. struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
  135. {
  136. unsigned long h = t->hash_fn(k);
  137. unsigned long n = h % t->size;
  138. t->lookups++;
  139. while( 1 ) {
  140. if(t->table[n].k == LH_EMPTY) return NULL;
  141. if(t->table[n].k != LH_FREED &&
  142. t->equal_fn(t->table[n].k, k)) return &t->table[n];
  143. if(++n == t->size) n = 0;
  144. }
  145. return NULL;
  146. }
  147. void* lh_table_lookup(struct lh_table *t, void *k)
  148. {
  149. struct lh_entry *e = lh_table_lookup_entry(t, k);
  150. if(e) return e->v;
  151. return NULL;
  152. }
  153. int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
  154. {
  155. int n = e - t->table;
  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, 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)