-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashTable.c
More file actions
112 lines (98 loc) · 2.99 KB
/
hashTable.c
File metadata and controls
112 lines (98 loc) · 2.99 KB
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
106
107
108
109
110
111
112
/*
Group:33
2016A7PS0036P Megh Thakkar
2016A7PS0103P Sahil Singla
2016A7PS0110P Sankalp Sangle
2016A7PS0150P Patel Parth
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashTable.h"
int hashValue(char* str, int m) {
int accumulator = 0;
for(int i = 0; i < strlen(str); i++) {
accumulator = accumulator * 10 + (str[i] - ' ');
if(accumulator < 0) {
accumulator = 30;
}
}
accumulator %= m;
return accumulator;
}
Table* createAndPopulateLookUpTable(char** tokens, int no_of_tokens, int no_of_slots) {
Table* table = (Table*)malloc(sizeof(Table));
table->entries = (Entry*) malloc(no_of_slots * sizeof(Entry));
table->m = no_of_slots;
for(int i = 0; i < no_of_tokens; i++) {
if(tokens[i] == NULL) {
continue;
}
Node* n = (Node*)malloc(sizeof(Node));
n->tType = i;
n->lexeme = (char*)malloc(sizeof(char) * 50);
strcpy(n->lexeme, tokens[i]);
n->next = NULL;
int hashed_value = hashValue(tokens[i], no_of_slots);
if(table->entries[hashed_value].head == NULL) {
table->entries[hashed_value].head = n;
}
else{
n->next = table->entries[hashed_value].head;
table->entries[hashed_value].head = n;
}
// printf("%s %d %d\n ", n->lexeme, n->tType, hashed_value);
}
return table;
}
int lookUp(char* lex) {
int hashedValue = hashValue(lex, lookUpTable->m);
Node* tmp = lookUpTable->entries[hashedValue].head;
while(tmp != NULL) {
if(strcmp(lex, tmp->lexeme) == 0) {
return tmp->tType;
}
tmp = tmp->next;
}
return -1;
}
pTable* createLookUpTable(int no_of_slots)
{
pTable* table = (pTable*)malloc(sizeof(pTable));
table->entries = (pEntry*) malloc(no_of_slots * sizeof(pEntry));
table->m = no_of_slots;
return table;
}
pTable* PopulateLookUpTable(pTable* table,char** tokens,int no_of_tokens, int no_of_slots) {
for(int i = 0; i < no_of_tokens; i++) {
if(tokens[i] == NULL) {
continue;
}
pNode* n = (pNode*)malloc(sizeof(pNode));
n->tType = i;
n->lexeme = (char*)malloc(sizeof(char) * 20);
strcpy(n->lexeme, tokens[i]);
n->next = NULL;
int hashed_value = hashValue(tokens[i], no_of_slots);
if(table->entries[hashed_value].head == NULL) {
table->entries[hashed_value].head = n;
}
else{
n->next = table->entries[hashed_value].head;
table->entries[hashed_value].head = n;
}
}
return table;
}
int pLookUp(pTable* lookupTable,char* lex) {
int hashedValue = hashValue(lex, lookupTable->m);
pNode* tmp = lookupTable->entries[hashedValue].head;
while(tmp != NULL) {
if(strcmp(lex, tmp->lexeme) == 0) {
// printf("returned type is %s", tokenMap[tmp->tType]);
return tmp->tType;
}
tmp = tmp->next;
}
return -1;
}