summaryrefslogtreecommitdiffstats
path: root/hashtbl.c
blob: 16b28a13fdae9a407b3a60a2a5717079687afdc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* hashtbl.c
 *
 * Hash Table
 *
 */

#include "hashtbl.h"
#include "hashfuncs.h"

char *strdup(const char *s)
{
    char *b;
    if (!(b = malloc(strlen(s) + 1)))
        return NULL;
    strcpy(b, s);

    return b;
}

HASHTBL *hashtbl_create(size_t size, unsigned int (*hashfunc)(const char *))
{
    HASHTBL *hashtbl;

    if (!(hashtbl = malloc(sizeof(HASHTBL))))
        return NULL;

    /* pointer array of nodes */
    if (!(hashtbl->nodes = calloc(size, sizeof(struct hashnode_s*))))
    {
        free(hashtbl);
        return NULL;
    }

    hashtbl->size = size;

    if (hashfunc)
        hashtbl->hashfunc = hashfunc;
    else
        hashtbl->hashfunc = aphash;

    return hashtbl;
}

void hashtbl_destroy(HASHTBL *hashtbl)
{
    size_t n;
    struct hashnode_s *node, *oldnode;
    
    for (n = 0; n<hashtbl->size; ++n) {
        node = hashtbl->nodes[n];
        while (node) {
            free(node->key);
            oldnode = node;
            node = node->next;
            free(oldnode);
        }
    }
    free(hashtbl->nodes);
    free(hashtbl);
}

int hashtbl_insert(HASHTBL *hashtbl, const char *key, void *data)
{
    struct hashnode_s *head;
    size_t hash = hashtbl->hashfunc(key) % hashtbl->size;

/*  fprintf(stderr, "hashtbl_insert() key = %s, hash = %d, data = %s\n", key, hash, (char*)data); */

    head = hashtbl->nodes[hash];
    while (head)
    {
        if (!strcmp(head->key, key))
        {
            /* update the data in currently existing node */
            head->data = data;
            return 0;
        }

        head = head->next;
    }

    /* if we get here, it means that this is the first node at particular index
     * or we are appending new node to the end of the linked list */
    if (!(head = malloc(sizeof(struct hashnode_s))))
        return -1;

    if (!(head->key = strdup(key)))
    {
        free(head);
        return -1;
    }

    head->data = data;
    head->next = hashtbl->nodes[hash];

    /* update head ptr */
    hashtbl->nodes[hash] = head;

    return 0;
}

int hashtbl_remove(HASHTBL *hashtbl, const char *key)
{
    struct hashnode_s *node, *prevnode = NULL;
    size_t hash = hashtbl->hashfunc(key) % hashtbl->size;

    node = hashtbl->nodes[hash];
    while (node)
    {
        if (!strcmp(node->key, key))
        {
            free(node->key);
            if (prevnode)
                prevnode->next = node->next;
            else
                hashtbl->nodes[hash] = node->next;

            free(node);

            return 0;
        }

        prevnode = node;
        node = node->next;
    }

    return -1;
}


void *hashtbl_get(HASHTBL *hashtbl, const char *key)
{
    struct hashnode_s *head;
    size_t hash = hashtbl->hashfunc(key) % hashtbl->size;

/*  fprintf(stderr, "hashtbl_get() key = %s, hash = %d\n", key, hash);*/

    head = hashtbl->nodes[hash];
    while (head)
    {
        /* return data only when keys match */
        if (!strcmp(head->key, key))
            return head->data;

        head = head->next;
    }

    return NULL;
}

int hashtbl_resize(HASHTBL *hashtbl, size_t size)
{
    HASHTBL newtbl;
    size_t n;
    struct hashnode_s *node,*next;

    newtbl.size = size;
    newtbl.hashfunc = hashtbl->hashfunc;

    if (!(newtbl.nodes = calloc(size, sizeof(struct hashnode_s*)))) return -1;

    for (n = 0; n<hashtbl->size; ++n) {
        for (node = hashtbl->nodes[n]; node; node = next) {
            next  =  node->next;
            hashtbl_insert(&newtbl, node->key, node->data);
            hashtbl_remove(hashtbl, node->key);
            
        }
    }

    free(hashtbl->nodes);
    hashtbl->size = newtbl.size;
    hashtbl->nodes = newtbl.nodes;

    return 0;
}

int main(int argc, char **argv)
{
    char *key = "abcdefghijklmnopqrstuvwxyz1234567890";

    (void) printf("Hash Functions\n");
    (void) printf("Key: %s\n\n", key);
    (void) printf("ASCII default hash function value: %u\n", defaulthash(key));
    (void) printf("RS hash function value: %u\n", rshash(key));
    (void) printf("JS hash function value: %u\n", jshash(key));
    (void) printf("PJW hash function value: %u\n", pjwhash(key));
    (void) printf("ELF hash function value: %u\n", elfhash(key));
    (void) printf("BKDR hash function value: %u\n", bkdrhash(key));
    (void) printf("SDBM hash function value: %u\n", sdbmhash(key));
    (void) printf("DJB hash function value: %u\n", djbhash(key));
    (void) printf("DEK hash function value: %u\n", dekhash(key));
    (void) printf("BP hash function value: %u\n", bphash(key));
    (void) printf("FNV hash function value: %u\n", fnvhash(key));
    (void) printf("AP hash function value: %u\n", aphash(key));
    (void) puts("");

    HASHTBL *hashtbl;
    char *italy, *spain;

    hashtbl = hashtbl_create(16, NULL);
    if (hashtbl == NULL)
    {
        fprintf(stderr, "error: hashtbl_create() failed\n");
        exit(-1);
    }

    hashtbl_insert(hashtbl, "France", "Paris");
    hashtbl_insert(hashtbl, "England", "London");
    hashtbl_insert(hashtbl, "Sweden", "Stockholm");
    hashtbl_insert(hashtbl, "Germany", "Berlin");
    hashtbl_insert(hashtbl, "Norway", "Oslo");
    hashtbl_insert(hashtbl, "Italy", "Rome");
    hashtbl_insert(hashtbl, "Spain", "Madrid");
    hashtbl_insert(hashtbl, "Estonia", "Tallinn");
    hashtbl_insert(hashtbl, "Netherlands", "Amsterdam");
    hashtbl_insert(hashtbl, "Ireland", "Dublin");

    italy = hashtbl_get(hashtbl, "Italy");
     (void) printf("Italy: %s\n", italy ? italy : "");
    spain = hashtbl_get(hashtbl, "Spain");
     (void) printf("Spain: %s\n", spain ? spain : "");

    hashtbl_destroy(hashtbl);

    return 0;
}