-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.h
104 lines (85 loc) · 1.97 KB
/
hashtable.h
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "types.h"
typedef struct {
char name[64];
type_t type;
flag_t flag;
value_t value;
} element_t;
typedef struct{
char name[64];
char func[64];
element_t *elements;
element_t **next, **last;
int size;
} hashtable_t;
hashtable_t* new_hashtable(int size, char* str){
hashtable_t* h;
h = (hashtable_t*) malloc(sizeof(hashtable_t));
h->elements = (element_t*) malloc(sizeof(element_t) * size);
h->last = h->next = (element_t **) malloc(sizeof(element_t *) * size);
memset(h->elements, 0, sizeof(element_t) * size);
h->size = size;
strcpy(h->name, str);
return h;
}
uint64_t hash_fnv1a(char s[]){
const uint64_t prime = 0x100000001b3;
uint64_t hash = 0xcbf29ce484222325;
int i, len = strlen(s);
for(i = 0; i < len; i++){
hash ^= s[i] & 0xff;
hash *= prime;
}
return hash;
}
element_t* store(hashtable_t* hashtable, char *s, type_t type){
int size = hashtable->size;
element_t* table = hashtable->elements;
uint64_t ind = hash_fnv1a(s) % size;
element_t *it, *el = &table[ind];
if(strcmp(el->name, s) == 0)
return el;
if(strlen(el->name) <= 0){
strcpy(el->name, s);
el->type = type;
el->flag = NONE_F;
*(hashtable->last)++ = el;
return el;
}
register int i;
for(i=(ind+1)%size; i!=ind; i=(i+1)%size){
it = table + i;
if(strcmp(it->name, s) == 0)
return it;
if(strlen(it->name) <= 0){
strcpy(it->name, s);
it->type = type;
it->flag = NONE_F;
*(hashtable->last)++ = it;
return it;
}
}
return NULL;
}
element_t *fetch(hashtable_t* hashtable, char *s){
int size = hashtable->size;
element_t* table = hashtable->elements;
uint64_t ind = hash_fnv1a(s) % size;
element_t *it, *el = &table[ind];
if(strcmp(el->name, s) == 0)
return el;
if(strlen(el->name) == 0)
return NULL;
register int i;
for(i=(ind+1)%size; i!=ind; i=(i+1)%size){
it = table + i;
if(strlen(it->name) == 0)
return NULL;
if(strcmp(it->name, s) == 0)
return it;
}
return NULL;
}