-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.cpp
More file actions
258 lines (243 loc) · 5.07 KB
/
hashmap.cpp
File metadata and controls
258 lines (243 loc) · 5.07 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// enable Visual C++ memory leak checking
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#include "hashmap.h"
hashmap::hashmap(int capacity) :
nStocks(0),
capacity(capacity),
slots(new slot[capacity])
{
for (int i = 0; i < capacity; i++)
{
slots[i].slotState = slot::empty;
}
}
hashmap::~hashmap(void)
{
if (slots)
{
delete [] slots;
slots = NULL;
}
}
bool hashmap::get(char const * const symbol, stock& s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth)
const
{
int ctr = 0;
int chain = 0;
int index = hashStr(symbol);
index %= capacity;
int next = index;
while (ctr <= capacity)
{
if (slots[next].slotState == slot::full)
{
if (strcmp(symbol, slots[next].slotStock.getSymbol()) == 0)
{
s = slots[next].slotStock;
symbolHash = hashStr(symbol);
hashIndex = index;
usedIndex = next;
ctr = 0;
while (index != next)
{
chain++;
next = prevIndex(next);
}
chainLgth = chain + 1;
return true;
}
else
{
next = nextIndex(next);
ctr++;
}
}
else
{
next = nextIndex(next);
ctr++;
}
}
return false;
}
bool hashmap::put(const stock& s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth)
{
stock test;
if (get(s.getSymbol(), test, symbolHash, hashIndex, usedIndex, chainLgth) == true)
{
return false;
}
else
{
int ctr = 0;
int chain = 0;
int index = hashStr(s.getSymbol());
index %= capacity;
int next = index;
while (ctr <= capacity)
{
if (slots[next].slotState == slot::empty || slots[next].slotState == slot::deleted)
{
slots[next].slotStock = s;
symbolHash = hashStr(slots[next].slotStock.getSymbol());
nStocks++;
hashIndex = index;
usedIndex = next;
slots[next].slotState = slot::full;
ctr = 0;
while (index != next)
{
chain++;
next = prevIndex(next);
}
chainLgth = chain + 1;
return true;
}
else
{
next = nextIndex(next);
ctr++;
}
}
return false;
}
}
bool hashmap::remove(char const * const symbol, stock& s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth)
{
int ctr = 0;
int chain = 0;
int index = hashStr(symbol);
index %= capacity;
int next = index;
while (ctr <= capacity)
{
if (slots[next].slotState == slot::full)
{
if (strcmp(symbol, slots[next].slotStock.getSymbol()) == 0)
{
s = slots[next].slotStock;
nStocks--;
symbolHash = hashStr(symbol);
hashIndex = index;
usedIndex = next;
slots[next].slotState = slot::deleted;
ctr = 0;
while (index != next)
{
chain++;
next = prevIndex(next);
}
chainLgth = chain + 1;
return true;
}
else
{
next = nextIndex(next);
ctr++;
}
}
else
{
next = nextIndex(next);
ctr++;
}
}
return false;
}
unsigned int hashmap::hashStr(char const * const str)
{
// Hash string according to the formula in java.lang.String.hashCode():
//
// s[0]*(31^(n-1)) + s[1]*(31^(n-2)) + ... + s[n-1]
//
// s is the array of characters, n is the number of characters in the string,
// and 31^n means 31 to the nth power.
//
// This function is declared static because its result depends
// only on the characters in the string. You will need to apply
// the modulo operator to the result of this function to generate
// the required table index. The place to do this is in the functions
// that call this function.
//
// You can and should do this computation entirely with integers. In other
// words, there is no need to use floating point values. In particular, you
// should not use the pow() function from <math.h> in this lab.
int hash = 0;
int i = 0;
for (i; i < strlen(str); i++)
{
hash = (31 * hash) + str[i];
}
return hash;
}
ostream& operator<<(ostream& out, const hashmap& h)
{
int i = 0;
if (h.nStocks == 0)
{
out << "no stocks" << endl;
}
else
{
stock::displayHeaders(out);
for (i; i < h.capacity; i++)
{
if (h.slots[i].slotState == h.slot::full)
{
out << h.slots[i].slotStock << endl;
}
}
}
return out;
}
int hashmap::nextIndex(int index) const
{
if (index == capacity-1)
{
return (0);
}
else
{
return (index + 1);
}
}
int hashmap::prevIndex(int index) const
{
if (index == 0)
{
return (capacity-1);
}
else
{
return (index - 1);
}
}
bool hashmap::slotEmpty(int index) const
{
if (slots[index].slotState == slot::empty || slots[index].slotState == slot::deleted)
{
return true;
}
else
{
return false;
}
}
void hashmap::setSlotEmpty(int index)
{
slots[index].slotState = slot::empty;
}