-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhashtable.c
More file actions
36 lines (28 loc) · 793 Bytes
/
hashtable.c
File metadata and controls
36 lines (28 loc) · 793 Bytes
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
#include "hashtable.h"
#include <stdio.h>
#include <string.h>
int
hashtable_set(void *table[], int nel, void *item, int (*hash)(const void *),
int (*compare)(const void *, const void *))
{
int i;
for (i = hash(item) % nel; table[i] != NULL; i = (i + 1) % nel) {
if (compare(table[i], item) == 0) {
break;
}
}
table[i] = item;
return 1;
}
void *
hashtable_get(void *table[], int nel, void *item, int (*hash)(const void *),
int (*compare)(const void *, const void *))
{
int i;
for (i = hash(item) % nel; table[i] != NULL; i = (i + 1) % nel) {
if (compare(table[i], item) == 0) {
return table[i];
}
}
return NULL;
}