hash_table.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <cds/hash_table.h>
  26. #include <cds/memory.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. int ht_init(hash_table_t *ht, hash_func_t hash_func, key_cmp_func_t cmp_keys, int size)
  31. {
  32. if (!ht) return -1;
  33. if ((!hash_func) || (!cmp_keys)) return -1;
  34. ht->cslots = (ht_cslot_t*)cds_malloc(size * sizeof(ht_cslot_t));
  35. if (!ht->cslots) return -1;
  36. memset(ht->cslots, 0, size * sizeof(ht_cslot_t));
  37. ht->size = size;
  38. ht->hash = hash_func;
  39. ht->cmp = cmp_keys;
  40. ht->find_cnt = 0;
  41. ht->cmp_cnt = 0;
  42. ht->nocmp_cnt = 0;
  43. ht->missed_cnt = 0;
  44. return 0;
  45. }
  46. void ht_destroy(hash_table_t *ht)
  47. {
  48. ht_element_t *e, *n;
  49. int i;
  50. if (!ht) return;
  51. if (ht->cslots) {
  52. for (i = 0; i < ht->size; i++) {
  53. e = ht->cslots[i].first;
  54. while (e) {
  55. n = e->next;
  56. cds_free(e);
  57. e = n;
  58. }
  59. }
  60. cds_free(ht->cslots);
  61. }
  62. ht->cslots = NULL;
  63. }
  64. int ht_add(hash_table_t *ht, ht_key_t key, ht_data_t data)
  65. {
  66. int h;
  67. ht_element_t *new_e;
  68. if (!ht) return -1;
  69. new_e = (ht_element_t*)cds_malloc(sizeof(ht_element_t));
  70. if (!new_e) return -1;
  71. new_e->next = NULL;
  72. new_e->key = key;
  73. new_e->data = data;
  74. h = ht->hash(key) % ht->size;
  75. if (h < 0) h = -h;
  76. if (!ht->cslots[h].last) {
  77. ht->cslots[h].first = new_e;
  78. }
  79. else {
  80. ht->cslots[h].last->next = new_e;
  81. }
  82. ht->cslots[h].cnt++;
  83. ht->cslots[h].last = new_e;
  84. return 0;
  85. }
  86. ht_data_t ht_find(hash_table_t *ht, ht_key_t key)
  87. {
  88. int h;
  89. ht_element_t *e;
  90. if (!ht) return NULL;
  91. ht->find_cnt++; //monitor
  92. h = ht->hash(key) % ht->size;
  93. if (h < 0) h = -h;
  94. e = ht->cslots[h].first;
  95. if (!e) ht->nocmp_cnt++; //monitor
  96. while (e) {
  97. ht->cmp_cnt++; //monitor
  98. if (ht->cmp(e->key, key) == 0) return e->data;
  99. e = e->next;
  100. }
  101. ht->missed_cnt++; //monitor
  102. return NULL;
  103. }
  104. ht_data_t ht_remove(hash_table_t *ht, ht_key_t key)
  105. {
  106. int h;
  107. ht_element_t *e,*p;
  108. ht_data_t data;
  109. if (!ht) return NULL;
  110. h = ht->hash(key) % ht->size;
  111. if (h < 0) h = -h;
  112. e = ht->cslots[h].first;
  113. p = NULL;
  114. while (e) {
  115. if (ht->cmp(e->key, key) == 0) {
  116. if (p) p->next = e->next;
  117. else ht->cslots[h].first = e->next;
  118. ht->cslots[h].cnt--;
  119. if (!e->next) ht->cslots[h].last = p;
  120. data = e->data;
  121. cds_free(e);
  122. return data;
  123. }
  124. p = e;
  125. e = e->next;
  126. }
  127. return NULL;
  128. }
  129. void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s)
  130. {
  131. if (!s) return;
  132. if (!ht) {
  133. s->find_cnt = 0;
  134. s->cmp_cnt = 0;
  135. s->nocmp_cnt = 0;
  136. s->missed_cnt = 0;
  137. }
  138. else {
  139. s->find_cnt = ht->find_cnt;
  140. s->cmp_cnt = ht->cmp_cnt;
  141. s->nocmp_cnt = ht->nocmp_cnt;
  142. s->missed_cnt = ht->missed_cnt;
  143. }
  144. }
  145. void ht_clear_statistic(hash_table_t *ht)
  146. {
  147. if (!ht) return;
  148. ht->find_cnt = 0;
  149. ht->cmp_cnt = 0;
  150. ht->nocmp_cnt = 0;
  151. ht->missed_cnt = 0;
  152. }
  153. /* --------- hash table traversing functions -------- */
  154. ht_element_t *get_first_ht_element(hash_table_t *ht, ht_traversal_info_t *info)
  155. {
  156. int i;
  157. if (!info) return NULL;
  158. info->ht = ht;
  159. info->current = NULL;
  160. for (i = 0; i < ht->size; i++) {
  161. if (ht->cslots[i].first) {
  162. info->current = ht->cslots[i].first;
  163. break;
  164. }
  165. }
  166. info->slot_pos = i;
  167. return info->current;
  168. }
  169. ht_element_t *get_next_ht_element(ht_traversal_info_t *info)
  170. {
  171. int i;
  172. if (!info) return NULL;
  173. if (info->current) info->current = info->current->next;
  174. if (info->current) return info->current;
  175. else {
  176. for (i = info->slot_pos + 1; i < info->ht->size; i++) {
  177. if (info->ht->cslots[i].first) {
  178. info->current = info->ht->cslots[i].first;
  179. break;
  180. }
  181. }
  182. info->slot_pos = i;
  183. }
  184. return info->current;
  185. }
  186. /* --------- HASH functions -------- */
  187. unsigned int rshash(const char* str, unsigned int len)
  188. {
  189. unsigned int b = 378551;
  190. unsigned int a = 63689;
  191. unsigned int hash = 0;
  192. unsigned int i = 0;
  193. for(i = 0; i < len; str++, i++) {
  194. hash = hash * a + (*str);
  195. a = a * b;
  196. }
  197. return (hash & 0x7FFFFFFF);
  198. }