-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
193 lines (157 loc) · 4.92 KB
/
hashtable.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
#include <stdlib.h>
#include <string.h>
#include "gc.h"
#define HT_INITIAL_CAPACITY 16
#define HT_LOAD_FACTOR 0.75f
struct HashTable;
struct HashTableEntry;
typedef struct HashTable HashTable;
typedef struct HashTableEntry HashTableEntry;
struct HashTableEntry {
int hash;
Object_struct *key;
Object_struct *value;
HashTableEntry *next;
};
struct HashTable {
HashTableEntry *entries;
int size;
int threshold;
float loadFactor;
int capacity;
};
static inline int hash_table_index(int hash, int capacity) {
return hash & (capacity - 1);
}
static HashTable *new_hash_table() {
HashTable *hashTable = GC_MALLOC(sizeof(HashTable));
if (!hashTable) return NULL;
hashTable->entries = NULL;
hashTable->size = 0;
hashTable->capacity = 0;
hashTable->threshold = HT_INITIAL_CAPACITY;
hashTable->loadFactor = HT_LOAD_FACTOR;
return hashTable;
}
static void hash_table_inflate(HashTable *hashTable, int size) {
int capacity, i;
if (size > 1) {
// Next power of 2 capacity
capacity = (size - 1) << 1;
capacity |= (capacity >> 1);
capacity |= (capacity >> 2);
capacity |= (capacity >> 4);
capacity |= (capacity >> 8);
capacity |= (capacity >> 16);
capacity -= (capacity >> 1);
} else {
capacity = 1;
}
HashTableEntry empty;
empty.hash = 0;
empty.key = NULL;
empty.value = NULL;
empty.next = NULL;
hashTable->entries = GC_MALLOC(capacity * sizeof(HashTableEntry));
for (i = 0; i < capacity; ++i) {
hashTable->entries[i] = empty;
}
hashTable->threshold = (int) (capacity * hashTable->loadFactor);
hashTable->capacity = capacity;
}
static void hash_table_double(HashTable *hashTable) {
int oldCapacity = hashTable->capacity;
int newCapacity = oldCapacity * 2;
int i;
HashTableEntry empty;
empty.hash = 0;
empty.key = NULL;
empty.value = NULL;
empty.next = NULL;
HashTableEntry *entries = GC_MALLOC(newCapacity * sizeof(HashTableEntry));
for (i = 0; i < newCapacity; ++i) {
entries[i] = empty;
}
for (i = 0; i < oldCapacity; ++i) {
HashTableEntry *e = &hashTable->entries[i];
int first = 1;
while (e && e->key != NULL) {
int j = hash_table_index(e->hash, newCapacity);
if (entries[j].key) {
if (first) {
HashTableEntry *c = GC_MALLOC(sizeof(HashTableEntry));
c->next = entries[j].next;
c->value = e->value;
c->hash = e->hash;
c->key = e->key;
entries[j].next = c;
} else {
e->next = entries[j].next;
entries[j].next = e;
}
e = e->next;
} else {
entries[j].key = e->key;
entries[j].hash = e->hash;
entries[j].value = e->value;
entries[j].next = NULL;
HashTableEntry *next = e->next;
if (!first) {
GC_FREE(e);
}
e = next;
}
first = 0;
}
}
GC_FREE(hashTable->entries);
hashTable->entries = entries;
hashTable->capacity = newCapacity;
hashTable->threshold = (int) (newCapacity * hashTable->loadFactor);
}
static void hash_table_insert_object_to_value(HashTable *hashTable, Object_struct *k, Object_struct *value) {
HashTableEntry *e;
int hashValue, i;
if (hashTable->entries == NULL) {
hash_table_inflate(hashTable, hashTable->threshold);
}
hashValue = k->_vtable->code(k);
i = hash_table_index(hashValue, hashTable->capacity);
for (e = &hashTable->entries[i]; e != NULL && e->key != NULL; e = e->next) {
if (k->_vtable->equals(k, e->key)) {
e->value = value;
return;
}
}
if (hashTable->size > hashTable->threshold && hashTable->entries[i].key) {
hash_table_double(hashTable);
i = hash_table_index(hashValue, hashTable->capacity);
}
if (hashTable->entries[i].key == NULL) {
e = &hashTable->entries[i];
e->next = NULL;
} else {
e = GC_MALLOC(sizeof(HashTableEntry));
e->next = hashTable->entries[i].next;
hashTable->entries[i].next = e;
}
e->hash = hashValue;
e->key = k;
e->value = value;
++hashTable->size;
}
static Object_struct *hash_table_get_value(HashTable *hashTable, Object_struct *k) {
HashTableEntry *e;
int hashValue, i;
if (hashTable->entries == NULL) {
return NULL;
}
hashValue = k->_vtable->code(k);
i = hash_table_index(hashValue, hashTable->capacity);
for (e = &hashTable->entries[i]; e != NULL && e->key != NULL; e = e->next) {
if (k->_vtable->equals(k, e->key)) {
return e->value;
}
}
return NULL;
}