-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.c
executable file
·72 lines (61 loc) · 1.78 KB
/
dictionary.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
/*Lukas Kinder (s3686566), [email protected], Bachelor Project 2021 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
HashTable createDictionary(int numberRows){
HashTable tb;
tb.numberRows = numberRows;
tb.numberItems = malloc(sizeof(int) * numberRows);
tb.sizeRows = malloc(sizeof(int) * numberRows);
tb.table = malloc(sizeof(Argument *) * numberRows);
for (int i =0; i < numberRows; i++){
tb.numberItems[i] = 0;
tb.sizeRows[i] = 1;
tb.table[i] = malloc(sizeof(Argument ));
}
return tb;
}
void freeDictionary(HashTable tb){
for (int i =0; i < tb.numberRows; i++){
free(tb.table[i]);
}
free(tb.numberItems);
free(tb.sizeRows);
free(tb.table);
}
//uses argument name as hash key
void enterArgument(Argument a, HashTable dic){
int key = stringToInt(a->name) % dic.numberRows;
if (dic.sizeRows[key] = dic.numberItems[key]){
dic.sizeRows[key] *=2;
dic.table[key] = realloc(dic.table[key], sizeof(Argument) * dic.sizeRows[key]);
}
dic.table[key][dic.numberItems[key]] = a;
dic.numberItems[key]++;
}
void unknownNameError(char *name){
printf("ERROR: Name '%s' is unknown in dictionary\n", name);
exit(1);
}
/* Find the argument given the name of the argument by first computing
the corresponding hash-key of the argument-name */
Argument nameToArgument(char *name, HashTable dic){
int key = stringToInt(name) % dic.numberRows;
Argument a;
for (int i =0; i < dic.numberItems[key]; i++){
a = dic.table[key][i];
if (strcmp(name,a->name) == 0){
return a;
}
}
unknownNameError(name);
}
//creates hash-key using the name of the argument
int stringToInt(char * name){
int key = 0, len = strlen( name);
for (int i =0; i < len; i++){
key += (int) (name[i]);
}
return key;
}