-
Notifications
You must be signed in to change notification settings - Fork 0
/
chash.c
203 lines (185 loc) · 4.83 KB
/
chash.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
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Hash table, mapping strings to void pointers. */
#include"chash.h"
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
static int hash(char *str) {
unsigned long hash = 5381;
int c;
while (c = *str++) {
hash = ((hash << 5) + hash) + c;
}
return hash % HASH_SIZE;
}
static void default_free(void* p) {
return;
}
static chash_entry* new_chash_entry(char* key, void *data) {
chash_entry* new_chash_entry = malloc(sizeof(*new_chash_entry));
if (new_chash_entry == NULL) {
return NULL;
}
new_chash_entry->key = malloc(strlen(key) + 1);
if (new_chash_entry->key == NULL) {
free(new_chash_entry);
return NULL;
}
strcpy(new_chash_entry->key, key);
new_chash_entry->data = data;
new_chash_entry->next = NULL;
return new_chash_entry;
}
static void free_chash_entry(chash_entry* e, chash_callback_t* on_free) {
if (e == NULL) {
return;
}
free_chash_entry(e->next, on_free);
free(e->key);
on_free(e->data);
free(e);
}
chash* chash_new(void) {
chash* table = malloc(sizeof(*table));
if (table == NULL) {
return NULL;
}
int i;
for (i = 0; i < HASH_SIZE; ++i) {
table->table[i] = malloc(sizeof(*(table->table[i])));
if (table->table[i] == NULL) {
int j;
for (j = 0; j < i; ++j) {
free(table->table[j]);
}
free(table);
return NULL;
}
table->table[i]->key = NULL;
table->table[i]->data = NULL;
table->table[i]->next = NULL;
}
table->size = 0;
table->free = default_free;
return table;
}
void chash_free(chash* table) {
int i;
for (i = 0; i < HASH_SIZE; ++i) {
free_chash_entry(table->table[i]->next, table->free);
free(table->table[i]);
}
free(table);
}
void chash_put(chash* table, char* key, void* data) {
int hashed_key = hash(key);
chash_entry* head = table->table[hashed_key];
chash_entry* i;
for (i = head->next; i != NULL; i = i->next) {
if (!strcmp(i->key, key)) {
void *temp = i->data;
i->data = data;
table->free(temp);
return;
}
}
chash_entry* e = new_chash_entry(key, data);
e->next = head->next;
head->next = e;
table->size += 1;
}
void* chash_get(chash* table, char* key) {
chash_entry* head = table->table[hash(key)]->next;
for (; head != NULL; head = head->next) {
if (!strcmp(head->key, key)) {
return head->data;
}
}
return NULL;
}
unsigned long chash_size(chash* table) {
return table->size;
}
int chash_contains_key(chash* table, char *key) {
return chash_get(table, key) != NULL;
}
int chash_contains_value(chash* table, void *data) {
chash_iterator iter;
chash_iterator_init(&iter, table);
char* key;
void* value;
while (chash_iterator_next(&iter, &key, &value)) {
if (value == data) {
return 1;
}
}
return 0;
}
void chash_del(chash* table, char* key) {
int hashed_key = hash(key);
chash_entry* head = table->table[hashed_key];
chash_entry* prev = head;
for (head = head->next; head != NULL; head = head->next) {
if (!strcmp(head->key, key)) {
prev->next = head->next;
free(head->key);
table->free(head->data);
free(head);
table->size -= 1;
break;
}
prev = head;
}
}
void chash_clear(chash* table) {
chash_iterator iter;
chash_iterator_init(&iter, table);
char* key;
void* value;
while (chash_iterator_next(&iter, &key, &value)) {
chash_del(table, key);
}
}
void chash_update(chash* dest, chash* src) {
chash_iterator iter;
chash_iterator_init(&iter, src);
char* key;
void* value;
while (chash_iterator_next(&iter, &key, &value)) {
chash_put(dest, key, value);
}
}
chash* chash_copy(chash *table) {
chash* result = chash_new();
if (result == NULL) {
return NULL;
}
chash_update(result, table);
return result;
}
void chash_iterator_init(chash_iterator* iterator, chash* table) {
iterator->table = table;
iterator->i = -1;
iterator->entry = NULL;
}
int chash_iterator_next(chash_iterator* iterator, char** key, void **data) {
while (iterator->entry == NULL) {
iterator->i++;
if (iterator->i == HASH_SIZE) {
return 0;
}
iterator->entry = iterator->table->table[iterator->i]->next;
}
*key = iterator->entry->key;
*data = iterator->entry->data;
iterator->entry = iterator->entry->next;
return 1;
}
void chash_dump(chash* table) {
chash_iterator iter;
chash_iterator_init(&iter, table);
char* key;
void *value;
while (chash_iterator_next(&iter, &key, &value)) {
fprintf(stderr, "%s: %p\n", key, value);
}
}