-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
290 lines (223 loc) · 7.48 KB
/
HashTable.cpp
File metadata and controls
290 lines (223 loc) · 7.48 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// HashTable.cpp
// proj5
//
// Created by Edward J Skrod on 11/13/13.
// Copyright (c) 2013 Edward J Skrod. All rights reserved.
//
#include "HashTable.h"
HashTable::HashTable( int currentTableSize ):currentSize(currentTableSize)
{
List * myList;
for (int lcv = 0; lcv < currentSize; ++lcv){
myList = new List;
theLists.push_back(myList);
}
}
HashTable::~HashTable()
{ makeEmpty(); }
void HashTable::makeEmpty( )
{
for (int lcv = 0; lcv < currentSize; ++lcv){
theLists[lcv]->clear( );
}
}
void HashTable::insert( const Play &playObj )
{
int Key = 0;
// Hash the current play
Key = playObj.Hash(this->currentSize);
//cout << "Key: " << Key << " " << "Play: ";
//playObj.Print(cout);
theLists[Key]->push_back(playObj);
}
void HashTable::print( )
{
List::iterator itr;
for (int lcv = 0; lcv < currentSize; ++lcv){
for (itr = theLists[lcv]->begin(); itr != theLists[lcv]->end(); ++itr){
cout << *itr;
}
}
}
int HashTable::myhash(string Off, int Down) const
// myhash is used by HashTable to create a unique
// bucket number for Plays based upon the Offense
// Team Name and the Down. I assign a unique hash value
// to each team that is a factor of 4. This allows each team
// to use 4 unique buckets.
{
string Key = Off;
int hash = 0;
if (Key == "ARI"){
hash = 0;
} else if (Key == "ATL"){
hash = 4;
} else if (Key == "BAL"){
hash = 8;
} else if (Key == "BUF"){
hash = 16;
} else if (Key == "CAR"){
hash = 20;
} else if (Key == "CHI"){
hash = 24;
} else if (Key == "CIN"){
hash = 28;
} else if (Key == "CLE"){
hash = 32;
} else if (Key == "DAL"){
hash = 36;
} else if (Key == "DEN"){
hash = 40;
} else if (Key == "DET"){
hash = 44;
} else if (Key == "GB"){
hash = 48;
} else if (Key == "HOU"){
hash = 52;
} else if (Key == "IND"){
hash = 56;
} else if ((Key == "JAX") || (Key == "JAC")){
hash = 60;
} else if (Key == "KC"){
hash = 64;
} else if (Key == "MIA"){
hash = 68;
} else if (Key == "MIN"){
hash = 72;
} else if (Key == "NE"){
hash = 76;
} else if (Key == "NO"){
hash = 80;
} else if (Key == "NYG"){
hash = 84;
} else if (Key == "NYJ"){
hash = 88;
} else if (Key == "OAK"){
hash = 92;
} else if (Key == "PHI"){
hash = 96;
} else if (Key == "PIT"){
hash = 100;
} else if (Key == "SD"){
hash = 104;
} else if (Key == "SEA"){
hash = 108;
} else if (Key == "SF"){
hash = 112;
} else if (Key == "STL"){
hash = 116;
} else if (Key == "TB"){
hash = 120;
} else if (Key == "TEN"){
hash = 124;
} else if (Key == "WAS"){
hash = 128;
} else {
cout << "Play.cpp: Hash() Error assigning a hash to a team!\n";
cout << Key << " does not have a name assocated to a hash num.\n";
}
hash = hash + Down;
return hash;
}
void HashTable::handleList(vector<Play> &relevantDataRecord, int Key, int minRemaining, string Off, string Def, int Down, int Togo, int Ydline)
// Calculates the relevance of similar plays and pushes them onto a vector for sorting
{
List::iterator itr;
float relevance = 0.0;
for (itr = theLists[Key]->begin(); itr != theLists[Key]->end(); ++itr){
if (itr.getCurrent()->playObj.isSimilar(Off, Down, Togo, Ydline)){
relevance = itr.getCurrent()->playObj.calculateRelevance(minRemaining, Def, Togo, Ydline);
Play tempSimilarData(itr.getCurrent()->playObj.getGameID(),
itr.getCurrent()->playObj.getQuarter(),
itr.getCurrent()->playObj.getMin(),
itr.getCurrent()->playObj.getSec(),
itr.getCurrent()->playObj.getOffenseName(),
itr.getCurrent()->playObj.getDefenseName(),
itr.getCurrent()->playObj.getDown(),
itr.getCurrent()->playObj.getYardsToGo(),
itr.getCurrent()->playObj.getStartLocation(),
itr.getCurrent()->playObj.getDescription(),
itr.getCurrent()->playObj.getOffscore(),
itr.getCurrent()->playObj.getDefscore(),
itr.getCurrent()->playObj.getSeason(),
relevance,
itr.getCurrent()->playObj.getPlayDescription(),
itr.getCurrent()->playObj.getPlayCount()
);
relevantDataRecord.push_back(tempSimilarData);
}
}
}
/*
Adapted from
http://simplestcodings.blogspot.com/2010/08/merge-sort-implementation-in-c.html
*/
void HashTable::mergeSort(vector<Play> &list1)
// Public member function to abstract the second list away from the
// user of the class
// Calculates the size of the list
{
int num = static_cast<int>(list1.size()); // Returns the number of elements in the array
vector<Play> list2(num); // Creates a vector of Plays of size num
// Call the private member function
this->mergeSort(list1, list2, 0, (num -1));
}
void HashTable::mergeSort(vector<Play> &list1, vector<Play> &list2, int begin, int end )
{
int pivot;
if (begin < end)
{
pivot = ( begin + end ) / 2;
// Recursivley (and conceptually) divide the list into smaller
// and smaller units
mergeSort(list1, list2, begin, pivot);
mergeSort(list1, list2, (pivot + 1), end);
merge(list1, list2, begin, pivot, end);
}
}
void HashTable::merge(vector<Play> &list1, vector<Play> &list2, int begin, int pivot, int end)
{
int h, i, j, k;
h = begin;
i = begin;
j = pivot + 1;
while ((h <= pivot) && (j <= end)) {
if (list1[h].getRelevance() >= list1[j].getRelevance()) {
list2[i] = list1[h];
++h;
}
else {
list2[i] = list1[j];
++j;
}
++i;
}
if (h > pivot) {
for (k = j; k <= end; ++k)
{
list2[i] = list1[k];
++i;
}
}
else {
for (k = h; k <= pivot; ++k){
list2[i] = list1[k];
++i;
}
}
// copy list2 into list1 from begining to end
for (k = begin; k <= end; ++k) {
list1[k] = list2[k];
}
}
int HashTable::totalNumRecords()
// This function returns the total number of Plays that are stored in the Hash Table
// Used for testing
{
int numRecords = 0;
for (int lcv = 0; lcv < currentSize; ++lcv) {
numRecords += theLists[lcv]->size();
}
return numRecords;
}