-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtbl.c
145 lines (130 loc) · 3.22 KB
/
hashtbl.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hashtbl.h"
/* Hash function (djb2 algorithm) with excellent distribution */
int hash(const int size, const char *str)
{
int h = 1337;
char c;
while ((c = (*str++))) {
h = (((h << 5) + h) + c) % size;
}
return h;
}
/* Initialize the data type */
hashtbl *hashtbl_init(const int size)
{
hashtbl *tbl = malloc(sizeof(hashtbl));
tbl->size = size;
tbl->table = calloc(size, sizeof(list_collisions *));
for (int i = 0; i < size; i++) {
tbl->table[i] = NULL;
}
return tbl;
}
/* Find an element in the hashtable by its label */
void *hashtbl_find(hashtbl *tbl, const char *label)
{
list_collisions *p;
int h = hash(tbl->size, label);
p = tbl->table[h];
while (p) {
if (!strcmp(p->label, label)) {
return p->data;
}
p = p->next;
}
return NULL;
}
/* Create an element in the table */
static list_collisions *hashtbl_create(const char *label, const void *data,
const size_t sdata)
{
list_collisions *new = malloc(sizeof(list_collisions));
strcpy(new->label, label);
new->data = malloc(sdata);
memcpy(new->data, data, sdata);
new->next = NULL;
return new;
}
/* Add an element in the table */
int hashtbl_add(hashtbl *tbl, const char *label, const void *data,
const size_t sdata)
{
list_collisions *p;
int h = hash(tbl->size, label);
list_collisions *new = hashtbl_create(label, data, sdata);
p = tbl->table[h];
if (!p) {
tbl->table[h] = new;
} else {
while (strcmp(p->label, label) && p->next) {
p = p->next;
}
if (!strcmp(p->label, label)) {
return 1;
}
p->next = new;
}
return 0;
}
/* Intermediate function used to remove an element in the table */
static void hashtbl_remove(list_collisions *elt)
{
free(elt->data);
free(elt);
}
/* Remove an element in the table */
int hashtbl_delete(hashtbl *tbl, const char *label)
{
list_collisions *p;
list_collisions *prev;
int h = hash(tbl->size, label);
prev = tbl->table[h];
if (prev && !strcmp(prev->label, label)) {
tbl->table[h] = prev->next;
hashtbl_remove(prev);
return 0;
}
while (prev) {
p = prev->next;
if (p && !strcmp(p->label, label)) {
prev->next = p->next;
hashtbl_remove(p);
return 0;
}
prev = prev->next;
}
return 1;
}
/* Display an ASCII representation of the hashtable, useful for debug */
void hashtbl_print(const hashtbl *tbl)
{
list_collisions *p;
for (int i = 0; i < tbl->size; ++i) {
printf("%d:\n", i);
p = tbl->table[i];
while (p) {
printf("\t- %s\n", p->label);
p = p->next;
}
}
}
/* Destroy the hashtable */
void hashtbl_destroy(hashtbl *tbl)
{
list_collisions *p;
list_collisions *old;
for (int i = 0; i < tbl->size; ++i) {
p = tbl->table[i];
tbl->table[i] = NULL;
while (p) {
old = p;
p = p->next;
hashtbl_remove(old);
}
}
free(tbl->table);
free(tbl);
}