-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.cpp
192 lines (139 loc) · 3.85 KB
/
HashTable.cpp
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
#include "HashTable.h"
#include "LinkedList.h"
#define BUCKET_COUNT 503
typedef struct Entry Entry;
typedef struct Bucket Bucket;
struct Entry {
uint8* Key;
uint8* Value;
uint32 KeyLength;
uint32 ValueLength;
};
struct Bucket {
LinkedList Entries;
};
struct HashTable {
Bucket Buckets[BUCKET_COUNT];
};
static Entry* GetEntryInBucket(Bucket* bucket, uint8* key, uint32 keyLength);
static uint64 ComputeHash(uint8* key, uint32 keyLength);
HashTable* HashTable_New() {
HashTable* table;
table = Allocate(HashTable);
HashTable_Initialize(table);
return table;
}
void HashTable_Initialize(HashTable* table) {
uint16 i;
assert(table != NULL);
for (i = 0; i < BUCKET_COUNT; i++)
LinkedList_Initialize(&table->Buckets[i].Entries, Memory_Free);
}
void HashTable_Free(HashTable* self) {
HashTable_Uninitialize(self);
Free(self);
}
void HashTable_Uninitialize(HashTable* self) {
uint32 i;
Bucket* bucket;
Entry* entry;
assert(self != NULL);
bucket = self->Buckets;
i = 0;
while (i < BUCKET_COUNT) {
LinkedList_ForEach(entry, &bucket->Entries, Entry*) {
Free(entry->Key);
Free(entry->Value);
}
LinkedList_Uninitialize(&bucket->Entries);
bucket++;
i++;
}
}
void* HashTable_Get(HashTable* self, uint8* key, uint32 keyLength, void** value, uint32* valueLength) {
uint32 index;
Bucket* bucket;
Entry* entry;
assert(self != NULL);
if (key == NULL)
return NULL;
index = ComputeHash(key, keyLength) % BUCKET_COUNT;
bucket = self->Buckets + index;
entry = GetEntryInBucket(bucket, key, keyLength);
if (entry == NULL) {
if (valueLength)
*valueLength = 0;
if (*value)
*value = NULL;
return NULL;
}
else {
if (valueLength)
*valueLength = entry->ValueLength;
if (*value)
*value = entry->Value;
return entry->Value;
}
}
void HashTable_Add(HashTable* self, uint8* key, uint32 keyLength, void* value, uint32 valueLength) {
uint32 index;
Bucket* bucket;
Entry* entry;
assert(self != NULL);
if (key == NULL || value == NULL)
return;
index = ComputeHash(key, keyLength) % BUCKET_COUNT;
bucket = self->Buckets + index;
entry = GetEntryInBucket(bucket, key, keyLength);
if (entry) {
if (entry->ValueLength < valueLength)
entry->Value = ReallocateArray(uint8, valueLength, entry->Value);
Memory_BlockCopy((uint8*)value, entry->Value, valueLength);
}
else {
entry = Allocate(Entry);
entry->Key = AllocateArray(uint8, keyLength);
entry->Value = AllocateArray(uint8, valueLength);
Memory_BlockCopy(key, entry->Key, keyLength);
Memory_BlockCopy((uint8*)value, entry->Value, valueLength);
LinkedList_Append(&bucket->Entries, entry);
}
}
void HashTable_Remove(HashTable* self, uint8* key, uint32 keyLength) {
uint32 index;
Bucket* bucket;
Entry* entry;
assert(self != NULL);
if (key == NULL)
return;
index = ComputeHash(key, keyLength) % BUCKET_COUNT;
bucket = self->Buckets + index;
entry = GetEntryInBucket(bucket, key, keyLength);
if (entry) {
Free(entry->Key);
Free(entry->Value);
LinkedList_Remove(&bucket->Entries, entry);
}
}
void* HashTable_GetInt(HashTable* self, uint64 key, void** value, uint32* valueLength) {
return HashTable_Get(self, (uint8*)&key, sizeof(key), value, valueLength);
}
void HashTable_AddInt(HashTable* self, uint64 key, void* value, uint32 valueLength) {
HashTable_Add(self, (uint8*)&key, sizeof(key), value, valueLength);
}
void HashTable_RemoveInt(HashTable* self, uint64 key) {
HashTable_Remove(self, (uint8*)&key, sizeof(key));
}
static Entry* GetEntryInBucket(Bucket* bucket, uint8* key, uint32 keyLength) {
Entry* entry;
LinkedList_ForEach(entry, &bucket->Entries, Entry*)
if ( Memory_Compare(entry->Key, key, entry->KeyLength, keyLength) )
return entry;
return NULL;
}
static uint64 ComputeHash(uint8* key, uint32 keyLength) {
uint64 hash = 5046436453;
while (--keyLength)
hash = ((hash << 7) + hash) + key[keyLength];
return hash;
}