-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
74 lines (66 loc) · 1.8 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
#include "hash.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
// extern int vari_cnt;
// extern int func_cnt;
// extern int array_cnt;
// extern int label_cnt;
unsigned int HashFunc(char* str){
unsigned int val = 0, i;
for(char* cur=str; *cur; cur++){
val = (val<<2) + (unsigned int)(*cur);
if (i = val & ~0x3fff) val = (val ^ (i >> 12)) & 0x3fff;
}
return val;
}
HashTable* HashInit(){
HashTable* self = (HashTable*)malloc(sizeof(HashTable)) ;
for(int i=0; i<16384; i++){
self->table_[i] = NULL;
}
return self;
}
void HashAdd(HashTable* self, Symbol sym){
// switch(sym.kind){
// case FUNCTIONN:
// func_cnt++;
// sym.rank = func_cnt;
// break;
// case VARIABLEE:
// if(sym.prop.sym_type->kind==BASEE){
// vari_cnt++;
// sym.rank = vari_cnt;
// }
// else if(sym.prop.sym_type->kind==ARRAYY){
// array_cnt++;
// sym.rank = array_cnt;
// }
// // else assert(0);
// break;
// // default:
// // assert(0);
// }
unsigned int val = HashFunc(sym.name);
HashTableNode* p = (HashTableNode*)malloc(sizeof(HashTableNode));
p->symbol_ = sym;
p->nxt = NULL;
HashTableNode* cur = self->table_[val];
if(cur==NULL) self->table_[val] = p;
else{ // add to list directly
p->nxt = cur;
self->table_[val] = p;
}
}
HashTableNode* HashFind(HashTable* self, Symbol sym){
unsigned int val = HashFunc(sym.name);
HashTableNode* cur = self->table_[val];
while(cur != NULL){
if(strcmp(cur->symbol_.name, sym.name)==0){
break;
}
cur = cur->nxt;
}
return cur;
}