-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.c
112 lines (78 loc) · 2.35 KB
/
optimizer.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
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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "optimizer.h"
#define TABLE_SIZE 347
/* --- STATIC VARIABLES AND FUNCTIONS --- */
static
OptimizeTabEntry **Table;
static
int calculateHash(char *name) {
int i;
int hashValue = 1;
for (i=0; i < strlen(name); i++) {
//printf("%c",name[i]);
hashValue = (hashValue * name[i]) % TABLE_SIZE;
}
return hashValue;
}
void InitOptimizeTable(){
int i;
int dummy;
Table = (OptimizeTabEntry **)malloc(sizeof(OptimizeTabEntry*) * TABLE_SIZE);
for (i=0; i < TABLE_SIZE; i++)
Table[i] = NULL;
}
OptimizeTabEntry * lookupOPT(char *name){
int currentIndex=0;
int visitedSlots = 0;
currentIndex = calculateHash(name);
while (Table[currentIndex] != NULL && visitedSlots < TABLE_SIZE) {
if (!strcmp(Table[currentIndex]->name, name) )
return Table[currentIndex];
currentIndex = (currentIndex + 1) % TABLE_SIZE;
visitedSlots++;
}
return NULL;
}
void insertOPT(char *name,int registerNumber){
int currentIndex;
int visitedSlots = 0;
currentIndex = calculateHash(name);
while (Table[currentIndex] != NULL && visitedSlots < TABLE_SIZE) {
if (!strcmp(Table[currentIndex]->name, name) )
printf("*** WARNING *** in function \"insert\": %s has already an entry\n", name);
currentIndex = (currentIndex + 1) % TABLE_SIZE;
visitedSlots++;
}
if (visitedSlots == TABLE_SIZE) {
printf("*** ERROR *** in function \"insert\": No more space for entry %s\n", name);
return;
}
Table[currentIndex] = (OptimizeTabEntry *) malloc (sizeof(OptimizeTabEntry));
Table[currentIndex]->name = (char *) malloc (strlen(name)+1);
strcpy(Table[currentIndex]->name, name);
Table[currentIndex]->value = registerNumber;
}
void deleteOptTab(){
int i;
for (i=0; i < TABLE_SIZE; i++)
{
if(Table[i]){
free(Table[i]);
Table[i] = NULL;
}
}
}
void PrintOptimizeTable()
{
int i;
printf("\n --- Optimizer Hash Table ---------------\n\n");
for (i=0; i < TABLE_SIZE; i++) {
if (Table[i] != NULL) {
printf("\"%s\"", Table[i]->name);
printf("\t\"%d\" \n",Table[i]->value);
}
}
printf("\n --------------------------------\n\n");
}