00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00035
00036
00037
00038
00039 #include <adt/hash_table.h>
00040 #include <adt/list.h>
00041 #include <unistd.h>
00042 #include <malloc.h>
00043 #include <assert.h>
00044 #include <str.h>
00045
00056 bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
00057 hash_table_operations_t *op)
00058 {
00059 assert(h);
00060 assert(op && op->hash && op->compare);
00061 assert(max_keys > 0);
00062
00063 h->entry = malloc(m * sizeof(link_t));
00064 if (!h->entry)
00065 return false;
00066
00067 memset((void *) h->entry, 0, m * sizeof(link_t));
00068
00069 hash_count_t i;
00070 for (i = 0; i < m; i++)
00071 list_initialize(&h->entry[i]);
00072
00073 h->entries = m;
00074 h->max_keys = max_keys;
00075 h->op = op;
00076
00077 return true;
00078 }
00079
00085 void hash_table_destroy(hash_table_t *h)
00086 {
00087 assert(h);
00088 assert(h->entry);
00089
00090 free(h->entry);
00091 }
00092
00099 void hash_table_insert(hash_table_t *h, unsigned long key[], link_t *item)
00100 {
00101 assert(item);
00102 assert(h && h->op && h->op->hash && h->op->compare);
00103
00104 hash_index_t chain = h->op->hash(key);
00105 assert(chain < h->entries);
00106
00107 list_append(item, &h->entry[chain]);
00108 }
00109
00118 link_t *hash_table_find(hash_table_t *h, unsigned long key[])
00119 {
00120 assert(h && h->op && h->op->hash && h->op->compare);
00121
00122 hash_index_t chain = h->op->hash(key);
00123 assert(chain < h->entries);
00124
00125 link_t *cur;
00126 for (cur = h->entry[chain].next; cur != &h->entry[chain];
00127 cur = cur->next) {
00128 if (h->op->compare(key, h->max_keys, cur)) {
00129
00130
00131
00132 return cur;
00133 }
00134 }
00135
00136 return NULL;
00137 }
00138
00149 void hash_table_remove(hash_table_t *h, unsigned long key[], hash_count_t keys)
00150 {
00151 assert(h && h->op && h->op->hash && h->op->compare &&
00152 h->op->remove_callback);
00153 assert(keys <= h->max_keys);
00154
00155 link_t *cur;
00156
00157 if (keys == h->max_keys) {
00158
00159
00160
00161
00162
00163 cur = hash_table_find(h, key);
00164 if (cur) {
00165 list_remove(cur);
00166 h->op->remove_callback(cur);
00167 }
00168
00169 return;
00170 }
00171
00172
00173
00174
00175
00176 hash_index_t chain;
00177 for (chain = 0; chain < h->entries; chain++) {
00178 for (cur = h->entry[chain].next; cur != &h->entry[chain];
00179 cur = cur->next) {
00180 if (h->op->compare(key, keys, cur)) {
00181 link_t *hlp;
00182
00183 hlp = cur;
00184 cur = cur->prev;
00185
00186 list_remove(hlp);
00187 h->op->remove_callback(hlp);
00188
00189 continue;
00190 }
00191 }
00192 }
00193 }
00194
00202 void hash_table_apply(hash_table_t *h, void (*f)(link_t *, void *), void *arg)
00203 {
00204 hash_index_t bucket;
00205 link_t *cur;
00206
00207 for (bucket = 0; bucket < h->entries; bucket++) {
00208 for (cur = h->entry[bucket].next; cur != &h->entry[bucket];
00209 cur = cur->next) {
00210 f(cur, arg);
00211 }
00212 }
00213 }
00214