-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMap.c
164 lines (138 loc) · 4.04 KB
/
HashMap.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
#include "HashMap.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
void HashMapInitialize(HashMap* _hashMap)
{
_hashMap->num_buckets = BUCKET_ARRAY_INIT_SIZE;
_hashMap->size = 0;
_hashMap->bucket_array = malloc(BUCKET_ARRAY_INIT_SIZE * sizeof(HashNode));
if(_hashMap->bucket_array)
{
for (uint32_t i = 0; i < BUCKET_ARRAY_INIT_SIZE; ++i)
{
_hashMap->bucket_array[i] = NULL;
}
}
}
voidPtr_t HashMapRemove(HashMap* _hashMap, const char* _key)
{
const uint32_t bucketIndex = GetBucketIndex(_hashMap, _key);
const uint64_t hashCode = HashCode(_key);
HashNode* head = _hashMap->bucket_array[bucketIndex];
HashNode* prev = NULL;
while (head)
{
if (strcmp(head->key, _key) == 0 && hashCode == head->hash_code)
{
break;
}
prev = head;
head = head->next;
}
if (!head)
{
return NULL;
}
_hashMap->size--;
if (prev)
{
free(prev->next);
prev->next = head->next;
}
else
{
_hashMap->bucket_array[bucketIndex] = head->next;
}
return head->value;
}
voidPtr_t HashMapGet(const HashMap* _hashMap, const char* _key)
{
const uint32_t bucketIndex = GetBucketIndex(_hashMap, _key);
const uint64_t hashCode = HashCode(_key);
HashNode* head = _hashMap->bucket_array[bucketIndex];
while (head)
{
if (strcmp(head->key, _key) == 0 && head->hash_code == hashCode)
{
return head->value;
}
head = head->next;
}
return NULL;
}
void HashMapAdd(HashMap* _hashMap, const char* _key, voidPtr_t _value)
{
const uint32_t bucketIndex = GetBucketIndex(_hashMap, _key);
const uint64_t hashCode = HashCode(_key);
HashNode* head = _hashMap->bucket_array[bucketIndex];
while (head)
{
if (strcmp(head->key, _key) == 0 && head->hash_code == hashCode)
{
head->value = _value;
return;
}
head = head->next;
}
_hashMap->size++;
head = _hashMap->bucket_array[bucketIndex];
HashNode* newNode = malloc(sizeof(HashNode));
if (newNode)
{
newNode->key = _key;
newNode->value = _value;
newNode->hash_code = hashCode;
newNode->next = head;
_hashMap->bucket_array[bucketIndex] = newNode;
}
if ((1.0 * _hashMap->size) / _hashMap->num_buckets >= 0.7)
{
const uint32_t tempSize = _hashMap->num_buckets;
HashNode** newArray = realloc(_hashMap->bucket_array, _hashMap->num_buckets * sizeof(HashNode));
if(newArray)
{
_hashMap->num_buckets = _hashMap->num_buckets * 2;
_hashMap->size = 0;
_hashMap->bucket_array = newArray;
for (uint32_t i = tempSize; i < _hashMap->num_buckets; i++)
{
_hashMap->bucket_array[i] = NULL;
}
for (uint32_t i = 0; i < tempSize; ++i)
{
HashNode* headNode = _hashMap->bucket_array[i];
while (headNode != NULL) {
HashMapAdd(_hashMap, headNode->key, headNode->value);
headNode = headNode->next;
}
}
}
}
}
void HashMapDestroy(HashMap* _hashMap)
{
for(uint32_t i = 0; i < _hashMap->num_buckets; ++i)
{
free(_hashMap->bucket_array[i]);
}
free(_hashMap->bucket_array);
free(_hashMap);
}
uint64_t HashCode(const char* _key)
{
uint64_t hash = 5381;
uint32_t c;
while ((c = (uint8_t)*_key++))
{
hash = ((hash << 5) + hash) + c;
}
return hash;
}
uint32_t GetBucketIndex(const HashMap* _hashMap, const char* _key)
{
const uint64_t hashCode = HashCode(_key);
int32_t index = (int32_t)(hashCode % _hashMap->num_buckets);
index = index < 0 ? index * -1 : index;
return index;
}