-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_table.c
185 lines (171 loc) · 4 KB
/
hash_table.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
/*
* @Description:
* @LastEditors: hsjfans
* @Email: [email protected]
* @Date: 2019-05-13 11:38:01
* @LastEditTime: 2019-05-13 17:42:20
*/
#include "include/hash_table.h"
#include <stdlib.h>
// hash table node
// just an double linked list
struct hash_table_node
{
struct hash_table_node *next;
struct hash_table_node *prev;
Element key; // the key
Element val; // the value
};
HashTableNode make_empty_node(Element key, Element val)
{
HashTableNode node = (HashTableNode)malloc(sizeof(HashTableNode));
node->key = key;
node->val = val;
return node;
};
//
HashTableNode
put(HashTableNode n, Element key, Element val)
{
HashTableNode temp = make_empty_node(key, val);
temp->next = n;
temp->prev = NULL;
n->next = temp;
n->prev = temp;
return temp;
};
HashTableNode
search(HashTableNode n, Element key, compare_func cmp)
{
HashTableNode temp = n;
while (temp != NULL)
{
if (cmp(temp->key, key) == 0)
{
return temp;
}
}
return NULL;
};
void update(HashTableNode n, Element key, Element newVal, compare_func cmp)
{
HashTableNode temp = search(n, key, cmp);
if (temp != NULL)
{
temp->val = newVal;
}
return;
}
HashTableNode remove(HashTableNode n, Element key, compare_func cmp)
{
HashTableNode temp = search(n, key, cmp);
if (temp != NULL)
{
HashTableNode prev = temp->prev;
HashTableNode next = temp->next;
if (prev != NULL && next != NULL)
{
prev->next = next;
next->prev = prev;
free(temp);
}
else if (prev != NULL)
{
prev->next = next;
free(temp);
}
else
{
next->prev = NULL;
free(temp);
return next;
}
}
return n;
};
// the hash table define
struct hash_table
{
HashTableNode *bucket;
int length; // the len of bucket
float factor; // the factor of impact
int size; // the current size of nodes
int threshold;
};
// make an empty hash table
HashTable make_empty(int capcity)
{
if (capcity <= 0)
{
capcity = DEFAULT_CAPACITY;
}
HashTableNode *bucket = (HashTableNode *)malloc(capcity * sizeof(HashTableNode));
HashTable table = (HashTable)malloc(sizeof(HashTable));
table->bucket = bucket;
table->factor = IMPACT_FACTOR;
table->length = capcity;
table->size = 0;
table->threshold = capcity * IMPACT_FACTOR;
return table;
};
// put value to hash table
void put_val(HashTable table, Element key, Element val, hash_code_func code)
{
if (table == NULL)
{
table = make_empty(0);
}
if (table->size >= table->threshold)
{
table = rehash(table, code);
}
int idx = get_index(table, key, code);
put(table->bucket[idx], key, val);
return;
}
int get_index(HashTable table, Element key, hash_code_func code)
{
int c = code(key);
return c % table->length;
}
HashTable rehash(HashTable table, hash_code_func code)
{
if (table->length >= MAXIMUM_CAPACITY)
{
return table;
}
HashTable t;
if (table->length << 1 >= MAXIMUM_CAPACITY)
{
t = make_empty(MAXIMUM_CAPACITY);
}
else
{
t = make_empty(table->length << 1);
}
for (int i = 0; i < t->length; i++)
{
HashTableNode n = t->bucket[i];
while (n != NULL)
{
HashTableNode temp = n->next;
put_val(t, n->key, n->val, code);
free(n);
n = temp;
}
}
free(table);
return t;
}
void remove_val(HashTable table, Element key, hash_code_func code, compare_func cmp)
{
int idx = get_index(table, key, code);
table->bucket[idx] = remove(table->bucket[idx], key, cmp);
return;
}
void update_val(HashTable table, Element key, Element newVal, compare_func cmp, hash_code_func code)
{
int idx = get_index(table, key, code);
update(table->bucket[idx], key, newVal, cmp);
return;
}