-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
190 lines (180 loc) · 4.54 KB
/
hash.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hash.h"
/* hash: form hash value for string s */
unsigned hash(char *key, int size)
{
unsigned hashval;
for (hashval = 0; *key != '\0'; key++)
{
hashval = *key + 31 * hashval;
}
return hashval % size;
}
/* lookup: look for s in hashtab */
hashtable *lookup(hashtable **table, char *key, int size)
{
hashtable *aux = NULL;
if (table == NULL || key == NULL)
return NULL;
for (aux = table[hash(key, size)]; aux != NULL; aux = aux->next)
{
if (strcmp(key, aux->key) == 0)
return aux; /* found */
}
return NULL; /* not found */
}
/* install: put (key, value) in hashtab */
hashtable *insert(hashtable **table, char *key, char *value, int size)
{
hashtable *aux = NULL;
unsigned hashval;
if (table == NULL || key == NULL || value == NULL)
return NULL;
if ((aux = lookup(table, key, size)) == NULL)
{ /* not found */
aux = (hashtable *)malloc(sizeof(hashtable));
if (aux == NULL || (aux->key = strdup(key)) == NULL)
return NULL;
hashval = hash(key, size);
aux->head = NULL;
aux->next = table[hashval];
table[hashval] = aux;
}
else /* already there */
free(aux->value);
if ((aux->value = strdup(value)) == NULL)
return NULL;
return aux;
}
hashtable **allocate_table(int size)
{
hashtable **table = (hashtable **)malloc(size * sizeof(hashtable *));
if (table == NULL)
{
perror("Hashtable allocate error");
exit(-1);
}
for (int i = 0; i < size; i++)
table[i] == NULL;
return table;
}
int delete_hash(hashtable **table, char *key, int size)
{
int flag_head = 1;
hashtable *aux = NULL, *prev = NULL;
callbacks *caux = NULL, *cprev = NULL;
unsigned hashval;
if (table == NULL || key == NULL)
return -1;
if ((aux = lookup(table, key, size)) == NULL)
{ /* not found */
return -1;
}
else
{ /* already there */
for (aux = table[hash(key, size)]; aux != NULL; aux = aux->next)
{
if (strcmp(key, aux->key) == 0 && flag_head == 0)
{
free(aux->key);
free(aux->value);
caux = aux->head;
while (caux != NULL)
{
cprev = caux;
caux = caux->next;
free(cprev);
}
prev->next = aux->next;
free(aux);
break;
}
else if (strcmp(key, aux->key) == 0 && flag_head == 1)
{
prev = aux->next;
free(aux->key);
free(aux->value);
caux = aux->head;
while (caux != NULL)
{
cprev = caux;
caux = caux->next;
free(cprev);
}
free(aux);
table[hash(key, size)] = prev;
break;
}
prev = aux;
flag_head = 0;
}
return 0;
}
}
callbacks *list_call(hashtable **table, char *key, int size)
{
hashtable *aux = NULL;
aux = lookup(table, key, size);
if (aux != NULL)
{
return aux->head;
}
else
{
return NULL;
}
}
int insert_callsocket(hashtable *group, int socket)
{
callbacks *new = NULL, *caux = NULL;
caux = group->head;
while (caux != NULL)
{
if (caux->callback_socket == socket)
{
return -2;
}
caux = caux->next;
}
new = (callbacks *)malloc(sizeof(callbacks));
new->callback_socket = socket;
if (group->head == NULL)
{
new->next = NULL;
}
else
{
new->next = group->head;
}
group->head = new;
return 0;
}
void delete_table(hashtable **table, int size)
{
hashtable *aux = NULL, *prev = NULL;
callbacks *caux = NULL, *cprev = NULL;
for (int i = 0; i < size; i++)
{
if (table[i] != NULL)
{
aux = table[i];
while (aux != NULL)
{
prev = aux;
aux = aux->next;
free(prev->key);
free(prev->value);
caux = prev->head;
while (caux != NULL)
{
cprev = caux;
caux = caux->next;
free(cprev);
}
free(prev);
}
}
}
}