-
Notifications
You must be signed in to change notification settings - Fork 0
/
htable.c
176 lines (140 loc) · 4.24 KB
/
htable.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
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "htable.h"
htable_t *ht_create() {
htable_t *htable = malloc(sizeof(htable_t));
assert(htable);
*htable = (htable_t){
.length = 0,
.capacity = INIT_CAPACITY,
};
htable->entries = calloc(htable->capacity, sizeof(ht_entry));
assert(htable->entries);
return htable;
}
void ht_destroy(htable_t *htable) {
for (int i = 0; i < htable->capacity; i++) {
free((void *)htable->entries[i].key);
free(htable->entries[i].value);
}
free(htable->entries);
free(htable);
}
static uint64_t hash_key(const char *key) {
uint64_t hash = FNV_OFFSET;
for (const char *ch = key; *ch; ch++) {
hash ^= (uint64_t)(*ch);
hash *= FNV_PRIME;
}
return hash;
}
void *ht_get(htable_t *htable, const char *key) {
uint64_t hash;
unsigned int index;
hash = hash_key(key);
index = (hash & (htable->capacity - 1));
while (htable->entries[index].key != NULL) {
if (!strcmp(key, htable->entries[index].key)) {
return htable->entries[index].value;
}
// Key isn't in current slot, move to next.(linear probing)
index++;
if (index >= htable->capacity) {
index = 0;
}
}
return NULL;
}
static int ht_set_entry(ht_entry *entries, unsigned int capacity,
const char *key, void *value, unsigned int *length) {
uint64_t hash;
unsigned int index;
const char *key_copy;
hash = hash_key(key);
index = hash & (capacity - 1);
while (entries[index].key != NULL) {
// key已经存在, 更新key. 然后返回. 没有strdup()步骤.
if (!strcmp(key, entries[index].value)) {
entries[index].value = value;
return 0;
}
// Key isn't in current slot, move to next.(linear probing)
index++;
if (index >= capacity) {
index = 0;
}
}
key_copy = strdup(key);
assert(key_copy);
(*length)++;
entries[index].key = key_copy;
entries[index].value = value;
return 0;
// TODO(chen): 异常时返回-1
}
static int ht_expand(htable_t *htable) {
unsigned int new_capacity;
ht_entry *new_entries;
ht_entry entry;
unsigned int length;
length = 0;
new_capacity = htable->capacity * 2;
assert(new_capacity > htable->capacity); // no overflow
new_entries = calloc(new_capacity, sizeof(ht_entry));
assert(new_entries);
for (int i = 0; i < htable->capacity; i++) {
entry = htable->entries[i];
if (entry.key) {
ht_set_entry(new_entries, new_capacity, entry.key, entry.value,
&length);
}
}
assert(length == htable->length);
// Free old entries
free(htable->entries);
htable->entries = new_entries;
htable->capacity = new_capacity;
return 0;
// TODO(chen): 异常的时候返回-1
}
int ht_set(htable_t *htable, const char *key, void *value) {
int ret;
assert(key != NULL);
assert(value != NULL);
if (htable->length >= htable->capacity * 0.75) {
ret = ht_expand(htable);
assert(ret == 0);
}
return ht_set_entry(htable->entries, htable->capacity, key, value,
&htable->length);
}
int ht_del(htable_t *htable, const char *key) {
// 为了实现简单, 不进行哈希表缩容操作
// 当然, 当存在很多key-value删除操作时, 需要认真考虑缩容步骤.
uint64_t hash;
unsigned int index;
assert(key != NULL);
hash = hash_key(key);
index = (hash & (htable->capacity - 1));
while (htable->entries[index].key != NULL) {
if (!strcmp(key, htable->entries[index].key)) {
free(htable->entries[index].value);
htable->entries[index].value = NULL;
free((void *)htable->entries[index].key);
htable->entries[index].key = NULL;
htable->length--;
break;
}
// Key isn't in current slot, move to next.(linear probing)
index++;
if (index >= htable->capacity) {
index = 0;
}
}
return 0;
}
unsigned int ht_length(htable_t *htable) {
return htable->length;
}