hash.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. static const char version[] = "$Id$";
  2. /*
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sd/hash.h>
  11. #include <sd/malloc.h>
  12. #define SD_HASH_FULLTAB 2 /* rehash when table gets this x full */
  13. #define SD_HASH_GROWTAB 4 /* grow table by this factor */
  14. #define SD_HASH_DEFAULT_SIZE 10 /* self explenatory */
  15. struct __sd_hash {
  16. size_t nelem;
  17. size_t size;
  18. sd_hash_iter_t** tab;
  19. const sd_hash_ops_t* ops;
  20. };
  21. #define hindex(h, n) ((h)%(n))
  22. /******************************************************************************/
  23. static void rehash(sd_hash_t* a_this)
  24. {
  25. size_t i;
  26. int h, size;
  27. sd_hash_iter_t** tab;
  28. sd_hash_iter_t* p;
  29. sd_hash_iter_t* q;
  30. size = SD_HASH_GROWTAB * a_this->size;
  31. tab = (sd_hash_iter_t**)sd_calloc(size, sizeof(*tab));
  32. if (tab == 0) return;
  33. for (i = 0; i < a_this->size; i++) {
  34. for (p = a_this->tab[i]; p; p = q) {
  35. q = p->__next;
  36. h = hindex(p->__hkey,
  37. size);
  38. p->__next = tab[h];
  39. tab[h] = p;
  40. if (p->__next != 0) p->__next->__prev = p;
  41. p->__prev = 0;
  42. }
  43. }
  44. free(a_this->tab);
  45. a_this->tab = tab;
  46. a_this->size = size;
  47. }
  48. /******************************************************************************/
  49. extern sd_hash_t* sd_hash_new(size_t a_size, const sd_hash_ops_t* a_ops)
  50. {
  51. const static sd_hash_ops_t default_ops = {
  52. /*(void*) &sd_hash_hash_string,
  53. (void*) &strcmp,*/
  54. (unsigned int ( *)(const void *))sd_hash_hash_string,
  55. (int ( *)(const void *,const void *))strcmp,
  56. 0, 0, 0, 0
  57. };
  58. sd_hash_t* hash;
  59. sd_hash_iter_t** tab;
  60. if (a_size == 0) a_size = SD_HASH_DEFAULT_SIZE;
  61. hash = (sd_hash_t*)sd_calloc(1, sizeof(*hash));
  62. tab = (sd_hash_iter_t**)sd_calloc(a_size, sizeof(*tab));
  63. if (hash == 0 || tab == 0) {
  64. free(hash);
  65. free(tab);
  66. return 0;
  67. }
  68. hash->nelem = 0;
  69. hash->size = a_size;
  70. hash->tab = tab;
  71. hash->ops = a_ops != 0 ? a_ops : &default_ops;
  72. return hash;
  73. }
  74. /******************************************************************************/
  75. extern sd_hash_iter_t* sd_hash_lookup(sd_hash_t* a_this, const void* a_key)
  76. {
  77. int h;
  78. sd_hash_iter_t* p;
  79. if (a_this == 0 || a_key == 0) return 0;
  80. h = hindex(a_this->ops->hash(a_key), a_this->size);
  81. for (p = a_this->tab[h]; p != 0; p = p->__next)
  82. if (a_this->ops->compare(a_key, p->key) == 0) {
  83. return p;
  84. }
  85. return 0;
  86. }
  87. /******************************************************************************/
  88. extern sd_hash_iter_t* sd_hash_lookadd(sd_hash_t* a_this, const void* a_key)
  89. {
  90. int h;
  91. sd_hash_iter_t* p;
  92. if (a_this == 0 || a_key == 0) return 0;
  93. if ((p = sd_hash_lookup(a_this, a_key)) != 0) return p;
  94. if ((p = (sd_hash_iter_t*)sd_calloc(1, sizeof(*p))) == 0) return 0;
  95. if (a_this->ops->key_dup != 0)
  96. p->key = a_this->ops->key_dup(a_key);
  97. else
  98. p->key = (void*) a_key;
  99. p->hash = a_this;
  100. p->__hkey = a_this->ops->hash(a_key);
  101. if (a_this->nelem > SD_HASH_FULLTAB * a_this->size) rehash(a_this);
  102. h = hindex(p->__hkey,
  103. a_this->size);
  104. p->__next = a_this->tab[h];
  105. a_this->tab[h] = p;
  106. if (p->__next != 0) p->__next->__prev = p;
  107. a_this->nelem++;
  108. return p;
  109. }
  110. /******************************************************************************/
  111. extern sd_hash_iter_t* sd_hash_add(sd_hash_t* a_this, const void* a_key,
  112. void* a_data)
  113. {
  114. sd_hash_iter_t* p;
  115. if ((p = sd_hash_lookadd(a_this, a_key)) == 0) return 0;
  116. if (a_this->ops->data_free != 0) a_this->ops->data_free(p->data);
  117. if (a_this->ops->data_dup != 0)
  118. p->data = a_this->ops->data_dup(a_data);
  119. else
  120. p->data = a_data;
  121. return p;
  122. }
  123. /******************************************************************************/
  124. extern void sd_hash_delete(sd_hash_t* a_this)
  125. {
  126. sd_hash_clear(a_this);
  127. free(a_this->tab);
  128. free(a_this);
  129. }
  130. /******************************************************************************/
  131. extern void sd_hash_clear(sd_hash_t* a_this)
  132. {
  133. size_t h;
  134. sd_hash_iter_t* p;
  135. sd_hash_iter_t* q;
  136. if (a_this == 0) return;
  137. for (h = 0; h < a_this->size; h++) {
  138. for (p = a_this->tab[h]; p; p = q) {
  139. q = p->__next;
  140. if (a_this->ops->key_free) a_this->ops->key_free(p->key);
  141. if (a_this->ops->data_free) a_this->ops->data_free(p->data);
  142. free(p);
  143. }
  144. a_this->tab[h] = 0;
  145. }
  146. a_this->nelem = 0;
  147. }
  148. /******************************************************************************/
  149. extern void sd_hash_del(sd_hash_t* a_this, const void* a_key)
  150. {
  151. int h;
  152. sd_hash_iter_t* p;
  153. h = hindex(a_this->ops->hash(a_key), a_this->size);
  154. for (p = a_this->tab[h]; p != 0; p = p->__next)
  155. if (a_this->ops->compare(a_key, p->key) == 0) break;
  156. if (p == 0) return;
  157. sd_hash_iter_del(p);
  158. }
  159. /******************************************************************************/
  160. extern void sd_hash_foreach(sd_hash_t* a_this, sd_hash_func_t a_func,
  161. void* a_data)
  162. {
  163. size_t h, ret;
  164. sd_hash_iter_t* p;
  165. sd_hash_iter_t* q;
  166. if (a_this == 0 || a_func == 0) return;
  167. for (h = 0; h < a_this->size; h++)
  168. for (p = a_this->tab[h]; p != 0; p = q) {
  169. p->__foreach = 1;
  170. ret = (*a_func)(p->key, p->data, a_data);
  171. q = p->__next;
  172. if (p->__foreach == 0)
  173. sd_hash_iter_del(p);
  174. else
  175. p->__foreach = 0;
  176. if (ret != 0) return;
  177. }
  178. }
  179. /******************************************************************************/
  180. extern unsigned int sd_hash_get_nelem(sd_hash_t* a_this)
  181. {
  182. if (a_this == 0) return 0;
  183. return a_this->nelem;
  184. }
  185. /******************************************************************************/
  186. extern unsigned int sd_hash_get_size(sd_hash_t* a_this)
  187. {
  188. if (a_this == 0) return 0;
  189. return a_this->size;
  190. }
  191. /******************************************************************************/
  192. extern sd_hash_iter_t* sd_hash_begin(sd_hash_t* a_this)
  193. {
  194. size_t h;
  195. if (a_this == 0) return 0;
  196. for (h = 0; h < a_this->size; h++)
  197. if (a_this->tab[h])
  198. return a_this->tab[h];
  199. return 0;
  200. }
  201. /******************************************************************************/
  202. extern sd_hash_iter_t* sd_hash_end(sd_hash_t* a_this)
  203. {
  204. return 0;
  205. }
  206. /******************************************************************************/
  207. extern sd_hash_iter_t* sd_hash_iter_next(sd_hash_iter_t* a_this)
  208. {
  209. int h;
  210. size_t i;
  211. if (a_this == 0) return 0;
  212. if (a_this->__next != 0) return a_this->__next;
  213. h = hindex(a_this->__hkey, a_this->hash->size);
  214. for (i = h + 1; i < a_this->hash->size; i++)
  215. if (a_this->hash->tab[i])
  216. return a_this->hash->tab[i];
  217. return 0;
  218. }
  219. /******************************************************************************/
  220. extern sd_hash_iter_t* sd_hash_iter_prev(sd_hash_iter_t* a_this)
  221. {
  222. int h, i;
  223. sd_hash_iter_t* p;
  224. if (a_this == 0) return 0;
  225. if (a_this->__prev != 0) return a_this->__prev;
  226. h = hindex(a_this->__hkey, a_this->hash->size);
  227. for (i = h - 1; i > 0; i--)
  228. for (p = a_this->hash->tab[i]; p; p = p->__next)
  229. if (p->__next == 0)
  230. return p;
  231. return 0;
  232. }
  233. /******************************************************************************/
  234. extern void sd_hash_iter_del(sd_hash_iter_t* a_this)
  235. {
  236. if (a_this == 0) return;
  237. if (a_this->hash->ops->data_free != 0)
  238. a_this->hash->ops->data_free(a_this->data);
  239. a_this->data = 0;
  240. if (a_this->hash->ops->key_free != 0)
  241. a_this->hash->ops->key_free(a_this->key);
  242. a_this->key = 0;
  243. if (a_this->__foreach == 1) {
  244. a_this->__foreach = 0;
  245. return;
  246. }
  247. if (a_this->__next != 0) a_this->__next->__prev = a_this->__prev;
  248. if (a_this->__prev != 0)
  249. a_this->__prev->__next = a_this->__next;
  250. else
  251. a_this->hash->tab[hindex(a_this->__hkey, a_this->hash->size)] =
  252. a_this->__next;
  253. a_this->hash->nelem--;
  254. free(a_this);
  255. }
  256. /******************************************************************************/
  257. extern unsigned int sd_hash_hash_string(const char* a_string)
  258. {
  259. register unsigned int h;
  260. for (h = 0; *a_string != '\0'; a_string++)
  261. h = *a_string + 31 * h;
  262. return h;
  263. }