-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
181 lines (160 loc) · 3.5 KB
/
hash.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
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hash.h"
void hash_init(struct hash *h, hash_proc hash, unsigned num_buckets)
{
assert(h != NULL);
h->hash = hash;
h->size = 0;
h->num_buckets = num_buckets;
h->buckets = malloc(num_buckets * (sizeof *h->buckets + sizeof *h->buckets_in_use));
h->buckets_in_use = (bool *)(h->buckets + num_buckets);
h->freeh = false;
memset(h->buckets, 0, num_buckets * (sizeof *h->buckets + sizeof *h->buckets_in_use));
}
struct hash *hash_new(hash_proc hash, unsigned num_buckets)
{
struct hash *h = malloc(sizeof *h);
hash_init(h, hash, num_buckets);
h->freeh = true;
return h;
}
void hash_delete(struct hash *h, bool free_values)
{
int i;
for (i = 0; i < h->num_buckets; i++)
{
if (!h->buckets_in_use[i]) continue;
if (free_values) free(h->buckets[i].value);
struct hashnode *node = h->buckets[i].next;
while (node != NULL)
{
struct hashnode *prev = node;
node = node->next;
if (free_values) free(prev->value);
free(prev);
}
}
free(h->buckets);
if (h->freeh) free(h);
}
void hash_clear(struct hash *h, bool free_values)
{
int i;
for (i = 0; i < h->num_buckets; i++)
{
if (!h->buckets_in_use[i]) continue;
if (free_values) free(h->buckets[i].value);
struct hashnode *node = h->buckets[i].next;
while (node != NULL)
{
struct hashnode *prev = node;
node = node->next;
if (free_values) free(prev->value);
free(prev);
}
}
h->size = 0;
}
static struct hashnode *hash_findnode(const struct hash *h, int key1, int key2, struct hashnode **prev_out)
{
unsigned hash = h->hash(key1, key2);
struct hashnode *result = NULL;
if (!h->buckets_in_use[hash])
{
if (prev_out != NULL) *prev_out = NULL;
result = NULL;
}
else if (h->buckets[hash].key1 == key1 && h->buckets[hash].key2 == key2)
{
result = h->buckets + hash;
if (prev_out != NULL) *prev_out = NULL;
}
else
{
struct hashnode *prev = h->buckets + hash;
struct hashnode *node;
for (node = prev->next; node != NULL; node = node->next)
{
if (node->key1 == key1 && node->key2 == key2)
{
result = node;
break;
}
prev = node;
}
if (prev_out != NULL) *prev_out = prev;
}
return result;
}
void *hashnode_delete(struct hash *h, int key1, int key2)
{
void *result;
struct hashnode *prev;
struct hashnode *node = hash_findnode(h, key1, key2, &prev);
if (node == NULL)
{
result = NULL;
}
else if (prev == NULL)
{
result = node->value;
if (node->next != NULL)
{
struct hashnode *next = node->next;
*node = *next;
free(next);
}
else
{
unsigned hash = h->hash(key1, key2);
h->buckets_in_use[hash] = false;
}
}
else
{
result = node->value;
prev->next = node->next;
free(node);
}
if (result != NULL) h->size--;
return result;
}
void *hashnode_set(struct hash *h, int key1, int key2, void *value)
{
struct hashnode *prev;
struct hashnode *node = hash_findnode(h, key1, key2, &prev);
if (node != NULL)
{
void *result = node->value;
node->value = value;
return result;
}
if (prev == NULL)
{
uint hash = h->hash(key1, key2);
h->buckets_in_use[hash] = true;
node = h->buckets + hash;
}
else
{
node = malloc(sizeof *node);
prev->next = node;
}
node->next = NULL;
node->key1 = key1;
node->key2 = key2;
node->value = value;
h->size++;
return NULL;
}
void *hashnode_get(const struct hash *h, int key1, int key2)
{
struct hashnode *node = hash_findnode(h, key1, key2, NULL);
return node != NULL ? node->value : NULL;
}
unsigned hash_size(const struct hash *h)
{
return h->size;
}