-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
320 lines (262 loc) · 13.5 KB
/
Copy pathhashtable.c
File metadata and controls
320 lines (262 loc) · 13.5 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/* ============================================================================
Hash Table Implementation in C (Equivalent to NASM Assembly)
Uses linear probing for collision resolution
============================================================================ */
#define TABLE_SIZE 16
#define ENTRY_SIZE 16
#define KEY_SIZE 8
#define VALUE_SIZE 8
#define EMPTY_KEY 0xFFFFFFFFFFFFFFFF
#define DELETED_KEY 0xFFFFFFFFFFFFFFFE
/* Hash table entry structure */
typedef struct {
uint64_t key;
uint64_t value;
} HashEntry;
/* Hash table structure */
typedef struct {
HashEntry entries[TABLE_SIZE];
int size;
} HashTable;
/* Global hash table instance */
HashTable hash_table;
/* Test data */
uint64_t test_keys[] = {42, 15, 97, 3, 88, 120};
uint64_t test_values[] = {100, 200, 300, 400, 500, 600};
const int test_count = 6;
/* ============================================================================
Hash function: Simple modulo hash
Input: key - 64-bit unsigned integer
Output: hash value (0 to TABLE_SIZE-1)
============================================================================ */
uint64_t hash_function(uint64_t key) {
return key % TABLE_SIZE;
}
/* ============================================================================
Initialize hash table - mark all slots as empty
============================================================================ */
void init_table(void) {
for (int i = 0; i < TABLE_SIZE; i++) {
hash_table.entries[i].key = EMPTY_KEY;
hash_table.entries[i].value = 0;
}
hash_table.size = 0;
}
/* ============================================================================
Insert key-value pair into hash table
Input: key - key to insert
value - value associated with key
Output: 0 (success), 1 (failure - table full)
============================================================================ */
int insert(uint64_t key, uint64_t value) {
uint64_t hash_index = hash_function(key);
int probe_count = 0;
while (probe_count < TABLE_SIZE) {
uint64_t idx = (hash_index + probe_count) % TABLE_SIZE;
/* Check if slot is empty or deleted */
if (hash_table.entries[idx].key == EMPTY_KEY ||
hash_table.entries[idx].key == DELETED_KEY) {
/* Store key and value */
hash_table.entries[idx].key = key;
hash_table.entries[idx].value = value;
hash_table.size++;
return 0; /* Success */
}
/* Slot occupied, probe next */
probe_count++;
}
return 1; /* Table full */
}
/* ============================================================================
Search for key in hash table
Input: key - key to search
Output: value if found, -1 (0xFFFFFFFFFFFFFFFF) if not found
============================================================================ */
uint64_t search(uint64_t key) {
uint64_t hash_index = hash_function(key);
int probe_count = 0;
while (probe_count < TABLE_SIZE) {
uint64_t idx = (hash_index + probe_count) % TABLE_SIZE;
/* Get key at this position */
uint64_t stored_key = hash_table.entries[idx].key;
/* Check if empty (no key found) */
if (stored_key == EMPTY_KEY) {
return (uint64_t)-1; /* Not found */
}
/* Check if this is our key */
if (stored_key == key) {
return hash_table.entries[idx].value; /* Found */
}
/* Continue probing */
probe_count++;
}
return (uint64_t)-1; /* Not found */
}
/* ============================================================================
Delete key from hash table (mark as deleted)
Input: key - key to delete
Output: 0 (success), 1 (not found)
============================================================================ */
int delete_key(uint64_t key) {
uint64_t hash_index = hash_function(key);
int probe_count = 0;
while (probe_count < TABLE_SIZE) {
uint64_t idx = (hash_index + probe_count) % TABLE_SIZE;
uint64_t stored_key = hash_table.entries[idx].key;
if (stored_key == EMPTY_KEY) {
return 1; /* Not found */
}
if (stored_key == key) {
hash_table.entries[idx].key = DELETED_KEY;
hash_table.size--;
return 0; /* Success */
}
probe_count++;
}
return 1; /* Not found */
}
/* ============================================================================
Display all entries in hash table
============================================================================ */
void display_table(void) {
printf("\n╔════════════════════════════════════════════╗\n");
printf("║ Hash Table Contents (Size: %d) ║\n", hash_table.size);
printf("╠════════════════════════════════════════════╣\n");
for (int i = 0; i < TABLE_SIZE; i++) {
printf("║ Slot %2d: ", i);
if (hash_table.entries[i].key == EMPTY_KEY) {
printf("[EMPTY] ║\n");
} else if (hash_table.entries[i].key == DELETED_KEY) {
printf("[DELETED] ║\n");
} else {
printf("Key: %3llu, Value: %3llu ║\n",
(unsigned long long)hash_table.entries[i].key,
(unsigned long long)hash_table.entries[i].value);
}
}
printf("╚════════════════════════════════════════════╝\n");
}
/* ============================================================================
Main program - Test hash table operations
============================================================================ */
int main(void) {
printf("\n╔═════════════════════════════════════════════════════════════╗\n");
printf("║ Hash Table Implementation in C (Equivalent to NASM) ║\n");
printf("║ Uses linear probing for collision resolution ║\n");
printf("╚═════════════════════════════════════════════════════════════╝\n\n");
/* Initialize hash table */
init_table();
printf("✓ Hash Table initialized\n");
printf(" Table Size: %d entries\n", TABLE_SIZE);
printf(" Entry Size: %d bytes\n\n", ENTRY_SIZE);
/* Test insertions */
printf("╔═════════════════════════════════════════════════════════════╗\n");
printf("║ TEST 1: INSERT ║\n");
printf("╠═════════════════════════════════════════════════════════════╣\n");
for (int i = 0; i < test_count; i++) {
uint64_t key = test_keys[i];
uint64_t value = test_values[i];
int result = insert(key, value);
if (result == 0) {
printf("✓ Inserted: key=%llu, value=%llu\n",
(unsigned long long)key, (unsigned long long)value);
} else {
printf("✗ Failed: Table full (key=%llu)\n",
(unsigned long long)key);
}
}
display_table();
/* Test searches */
printf("\n╔═════════════════════════════════════════════════════════════╗\n");
printf("║ TEST 2: SEARCH ║\n");
printf("╠═════════════════════════════════════════════════════════════╣\n");
int search_passed = 0;
int search_failed = 0;
for (int i = 0; i < test_count; i++) {
uint64_t key = test_keys[i];
uint64_t expected_value = test_values[i];
uint64_t found_value = search(key);
if (found_value != (uint64_t)-1) {
if (found_value == expected_value) {
printf("✓ Found: key=%llu, value=%llu (Expected: %llu)\n",
(unsigned long long)key,
(unsigned long long)found_value,
(unsigned long long)expected_value);
search_passed++;
} else {
printf("✗ Mismatch: key=%llu, found=%llu, expected=%llu\n",
(unsigned long long)key,
(unsigned long long)found_value,
(unsigned long long)expected_value);
search_failed++;
}
} else {
printf("✗ Not found: key=%llu (Expected: %llu)\n",
(unsigned long long)key,
(unsigned long long)expected_value);
search_failed++;
}
}
printf("\nSearch Results: %d passed, %d failed\n", search_passed, search_failed);
/* Test collision handling */
printf("\n╔═════════════════════════════════════════════════════════════╗\n");
printf("║ TEST 3: COLLISION HANDLING ║\n");
printf("╠═════════════════════════════════════════════════════════════╣\n");
/* Try inserting a key that will collide */
uint64_t collision_key = 42 + TABLE_SIZE; /* Should hash to same slot as 42 */
uint64_t collision_value = 999;
printf("Attempting collision test:\n");
printf(" Original key (42) hashes to: %llu\n",
(unsigned long long)hash_function(42));
printf(" Collision key (%llu) hashes to: %llu\n",
(unsigned long long)collision_key,
(unsigned long long)hash_function(collision_key));
int result = insert(collision_key, collision_value);
if (result == 0) {
printf("✓ Collision handled successfully via linear probing\n");
printf("✓ Inserted: key=%llu, value=%llu\n",
(unsigned long long)collision_key,
(unsigned long long)collision_value);
} else {
printf("✗ Failed to handle collision\n");
}
display_table();
/* Test deletion */
printf("\n╔═════════════════════════════════════════════════════════════╗\n");
printf("║ TEST 4: DELETE ║\n");
printf("╠═════════════════════════════════════════════════════════════╣\n");
uint64_t key_to_delete = 15;
printf("Deleting key=%llu\n", (unsigned long long)key_to_delete);
int delete_result = delete_key(key_to_delete);
if (delete_result == 0) {
printf("✓ Successfully deleted key=%llu\n",
(unsigned long long)key_to_delete);
} else {
printf("✗ Failed to delete key=%llu (not found)\n",
(unsigned long long)key_to_delete);
}
/* Verify deletion */
uint64_t search_result = search(key_to_delete);
if (search_result == (uint64_t)-1) {
printf("✓ Verified: key=%llu is no longer in table\n",
(unsigned long long)key_to_delete);
}
display_table();
/* Final statistics */
printf("\n╔═════════════════════════════════════════════════════════════╗\n");
printf("║ FINAL STATISTICS ║\n");
printf("╠═════════════════════════════════════════════════════════════╣\n");
printf("║ Total entries in table: %d ║\n", hash_table.size);
printf("║ Table capacity: %d ║\n", TABLE_SIZE);
printf("║ Load factor: %.2f%% ║\n",
(float)hash_table.size / TABLE_SIZE * 100);
printf("║ Empty slots: %d ║\n",
TABLE_SIZE - hash_table.size);
printf("╚═════════════════════════════════════════════════════════════╝\n\n");
printf("✓ All tests completed successfully!\n\n");
return 0;
}