15 |
*/ |
*/ |
16 |
|
|
17 |
/* HW: HenkJan Wolthuis, 1997, public domain |
/* HW: HenkJan Wolthuis, 1997, public domain |
18 |
|
|
19 |
changed functionnames, all public functions now have a 'hash_' prefix |
changed functionnames, all public functions now have a 'hash_' prefix |
20 |
minor editing, marked 'm all(?) with a description |
minor editing, marked 'm all(?) with a description |
21 |
removed a bug in hash_del and one in hash_enumerate |
removed a bug in hash_del and one in hash_enumerate |
137 |
return NULL; |
return NULL; |
138 |
|
|
139 |
if (NULL == |
if (NULL == |
140 |
((table->table)[val]->key = (char *) malloc(strlen(key) + 1))) { |
((table->table)[val]->key = (char *) malloc(strlen(key) + 1))) { |
141 |
free((table->table)[val]); |
free((table->table)[val]); |
142 |
(table->table)[val] = NULL; |
(table->table)[val] = NULL; |
143 |
return NULL; |
return NULL; |
149 |
return (table->table)[val]->data; |
return (table->table)[val]->data; |
150 |
} |
} |
151 |
|
|
152 |
/* HW: added a #define so the hashtable can accept duplicate keys */ |
/* HW: added a #define so the hashtable can accept duplicate keys */ |
153 |
#ifndef DUPLICATE_KEYS |
#ifndef DUPLICATE_KEYS |
154 |
/* |
/* |
155 |
** This spot in the table is already in use. See if the current string |
** This spot in the table is already in use. See if the current string |
237 |
** contains. |
** contains. |
238 |
*/ |
*/ |
239 |
for (last = NULL, ptr = (table->table)[val]; |
for (last = NULL, ptr = (table->table)[val]; |
240 |
NULL != ptr; last = ptr, ptr = ptr->next) { |
NULL != ptr; last = ptr, ptr = ptr->next) { |
241 |
if (0 == strcmp(key, ptr->key)) { |
if (0 == strcmp(key, ptr->key)) { |
242 |
if (last != NULL) { |
if (last != NULL) { |
243 |
data = ptr->data; |
data = ptr->data; |
290 |
|
|
291 |
if (NULL != function) { |
if (NULL != function) { |
292 |
function(hash_del(key, the_table)); |
function(hash_del(key, the_table)); |
293 |
} |
} else |
|
else |
|
294 |
hash_del(key, the_table); |
hash_del(key, the_table); |
295 |
} |
} |
296 |
|
|
343 |
} |
} |
344 |
|
|
345 |
/* HW: added hash_sorted_enum() |
/* HW: added hash_sorted_enum() |
346 |
|
|
347 |
hash_sorted_enum is like hash_enumerate but gives |
hash_sorted_enum is like hash_enumerate but gives |
348 |
sorted output. This is much slower than hash_enumerate, but |
sorted output. This is much slower than hash_enumerate, but |
349 |
sometimes nice for printing to a file... |
sometimes nice for printing to a file... |
353 |
{ |
{ |
354 |
char *key; |
char *key; |
355 |
void *data; |
void *data; |
356 |
} sort_struct; |
} |
357 |
|
sort_struct; |
358 |
static sort_struct *sortmap = NULL; |
static sort_struct *sortmap = NULL; |
359 |
|
|
360 |
static int counter = 0; |
static int counter = 0; |
389 |
|
|
390 |
/* malloc an pointerarray for all hashkey's and datapointers */ |
/* malloc an pointerarray for all hashkey's and datapointers */ |
391 |
if (NULL == |
if (NULL == |
392 |
(sortmap = |
(sortmap = |
393 |
(sort_struct *) malloc(sizeof(sort_struct) * table->count))) |
(sort_struct *) malloc(sizeof(sort_struct) * table->count))) |
394 |
return 0; |
return 0; |
395 |
|
|
396 |
/* copy the pointers to the hashkey's */ |
/* copy the pointers to the hashkey's */ |
445 |
hash_table table; |
hash_table table; |
446 |
|
|
447 |
char *strings[] = { |
char *strings[] = { |
448 |
"The first string", |
"The first string", |
449 |
"The second string", |
"The second string", |
450 |
"The third string", |
"The third string", |
451 |
"The fourth string", |
"The fourth string", |
452 |
"A much longer string than the rest in this example.", |
"A much longer string than the rest in this example.", |
453 |
"The last string", |
"The last string", |
454 |
NULL |
NULL |
455 |
}; |
}; |
456 |
|
|
457 |
char *junk[] = { |
char *junk[] = { |
458 |
"The first data", |
"The first data", |
459 |
"The second data", |
"The second data", |
460 |
"The third data", |
"The third data", |
461 |
"The fourth data", |
"The fourth data", |
462 |
"The fifth datum", |
"The fifth datum", |
463 |
"The sixth piece of data" |
"The sixth piece of data" |
464 |
}; |
}; |
465 |
|
|
466 |
int i; |
int i; |
467 |
void *j; |
void *j; |