-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtab.c
199 lines (122 loc) · 4.51 KB
/
hashtab.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
/******************************************************************************
* @file hashtab.c
*****************************************************************************/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "hashtab.h"
static struct hashtab_entry *find_entry (struct hashtab_entry *entries, unsigned long capacity, struct hashtab_name *key);
static int adjust_capacity (struct hashtab *table, unsigned long new_capacity) {
struct hashtab_entry *new_entries, *old_entries;
unsigned long i, new_count, old_capacity;
if ((new_entries = malloc (sizeof (*new_entries) * new_capacity)) == NULL) {
return -2;
}
for (i = 0; i < new_capacity; ++i) {
struct hashtab_entry *entry = &new_entries[i];
entry->key = NULL;
entry->value = NULL;
}
old_entries = table->entries;
old_capacity = table->capacity;
new_count = 0;
for (i = 0; i < old_capacity; ++i) {
struct hashtab_entry *entry = &old_entries[i], *dest;
if (entry->key == NULL) {
continue;
}
dest = find_entry (new_entries, new_capacity, entry->key);
dest->key = entry->key;
dest->value = entry->value;
++new_count;
}
free (old_entries);
table->capacity = new_capacity;
table->count = new_count;
table->entries = new_entries;
table->used = new_count;
return 0;
}
static struct hashtab_entry *find_entry (struct hashtab_entry *entries, unsigned long capacity, struct hashtab_name *key) {
struct hashtab_entry *tombstone = NULL;
unsigned long index;
for (index = key->hash % capacity; ; index = (index + 1) % capacity) {
struct hashtab_entry *entry = &entries[index];
if (entry->key == NULL) {
if (entry->value == NULL) {
if (tombstone == NULL) {
return entry;
}
return tombstone;
} else if (tombstone == NULL) {
tombstone = entry;
}
} else if (entry->key->bytes == key->bytes) {
if (memcmp (entry->key->chars, key->chars, key->bytes) == 0 && entry->key->hash == key->hash) {
return entry;
}
}
}
}
static unsigned long hash_string (const void *p, unsigned long length) {
const unsigned char *str = (const unsigned char *) p;
unsigned long i, result = 0;
for (i = 0; i < length; ++i) {
result = (str[i] << 24) + (result >> 19) + (result << 16) + (result >> 13) + (str[i] << 8) - result;
}
return result;
}
struct hashtab_name *hashtab_alloc_name (const char *str) {
struct hashtab_name *name;
unsigned long bytes = strlen (str), hash = hash_string (str, bytes);
if ((name = malloc (sizeof (*name))) == NULL) {
return NULL;
}
name->bytes = bytes;
name->chars = str;
name->hash = hash;
return name;
}
void *hashtab_get (struct hashtab *table, struct hashtab_name *key) {
struct hashtab_entry *entry;
if (table == NULL || table->count == 0) {
return NULL;
}
entry = find_entry (table->entries, table->capacity, key);
if (entry->key == NULL) {
return NULL;
}
return entry->value;
}
int hashtab_put (struct hashtab *table, struct hashtab_name *key, void *value) {
const long MIN_CAPACITY = 15;
struct hashtab_entry *entry;
int ret = 0;
if (table->used >= table->capacity / 2) {
long capacity = table->capacity * 2 - 1;
if (capacity < MIN_CAPACITY) {
capacity = MIN_CAPACITY;
}
if ((ret = adjust_capacity (table, capacity))) {
return ret;
}
}
entry = find_entry (table->entries, table->capacity, key);
if (entry->key == NULL) {
++table->count;
if (entry->value == NULL) {
++table->used;
}
}
entry->key = key;
entry->value = value;
return 0;
}
void hashtab_remove (struct hashtab *table, struct hashtab_name *key) {
struct hashtab_entry *entry;
if ((entry = find_entry (table->entries, table->capacity, key)) != NULL) {
entry->key = NULL;
entry->value = NULL;
--table->count;
}
}