Add htable_grow()
[eleutheria.git] / genstructs / htable / main.c
blobdb78ab8285caee762c08c4c835b390077885068f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "htable.h"
7 unsigned int djb_hash(const void *key)
9 unsigned int i, hash = 5381;
10 char *str = (char *)key;
12 for (i = 0; i < strlen(str); i++)
13 hash = ((hash << 5) + hash) + str[i];
15 return (hash & 0x7FFFFFFF);
18 int mystrcmp(const void *arg1, const void *arg2)
20 return (strcmp((char *) arg1, (char *) arg2));
23 void myprintf(const void *key, const void *data)
25 printf("%s(%s) ", (char *)key, (char *)data);
28 int main(void)
30 htable_t htable;
32 /* Initialize table */
33 if (htable_init(&htable, 1, 1) == HT_NOMEM) {
34 perror("malloc");
35 exit(EXIT_FAILURE);
38 /* Setup callback functions */
39 htable.ht_hashf = djb_hash;
40 htable.ht_cmpf = mystrcmp;
41 htable.ht_printf = myprintf;
43 htable_insert(&htable, "stathis", "stathis");
44 htable_insert(&htable, "maria", "maria");
45 htable_insert(&htable, "kostas", "kostas");
46 htable_insert(&htable, "panagiotopoulos", "panagiotopoulos");
47 htable_insert(&htable, "eleni", "eleni");
48 htable_print(&htable);
50 printf("-------------------------\n");
52 htable_remove(&htable, "maria");
53 htable_print(&htable);
55 /* Free memory */
56 htable_free(&htable);
58 return EXIT_SUCCESS;