-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.c
105 lines (84 loc) · 2.33 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
/* hash_table.c -- part of vulcan
*
* This program is copyright (C) 2006 Mauro Persano, and is free
* software which is freely distributable under the terms of the
* GNU public license, included as the file COPYING in this
* distribution. It is NOT public domain software, and any
* redistribution not permitted by the GNU General Public License is
* expressly forbidden without prior written permission from
* the author.
*
*/
#include <stdlib.h>
#include "hash_table.h"
struct hash_table_entry {
void *key;
void *value;
struct hash_table_entry *next;
};
struct hash_table {
int nbuckets;
struct hash_table_entry **buckets;
unsigned (*key_hash)(const void *key);
int (*key_equals)(const void *key1, const void *key2);
};
struct hash_table *
hash_table_make(int nbuckets, unsigned (*key_hash)(const void *key),
int (*key_equals)(const void *key1, const void *key2))
{
struct hash_table *hash_table = malloc(sizeof *hash_table);
hash_table->nbuckets = nbuckets;
hash_table->buckets = calloc(hash_table->nbuckets,
sizeof *hash_table->buckets);
hash_table->key_hash = key_hash;
hash_table->key_equals = key_equals;
return hash_table;
}
static struct hash_table_entry *
hash_table_entry_make(void *key, void *value, struct hash_table_entry *next)
{
struct hash_table_entry *p = malloc(sizeof *p);
p->key = key;
p->value = value;
p->next = next;
return p;
}
static int
bucket_idx(struct hash_table *hash_table, const void *key)
{
return hash_table->key_hash(key)%hash_table->nbuckets;
}
void
hash_table_put(struct hash_table *hash_table, void *key, void *value)
{
int idx = bucket_idx(hash_table, key);
hash_table->buckets[idx] = hash_table_entry_make(key, value,
hash_table->buckets[idx]);
}
void *
hash_table_get(struct hash_table *hash_table, const void *key)
{
struct hash_table_entry *p;
for (p = hash_table->buckets[bucket_idx(hash_table, key)]; p;
p = p->next) {
if (key == p->key || hash_table->key_equals(key, p->key))
return p->value;
}
return NULL;
}
void
hash_table_free(struct hash_table *hash_table, void (*free_key)(void *), void (*free_data)(void *))
{
int i;
for (i = 0; i < hash_table->nbuckets; i++) {
struct hash_table_entry *p, *n;
for (p = hash_table->buckets[i]; p; p = n) {
n = p->next;
if (free_key)
free_key(p->key);
if (free_data)
free_data(p->value);
free(p);
}
}
}