-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhashtable.c
70 lines (63 loc) · 1.64 KB
/
hashtable.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
#include <string.h>
#include <stdlib.h>
#include "hashtable.h"
#include "sstrhash.h"
void ht_init(struct hashtable *h, size_t size)
{
h->count = 0;
h->size = size;
h->bucket = calloc(sizeof(struct hashnode), h->size);
}
void * ht_lookup(struct hashtable *h, const char *name)
{
size_t start = hashfunc(name) & (h->size -1);
size_t idx = (start + 1) & (h->size -1);
for(; idx != start; idx = (idx + 1) & (h->size -1)){
if(h->bucket[idx].name){
if( !strcmp(h->bucket[idx].name, name)){
return h->bucket[idx].val;
}
}else{
break;
}
}
return NULL;
}
static void resize(struct hashtable *h)
{
struct hashtable newht;
ht_init(&newht, h->size*2);
size_t i;
for(i = 0; i != h->size; i++){
if(h->bucket[i].name){
ht_put(&newht, h->bucket[i].name, h->bucket[i].val);
}
}
free(h->bucket);
h->bucket = newht.bucket;
h->size = newht.size;
h->count = newht.count;
}
bool ht_put(struct hashtable *h, const char *name, void *val)
{
size_t start = hashfunc(name) & (h->size-1);
size_t idx = (start + 1) & (h->size-1);
for(; /*idx != start*/; idx = (idx + 1) & (h->size-1)){
if(!h->bucket[idx].name){
h->bucket[idx].name = name;
h->bucket[idx].val = val;
break;
}else if( !strcmp(h->bucket[idx].name, name)){
return false;
}
}
h->count++;
if(h->count*3/2 > h->size){
resize(h);
}
return true;
}
void ht_clear(struct hashtable *h) {
memset(h->bucket, 0, h->size*sizeof(struct hashnode));
h->count = 0;
}