-
Notifications
You must be signed in to change notification settings - Fork 0
/
VarVector.cpp
140 lines (123 loc) · 2.37 KB
/
VarVector.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Header.h"
#include <cstdlib>
VarVector::VarVector()
{
this->currentUse = 0;
this->memory = 1;
tab = new var[1];
InitalizeHashTable();
}
void VarVector::InitalizeHashTable()
{
hashTable = (int**)malloc(sizeof(int*) * 172* 2);
for (int i = 0; i < 172; i++)
{
hashTable[i] = (int*)malloc(sizeof(int) * 5);
hashTable[i][0] = 0;
hashTable[i][4] = -1;
}
}
VarVector::~VarVector()
{
delete[] tab;
free(hashTable);
}
void VarVector::pop()
{
currentUse--;
if ((3 * currentUse < memory) && (currentUse > 1))
{
resize(memory / 2);
}
}
bool VarVector::empty()
{
return (currentUse == 0);
}
void VarVector::resize(int newSize)
{
memory = newSize;
var* buffer = new var[newSize];
for (int i = 0; i < currentUse; i++)
{
buffer[i] = tab[i];
}
delete[] tab;
tab = buffer;
}
var &VarVector::at(int index)
{
return tab[index];
}
int VarVector::isInitalizedVariable(char* nameToFind)
{
int position;
if (isVarLetter(*(nameToFind+1)))
{
position = (*nameToFind - 65) + (*(nameToFind + 1) - 65);
}
else
{
position = (*nameToFind - 65);
}
int index;
for (int i = 1; i < hashTable[position][0]; i++)
{
index = hashTable[position][i];
if (compareStrings(tab[index].name, nameToFind))
{
return index;
}
}
return -1;
}
int VarVector::size()
{
return currentUse;
}
void VarVector::clear()
{
delete[] tab;
tab = new var[1];
currentUse = 0;
memory = 1;
}
void VarVector::push_back(var newObject)
{
if (newObject.name[0] == NULL)
return;
tab[currentUse] = newObject;
addToHashTable(currentUse);
currentUse++;
if (currentUse == memory)
{
resize(memory*2);
}
addToHashTable(currentUse - 1);
}
void VarVector::addToHashTable(int index)
{
int position;
if (isVarLetter(tab[index].name[1]))
{
position = (tab[index].name[0] - 65) + (tab[index].name[0] - 65);
}
else
{
position = (tab[index].name[0] - 65);
}
int sizeOfParticularHashField = hashTable[position][0];
if (hashTable[position][sizeOfParticularHashField + 1] == -1)
{
int* newArray = (int*)malloc(sizeof(int*) * (sizeOfParticularHashField + 2) * 2);
for (int i = 0; i < (sizeOfParticularHashField + 2); i++)
{
newArray[i] = hashTable[position][i];
}
free(hashTable[position]);
hashTable[position] = newArray;
hashTable[position][2*sizeOfParticularHashField - 1] = -1;
}
hashTable[position][sizeOfParticularHashField + 1] = index;
hashTable[position][0]++;
}