-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.c
More file actions
84 lines (74 loc) · 2.2 KB
/
hash_table.c
File metadata and controls
84 lines (74 loc) · 2.2 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
#include <stdlib.h>
#include "hash_table.h"
void ht_resize (hash_table_t* ht) {
//printf("start resize: %lu, %lu\n", ht->num_items, ht->capacity);
//fflush(stdout);
capacity_t new_capacity = ht->capacity * 2;
hash_table_t* ht_new = ht_create_capacity(new_capacity);
int i;
//int count1 = 0;
//int count2 = 0;
for (i = 0; i < ht->capacity; i++) {
//count2++;
if (ht->items[i].exists) {
//count1++;
ht_insert(ht_new, ht->items[i]);
}
}
//printf("Iterated over %d\n items\n", count2);
//printf("Attempted to insert %d\n items\n", count1);
free(ht->items);
ht->items = ht_new->items;
ht->capacity = ht_new->capacity;
ht->num_items = ht_new->num_items;
free(ht_new);
//printf("end resize: %lu, %lu\n", ht->num_items, ht->capacity);
//fflush(stdout);
return;
}
hash_index_t hash (key_t key) {
return (key+528042)*key;
}
hash_table_t* ht_create () {
return ht_create_capacity(INIT_CAPACITY);
}
hash_table_t* ht_create_capacity (capacity_t init_capacity) {
hash_item_t* items = calloc (init_capacity, sizeof(hash_item_t));
hash_table_t* ht = malloc (sizeof(hash_table_t));
ht->items = items;
ht->capacity = init_capacity;
ht->num_items = 0;
return ht;
}
int ht_insert (hash_table_t* ht, hash_item_t item) {
item.exists = 1;
hash_index_t index = hash(item.key) % (ht->capacity);
while (true) {
hash_item_t current_item = ht->items[index];
if (!current_item.exists) {
ht->items[index] = item;
ht->num_items++;
break;
}
else if (current_item.key == item.key) {
ht->items[index] = item;
break;
}
index = (index + 1) % ht->capacity;
}
if (ht->num_items > (ht->capacity/2))
ht_resize(ht);
return 0;
}
value_t ht_lookup (hash_table_t* ht, key_t key) {
hash_index_t index = hash(key) % ht->capacity;
while (true) {
hash_item_t item = ht->items[index];
if (!item.exists)
return EMPTY;
else if (item.key == key) {
return item.value;
}
index = (index + 1) % ht->capacity;
}
}